Multiple time period on a single stock
-
Hello, I have my customized indicator (similar to RSI and stochastic) that changes values based on a certain period. The value of the indicator will change as time passes. I wanted to do a backtest for the following scenario :
1/1/2000 to 1/1/2005 is the initial period to generate the indicator value.
2/1/2005 onwards , start simulation of backtesting to track indicator for buy and sell signal.As each day pass, the value of the indicator will change but the starting point i.e. 1/1/2000 is anchored. I read that Backtrader support multiple timeframe but this is multiple period. Is there a code reference for multi period back testng ? I am trying to code along the following but somehow, i sense that it is not the correct way and even if it is, i need some help with the looping of the date...
cerebro = bt.Cerebro(stdstats=True)
cerebro.addstrategy(MyStrategy)-- a special loop to increment date by 1 day until year Jun 2019
full_df = ...panda frame obtained from reading database for indicator value from period 1/1/2000 to 1/1/2005. Every loop increment by 1 day.data = MyDataLoader(dataname=full_df)
cerebro.adddata(data)
---- end loop ---------------
cerebro.broker.setcash(100000.0)
cerebro.addsizer(bt.sizers.FixedSize, stake=1)
cerebro.broker.setcommission(commission=0.0)
cerebro.run() -
What I seem to understand is that you are creating survivorship bias ...
Read:
- Articles - Momentum Strategy - Next and PreNext
- Docs - - Minimum Period
-
@backtrader
Thanks, i am studying the following code and need clarification on two itemclass Momentum(bt.Indicator): lines = ('trend',) params = (('period', 90),)``` def __init__(self): self.addminperiod(self.params.period) def next(self): returns = np.log(self.data.get(size=self.p.period)) x = np.arange(len(returns)) slope, _, rvalue, _, _ = linregress(x, returns) annualized = (1 + slope) ** 252 self.lines.trend[0] = annualized * (rvalue ** 2)
Item 1 :
I understand that next method will be called every tick after the minimum period. As an example, if minimum period is 20, after iterating through 20 closing prices, on the 21st closing price,self.data.get(size=self.p.period)
will have the first 20 data points from the datafeed. Subsequent call will be retrieving 0 to 21 data point, and following this, the third call will have 0 to 22 set of data point and so on. Is this the behavior ? I am looking for an anchor starting point and not a moving period.
Item 2:
Also i need two array to do computation so since i am using Panda Dataframe, instead of self.data.get(), can I use self.data.close.get() and self.data.datetime.get() ? -
Item 0
For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
It's at the top of each page.
-
@backtrader
Thanks for the help with the formatting. I had edited the second post with the correct formatting. Appreciate if you are able to provide some answer to the clarification. -
@andrewsky said in Multiple time period on a single stock:
self.data.get(size=self.p.period)
If you ask for
size=self.p.period
you are always going to getperiod
data points. If you need the array to grow with each iteration use thelen
of the data which keeps on growing with each iteration- Docs - Platform Concepts - Lines
len
@andrewsky said in Multiple time period on a single stock:
instead of self.data.get(), can I use self.data.close.get() and self.data.datetime.get() ?
self.data.get
is giving you the default for a multi-line object, which in most cases will beself.data.close.get
. Working with the individual lines is obviously possible. - Docs - Platform Concepts - Lines