This tutorial walks you through how to do simple Spot trading by calling the functions in the python-okx library on a Jupyter Notebook.
Here are the steps we’ll cover in this article:
- How to run Python code snippets on a Jupyter Notebook
- How to install the python-okx package
- How to create API keys
- How to import OKX modules
- How to access our market data
- How to read our available trading pairs
- How to read your account balance
- How to access the four different account modes
- How to figure out what mode your current account is configured as
- How to place spot orders
- How to access the details of an order
- How to cancel an order
- How to amend an order
- How to access the list of open orders
- How to access the order history
- How to go further with the OKX API with a Jupyter Notebook
1. How to run Python code snippets on a Jupyter Notebook
The Jupyter Notebook is an incredibly powerful and easy-to-use tool for Python development and data analysis. You can run a Jupyter Notebook server on Windows, Mac OS or Linux.
This tutorial provides a pretty comprehensive guide on how to get a Jupyter Notebook up and running.
2. How to install the python-okx package
Once you start running a Jupyter Notebook, you can install the python-okx package by simply running the pip install python-okx
in the notebook or in a terminal (or via command prompt for Windows):
3. How to create API keys
- After signing in OKX, you can go to Account -> API to create API keys.
- Make sure to go to Assets -> Demo trading if you’d like create API keys for testing purposes.
- You can now create API keys for the different master/sub accounts you might have.
- Select Trade in the Permissions menu so you can trade with the API key.
- You now have access to your API key, your Secret key, and your passphrase. Keep them in a safe place!
- You can instantiate python variables to save your API details in the notebook for later usage.
4. How to import OKX modules
In python-okx, we provide the following modules based on our REST API modules. Read our guide to learn how to import OKX modules.
- Trade
- BlockTrading
- Funding
- Account
- Convert
- Earning
- SubAccount
- MarketData
- PublicData
- TradingData
- Status
- NDBroker
- FDBroker
To import the Trade module, you can run:
Congrats, you are now ready to use the comprehensive features available in python-okx!
5. How to access our market data
For more information on how to access our market data, please read our dedicated guide.
6. How to read our available trading pairs
For more information on how to read our available trading pairs, please read our dedicated guide.
7. How to read your account balance
For more information on how to read your account balance, please read our dedicated guide.
Note: For spot trading under “cash” tdMode
, you mainly need to check the cashBal
, frozenBal
parameters for each ccy
under details
, and the totalEq
parameter.
8. How to access the four different account modes
In our unified account system, there are four account modes:
- Simple account
- Single-currency margin account
- Multi-currency margin account
- Portfolio margin account
To understand the difference between different account modes and how to set up the account mode via the web UI, please read our dedicated guide.
In margin mode or trade mode, the parameter tdMode
determines how your position is going to be margined, which you need to set everytime you place a new order.
For spot trading under simple or single-currency margin account mode, please set tdMode
=’cash’.
For spot trading under multi-currency margin or porfolio margin account mode, please set tdMode
= ‘cross’.
You will find below a quick explanation of how to figure out what mode your current account is configured as.
9. How to figure out what mode your current account is configured as
For more information on how to figure out what mode your current account is configured as, please read our dedicated guide and enter the acctLv
parameter.
10. How to place spot orders under Simple / Single-currency margin mode
1. How to place a limit order
For more information on how to place a limit order under a Simple or Single-currency margin account mode, please read our dedicated guide.
Here’s an example of buying 0.01 BTC at the price of 19000 USDT.
2. How to place a market order
For more information on how to place a market order under a Simple or Single-currency margin account mode, please read our dedicated guide.
Here’s an example of buying 100 BTC at the current market price.
3. How to use the target currency parameter tgtCcy
while spot trading
In spot trading, the parameter tgtCcy
determines the unit of the size parameter sz
, which can be either the base currency or the quote currency of the trading pair. For example, in the pair BTC-USDT, the base currency is BTC and the quote currency is USDT.
By default, tgtCcy
= base_ccy, which mean the sz
you specified is in terms of the base currency. However, if you set tgtCcy
= quote_ccy as shown below, you would for example be set to buy 100 USDT worth of BTC at market price, rather than to buy 100 BTC at market price.
4. How to use the client order ID parameter clOrdId
When you place an order, you can specify your own client order ID by specifying the parameter clOrdId
, which can later be used as an identifier in place of ordId
when calling an order cancellation, amendment or retrieval endpoint.
11. How to access the details of a certain order
For more information on how to get details about a certain order, please read our dedicated guide.
1. Using ordId
2. Using clOrdId
12. How to cancel an order
For more information on how to cancel an order, please read our dedicated guide.
You also use clOrdId
instead of ordId
.
13. How to amend an order
For more information on how to amend an order, please read our dedicated guide.
You also use clOrdId
instead of ordId
.
14. How to access the list of open orders
For more information on how to access the list of open orders, please read our dedicated guide.
15. How to access the order history
1. For the last 7 days
For more information on how to access the order history for the last 7 days, please read our dedicated guide.
2. For the last 3 months
For more information on how to access the order history for the last 3 months, please read our dedicated guide.
16. How to go further with the OKX API with a Jupyter Notebook
For more examples, please download the full Jupyter Notebook.
If have any questions about our APIs, please feel free to ask in the OKX API support Telegram channel.
Source: https://www.okx.com/academy/en/spot-trading-with-jupyter-notebook