Skip to content

Commit

Permalink
feat(client): add futures_mark_price_klines, futures_index_price_klin…
Browse files Browse the repository at this point in the history
…es, futures_premium_index_klines (#1498)

* feat(module): add futures mark price klines

* add tests

* add indexPriceKlines

* extend enum and complete _klines

* add futures_premium_index_klines

* complete klines

* add skipped test

* format files

---------

Co-authored-by: carlosmiei <[email protected]>
  • Loading branch information
zhen1007 and carlosmiei authored Dec 4, 2024
1 parent f5c63e1 commit 7226996
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 2 deletions.
38 changes: 37 additions & 1 deletion binance/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,14 @@ async def _klines(
return await self.futures_klines(**params)
elif HistoricalKlinesType.FUTURES_COIN == klines_type:
return await self.futures_coin_klines(**params)
elif HistoricalKlinesType.FUTURES_MARK_PRICE == klines_type:
return await self.futures_mark_price_klines(**params)
elif HistoricalKlinesType.FUTURES_INDEX_PRICE == klines_type:
return await self.futures_index_price_klines(**params)
elif HistoricalKlinesType.FUTURES_COIN_MARK_PRICE == klines_type:
return await self.futures_coin_mark_price_klines(**params)
elif HistoricalKlinesType.FUTURES_COIN_INDEX_PRICE == klines_type:
return await self.futures_coin_index_price_klines(**params)
else:
raise NotImplementedException(klines_type)

Expand Down Expand Up @@ -1667,6 +1675,21 @@ async def futures_aggregate_trades(self, **params):
async def futures_klines(self, **params):
return await self._request_futures_api("get", "klines", data=params)

async def futures_mark_price_klines(self, **params):
return await self._request_futures_api("get", "markPriceKlines", data=params)

futures_mark_price_klines.__doc__ = Client.futures_mark_price_klines.__doc__

async def futures_index_price_klines(self, **params):
return await self._request_futures_api("get", "indexPriceKlines", data=params)

futures_index_price_klines.__doc__ = Client.futures_index_price_klines.__doc__

async def futures_premium_index_klines(self, **params):
return await self._request_futures_api("get", "premiumIndexKlines", data=params)

futures_premium_index_klines.__doc__ = Client.futures_index_price_klines.__doc__

async def futures_continous_klines(self, **params):
return await self._request_futures_api("get", "continuousKlines", data=params)

Expand Down Expand Up @@ -1992,6 +2015,17 @@ async def futures_coin_mark_price_klines(self, **params):
"get", "markPriceKlines", data=params
)

futures_coin_mark_price_klines.__doc__ = Client.futures_mark_price_klines.__doc__

async def futures_coin_premium_index_klines(self, **params):
return await self._request_futures_coin_api(
"get", "premiumIndexKlines", data=params
)

futures_coin_premium_index_klines.__doc__ = (
Client.futures_premium_index_klines.__doc__
)

async def futures_coin_mark_price(self, **params):
return await self._request_futures_coin_api("get", "premiumIndex", data=params)

Expand Down Expand Up @@ -3803,4 +3837,6 @@ async def options_account_get_block_trades(self, **params):
"get", "block/user-trades", signed=True, data=params
)

