Pine script for Intraday backtesting Previous Day high and Previous Day Low Breakout strategy

In this article, I will try to explain the backtesting of range breakout strategy using Pine Script.

 

Intraday range breakout strategy is a well known concept among trading community. We will test previous day range breakout, as soon as underlying script crosses previous day high we will initate buy and opposite conditions for sell (if script goes below previous day low, we will sell it). We will keep fixed stop loss which is configurable.

 


// © ObserveNifty
//@version=4
strategy("Previous Day High and Low Breakout Strategy", overlay=true, calc_on_every_tick=true)

D_High = security(syminfo.tickerid, 'D', high[1])
D_Low = security(syminfo.tickerid, 'D', low[1])
D_Close = security(syminfo.tickerid, 'D', close[1])
D_Open = security(syminfo.tickerid, 'D', open[1])

// Go Long - if prev day high is broken
// Go Short - if prev day low is broken
plot(timeframe.isintraday ? D_High : na, title="Daily High", style=plot.style_line, color=#0000FF, linewidth=2)
plot(timeframe.isintraday ? D_Low : na, title="Daily Low", style=plot.style_line, color=#FF0000, linewidth=2)


Buy=crossover(high,D_High)
Sell=crossunder(low,D_Low)

if crossover(high,D_High)
    strategy.entry("Long", strategy.long, comment="Long")

if crossunder(low,D_Low)
    strategy.entry("Short", strategy.short, comment="Short")

if( hour(time) == 15 and minute(time) > 20)
    strategy.close("Short", comment="Short exit")
    strategy.close("Long", comment="Long exit")

stop_loss = input(title="Stop loss value", type=input.integer, defval=100)

// Determine stop loss price
longStopPrice  = strategy.position_avg_price - stop_loss
shortStopPrice = strategy.position_avg_price + stop_loss

if (strategy.position_size > 0)
    strategy.exit(id="Long", stop=longStopPrice, comment="Long exit")

if (strategy.position_size < 0)
    strategy.exit(id="Short", stop=shortStopPrice, comment="Short exit")

 

 I will try to revist this post to provide more explanation.

Happy Trading.

Comments

Popular posts from this blog

Sending message to Telegram Group, User or Bot using Python

Pinescript Code for Hilega Milega