TradingView
pbergden
١ حزيران يونيو ٢٠١٦ ١٠:٤٩

Backtesting Period Selector | Component 

VOLVO, AB SER. BSTO

الوصف

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
التعليقات
HedgedTrader
@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[length]))
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)
CryptoMania17
Thank you very much for sharing this code! :)

I'm trying to use it on a strategy that is not mine. I have no idea how to write code.
I therefore have also no idea what i need to do with the following line of code:

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

Without the use of this line it will not work, as I understand. When I just copy/paste the whole code the date range setting doesn't work.

Is this something you or someone else can easily explain to me? Or is it to complex?
sgtview
You got me at 'Pineful'
scotv888
@sgtview, ME TOO!!!
Germaine_Mills
line 61: mismatched input 'strategy.entry' expecting LEND i dont believe this is working anymore
pbergden
If post your code - or send me a PM - I can have a look at what might be wrong.
Germaine_Mills
pbergden
//@version=2
strategy("Daily Close Comparison Strategy (by ChartArt)", shorttitle="CA_-_Daily_Close_Strat", overlay=false)

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

testStopYear = input(2016, "Backtest Stop Year")
testStopMonth = input(7, "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
//////////////////////////////////////////////////////////////////////

// ChartArt's Daily Close Comparison Strategy
//
// Version 1.0
// Idea by ChartArt on February 28, 2016.
//
// This strategy is equal to the very
// popular "ANN Strategy" coded by sirolf2009,
// but without the Artificial Neural Network (ANN).
//
// Main difference besides stripping out the ANN
// is that I use close prices instead of OHLC4 prices.
// And the default threshold is set to 0 instead of 0.0014
// with a step of 0.001 instead of 0.0001.
//
// This strategy goes long if the close of the current day
// is larger than the close price of the last day.
// If the inverse logic is true, the strategy
// goes short (last close larger current close).
//
// This simple strategy does not have any
// stop loss or take profit money management logic.
//
// List of my work:
// tradingview.com/u/ChartArt/
//
// __ __ ___ __ ___
// / ` |__| /\ |__) | /\ |__) |
// \__, | | /~~\ | \ | /~~\ | \ |
//
//

threshold = input(title="Price Difference Threshold", type=float, defval=0, step=0.001)

getDiff() =>
yesterday=security(tickerid, 'D', close[1])
today=security(tickerid, 'D', close)
delta=today-yesterday
percentage=delta/yesterday

closeDiff = getDiff()

buying = closeDiff > threshold ? true : closeDiff < -threshold ? false : buying[1]

hline(0, title="zero line")

bgcolor(buying ? green : red, transp=25)
plot(closeDiff, color=silver, style=area, transp=75)
plot(closeDiff, color=aqua, title="prediction")

longCondition = buying
if testPeriod()
if (longCondition)
strategy.entry("Long", strategy.long)

shortCondition = buying != true
if testPeriod()
if (shortCondition)
strategy.entry("Short", strategy.short)
pbergden
Check the formating for the if tests at the end - a new if need to 4 spaces or a tab
if testPeriod()
[4 spaces or a tab] if (shortCondition)
[4 spaces or a tab] [4 spaces or a tab] strategy.entry("Short", strategy.short)
svyarnall
thanks
I discovered that the "order" of the coding must be the pbergden's code 1st than enter the if statement of your startegy....
المزيد