TradingView

Adding Backtesting Range to TradingView Strategy in 3 lines of code

Adding Backtesting Range to TradingView Strategy in 3 lines of code

With the new update of PineScript language, it’s become very easy to add a backtesting range to TradingView Strategies.

You can do that in 3 easy steps:

  1. Insert the following code in your strategy. You can add it after the last input you have in the code, this way you’ll have range inputs last in your input list.
// Calculate start/end date and time condition
startDate  = input(timestamp("2019-01-01T00:00:00"), type = input.time)
finishDate = input(timestamp("2021-01-01T00:00:00"), type = input.time)

time_cond  = time >= startDate and time <= finishDate

2. Add time condition to all your entries.

If you’re using market orders entries you can use the following syntax:

strategy.entry("long",  strategy.long,  when = crossover(ma_fast, ma_slow)  and time_cond)

If you’re using stop/limit orders you can use if statement:

if (time_cond)
    strategy.entry("ParLE", strategy.long, stop=psar, comment="ParLE")

3. Last you can close all your open positions and pending orders using the following code:

if (not time_cond)
    strategy.close_all()
    strategy.cancel_all()

Example

Here is entire code for a strategy with a backtesting range:

//@version=4
strategy("MA Strategy", overlay=true)

// MA Inputs 
ma_fast_length = input(5)
ma_slow_length = input(25)

//////////////////////////////////////////////////////////////

// Calculate start/end date and time condition
startDate  = input(timestamp("2019-01-01T00:00:00"), type = input.time)
finishDate = input(timestamp("2021-01-01T00:00:00"), type = input.time)

time_cond  = time >= startDate and time <= finishDate

//////////////////////////////////////////////////////////////

ma_fast = sma(close, ma_fast_length)
ma_slow = sma(close, ma_slow_length)

plot(ma_fast, color = color.red,  linewidth = 2)
plot(ma_slow, color = color.blue, linewidth = 2)

//////////////////////////////////////////////////////////////

strategy.entry("long",  strategy.long,  when = crossover(ma_fast, ma_slow)  and time_cond)
strategy.entry("short", strategy.short, when = crossunder(ma_fast, ma_slow) and time_cond)
	
if (not time_cond)
    strategy.close_all()
    strategy.cancel_all()

5 thoughts on “Adding Backtesting Range to TradingView Strategy in 3 lines of code”

  1. Hello and happy new year! Do not understand why it is not possible to use timestamp code in this script.

    // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
    //

    //@version=4
    //

    //Lead-In
    strategy(“Två HMA DEMA Strategi”,
    shorttitle=”Två HMA DEMA”,
    overlay=true,
    max_bars_back = 5000,
    initial_capital=100000,
    max_bars_back = 5000,
    default_qty_type=strategy.percent_of_equity,
    default_qty_value=100,
    commission_type=strategy.commission.percent,
    commission_value=0.025,
    pyramiding = 0)

    short = input(9, minval=1)
    srcShort = input(ohlc4, title=”Source Dema 1″)

    long = input(10, minval=1)
    srcLong = input(ohlc4, title=”Source Dema 2″)

    long2 = input(14, minval=1)
    srcLong2 = input(ohlc4, title=”Source Dema 3″)

    e1 = hma(srcShort, short)
    e2 = hma(e1, short)
    dema1 = 2 * e1 – e2
    plot(dema1, color=color.green, linewidth=2)

    e3 = sma(srcLong, long)
    e4 = sma(e3, long)
    dema2 = 2 * e3 – e4
    plot(dema2, color=color.blue, linewidth=2)

    e5 = ema(srcLong2, long2)
    e6 = ema(e5, long2)
    dema3 = 2 * e5 – e6
    plot(dema3, color=color.black, linewidth=2)

    longC = dema1 > dema2 and dema1 > dema3
    shortC = dema1 < dema2 and dema1 < dema3

    alertlong = longC and not longC[1]
    alertshort = shortC and not shortC[1]

    strategy.entry("Long" , strategy.long , when = longC ,comment="Long")
    strategy.entry("Short", strategy.short, when = shortC,comment="Short")

    // Alerts

    alertcondition(longC , title='Long' , message=' Buy Signal ')
    alertcondition(shortC , title='Short', message=' Sell Signal ')

    1. Hi, no idea. I believe it should work for this strategy as well if applied properly.

  2. Hello and happy new year! Do not understand why it is not possible to use timestamp code in this script.

    // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
    //

    //@version=4
    //

    //Lead-In
    strategy(“Två HMA DEMA Strategi”,
    shorttitle=”Två HMA DEMA”,
    overlay=true,
    max_bars_back = 5000,
    initial_capital=100000,
    max_bars_back = 5000,
    default_qty_type=strategy.percent_of_equity,
    default_qty_value=100,
    commission_type=strategy.commission.percent,
    commission_value=0.025,
    pyramiding = 0)

    short = input(9, minval=1)
    srcShort = input(ohlc4, title=”Source Dema 1″)

    long = input(10, minval=1)
    srcLong = input(ohlc4, title=”Source Dema 2″)

    long2 = input(14, minval=1)
    srcLong2 = input(ohlc4, title=”Source Dema 3″)

    e1 = hma(srcShort, short)
    e2 = hma(e1, short)
    dema1 = 2 * e1 – e2
    plot(dema1, color=color.green, linewidth=2)

    e3 = sma(srcLong, long)
    e4 = sma(e3, long)
    dema2 = 2 * e3 – e4
    plot(dema2, color=color.blue, linewidth=2)

    e5 = ema(srcLong2, long2)
    e6 = ema(e5, long2)
    dema3 = 2 * e5 – e6
    plot(dema3, color=color.black, linewidth=2)

    longC = dema1 > dema2 and dema1 > dema3
    shortC = dema1 < dema2 and dema1 < dema3

    alertlong = longC and not longC[1]
    alertshort = shortC and not shortC[1]

    strategy.entry("Long" , strategy.long , when = longC ,comment="Long")
    strategy.entry("Short", strategy.short, when = shortC,comment="Short")

    // Alerts

    alertcondition(longC , title='Long' , message=' Buy Signal ')
    alertcondition(shortC , title='Short', message=' Sell Signal ')

    1. Hi, no idea. I believe it should work for this strategy as well if applied properly.

  3. Sir,
    Do not understand why timestamp code is not working in this script.
    //@version=4
    //original idea from Way of the Turtle trading
    strategy(“Turtles strategy”, shorttitle = “Turtles”, overlay=true, calc_on_every_tick=true, pyramiding=0, default_qty_type= strategy.cash, default_qty_value = 50)
    /////////////////////////////////////////////////////////////////////////////////////////////
    //input condition
    enter = input(20, minval=1)
    exit = input(10, minval=1)
    /////////////////////////////////////////////////////////////////////////////////////////////
    // Calculate start/end date and time condition
    startDate = input(timestamp(“2022-09-01T00:00:00”), type = input.time)
    finishDate = input(timestamp(“2022-10-01T00:00:00”), type = input.time)

    time_cond = time >= startDate and time LongeE[1]
    exitL1 = low <= LongC[1]

    //bgcolor(exitL1 or exitL2? red: enterL1 or enterL2? navy:white)
    strategy.entry("LongeE", strategy.long, when = (enterL1) and time_cond)
    strategy.close("LongeC", when = (exitL1) and time_cond)

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Pine Script Programming Courses
Pine Script Programming Courses
Learn to build your own TradingView Indicators and Strategies
Sidebar Signup Form
If you want to be the first in this business, subscribe to the latest news