eetiaro
eetiaro

Reputation: 352

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

Upvotes: 12

Views: 30220

Answers (5)

P i
P i

Reputation: 30684

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

Upvotes: 3

StackG
StackG

Reputation: 2858

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

Upvotes: 4

Yeabsra Hailu
Yeabsra Hailu

Reputation: 29

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

Upvotes: 2

Igor Kroitor
Igor Kroitor

Reputation: 1646

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

Upvotes: 23

zok
zok

Reputation: 69

Put quotes around defaultMarket:

binance_futures = ccxt.binance({ 'option': { 'defaultMarket': 'futures' } })

Upvotes: 6

Related Questions