How to get the first date of a datafeed?
-
In def init of a strategy, I need to get external data depend on the first day of data-feed. Is it possible to to that?
# Create a Stratey class TestStrategy(bt.Strategy): params = (('maperiod', 60), ) def log(self, txt, dt=None): ''' Logging function fot this strategy''' dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close self.sma = bt.indicators.SimpleMovingAverage( self.datas[0], period=self.params.maperiod) # my question is here first_date = ????? my_ind_data = mydb.get_my_data("news_hit").loc[start_day:] news_ind = bt.indicators.SimpleMovingAverage(my_ind_data,period=5)
-
I try this , but fail
# Create a Stratey class TestStrategy(bt.Strategy): params = (('maperiod', 60), ) def log(self, txt, dt=None): ''' Logging function fot this strategy''' dt = dt or self.datas[0].datetime.date(0) print('%s, %s' % (dt.isoformat(), txt)) def __init__(self): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close self.sma = bt.indicators.SimpleMovingAverage( self.datas[0], period=self.params.maperiod) # my question is here first_date = self.datas[0][0] my_ind_data = mydb.get_my_data("news_hit").loc[first_data:] news_ind = bt.indicators.SimpleMovingAverage(my_ind_data,period=5)
-
The data bars can not be accessed (examined) during the
__init__
method since the bars were not yet loaded and the data feed was not yet started. You may try to access thefromdate
data parameter (self.datas[0].p.fromdate
) if such was supplied. However, this is also not a good option since firstly such a parameter may not be actually supplied, and secondly, the actual dates in the data feed may not exactly match thefromdate
parameter.As for the actual reason for your question - IMHO it is better to just define a custom data feed for your database ( if such does not already exist ) and use it instead of (or in addition to) the data feed you already supply to Cerebro engine.
-
@vladisld Thanks for you advice. I thought about define custom data feed, but there are thousands of stocks when I run the strategy, and they share a same "my_ind_data" line(and "my_ind_data" is not the only custom data). so that will increase memory usage obviously.