How to Conduct Spot Trading Using Jupyter Notebook

·

Learn how to execute simple spot trading by leveraging library functions in a Jupyter Notebook environment.

1. Executing Python Code Snippets in Jupyter Notebook

Jupyter Notebook is a powerful and user-friendly tool for Python development and data analysis. You can run a Jupyter Notebook server on Windows, Mac, or Linux.

👉 Learn how to launch Jupyter Notebook

2. Installing the python-okx Package

Once your Jupyter Notebook is running, install the python-okx package by executing:

pip install python-okx

3. Creating API Keys

  1. After logging into the platform, navigate to Trade > Demo Trading to create API keys for testing.
  2. Go to your profile and select API Management to generate keys.
  3. Create API keys for master/sub-accounts if applicable.
  4. Select Trade under Permissions to enable trading.
  5. Securely store your API key, secret key, and passphrase.

Example Python variables for API details:

api_key = "xxxxx"
secret_key = "xxxxx"
passphrase = "xxxxxx"

4. Importing OKX Modules

python-okx includes modules based on REST APIs. Key modules:

Example Trade module import:

import okx.Trade as Trade

5. Accessing Market Data

Fetch tickers for spot instruments:

import okx.MarketData as MarketData
marketDataAPI = MarketData.MarketAPI(flag="1")  # Demo mode
result = marketDataAPI.get_tickers(instType="SPOT")
print(result)

👉 Explore advanced market data features

6. Reading Available Trading Pairs

List all spot instruments:

import okx.Account as Account
accountAPI = Account.AccountAPI(api_key, secret_key, passphrase, False, "1")
result = accountAPI.get_instruments(instType="SPOT")
print(result)

7. Checking Account Balance

Retrieve balances and equity:

result = accountAPI.get_account_balance()
print(result)

8. Understanding Account Modes

OKX offers four account modes:

  1. Simple Mode (acctLv = 1)
  2. Single-currency Margin (acctLv = 2)
  3. Multi-currency Margin (acctLv = 3)
  4. Portfolio Margin (acctLv = 4)

Check your mode:

result = accountAPI.get_account_config()
acctLv = result["data"][0]["acctLv"]
print(f"Current mode: {acctLv}")

9. Placing Spot Orders

Limit Order Example

tradeAPI = Trade.TradeAPI(api_key, secret_key, passphrase, False, "1")
result = tradeAPI.place_order(
    instId="BTC-USDT",
    tdMode="cash",
    side="buy",
    ordType="limit",
    px="19000",
    sz="0.01"
)
print(result)

Market Order Example

result = tradeAPI.place_order(
    instId="BTC-USDT",
    tdMode="cash",
    side="buy",
    ordType="market",
    sz="100",
    tgtCcy="quote_ccy"
)

10. Managing Orders

FAQ

How secure are API keys?

API keys are encrypted and should be stored securely. Never share them publicly.

Can I test trades without real funds?

Yes! Use the demo trading mode (flag="1") for practice.

What’s the rate limit for API calls?

Refer to OKX’s API documentation for the latest rate limits.

Next Steps

👉 Download full Jupyter Notebook examples
Join developer communities for advanced queries.