Discover how to execute straightforward derivatives trading using the same tools. Leverage the extensive features available at a higher level!
Types of Derivatives
OKX offers three types of derivatives for trading:
- Expiry Contracts
- Perpetual Swaps
- Options
๐ Explore derivatives on OKX to learn more about their characteristics. This tutorial uses Perpetual Swaps as an example.
Frequently Asked Questions (FAQ)
1. How to Retrieve Market Data?
Replace instType with EXPIRY or OPTION to fetch relevant data.
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?
Select instType to filter instruments.
import okx.PublicData as PublicData
publicDataAPI = PublicData.PublicAPI(flag="1")
result = publicDataAPI.get_instruments(instType="SWAP")
print(result)2.1 Calculate Contract Nominal Value
Use ctVal (contract value) and ctMult (contract multiplier) from instrument parameters:
Nominal Value = ctVal * ctMult (unit: ctValCcy).
Example for LTC-USD-SWAP: 10 (ctVal) * 1 (ctMult) = 10 USD
3. How to Check Account Balance?
import okx.Account as Account
accountAPI = Account.AccountAPI(api_key, secret_key, passphrase, False, "1")
result = accountAPI.get_account_balance()
print(result)4. Which Account Modes Allow Derivatives Trading?
Only these margin modes qualify:
- Spot & Futures
- Multi-Currency Margin
- Portfolio Margin
Verify mode via acctLv parameter:
result = accountAPI.get_account_config()
if result["code"] == "0":
acctLv = result["data"][0]["acctLv"]
print(f"Current mode: {acctLv}")5. How to Set Leverage?
Traders can set leverage up to 125x. Key terms:
- Max Leverage: Capital multiplier (e.g., 75x).
- Initial Margin Ratio (IMR): 1.3% for 75x leverage.
- Maintenance Margin Ratio (MMR): Minimum 0.8% to avoid liquidation.
Example for BTC-USDT-SWAP:
accountAPI.set_leverage(
instId="BTC-USDT-SWAP",
lever="5",
mgnMode="cross" # or "isolated"
)6. How to Place Orders in Long/Short or Buy/Sell Modes?
Switch position mode via API:
accountAPI.set_position_mode(posMode="long_short_mode")- Buy/Sell (Net): Net position;
posSidedefaults tonet. - Long/Short: Independent positions; specify
posSideaslongorshort.
Place a limit order:
tradeAPI.place_order(
instId="BTC-USDT-SWAP",
tdMode="isolated",
side="buy",
posSide="long",
ordType="limit",
px="19000",
sz="100"
)7. How to Check Order Status?
tradeAPI.get_order(instId="BTC-USDT-SWAP", ordId="505073046126960640")8. How to Cancel an Order?
tradeAPI.cancel_order(instId="BTC-USDT-SWAP", ordId="505073046126960640")9. How to Modify an Order?
Example: Adjust order size.
tradeAPI.amend_order(
instId="BTC-USDT-SWAP",
ordId="505073046126960640",
newSz="80"
)10. How to List Open Orders?
tradeAPI.get_order_list()11. How to Access Order History?
# Last 7 days
tradeAPI.get_orders_history(instType="SWAP")
# Last 3 months
tradeAPI.get_orders_history_archive(instType="SWAP")12. How to Retrieve Transaction Details?
# Last 3 days
tradeAPI.get_fills()
# Last 3 months
tradeAPI.get_fills_history(instType="SWAP")13. How to Monitor Positions?
accountAPI.get_positions()Track unrealized P&L via the upl parameter.
Additional Resources
๐ Download full Jupyter Notebook for extended examples. Join the OKX community for API-related queries.
Key Takeaways
- Use Perpetual Swaps for continuous trading without expiry.
- Optimize leverage settings to balance risk and reward.
- Choose between Long/Short or Buy/Sell modes based on strategy.
For advanced trading scenarios, explore OKXโs comprehensive API documentation.