How To Set Backtest Date Range

Example how to select and set date range window to be backtested. Normally when you change chart period it changes the number of days being backtested which means as you increas the chart period (for example from 5min to 15min) you also increase the number of days traded, so you can not compare apples to apples for which period would yield best returns for your strategy. Now you can. Incorporate this code replacing buy and sell with your strategy, then simply input the From and To dates in Format -> Inputs, and then change the chart period to view updated results.

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!)
ملاحظات الأخبار: Changed default settings to remove need for setting present date if backtesting up to current moment in time.
ملاحظات الأخبار: Per requests added examples of:

- 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.
ملاحظات الأخبار: Added "max_bars_back=2000" in top strategy line to prevent possibility of exceeding 2000 event limit. This will prevent that error from possibly occurring.
ملاحظات الأخبار: Statement in "Release Notes" immediately above is incorrect. Setting "max_bars_back=2000" in top strategy line does not prevent possibility of exceeding 2000 order limit. It instead is used to manually define how many historical bars are preloaded for indicator calculations. If you receive an ‘out of depth at index’ error this means something in your script calculations requires a certain number of historical bars and for some reason those bars were not automatically allocated. Raising this setting may help prevent that error from occurring.

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.
إزالة من البرامج النصية المفضلة أضف إلى البرامج النصية المفضلة
If you find something useful enough that you would like to buy me a refreshing beverage :)...

LTC (preferred): LTDaAe7nPzbpBhZVT9o5f9f8M3aZSPa46Y

BTC: 13LhkeXBYrRomVAqbXcoLXHuqMk6Mf7G6N

great job it works .
nice job thx
Thanks so much!
You made my day <8-)
doesn't work, i tried to select january to february as period but it shows nothing.
Smikel Smikel
@Smikel, I don't know why. apparently it can only show the last 20 days' data when using 5min graft, and if i select a period like several months ago, it says no data :(
@Smikel, this applies to ALL pine scripts and is unrelated to my specific example. There is a limit to how far back you can go that depends on a multitude of factors including pair, period, strategy trade frequency, maximum event limit, and your account plan. You can click the button on the chart to see how much history is available.


Smikel allanster
@allanster, ok thank you
Smikel allanster
@allanster, one question about autoview, do you know how to copy and paste in pine?, I am new here, I tried to use ctrl+c and ctrl+d, and using mouse right click, and then clicking on copy and paste, but none of them worked :( I have been searching for a solution to this but nothing. Please help me. sorry it is not related to your scrip.
@Smikel, sorry but I am unsure what issue you may be having other than you mentioning using ctrl+d (paste is ctrl+v). When editing in Pine it should behave like any other text editor.
Smikel allanster
@allanster, thank you very much. my mistake. It was an error on the navigator.
الصفحة الرئيسية منصة الأسهم منصًة العملات منصّة العملات الرقمية جدول الأعمال الاقتصادي كيف نعمل مميزات الرسم البياني أسعار العضوية قوانين الموقع المشرفون حلول المواقع الإلكترونية والوسطاء الأدوات حلول الرسوم البيانية احصل على المساعدة طلب الخصائص المدوّنة والأخبار الأسئلة الأكثر شيوعًا معرفة تويتر
الملف الشخصي إعدادات الصفحة الشخصية الحساب وإعداد الفواتير عملات TradingView تذاكر الدعم الخاصة بي احصل على المساعدة أفكاري المنشورة المتابعين تتابع رسالة خاصة المحادثة تسجيل الخروج