How to Perform Derivative Trading Using Jupyter Notebook: A Step-by-Step Guide

ยท

Discover how to execute derivative trading efficiently using Jupyter Notebook and OKX's robust trading tools. This guide simplifies complex processes while leveraging advanced features for optimal results.

Types of Derivatives Available on OKX

OKX offers three primary derivative instruments:

  1. Expiry Contracts
    Fixed-term agreements settled at a future date.
  2. Perpetual Contracts
    No expiration date, with funding rates to balance prices.
  3. Options
    Rights (not obligations) to buy/sell at predetermined prices.

For this tutorial, we'll focus on Perpetual Contracts due to their flexibility and popularity.


Core Workflow for Derivative Trading

Step 1: Fetching Market Data

Retrieve real-time ticker data using OKX's Market Data API:

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

Key Parameters:

Step 2: Identifying Tradable Pairs

List available trading instruments:

import okx.PublicData as PublicData
publicDataAPI = PublicData.PublicAPI(flag="1")
result = publicDataAPI.get_instruments(instType="SWAP")
print(result)

Notional Value Calculation:
Use ctVal (contract value) and ctMult (contract multiplier) from the response:

notional_value = float(ctVal) * float(ctMult)  # Example: 10 * 1 = 10 USD

Account Configuration

Step 3: Checking Account Balance

Verify your trading balance:

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

Account Modes:

Only the last three modes support derivative trading.

Step 4: Setting Leverage

Adjust leverage for risk management (up to 125x on OKX):

# Cross-margin example
result = accountAPI.set_leverage(
    instId="BTC-USDT-SWAP",
    lever="5",
    mgnMode="cross"
)

Critical Parameters:


Placing and Managing Orders

Step 5: Executing Orders

Limit Order Example:

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

Market Order Example:

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

๐Ÿ‘‰ Master advanced order types with our pro trading guide.


Monitoring Positions and Performance

Step 6: Tracking Open Positions

result = accountAPI.get_positions()
print(result["data"][0]["upl"])  # Unrealized P&L

Step 7: Order History

Retrieve 3-month trade archives:

result = tradeAPI.get_orders_history_archive(instType="SWAP")

Frequently Asked Questions (FAQ)

Q1: How do I switch between long/short and net position modes?

accountAPI.set_position_mode(posMode="long_short_mode")

Q2: What's the difference between isolated and cross margin?

๐Ÿ‘‰ Detailed margin explanation

Q3: How are funding rates calculated in perpetual contracts?

Rates vary based on market conditions and are applied every 8 hours.


Pro Tips

For complete Jupyter Notebook examples, visit OKX's developer portal. Join our API community forum for real-time support.