For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Having historic data in the strategy

  • Firstly thanks for developing such a fabulous program.
    I want to test a strategy. This strategy involves trading on 1 minutes chart. But it involves looking at historical data (Say Daily data of last 200 days and previous close etc.)
    I want to calculate SMA/EMA of historic data only once. How can I load historical data and calculate SMA/EMA over it.

    Can someone please point me to documentation.

    Thanks in advance.

  • In the below examlpe, I am trying to calculate historic avegarge volume in the constructor. I am doing this in gethistoricAverageOpeningVolume(). I am printing length of historic data which is 0.

    class TestStrategy(MyBTStrategy):
        def __init__(self):
            self.dataclose = self.datas[0].close
            self.histAvgVolume = TestStrategy.gethistoricAverageOpeningVolume(self.datas[1], 3)
        def gethistoricAverageOpeningVolume(cls, histData, numDays):
            prevDate = cls.utcfloat_to_datetime(histData.datetime[0]).date()
            print(len((histData))) #The length is 0
        def next(self):
    		#I would use self.histAvgVolume here to compare with current volume
    if __name__ == '__main__':
        # Create a cerebro entity
        cerebro = bt.Cerebro()
        instrument = 'SBIN'
        # Add a strategy
        cerebro.addstrategy(TestStrategy, maperiod=10)
        # Create a Data Feed
        data_min = bt.feeds.ElasticDB(
    	fromdate=datetime.datetime(2018, 12, 12),
            todate=datetime.datetime(2018, 12, 12),
        data_day = bt.feeds.ElasticDB(
            fromdate=datetime.datetime(2018, 12, 12) - datetime.timedelta(days=10),
            todate=datetime.datetime(2018, 12, 12) - datetime.timedelta(days=1),
        cerebro.adddata(data_min, name=instrument + '_min')
        cerebro.adddata(data_day, name=instrument + '_day')

  • administrators

    It seems surprising in any case that someone who has a feed named ElasticDB (which is not part of the ecosystem) doesn't even know the basics ...

  • @backtrader That was rude! I didn't find about historic data in Documentation.

  • @noobtrader Including ElasticSearch was not that difficult.

  • Please tell how to do it. Currently I am fetching pandas dataframe from inside the strategy.

  • @ab_trader The link doesn't describe give inputs on how to have historical data along with live data.

  • @noobtrader nothing in your post shows that you are using live data. So why do you expect to have it in the answer?