How to Trade Derivatives Using Jupyter Notebook

·

Learn how to simplify derivative trading with unified tools and leverage high-level functionalities for seamless execution.

Types of Derivatives

OKX supports three primary types of derivatives:

For this guide, we’ll focus on perpetual swaps as an example.


Frequently Asked Questions (FAQs)

1. How to Fetch Market Data Using Python?

To retrieve market data, replace InstType with FUTURES or OPTION as needed:

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

2. How to List Available Trading Pairs?

Filter by instType to fetch relevant instruments:

import okx.PublicData as PublicData  
if __name__ == '__main__':  
    flag = "1"  # Demo trading mode  
    publicDataAPI = PublicData.PublicAPI(flag=flag)  
    result = publicDataAPI.get_instruments(instType="SWAP")  
    print(result)  

Key Parameters:

Example:

"instType": "SWAP",  
"instId": "LTC-USD-SWAP",  
"ctVal": "10",  
"ctMult": "1",  
"ctValCcy": "USD"  

👉 Explore OKX’s derivative tools for advanced features.


3. How to Check Account Balance?

import okx.Account as Account  
flag = "1"  
accountAPI = Account.AccountAPI(api_key, secret_key, passphrase, False, flag)  
result = accountAPI.get_account_balance()  
print(result)  

4. Which Account Modes Support Derivatives Trading?

OKX offers four modes:

  1. Spot Mode (excluded).
  2. Futures & Spot Mode
  3. Multi-Currency Margin Mode
  4. Portfolio Margin Mode

Verify your mode via acctLv:

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

5. Setting Leverage for Derivatives

Max leverage: 125x. Configure via:

result = accountAPI.set_leverage(  
    instId="BTC-USDT-SWAP",  
    lever="5",  
    mgnMode="cross"  # or "isolated"  
)  
print(result)  

Key Terms:


6. Placing Orders in Long/Short vs. Buy/Sell Modes

Modes:

API Example:

result = tradeAPI.place_order(  
    instId="BTC-USDT-SWAP",  
    tdMode="isolated",  
    side="buy",  
    posSide="long",  # or "net"  
    ordType="limit",  
    px="19000",  
    sz="100"  
)  

👉 Optimize trades with OKX’s API.


7. Checking Order Status

result = tradeAPI.get_order(instId="BTC-USDT-SWAP", ordId="505073046126960640")  
print(result)  

8. Canceling Orders

result = tradeAPI.cancel_order(instId="BTC-USDT-SWAP", ordId="505073046126960640")  
print(result)  

9. Modifying Orders

Update order size or price:

result = tradeAPI.amend_order(  
    instId="BTC-USDT-SWAP",  
    ordId="505073046126960640",  
    newSz="80"  
)  

10. Fetching Open Orders

result = tradeAPI.get_order_list()  
print(result)  

11. Order History (7 Days / 3 Months)

# Last 7 days  
result = tradeAPI.get_orders_history(instType="SWAP")  

# Last 3 months  
result = tradeAPI.get_orders_history_archive(instType="SWAP")  

12. Transaction Details

# Recent (3 days)  
result = tradeAPI.get_fills()  

# Extended (3 months)  
result = tradeAPI.get_fills_history(instType="SWAP")  

13. Monitoring Positions

Track unrealized PnL via upl:

result = accountAPI.get_positions()  
print(result)  

Advanced Examples

Download the full Jupyter Notebook here for comprehensive tutorials.

Need Help? Join the OKX API community for developer support.