options_account_get_block_trades.__doc__ = Client.options_account_get_block_trades.__doc__
options_account_get_block_trades.__doc__ = (
Client.options_account_get_block_trades.__doc__
)
68 changes: 68 additions & 0 deletions binance/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,14 @@ def _klines(
return self.futures_klines(**params)
elif HistoricalKlinesType.FUTURES_COIN == klines_type:
return self.futures_coin_klines(**params)
elif HistoricalKlinesType.FUTURES_MARK_PRICE == klines_type:
return self.futures_mark_price_klines(**params)
elif HistoricalKlinesType.FUTURES_INDEX_PRICE == klines_type:
return self.futures_index_price_klines(**params)
elif HistoricalKlinesType.FUTURES_COIN_MARK_PRICE == klines_type:
return self.futures_coin_mark_price_klines(**params)
elif HistoricalKlinesType.FUTURES_COIN_INDEX_PRICE == klines_type:
return self.futures_coin_index_price_klines(**params)
else:
raise NotImplementedException(klines_type)

Expand Down Expand Up @@ -7092,6 +7100,30 @@ def futures_klines(self, **params):
"""
return self._request_futures_api("get", "klines", data=params)

def futures_mark_price_klines(self, **params):
"""Kline/candlestick bars for the mark price of a symbol. Klines are uniquely identified by their open time.
https://binance-docs.github.io/apidocs/futures/en/#mark-price-kline-candlestick-data
"""
return self._request_futures_api("get", "markPriceKlines", data=params)

def futures_index_price_klines(self, **params):
"""Kline/candlestick bars for the index price of a symbol. Klines are uniquely identified by their open time.
https://binance-docs.github.io/apidocs/futures/en/#index-price-kline-candlestick-data
"""
return self._request_futures_api("get", "indexPriceKlines", data=params)

def futures_premium_index_klines(self, **params):
"""Premium index kline bars of a symbol.l. Klines are uniquely identified by their open time.
https://binance-docs.github.io/apidocs/futures/en/#premium-index-kline-data
"""
return self._request_futures_api("get", "premiumIndexKlines", data=params)

def futures_continous_klines(self, **params):
"""Kline/candlestick bars for a specific contract type. Klines are uniquely identified by their open time.
Expand Down Expand Up @@ -7128,6 +7160,34 @@ def futures_historical_klines(
klines_type=HistoricalKlinesType.FUTURES,
)

def futures_historical_mark_price_klines(
self, symbol, interval, start_str, end_str=None, limit=500
):
"""Get historical futures mark price klines from Binance
:param symbol: Name of symbol pair e.g. BNBBTC
:type symbol: str
:param interval: Binance Kline interval
:type interval: str
:param start_str: Start date string in UTC format or timestamp in milliseconds
:type start_str: str|int
:param end_str: optional - end date string in UTC format or timestamp in milliseconds (default will fetch everything up to now)
:type end_str: str|int
:param limit: Default 500; max 1000.
:type limit: int
:return: list of OHLCV values (Open time, Open, High, Low, Close, Volume, Close time, Quote asset volume, Number of trades, Taker buy base asset volume, Taker buy quote asset volume, Ignore)
"""
return self._historical_klines(
symbol,
interval,
start_str,
end_str=end_str,
limit=limit,
klines_type=HistoricalKlinesType.FUTURES_MARK_PRICE,
)

def futures_historical_klines_generator(
self, symbol, interval, start_str, end_str=None
):
Expand Down Expand Up @@ -7769,6 +7829,14 @@ def futures_coin_index_price_klines(self, **params):
"""
return self._request_futures_coin_api("get", "indexPriceKlines", data=params)

def futures_coin_premium_index_klines(self, **params):
"""Kline/candlestick bars for the index price of a pair..
https://binance-docs.github.io/apidocs/delivery/en/#premium-index-kline-data
"""
return self._request_futures_coin_api("get", "premiumIndexKlines", data=params)

def futures_coin_mark_price_klines(self, **params):
"""Kline/candlestick bars for the index price of a pair..
Expand Down
4 changes: 4 additions & 0 deletions binance/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class HistoricalKlinesType(Enum):
SPOT = 1
FUTURES = 2
FUTURES_COIN = 3
FUTURES_MARK_PRICE = 4
FUTURES_INDEX_PRICE = 5
FUTURES_COIN_MARK_PRICE = 6
FUTURES_COIN_INDEX_PRICE = 7


class FuturesType(Enum):
Expand Down
17 changes: 17 additions & 0 deletions tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ async def test_get_klines(clientAsync):
await clientAsync.get_klines(symbol="BTCUSDT", interval="1d")


async def test_futures_mark_price_klines(clientAsync):
await clientAsync.futures_mark_price_klines(symbol="BTCUSDT", interval="1h")


async def test_futures_index_price_klines(clientAsync):
await clientAsync.futures_index_price_klines(pair="BTCUSDT", interval="1h")


async def test_futures_premium_index_klines(clientAsync):
await clientAsync.futures_premium_index_klines(symbol="BTCUSDT", interval="1h")


@pytest.mark.skip(reason="network error")
async def test_futures_coin_premium_index_klines(clientAsync):
await clientAsync.futures_coin_premium_index_klines(symbol="BTCUSD", interval="1h")


async def test_get_avg_price(clientAsync):
await clientAsync.get_avg_price(symbol="BTCUSDT")

Expand Down
4 changes: 3 additions & 1 deletion tests/test_async_client_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,4 +598,6 @@ async def test_futures_coin_account_trade_history_download(futuresClientAsync):

@pytest.mark.skip(reason="No sandbox support")
async def test_futures_coin_account_trade_download_id(futuresClientAsync):
await futuresClientAsync.futures_coin_account_trade_history_download_link(downloadId="123")
await futuresClientAsync.futures_coin_account_trade_history_download_link(
downloadId="123"
)
15 changes: 15 additions & 0 deletions tests/test_client_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ def test_futures_klines(futuresClient):
futuresClient.futures_klines(symbol="BTCUSDT", interval="1h")


def test_futures_mark_price_klines(futuresClient):
futuresClient.futures_mark_price_klines(symbol="BTCUSDT", interval="1h")


def test_futures_index_price_klines(futuresClient):
futuresClient.futures_index_price_klines(pair="BTCUSDT", interval="1h")


def test_futures_premium_index_klines(futuresClient):
futuresClient.futures_premium_index_klines(symbol="BTCUSDT", interval="1h")


def test_futures_continous_klines(futuresClient):
futuresClient.futures_continous_klines(
pair="BTCUSDT", contractType="PERPETUAL", interval="1h"
Expand Down Expand Up @@ -567,10 +579,12 @@ def test_futures_coin_stream_close(futuresClient):
listen_key = futuresClient.futures_coin_stream_get_listen_key()
futuresClient.futures_coin_stream_close(listenKey=listen_key)


########################################################
# Test block trades
########################################################


@pytest.mark.skip(reason="No sandbox support")
def test_futures_coin_account_order_history_download(futuresClient):
futuresClient.futures_coin_account_order_download()
Expand Down Expand Up @@ -632,6 +646,7 @@ def test_futures_coin_account_order_download_id_mock(futuresClient):
)
assert response == expected_response


def test_futures_coin_account_trade_history_download_id_mock(futuresClient):
expected_response = {
"avgCostTimestampOfLast30d": 7241837,
Expand Down

0 comments on commit 7226996

Please sign in to comment.