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:
- Expiry Contracts
Fixed-term agreements settled at a future date. - Perpetual Contracts
No expiration date, with funding rates to balance prices. - 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:
instType: Instrument type (e.g., "SWAP" for perpetuals)flag: "0" for live trading, "1" for demo
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 USDAccount 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:
- Simple (Spot-only)
- Single-currency Margin
- Multi-currency Margin
- Portfolio Margin
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:
mgnMode: "isolated" or "cross"posSide: Required for isolated long/short positions
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&LStep 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?
- Isolated: Margin per position (no risk spillover)
- Cross: Shared margin across positions
๐ 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
- Use Markdown tables to organize parameter lists
- Regularly check
acctLvto confirm trading permissions - Monitor
mmr(Maintenance Margin Ratio) to avoid liquidation
For complete Jupyter Notebook examples, visit OKX's developer portal. Join our API community forum for real-time support.