Multiple data feeds - Strategy starting later
-
Hi,
I am using multiple data feeds (all daily data but with different timeline) and a dummy data feed added first that has the full timeline (all possible times 'seen' in the rest). The strategy does not use any indicators, just printing the timeline.
The strategy starts later than expected. I can see using prenext() that minimum period is later than expected. If I prepend all data with a dummy bar with the first time as in the dummy data feed, the strategy starts correctly.
I wonder why the above behaviour happens. I thought minimum period is something used for indicators. Is there some option I am missing?
Thanks
from __future__ import (absolute_import, division, print_function, unicode_literals) import backtrader as bt import numpy as np import pandas as pd import random # Create a Stratey class TestStrategy(bt.Strategy): def log(self, txt, dt=None): ''' Logging function for this strategy''' dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def next(self): self.log("{}, {}".format(self.datas[0].lines.close[0], self.datas[1].lines.close[0])) if __name__ == '__main__': # Create a cerebro entity cerebro = bt.Cerebro() # Add a strategy with parameters cerebro.addstrategy(TestStrategy) df_timeline = pd.DataFrame({'Close': random.sample(range(100), 10)}, index=pd.date_range(start='1/1/2018', periods=10)) df1 = pd.DataFrame({'Close': random.sample(range(100), 7)}, index=pd.date_range(start='1/3/2018', periods=7)) # Adding the below it works # df_first = pd.DataFrame([[np.nan]], index=[df_timeline.index[0]], columns=df1.columns) # df1 = df_first.append(df1) cerebro.adddata(bt.feeds.PandasData(dataname=df_timeline, nocase=True)) cerebro.adddata(bt.feeds.PandasData(dataname=df1, nocase=True)) # Set our desired cash start cerebro.broker.setcash(100000.0) # Print out the starting conditions print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) # Run over everything cerebro.run(runonce=True, preload=True) # Print out the final result print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
-
@momentum said in Multiple data feeds - Strategy starting later:
I thought minimum period is something used for indicators. Is there some option I am missing?
Yes. The minimum period applies to anything to all objects with lines, because the buffers (i.e.: when you access
[0]
) have to produce something. That's guaranteed innext
.If some data feeds start later, they wouldn't be able to produce data when the earlier data feeds do it, hence breaking the contract which you have in
next
.