Nifty 2 Day Breakout strategy
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © 487551

//@version=5
strategy("Nifty 2 Day Breakout strategy", overlay=true)

// Calculate the high and low of yesterday and the day before yesterday
yesterdayHigh = request.security(syminfo.tickerid, "D", high[1], lookahead=barmerge.lookahead_on)
yesterdayLow = request.security(syminfo.tickerid, "D", low[1], lookahead=barmerge.lookahead_on)
dayBeforeYesterdayHigh = request.security(syminfo.tickerid, "D", high[2], lookahead=barmerge.lookahead_on)
dayBeforeYesterdayLow = request.security(syminfo.tickerid, "D", low[2], lookahead=barmerge.lookahead_on)

// Strategy logic
buyCondition = close > yesterdayHigh and close > dayBeforeYesterdayHigh
sellCondition = close < yesterdayLow and close < dayBeforeYesterdayLow

strategy.entry("Buy", strategy.long, when = buyCondition)
strategy.close("Buy", when = sellCondition)

strategy.entry("Sell", strategy.short, when = sellCondition)
strategy.close("Sell", when = buyCondition)

// Plot the breakout levels on the chart
plot(yesterdayHigh, color=color.green, linewidth=2, title="Yesterday High")
plot(yesterdayLow, color=color.red, linewidth=2, title="Yesterday Low")
plot(dayBeforeYesterdayHigh, color=color.blue, linewidth=2, title="Day Before Yesterday High")
plot(dayBeforeYesterdayLow, color=color.orange, linewidth=2, title="Day Before Yesterday Low")
