12

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
eetiaro
  • 342
  • 1
  • 3
  • 14

5 Answers5

22

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...
Igor Kroitor
  • 1,548
  • 13
  • 16
  • 1
    Could you also provide an example for a market buy and a market sell order on binance futures? How to set the leverage? – eetiaro Dec 14 '19 at 13:34
  • 2
    @eetiaro you can find some examples here: https://github.com/ccxt/ccxt/tree/master/examples/py let us know if that does not help – Igor Kroitor Jun 07 '20 at 09:43
  • Note that this is no longer the recommended way. See my answer below. – P i Jan 29 '22 at 09:57
6

Put quotes around defaultMarket:

binance_futures = ccxt.binance({ 'option': { 'defaultMarket': 'futures' } })
Pedram Parsian
  • 3,750
  • 3
  • 19
  • 34
zok
  • 69
  • 1
  • With quotes I get no error anymore, but it still doesn't trades on the futures account. Orders are placed on the normal exchange instead. Any ideas? – eetiaro Dec 14 '19 at 07:55
  • 1
    @eetiaro it is `'future'`, not `'futures'`: `binance_futures = ccxt.binance({ 'option': { 'defaultMarket': 'future' } })` – Igor Kroitor Dec 14 '19 at 10:30
  • 1
    Also answered here: https://github.com/ccxt/ccxt/issues/6273#issuecomment-565563176 – Igor Kroitor Dec 14 '19 at 10:32
4

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:

Dated Futures


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:

Spot Markets

StackG
  • 2,730
  • 5
  • 29
  • 45
3

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).

P i
  • 29,020
  • 36
  • 159
  • 267
  • The latest ccxt document shows nothing when you search `defaultType`. The ccxt **official document really really sucks**, every exchange category their API endpoint by Spot/Margin, Futures, Linear/Inverse, the ccxt is Unified API, Implicit API, bla bla,,, nobody understand what's that about. – Crypto营长 Sep 29 '22 at 11:29
  • Fortunately they have an active Discord (you can even connect with the lead dev). – P i Sep 30 '22 at 09:34
2

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
  • This is incorrect and dangerous. By setting `.options` you are erasing the existing `.options` dict. `exchange.options['defaultType'] = 'future'` is safe, as it only APPENDS to the dict. – P i Jan 29 '22 at 09:54