Hi Sam, any plan to add also FUTURES API somewhere soon?
Hi Sam, doing amazing work !!!! I kinda have the same question as Bunkast haha, are you adding Futures Api ant time soon :D
Hi Sam,
Would love to see an API coming for this. Could solve many stuggles that I have on a project.
Kind regards
Hey Sam,
I also need the Futures API for my project.
Do you plan on adding this anytime soon?
Great work so far , love what you are doing!
Hi Sam,
I join the above, I also want to see the Futures API.
Thanks for the great API kit for Binance.
Hi Sam, when are you planning to release the future API feature? is there anything we can help with to speed up the development?
Let us know if we can help :)
If you need the websockets from binance.com/futures you can use https://github.com/oliver-zehentleitner/unicorn-binance-websocket-api
But its a plain Websocket API, no REST!
I tried to make a library for futures and testnet here https://github.com/morozdima/futurespy
v0.7.5 has been updated with Futures REST endpoints
@sammchardy Hi, are you going to create futures_historical_klines()? By analogy with get_historical_klines()
@alexzaporozhets did you get an answer? there are not that much data online....
@glaiveVII @alexzaporozhets
Study the repository, if you look in binance/client.py and search for 'futures' you'll see all the futures functions available.
As an example futures 1 minute kline data for ETHUSDT is obtained like this
klines = futures_klines(symbol='ETHUSDT',interval=Client.KLINE_INTERVAL_1MINUTE)
You can pass other options too, read
https://binance-docs.github.io/apidocs/futures/en/#kline-candlestick-data-market_data
@sloth456 thanks mate!
I have build some trading bot on the spot exchange I would like to move on the futures ones !
I want to build a WebSocket for the future exchange, pass different type of order, have any advice?
@glaiveVII
Websockets is only used to get realtime market data from binance, you cannot place trades via websocket.
Right now I have been using another module, unicorn binance websockets, to pull futures market data via websockets, I wrote about it here:
https://github.com/sammchardy/python-binance/issues/489#issuecomment-613572802
Placing orders on the futures exchange is covered by python-binance, example:
order = client.futures_create_order(symbol='ETHUSDT', side='SELL', type='MARKET', quantity='1')
For all the other available functions please just look at the repository code, and seach for 'futures'. All the functions are there along with a comment to the correct binance documentation page so you know what parameters to send.
https://github.com/sammchardy/python-binance/blob/master/binance/client.py
Ok thanks mate, I am looking your future solution it doesn't seem easy haha, I will try implement it.
So far I have some strategy build for the spot exchange, I only do market order, I will try order limit but it seems harder, any advice ?
Thanks a lot mate for your time !
@glaiveVII
Your questions are starting to reach outside of the scope of this thread. Maybe start a new thread with questions and relevant title, then I will try to help. It's just better for others searching for the same thing as you.
I've made my changes on the websocket.py file to have futures websocket :
class BinanceSocketManager(threading.Thread):
STREAM_URL = 'wss://stream.binance.com:9443/'
FUTURES_STREAM_URL = 'wss://fstream.binance.com/'
WEBSOCKET_DEPTH_5 = '5'
WEBSOCKET_DEPTH_10 = '10'
WEBSOCKET_DEPTH_20 = '20'
DEFAULT_USER_TIMEOUT = 30 * 60 # 30 minutes
def __init__(self, client, user_timeout=DEFAULT_USER_TIMEOUT, futures=False):
"""Initialise the BinanceSocketManager
:param client: Binance API client
:type client: binance.Client
:param user_timeout: Custom websocket timeout
:type user_timeout: int
"""
threading.Thread.__init__(self, name="binance-ws")
self._conns = {}
self._user_timer = None
self._user_listen_key = None
self._user_callback = None
self._client = client
self._user_timeout = user_timeout
self._future = futures
self._url = BinanceSocketManager.FUTURES_STREAM_URL if futures else BinanceSocketManager.FUTURES_STREAM_URL
[...]
def start_user_socket(self, callback):
"""Start a websocket for user data
https://www.binance.com/restapipub.html#user-wss-endpoint
:param callback: callback function to handle messages
:type callback: function
:returns: connection key string if successful, False otherwise
Message Format - see Binance API docs for all types
"""
# Get the user listen key
user_listen_key = self._client.future_stream_get_listen_key() if self._future else self._client.stream_get_listen_key()
# and start the socket with this specific key
conn_key = self._start_user_socket(user_listen_key, callback)
return conn_key
[...]
def _keepalive_user_socket(self):
try:
user_listen_key = self._client.future_stream_get_listen_key() if self._future else self._client.stream_get_listen_key()
except Exception as e:
# very rare exception ConnectTimeout
error_logger.error(repr(e))
# assume unchanged
user_listen_key = self._user_listen_key
# check if they key changed and
if user_listen_key != self._user_listen_key:
# Start a new socket with the key received
# `_start_user_socket` automatically cleanup open sockets
# and starts timer to keep socket alive
self._start_user_socket(user_listen_key, self._user_callback)
else:
# Restart timer only if the user listen key is not changed
self._start_user_timer()
[...]
For bookticker :
[...]
def start_book_ticker_socket(self, callback):
"""Start a websocket for all book ticker data
By default all markets are included in an array.
https://binance-docs.github.io/apidocs/futures/en/#all-market-tickers-streams
:param callback: callback function to handle messages
:type callback: function
:returns: connection key string if successful, False otherwise
Message Format
.. code-block:: python
[
{
"u":400900217, // order book updateId
"s":"BNBUSDT", // symbol
"b":"25.35190000", // best bid price
"B":"31.21000000", // best bid qty
"a":"25.36520000", // best ask price
"A":"40.66000000" // best ask qty
}
]
"""
return self._start_socket('!bookTicker', callback)
[...]
And in client.py
def future_stream_get_listen_key(self):
"""Start a new user data stream and return the listen key
If a stream already exists it should return the same key.
If the stream becomes invalid a new key is returned.
Can be used to keep the user stream alive.
https://binance-docs.github.io/apidocs/futures/en/#start-user-data-stream-user_stream
:returns: API response
.. code-block:: python
{
"listenKey": "pqia91ma19a5s61cv6a81va65sdf19v8a65a1a5s61cv6a81va65sdf19v8a65a1"
}
:raises: BinanceRequestException, BinanceAPIException
"""
res = self._request_futures_api('post', 'listenKey', True, data={})
return res['listenKey']
So that I can call like :
ws = BinanceSocketManager(self._session, futures=True)
@sloth456
How do you create a close position order on futures api? As I understand when an order is opened and in position of trading, when we set limit or market close position, they will create an opposite order to close the current one but I think I having some issue
@chaudoe I have answered this on your thread #536
@glaiveVII @alexzaporozhets
Study the repository, if you look in binance/client.py and search for 'futures' you'll see all the futures functions available.
As an example futures 1 minute kline data for ETHUSDT is obtained like this
klines = futures_klines(symbol='ETHUSDT',interval=Client.KLINE_INTERVAL_1MINUTE)You can pass other options too, read
https://binance-docs.github.io/apidocs/futures/en/#kline-candlestick-data-market_data
is these functions supports coin futures as well ?
Most helpful comment
Hi Sam,
I join the above, I also want to see the Futures API.
Thanks for the great API kit for Binance.