Binance Delivery Futures Historical Prices

Viewed 570

I'm trying to get from binance the historical futures prices of the futures contract that expires on the 31st of December 2021.
I have figured this out for perps but am struggling with a futures contract with a delivery date. The code for the perps is below

df = pd.DataFrame(client.futures_historical_klines(
    symbol='BTCUSDT',
    interval='1d',
    start_str='2021-06-01',
    end_str='2021-06-30'
))

I assumed that replacing the symbol with BTCUSD_211231 or BTCUSDT_211231 would have done the trick, but unfortunately I get the below error message:

BinanceAPIException: APIError(code=-1121): Invalid symbol.

Any help is much appreciated!
Thanks

1 Answers

According to the binance documentation, you can set contractType for the desired contract.

The following options for contractType are available:

  • PERPETUAL
  • CURRENT_MONTH
  • NEXT_MONTH
  • CURRENT_QUARTER
  • NEXT_QUARTER

The following code works for me:

import binance
import pandas as pd

client = binance.Client()

r = client.futures_continous_klines(
    pair='BTCUSDT',
    contractType='CURRENT_QUARTER',
    interval='1d',
)
df = pd.DataFrame(r)

print(df)

Output:

                0        1        2        3        4         5              6               7      8         9              10 11
0    1612310400000  36054.1  39000.0  31111.0  38664.1   688.738  1612396799999   21632849.1822  33908   336.944  10440572.5057  0
1    1612396800000  38664.1  43820.2  37445.4  38328.3   757.761  1612483199999   29584739.5781  16925   387.058  15156395.7362  0
2    1612483200000  38304.6  39955.4  37858.3  39848.1   383.410  1612569599999   14995639.3696   5563   183.214   7170636.7752  0
3    1612569600000  39876.5  42727.3  39437.6  41245.1   453.336  1612655999999   18775322.9609   8898   225.566   9347858.6798  0
4    1612656000000  41240.3  41642.5  38639.2  40592.0   428.693  1612742399999   17269756.5859   7553   202.850   8175165.3435  0
..             ...      ...      ...      ...      ...       ...            ...             ...    ...       ...            ... ..
242  1633219200000  48654.6  50479.1  48100.0  49325.1  2222.058  1633305599999  109305621.1491  27633  1106.026  54396950.3347  0
243  1633305600000  49325.1  50738.3  47961.5  50461.2  1808.321  1633391999999   89174286.5122  28367   925.258  45643475.0561  0
244  1633392000000  50491.5  53151.3  50245.3  52764.5  1860.870  1633478399999   95741544.2087  29105   921.897  47452449.1528  0
245  1633478400000  52769.4  57442.1  51606.6  56710.1  2431.580  1633564799999  132849081.0296  38013  1225.920  67014360.6873  0
246  1633564800000  56723.3  56739.9  54769.0  55645.2  1188.181  1633651199999   66322580.2665  21176   570.967  31878146.7653  0

[247 rows x 12 columns]

The meaning of each column in the above dataframe is documented in the Binance documentation (see right side, under "Response").

Related