Function to detect minimum number of bars required to run current loop
Author: kelvinyip
Creation Date: 12/19/2020 3:24 PM
profile picture

kelvinyip

#1
There was a function to detect the starting bar number required to run the backtesting ?
For example, symbol 1 needs to do 200 days mv and symbol 2 only needs to do 50 days mv... so I want the minimum amount of data for the current symbol to run my code. I think I saw it before but can't find it now
profile picture

Eugene

#2
CODE:
Please log in to see this code.
profile picture

superticker

#3
This is a "loaded" question with multiple answers. For a Bars object, it's
CODE:
Please log in to see this code.

But if you're backtesting against a particular indicator, then it's
CODE:
Please log in to see this code.

Both are documented in the QuickRef guide for Bars and DataSeries. The Chart is smart enough to only start the plot of an indicator after its FirstValidValue, which is a nice feature. Some WL indicators don't set their FirstValidValue, so be careful.
profile picture

kelvinyip

#4
I am doing backtesting and I assign say 10 yrs of data. Then, I notice that for some symbols, I am error out so I notice that perhaps my 50 days moving average on bar number 2 cannot look back 50 days... perhaps.. even with FirstActualBar, that's not going solve my problem I guess. I need at least 51 bars to calculate 50 days SMA. Let me test it out, I probably don't fully understand
profile picture

Eugene

#5
You need this: GetAllDataForSymbol

Wiki FAQ > How to start Buy&Hold from specific bar (not at bar #0)?
profile picture

superticker

#6
QUOTE:
... I notice that for some symbols, I am error out so I notice that perhaps my 50 days moving average on bar number 2 cannot look back 50 days.

Is that because the stock went public less than 50 days ago? ... like some new stocks discussed on Mad Money? If so, "maybe" adding ...
CODE:
Please log in to see this code.

at the top of your strategy might help. I haven't tried it though.

I'm still confused. These new releases will often run the strategy without erroring out, but they may not trade at all. Are you including a GetTradingLoopStartBar(...) on the limits of your trading FOR loop for all relevant indicators? This is mine for my two longest-period indicators.
CODE:
Please log in to see this code.

I just delete stocks out of the dataset that don't have enough bars to trade on my strategy.
profile picture

kelvinyip

#7
I think I see my issue now... Let me give an example, yeah superticker is right. If I have a DataSet of SPY and say ABNB (just went IPO last 2 weeks). In my backtesting, I would have stuff like if Close[bar] > SMA.Value(bar-50, Close, 50);

The bar-50 makes it error out. If I put in my trading loop to start after bar 51, I miss out the 1st 50 bar's signal for SPY.
GetTradingLoopStartBar and GetAllDataForSymbol I believe (after reading the docs), "knows" how many bars because it is used in the indicator context.

Let me try this Bars.FirstActualBar < 51
profile picture

kelvinyip

#8
How come Bars.FirstActualBar is always zero ?? I was printing it out
profile picture

Eugene

#9
Have you carefully read the first paragraph of the QuickRef for FirstActualBar?
profile picture

superticker

#10
QUOTE:
I would have stuff like if Close[bar] > SMA.Value(bar-50, Close, 50) [throwing an error for low bar values]

If that's the only problem causing your strategy to throw an error for effectively negative "bar values" (Is it?), why not just change it to
CODE:
Please log in to see this code.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).