Tommy

How to get historical data before algo starts?

Programming


My strategy needs 1-year of past closing prices to build the tensorflow model. 
I see the platform processing data in a stream format. 
Is it possible to get historical data before the algo starts? 

 
admin

Hi Tommy, you can use the getHistoricalBar function at def start to collect and initialize your closing price variable.

You can refer to line 13 in the example below.

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

class AlgoEvent:
    def __init__(self):
        self.arrClose = []
        self.numobs = 5
        self.instrument = "USDJPY"

    def start(self, mEvt):
        self.evt = AlgoAPI_Backtest.AlgoEvtHandler(self, mEvt)

        # initialize array with historical observations
        obs = self.evt.getHistoricalBar(contract={"instrument":self.instrument}, numOfBar=self.numobs, interval="D")
        self.arrClose = [obs[t]['c'] for t in obs]

        self.evt.start()

    def on_marketdatafeed(self, md, ab):
        # append new data 
        self.arrClose.append(md.lastPrice)
        self.arrClose = self.arrClose[-self.numobs:]

        # trading logics below ...