'How to make a binance futures order with ccxt in python?
How can I place a market order in ccxt for binance futures? Trading on binance futures with ccxt is already implemented
https://github.com/ccxt/ccxt/pull/5907
In this post they suggest to use this line of code:
let binance_futures = new ccxt.binance({ options: { defaultMarket: 'futures' } })
The above line was written in JavaScript. How would the equivalent line in python look like? Like this I get an error:
binance_futures = ccxt.binance({ 'option': { defaultMarket: 'futures' } })
NameError: name 'defaultMarket' is not defined
Solution 1:[1]
The correct answer is that 'defaultType' (instead of defaultMarket) must be in quotes, but also the value must be 'future' (not 'futures')
import ccxt
print('CCXT version:', ccxt.__version__) # requires CCXT version > 1.20.31
exchange = ccxt.binance({
'apiKey': 'YOUR_API_KEY',
'secret': 'YOUR_API_SECRET',
'enableRateLimit': True,
'options': {
'defaultType': 'future', # ?-------------- quotes and 'future'
},
})
exchange.load_markets()
# exchange.verbose = True # uncomment this line if it doesn't work
# your code here...
Solution 2:[2]
Put quotes around defaultMarket:
binance_futures = ccxt.binance({ 'option': { 'defaultMarket': 'futures' } })
Solution 3:[3]
If you are looking for Binance's COIN-M futures (eg. for cash-futures basis trading), you need to use the delivery option instead:
import ccxt
import pandas as pd
binance = ccxt.binance()
binance.options = {'defaultType': 'delivery', 'adjustForTimeDifference': True}
securities = pd.DataFrame(binance.load_markets()).transpose()
securities
dated futures as expected:
Please note that the above snippet returns the dated futures as expected. However, the alternative one-line solution shown below DOES NOT (it seems to be defaulting to spot markets):
import ccxt
import pandas as pd
binance = ccxt.binance({'option': {'defaultType': 'delivery', 'adjustForTimeDifference': True}})
securities = pd.DataFrame(binance.load_markets()).transpose()
securities
returns spot markets:
Solution 4:[4]
Instantiate the binance exchange and tweak its options property to {'defaultType': 'future'}
import ccxt
exchange_id = 'binance'
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class({
'apiKey': 'your-public-api-key',
'secret': 'your-api-secret',
'timeout': 30000,
'enableRateLimit': True,
})
exchange.options = {'defaultType': 'future'}
markets = exchange.load_markets() # Load the futures markets
for market in markets:
print(market) # check for the symbol you want to trade here
# The rest of your code goes here
Solution 5:[5]
The new recommended way to create the exchange object is:
exchange = ccxt.binanceusdm({
'apiKey': ...,
'secret': ...,
})
This has been comfirmed by the project maintainer (@kroitor) on Discord.
The old way (which at time of writing still works) was:
exchange = ccxt.binance({
'apiKey': ...,
'secret': ...,
'options': {
'defaultType': 'future',
},
})
Example code in the repo has not yet been updated (also confirmed).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Pedram |
| Solution 3 | |
| Solution 4 | |
| Solution 5 | P i |


