NOTE: There is a limit in backtesting to 2000 orders, so please be aware of this when setting your date ranges. If you set your range too high, you may be exceeding this limit on some periods and not on others, so this would yield incorrect comparison of returns per period. If you see in your backtesting results that you are nearing this limit for one of your periods you are testing, then reduce the date range to a smaller number of days.
(Thanks to @Gesundheit "Adeel" for pointing me in the right direction on this!)
- Using input statements for MA lengths.
- Using date range condition within execution.
- BONUS example of creating a function for date range.
Also added simple error checking to input statements for month and day.
For preventing the error on exceeding 2000 orders you must take steps to reduce the number of trades. Various means to do this include changing the range you are backtesting, changing the period, changing the frequency of how often your strategy trades, etc.
In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.
Where do I have to include the time range macro in the code to have the possibility to changes the number of days being backtested.
Have a nice day