allanster

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.

Enjoy!

(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.
เอกสารเผยแพร่: Updated for Pine Script v4.
ลบออกจากสคริปส์โปรด เพิ่มเข้าไปในสคริปส์โปรด
Telegram: https://t.me/allanster • Gunbot Tuners: http://www.allanster.com

If you would like to buy me a refreshing beverage :)...

LTC (preferred): LTDaAe7nPzbpBhZVT9o5f9f8M3aZSPa46Y

BTC: 13LhkeXBYrRomVAqbXcoLXHuqMk6Mf7G6N

ความคิดเห็น

Ah i really needed this! I tried implementing this on my strategy and i managed to get it to work on other charts but it wont work on BTC any chance you could help me troubleshoot this problem? Have a great day and thanks again for sharing
+4 ตอบกลับ
@Sabito, just noticed I missed your question so apologies for never responding. I see that you are still active, but given the extreme delay in my answer I would assume you sorted this long ago. At the time you asked, when backtesting with cryptocurrencies it was necessary to use an order amount that was equal to or greater than the cost of the instrument being purchased or a script would never buy. This is due to the backtesting engine equating a single contract as being one whole coin, and at that time fractional purchases of cryptocurrencies were not able to be made within the engine.
ตอบกลับ
Sabito allanster
@allanster, Yeah i was unaware of this at the time thanks responding though maybe it will help someone else in the future! Have a great day!
ตอบกลับ
Thanks For the code.
+3 ตอบกลับ
Hello, thanks for sharing this script however, as I am a beginner I have difficulties to include it in the script call : "Heiken Ashi + Ichimoku Kinko Hyo Strategy".

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.

Thanks again
Have a nice day
+2 ตอบกลับ
Hi,

Very nice code, but my trades don`t get filtered to the desired timeframe. On hourly I still have trades from 1.1.2017 even though I am using your time period from 20th April.

Any ideas?

Thank you

+2 ตอบกลับ
Fonjacha Fonjacha
@Fonjacha, I just found a solution. Tnx anyway
ตอบกลับ
allanster Fonjacha
@Fonjacha, glad to hear you got sorted. I don't have any ideas why you would be experiencing that behavior. Dates are from midnight UTC time. I just loaded script to test and was unable to reproduce behavior described.
ตอบกลับ
Fonjacha allanster
@allanster, It was just weird. I used another script. Thank you anyway.
ตอบกลับ
Hi,

Thanks alot for the script, you just did hell of a job there helping us out.

From what I understand, this is useful for chopping the already available (on the chart) time frame.

How about creating a chart with 3m intervals in the past, let's say in 2017 for the purposes of testing? I have been looking the whole day for a way to get back to certain periods in the past with different resoltions but I have not figured it out.

If you have any ideas that would be awesome. Because, honestly, a max of 5000 bars back on a 3m strategy is 10 days which is definitely not enough IMO.

Thanks once again.
+1 ตอบกลับ
หน้าแรก ตัวช่วยคัดกรองหุ้น ตัวคัดกรองฟอเร็กซ์ ตัวคัดกรองคริปโต ปฏิทินเศรษฐกิจ มันทำงานได้อย่างไร ฟีเจอร์ของชาร์ต อัตราค่าบริการ แนะนำเพื่อน กฎระเบียบการใช้งาน ศูนย์ช่วยเหลือ โซลูชั่นสำหรับเว็บไซต์ & โบรกเกอร์ วิดเจ็ต ชาร์ตโซลูชั่น Lightweight ชาร์ตไลบรารี บล็อก & ข่าว Twitter
โปรไฟล์ ตั้งค่าโปรไฟล์ บัญชีและการเรียกเก็บเงิน เพื่อนที่ชวนมา เหรียญ ตั๋วการขอความสนับสนุนของฉัน ศูนย์ช่วยเหลือ ไอเดียที่เผยแพร่ ผู้ติดตาม ติดตาม ข้อความส่วนตัว แชท ออกจากระบบ