MARKET DYNAMICS HH LL BREAKOUT
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sincereLizard

//@version=4
strategy(title="MD BANKNIFTY STRATEGY ", overlay=true,initial_capital=300000, default_qty_value=1)
strat_dir_input = input(title="Strategy Direction", defval="all", options=["long", "short", "all"])
strat_dir_value = strat_dir_input == "long" ? strategy.direction.long : strat_dir_input == "short" ? strategy.direction.short : strategy.direction.all
////
strategy.risk.allow_entry_in(strat_dir_value)
testStartYear = input(2016, "Backtest Start Year")
testStartMonth = input(1, "Backtest Start Month")
testStartDay = input(1, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
//Stop date if you want to use a specific range of dates
testStopYear = input(2022, "Backtest Stop Year")
testStopMonth = input(12, "Backtest Stop Month")
testStopDay = input(30, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
sl_inp = (input(2.0, title='Stop Loss %', type=input.float))/100
tp_inp = (input(7.0, title='Take Profit %', type=input.float))/100

testPeriod() =>
    time >= testPeriodStart and time <= testPeriodStop ? true : false

//Higher High or Lower Low Entry Inputs
price = input(close)
LookBack = input(26)
Highest = highest(LookBack)
Lowest = lowest(LookBack)

long = price > Highest[1] 
short = price < Lowest[1]

avg_price = strategy.position_avg_price
stop_level = avg_price * (1 - sl_inp)
take_level = avg_price * (1 + tp_inp)


//Safety Confirmation Inputs - Helps to thin out false breakouts or break downs
length = input(10)
High_Guard = highest(length)
Low_Guard = lowest(length)
length2 = input(1)

long1 = long == 1 and (Highest[1] > High_Guard[length2])
short1 = short == 1 and Lowest[1] < Low_Guard[length2]
long_exit  = short1 or (close < stop_level) or (close > take_level)
short_exit = long1 or (close > take_level) or (close < stop_level)

if strat_dir_input == "long"
    strategy.entry("Long", strategy.long, when=long1 and testPeriod())
    strategy.close(id = "Long", when = long_exit)

if strat_dir_input == "short"    
    strategy.entry("Short", strategy.short, when=short1  and testPeriod())
    strategy.close(id = "Short", when = short_exit)

if strat_dir_input =="all"
    strategy.entry("Long",strategy.long, when=long1 and testPeriod())
    
    
    strategy.entry("Short", strategy.short, when=short1 and testPeriod())
    