allanster

How To Set Backtest Date Range

จำนวนเข้าชม 34819
2714
34819 92
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!)
Nov 04
เอกสารเผยแพร่: Changed default settings to remove need for setting present date if backtesting up to current moment in time.
Apr 27
เอกสารเผยแพร่: 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.
Apr 27
เอกสารเผยแพร่: 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.
Sep 18
เอกสารเผยแพร่: 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

Thank you! Very helpful!

How would you add a limited trading session timeframe for each day? In other words, I want the backtest to cover a full month, but I only want to backtest a specified timeframe during each trading day?
ตอบกลับ
thanks, helped me a lot
ตอบกลับ
allanster Dheerajkumar
@Dheerajkumar, glad to hear!
ตอบกลับ
thank you. very well explained and presented.
ตอบกลับ
@hillab, thanks!
ตอบกลับ
Thank you for this.
ตอบกลับ
There is a more recent example script which also shows how to automatically set the date range window to be backtested from X days or weeks ago to present. Additional options are also included to manually set the date range or to show entire range available. It can be seen here:

ตอบกลับ
It is very helpful. Is it possible to exit to cash at finish to cash to compare with Buy and hold. Thanks.
ตอบกลับ
allanster areddy01
@areddy01, this script is intended only as an example of how to include option to manually set your desired date range to backtest. Net profit is calculated from first entry through last exit. Buy and hold is calculated from first entry through close, so with the formation of each new candle your buy and hold will change. If you wanted to compare the two you would need to manually calculate buy and hold for the same range as net profit. The values needed for this are shown in list of trades.
ตอบกลับ
allanster allanster
@areddy01, my previous reply stripped the bracketed 1 that I typed after close. "Buy and hold is calculated from first entry through close previous to current bar".
ตอบกลับ
หน้าแรก ตัวช่วยคัดกรองหุ้น ตัวคัดกรองฟอเร็กซ์ ตัวคัดกรองคริปโต ปฏิทินเศรษฐกิจ รายการ มันทำงานได้อย่างไร ข้อมูลสำคัญของชาร์ต ราคา กฎของบ้าน Pine Wizards ผู้ดูแลการใช้งาน เว็บไซต์ & โบรกเกอร์ โซลูชั่น วิดเจ็ต โซลูชั่นชาร์ต Lightweight Charting Library ศูนย์ช่วยเหลือ แนะนำเพื่อน คำร้องขอเพิ่มเติมลักษณะและบริการ บล็อก & ข่าว Twitter
ข้อมูลส่วนตัว ตั้งค่าโปรไฟล์ บัญชีและการเรียกเก็บเงิน แนะนำเพื่อน ตั๋วการขอความสนับสนุนของฉัน ศูนย์ช่วยเหลือ ไอเดียที่ถูกตีพิมพ์ ผู้ติดตาม กำลังติดตาม ข้อความส่วนตัว แชท ออกจากระบบ