Previous Day High Low Strategy only for Long
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mangeshabhang19

//@version=5
strategy("Previous Day High Low Strategy only for Long", overlay=true, margin_long=100, margin_short=0, calc_on_every_tick=true, default_qty_value = 50)
[HH, LL] = request.security(syminfo.tickerid, '1D', [high, low], barmerge.gaps_off, barmerge.lookahead_off)

OpenTime = timestamp(year, month, dayofmonth , 09, 30)   
CloseTime = timestamp(year, month, dayofmonth, 15, 10)  
ExitTime = timestamp(year, month, dayofmonth, 15, 15)  

TradeTime = time >= OpenTime and time <= CloseTime

UW = open > close ? high - open : high - close
LW = open > close ? close - low : open - low
Body = open > close ? open - close : close - open

UWP = (UW / (LW + UW)) * 100
LWP = (LW / (LW + UW)) * 100

longCondition = ta.crossover(close, HH)
longCondition := longCondition or ta.crossover(close, LL)
longCondition := longCondition //and  high == close


plot(HH, color=color.red,linewidth = 1)
plot(LL, color=color.red,linewidth = 1) 

//============================================= Market Strength ============================
LWadxlength = input(9, title='ADX period')
LWdilength = input(11, title='DMI Length')

dirmov(len) =>
    up = ta.change(high)
    down = -ta.change(low)
    truerange = ta.rma(ta.tr, len)
    plus = fixnan(100 * ta.rma(up > down and up > 0 ? up : 0, len) / truerange)
    minus = fixnan(100 * ta.rma(down > up and down > 0 ? down : 0, len) / truerange)
    [plus, minus]

adx(LWdilength, LWadxlength) =>
    [plus, minus] = dirmov(LWdilength)
    sum = plus + minus
    adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), LWadxlength)
    [adx, plus, minus]

[ADX, up, down] = adx(LWdilength, LWadxlength)
LWADX = (ADX - 15) * 2.5
adxcolor = up > down ? color.green : color.red

strength_up = LWADX > 11 and  up > down and LWADX > ta.highest(LWADX[1],4)

longCondition := longCondition and strength_up // and close>= high-input(15,'LW Size') 
plotshape(longCondition, title="Buy Signal", location=location.belowbar, color=color.green, textcolor = color.white, style=shape.labelup, text="BUY")
//========================== POSITIONS ================================
float MaxProfit = input(150.00,"Max Profit")
float T1 = MaxProfit * 0.3
float T2 = MaxProfit * 0.5
float T3 = MaxProfit * 0.8
float SL =  na
float CP = na
float EP = na


if not na(EP[1]) 
    EP := EP[1]

if not na(CP[1]) 
    CP := CP[1]

if not na(SL[1]) 
    SL := SL[1]


// Execute strategy on tick
if (barstate.isconfirmed)
    if (strategy.position_size == 0)
        EP := na
        CP := na
        SL := na
    if (longCondition and TradeTime)
        strategy.entry("Long", strategy.long)


if (strategy.position_size > 0)
    if na(EP[1]) 
        EP := strategy.position_avg_price
        CP := EP
        SL := math.min(input(15,"Max SL"), MaxProfit * 0.4)
    if (high >= EP + T1) and CP <  EP + T1
        CP := EP + T1
        SL := CP-EP//SL * 0.8
    if (high >= EP + T2) and CP < EP + T2
        CP := EP + T2
        SL := CP-EP-T1//SL * 0.8
    if (high >= EP + T3) and CP < EP + T3
        CP := EP + MaxProfit
        SL := CP-EP-T2//SL * 0.8
    strategy.exit("Exit Long", "Long", limit = EP + MaxProfit, stop =CP-SL)

if (strategy.position_size > 0 and time > CloseTime)
    strategy.close("Long")

//PLOT
plot(CP, title="Stop Loss", color=color.yellow, linewidth=1, style = plot.style_linebr)
plot(EP, title="Stop Loss", color=color.white, linewidth=1, style = plot.style_linebr)
plot(CP - SL, title="Stop Loss", color=color.red, linewidth=1, style = plot.style_linebr)
plot(EP + T1, title="Target 1", color=color.blue, linewidth=1, style = plot.style_linebr)
plot(EP + T2, title="Target 2", color=color.blue, linewidth=1, style = plot.style_linebr)
plot(EP + T3, title="Target 3", color=color.blue, linewidth=1, style = plot.style_linebr)
plot(EP + MaxProfit, title="Max Profit", color=color.green, linewidth=1, style = plot.style_linebr)