pbergden

Backtesting Period Selector | Component

7623 17 291
Description
It's nice to quickly be able to set the backtesting period when writing strategies.
To make this process faster I wrote a simple 'component'.
So this is not a strategy but rather code you can plug-into your strategy and use
if you need that specific functionality.

Then it's just a matter of selecting which dates you want to backtest.
You can also chose to color the background to visually show the testing period.
Unfortunately, the background color is fixed at 'blue' for now.

Ps. I like the idea of writing small components to be pluged into other strategies
I'll try to develop this idea a bit further and see how small pieces of code can
easily provide specific functionality to assist and make deving strategies a bit less 'Pineful'.

Usage
First copy the instructed part of the component code over to your strategy.
Next, use the testPeriod() function to limit strategies to the specified backtesting period.

Example usage:
if testPeriod()
strategy.entry("LE", strategy.long)

Todo / Improvements
There are many ways to improve this component and I'm not a very good coder so this is a very crude solutions.
Anyway, here are some things which would be nice to improve:
1. Enable color selection so that the user can choose the background color of his own liking.
2. Improve naming of variables.
3. Test for ilogical choices, such as test period start being at a later date, than test period stop.
4. Account for time zones.

As always, any feedback, corrections or thoughts are very much welcome!
/pbergden
Remove from Favorite Scripts Add to Favorite Scripts
//@version=2
strategy("Backtesting Period Selector | [pbergden]", shorttitle="Backtesting Period Selector", overlay=true)

//////////////////////////////////////////////////////////////////////
// Date:2016.06.01
// Author: pbergden
// Version: 0.1
// Description: A 'component' which simplifies selecting backtesting period

//////////////////////////////////////////////////////////////////////
// Long description:
// With this component code you can select between which dates 
// you want to backtest.
// You can also select a background color to visually show the 
// testing period.

//////////////////////////////////////////////////////////////////////
// Todo
// 1.   Enable color selection to that the user can choose the 
//      background color of his own choice.
// 2.   Improve naming of variables.
// 3.   Test for ilogical choices, such as test period start being at
//      a later date than test period stop.
// 4.   Account for time zones.


//////////////////////////////////////////////////////////////////////
// Example usage:
// if testPeriod()
//   strategy.entry("LE", strategy.long)

//////////////////////////////////////////////////////////////////////
// Simply copy the code below and paste it into your own strategy

//////////////////////////////////////////////////////////////////////
// Component Code Start
testStartYear = input(2014, "Backtest Start Year")
testStartMonth = input(1, "Backtest Start Month")
testStartDay = input(2, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)

testStopYear = input(2015, "Backtest Stop Year")
testStopMonth = input(12, "Backtest Stop Month")
testStopDay = input(30, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)

// A switch to control background coloring of the test period
testPeriodBackground = input(title="Color Background?", type=bool, defval=true)
testPeriodBackgroundColor = testPeriodBackground and (time >= testPeriodStart) and (time <= testPeriodStop) ? #00FF00 : na
bgcolor(testPeriodBackgroundColor, transp=97)

testPeriod() =>
    time >= testPeriodStart and time <= testPeriodStop ? true : false
// Component Code Stop
//////////////////////////////////////////////////////////////////////
Great function. Would be awesome if trading view allowed users to upload these to be pulled into the code directly to keep the code clean.

Also If anyone is looking for a simple way to limit your backtesting period you can also achieve this by simply nesting the strategy in a time conditional statement like this: `if (time>timestamp(2018, 05, 27, 0, 0))` e.g. only test for periods beyond this date.
Reply
amazing, thanks so much =)
Reply
@pbergden Thank you so much for this. For some reason, I'm having problems adding this to built in strategies, specifically the testPeriod part at the end. If it's not too much trouble, can you please show this on the Channel Break Out Strategy for example? Thanks!

//@version=3
strategy("ChannelBreakOutStrategy", overlay=true)

length = input(title="Length", type=integer, minval=1, maxval=1000, defval=5)

upBound = highest(high, length)
downBound = lowest(low, length)

if (not na(close))
strategy.entry("ChBrkLE", strategy.long, stop=upBound + syminfo.mintick, comment="ChBrkLE")
strategy.entry("ChBrkSE", strategy.short, stop=downBound - syminfo.mintick, comment="ChBrkSE")

//plot(strategy.equity, title="equity", color=red, linewidth=2, style=areabr)
+1 Reply
Thank you for this, it's exactly what I needed!
Reply
This is a great idea. I hope to see more!
Reply
@scarf, @pbergden This is a great idea and needed. However here's my problem, not sure if I'm not using it right or limitation of Tradingview.

I'm using 5min bars, I think TV hard caps bar analysis at around 9000 bars. This means I can't backtest anything at the 5min level more than a month out.

1. Does this selector allow selecting a period beyond 9000 bars ago? For ex today is 9/28/17, I'd like to backtest 7/1/17 - 7/30/17. Right now the code will return "No Data" in the strategy. I suspect this is because of the bar cap by TV, not sure if anyone can confirm.

2. Is there a way around the 9000 bar cap, for TV to ignore analysis on the first 9000+ bars and for backtesting/analysis to cover a period in the past.
+8 Reply
@scarf, Ha lad I just saw you pop up on here, what are the chances, you must be at the same thing as me
Reply
line 61: mismatched input 'strategy.entry' expecting LEND i dont believe this is working anymore
Reply
pbergden Germaine_Mills
If post your code - or send me a PM - I can have a look at what might be wrong.
Reply
Im using his strategy
Reply
EN English
EN English (UK)
EN English (IN)
DE Deutsch
FR Français
ES Español
IT Italiano
PL Polski
SV Svenska
TR Türkçe
RU Русский
PT Português
ID Bahasa Indonesia
MS Bahasa Melayu
TH ภาษาไทย
VI Tiếng Việt
JA 日本語
KO 한국어
ZH 简体中文
ZH 繁體中文
AR العربية
Home Stock Screener Forex Signal Finder Cryptocurrency Signal Finder Economic Calendar How It Works Chart Features House Rules Moderators Website & Broker Solutions Widgets Stock Charting Library Feature Request Blog & News FAQ Help & Wiki Twitter
Profile Profile Settings Account and Billing My Support Tickets Contact Support Ideas Published Followers Following Private Messages Chat Sign Out