David

How to query on-the-run future data?

Programming


Hello community! 
According to REST API Doc, I know I can query historical OHLC data for plain instrument using /history_price

However, for future contract like HSIFUT, it requires specifying expiry date in request parameters. 
For my case, I want to get all the historical on-the-run future data. 
Is there a simpler way to get the data at once, instead of repeatedly sending requests for different expiry date?
 
admin

Our API newly supports query of combined future chain data. In request parameter 'chain_dated', you can set value '1' to get OHLC for all spot month contracts.

REST API

Please refer to the "Future (combined chain)" example at Deverloper API Doc.


Web API

Technical details please refer to secion Query Historical Market Data in Web API Doc.

Here is a simple example to implement on the Web IDE.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
from AlgoAPI import AlgoAPIUtil, AlgoAPI_Backtest
import pandas as pd

class AlgoEvent:
    def __init__(self):
        self.data = []
        
    def start(self, mEvt):
        self.evt = AlgoAPI_Backtest.AlgoEvtHandler(self, mEvt)
        self.evt.start()
        
    def on_marketdatafeed(self, md, ab):
        if len(self.data)==0:

            # get last 60 closing price of the Spot month future 
            self.data = self.evt.getHistoricalBar(contract={"instrument":"HSIFUT"}, numOfBar=40, interval="D", chain_dated=1)

            # make data frame
            df = pd.DataFrame([self.data[i] for i in self.data])

            # print to console
            self.evt.consoleLog(df)

From console output, we can see the 2 spot-month future contracts (i.e. expiry date 2021/04/29 and 2021/05/28) are concat in 1 time series.

result