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

//@version=5
indicator("TV Draft1",overlay=true)
//overlay=true, timeframe="", timeframe_gaps=true


BRange = input.int(20, minval=0, title="Bull Bear Range (eg. NF-25% BNF-17%)")
ema = input(33, title = "MA Length")
//show_table5 = input(true,  "Show Table?")

EMA_HTF = input.int(33, title="EMA HTF", minval=1, maxval=100)
res = input.timeframe(defval='240', title="Timeframe")

// symin1 = input.symbol(title="Symbol", defval='NIFTY', confirm=true)
// symin2 = input.symbol(title="Symbol", defval='BANKNIFTY', confirm=true)
// symin3 = input.symbol(title="Symbol", defval='HDFCBANK', confirm=true)
// symin4 = input.symbol(title="Symbol", defval='ICICIBANK', confirm=true)
// symin5 = input.symbol(title="Symbol", defval='RELIANCE', confirm=true)
// symin6 = input.symbol(title="Symbol", defval='INFY', confirm=true)
// symin7 = input.symbol(title="Symbol", defval='SBIN', confirm=true)
// symin8 = input.symbol(title="Symbol", defval='AXISBANK', confirm=true)
// symin9 = input.symbol(title="Symbol", defval='LT', confirm=true)

// atrPeriod = input(23, "ATR Length")
// factor = input.float(2.7, "Factor", step = 0.01)

lensig = input.int(14, title="ADX Smoothing", minval=1, maxval=50)
len = input.int(14, minval=1, title="DI Length")

// use_tp      = input(false, "Take Profit?")
// tp_trigger  = input.float(0.8, "Take profit %", minval=0, step=0.5) * 0.01
// ttp_trigger = input.float(0.2, "Trailing Take Profit %", minval=0, step=0.5) * 0.01

// [supertrend, direction] = ta.supertrend(factor, atrPeriod)


//upTrend = plot(direction < 0 ? supertrend : na, "Up Trend", color = color.green, style=plot.style_circles)
//downTrend = plot(direction < 0? na : supertrend, "Down Trend", color = color.red, style=plot.style_circles)

// plotshape(ta.crossover(close,supertrend) ,text="U",style=shape.labelup,color=color.green,textcolor=color.black,location=location.belowbar)
// plotshape(ta.crossunder(close, supertrend),text="D",style=shape.labeldown,color=color.red,textcolor=color.black)


up = ta.change(high)
down = -ta.change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
trur = ta.rma(ta.tr, len)
plus = fixnan(100 * ta.rma(plusDM, len) / trur)
minus = fixnan(100 * ta.rma(minusDM, len) / trur)
sum = plus + minus
adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)
adxlevel = input.int(title="ADX Level", defval=25)
//adxlevel2 = input.int(title="ADX Level 2", defval=20)


// RSI Parameters
// ====================================================================================
RSI_length = input(14, title='RSI Lenght')
//RSI2_length = input(14, title='RSI Lenght')
//RSI_overSold = input(20, title='RSI Oversold Limit')
//RSI_overBought = input(80, title='RSI Overbought Limit')
RSI_High_Trigger= input.int(55, minval=1, title="RSI High Trigger")
RSI_Low_Trigger= input.int(45, minval=1, title="RSI Low Trigger")
//RSI_50= input.int(55, minval=1, title="RSI Low Trigger")
//RSI_40= input.int(50, minval=1, title="RSI Low Trigger")

//RSI2 = ta.rsi(close, RSI2_length)

RSI = ta.rsi(close, RSI_length)

//adxplus = ((ta.crossover((plus),minus))) //and plus>adxlevel
//adxminus = ((ta.crossover(minus,plus)) //and RSI2 < RSI_40) and plus>adxlevel
//adxplus = (plus>adxlevel)


//study("EMA/SMA Band", overlay = true)
//ema = input(33, title = "MA Length")
//on = input.bool(title = "Plot EMA's", defval = false)
//on_sma = input.bool(title = "Use SMA?", defval = false)
//ema_color = input.bool(title = "Plot Color", defval = false)
// ema_1 = on_sma?ta.ema(high,ema):ta.ema(high,ema)
// ema_2 = on_sma?ta.ema(close,ema):ta.ema(close,ema)
// ema_3 = on_sma?ta.ema(low,ema):ta.ema(low,ema)
//color_ = close>ema_2?color.green:color.red
//ema33_1 = ta.ema(high, ema )
//ema33_2 = ta.ema(close, ema )
//ema33_3 = ta.ema(low, ema )

ema_1 = ta.ema(high,ema)
ema_2 = ta.ema(close,ema)
ema_3 = ta.ema(low,ema)




//p1 = plot(ema_1, transp = on? 0:100, linewidth = 2, title = "High", color = ema_color?color_:color.new(color.orange, 80))
//plot(ema_2, transp = on? 0:100, linewidth = 2, title = "Close",color =ema_color? color_:color.rgb(244, 232, 130))
//p2 = plot(ema_3, transp = on?0: 100, linewidth = 2, title = "Low",color =ema_color? color_:color.new(color.orange, 80))
//fill(p1,p2, color = color.rgb(244, 235, 141), transp = 90, color = ema_color?color_:color.rgb(244, 235, 141))
//fill(p1,p2, color.new(color.blue, 70), title = "EMA 33 BAND")

//p1 = plot(ema_1, transp = on? 0:100, linewidth = 2, title = "EMA High", color = ema_color?color_:color.new(color.orange, 80))
//plot(ema_2, transp = on? 0:100, linewidth = 2, title = "Close",color =ema_color? color_:color.rgb(244, 232, 130))
//p2 = plot(ema_3, transp = on?0: 100, linewidth = 2, title = "EMA Low",color =ema_color? color_:color.new(color.orange, 80))
//fill(p1,p2, color = color.rgb(244, 235, 141), transp = 90, color = ema_color?color_:color.rgb(244, 235, 141))

p1 = plot(ema_1, linewidth = 2, title = "EMA High", color = color.new(color.orange, 80))
//plot(ema_2, transp = on? 0:100, linewidth = 2, title = "Close",color =ema_color? color_:color.rgb(244, 232, 130))
p2 = plot(ema_3,  linewidth = 2, title = "EMA Low",color =color.new(color.orange, 80))
//fill(p1,p2, color = color.rgb(244, 235, 141), transp = 90, color = ema_color?color_:color.rgb(244, 235, 141))


fill(p1,p2, color.new(color.blue, 70), title = "EMA 33 BAND")



emahcr = (ta.crossover(close,ema_1))
emalcr = (ta.crossunder(close,ema_3))

RBbuy = (plus>adxlevel) and(RSI>RSI_High_Trigger) and emahcr
RSsell = (minus>adxlevel) and(RSI<RSI_Low_Trigger) and emalcr
// RBbuy = emahcr and (plus>adxlevel2) and(RSI>RSI_High_Trigger) 
// RSsell = emalcr and (minus>adxlevel2) and(RSI<RSI_Low_Trigger) 


plotshape((RBbuy),text="RB",textcolor=color.black, style=shape.labelup,color=color.green,location=location.belowbar,style=shape.triangleup,size=size.auto)
plotshape((RSsell),text="RS",textcolor=color.black,style=shape.labeldown,color=color.red,location=location.abovebar,style=shape.triangledown,size=size.auto)

// buy=ta.crossover(close,supertrend)
// sell=ta.crossunder(close,supertrend)


// since_buy       = ta.barssince(buy)
// since_sell      = ta.barssince(sell)
// buy_trend       = since_sell > since_buy
// sell_trend      = since_sell < since_buy
// change_trend    = (buy_trend and sell_trend[1]) or (sell_trend and buy_trend[1])


// entry_price = ta.valuewhen(buy or sell, close, 0)


// var tp_price_trigger    = -1.0
// var is_tp_trigger_hit   = false
// var tp_trail            = 0.0
// tp_close_long       = false
// tp_close_short      = false

// if use_tp

//     if change_trend

//         tp_price_trigger    := entry_price * (1.0 + (buy_trend ? 1.0 : sell_trend ? -1.0 : na) * tp_trigger)
//         is_tp_trigger_hit   := false


//     if buy_trend
 
//         is_tp_trigger_hit   := (high >= tp_price_trigger) or (not change_trend and is_tp_trigger_hit[1])


//         tp_trail            := math.max(high, change_trend ? tp_price_trigger : tp_trail[1])
//         tp_close_long       := (is_tp_trigger_hit and (high <= tp_trail * (1.0 - ttp_trigger))) or (not change_trend and tp_close_long[1])
//     else if sell_trend

//         is_tp_trigger_hit   := (low <= tp_price_trigger) or (not change_trend and is_tp_trigger_hit[1])


//         tp_trail            := math.min(low, change_trend ? tp_price_trigger : tp_trail[1])
//         tp_close_short      := (is_tp_trigger_hit and (low >= tp_trail * (1.0 + ttp_trigger))) or (not change_trend and tp_close_short[1])


//plot(use_tp and tp_price_trigger != -1.0 ? tp_price_trigger : na,title='Take Profit Price Trigger', color=color.blue,style=plot.style_circles, linewidth=2)







//Created by Robert Nance on 072315
//study(title="Moving Average Colored EMA/SMA", shorttitle="Colored EMA /SMA", overlay=true)
//emaplot = input (true, title="Show EMA on chart")
//len3 = input.int(33, minval=1, title="ema Length")
//src = low
//out = ta.ema(src, len3)
//up3 = out > out[1]
//down3 = out < out[1]
//mycolor = up3 ? color.green : down3 ? color.red : color.blue
//plot(out and emaplot ? out :na, title="EMA", color=mycolor, linewidth=3)


//smaplot = input (false, title="Show EMA on chart")
//len2 = input(33, title="sma Length")
//src2 = high
//out2 = ta.ema(src2, len2)
//up2 = out2 > out2[1]
//down2 = out2 < out2[1]
//mycolor2 = up2 ? color.green : down2 ? color.red : color.blue
//plot(out2 and smaplot ? out2 :na , title="SMA", color=mycolor2, linewidth=1)







// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Developed by Marco Jarquin as part of Arkansas 22 Project for Binary Options
// CBRA for binary options (Configurable Bollinger Bands, RSI and Aroon)

//@version=5
// ====================================================================================

//strategy('A22.CBRA.Strat', overlay=true, initial_capital=10000, currency='USD', calc_on_every_tick=true, default_qty_type=strategy.cash, default_qty_value=4000, commission_type=strategy.commission.cash_per_order, commission_value=0)

// Aroonish Parameters
// ====================================================================================

// Aroonish_length = input.int(4, minval=1, title='Aroonish Lenght')
// Aroonish_ConfVal = input.int(50, minval=0, maxval=100, step=25, title='Aroonish Confirmation Value')
// Aroonish_upper = 100 * (-ta.highestbars(high, Aroonish_length + 1) + Aroonish_length) / Aroonish_length
// Aroonish_lower = 100 * (-ta.lowestbars(low, Aroonish_length + 1) + Aroonish_length) / Aroonish_length

// // Aroonish confirmations
// // ====================================================================================
// Aroonish_ConfLong = Aroonish_lower >= Aroonish_ConfVal and Aroonish_upper < Aroonish_lower
// Aroonish_ConfShrt = Aroonish_upper >= Aroonish_ConfVal and Aroonish_upper > Aroonish_lower

// //plotshape(ta.crossover(Aroonish_lower, Aroonish_upper), color=color.new(color.red, 0), style=shape.triangledown, location=location.abovebar, size=size.auto, title='Ar-OB')
// //plotshape(ta.crossover(Aroonish_upper, Aroonish_lower), color=color.new(color.green, 0), style=shape.triangleup, location=location.belowbar, size=size.auto, title='Ar-OS')
// ArOB = (ta.crossover(Aroonish_lower, Aroonish_upper))
// ArOS = (ta.crossover(Aroonish_upper, Aroonish_lower))



// plotshape(ta.crossover(RSI, RSI_overSold), color=color.new(color.orange, 0), style=shape.square, location=location.belowbar, size=size.auto, title='RSI-B')
// plotshape(ta.crossunder(RSI, RSI_overBought), color=color.new(color.orange, 0), style=shape.square, location=location.abovebar, size=size.auto, title='RSI-S')
// RSI_overSold2 = ta.crossover(RSI, RSI_overSold)
// RSI_overBought2 = ta.crossunder(RSI, RSI_overBought)

// // Bollinger Parameters
// // ====================================================================================
// BB_length = input.int(20, minval=1, title='Bollinger Lenght')
// BB_mult = input.float(2.5, minval=0.1, maxval=50, step=0.1, title='Bollinger Std Dev')
// // BB_bars = input(3, minval=1, maxval=5, title="Check bars after crossing")

// BB_basis = ta.sma(close, BB_length)
// BB_dev = BB_mult * ta.stdev(close, BB_length)

// BB_upper = BB_basis + BB_dev
// BB_lower = BB_basis - BB_dev

// //p1 = plot(BB_upper, color=color.new(color.blue, 0))
// //p2 = plot(BB_lower, color=color.new(color.blue, 0))

// // Bars to have the operation open
// // ====================================================================================
// nBars = input.int(3, minval=1, maxval=30, title='Bars to keep the operation open')

// // Strategy condition short or long
// // ====================================================================================
// //ConditionShrt = (ta.crossunder(close, BB_upper) or ta.crossunder(close[1], BB_upper[1])) and Aroonish_ConfShrt and (ta.crossunder(RSI, RSI_overBought) or ta.crossunder(RSI[1], RSI_overBought[1]))
// //ConditionLong = (ta.crossover(close, BB_lower) or ta.crossover(close[1], BB_lower[1])) and Aroonish_ConfLong and (ta.crossover(RSI, RSI_overSold) or ta.crossover(RSI[1], RSI_overSold[1]))

// plotshape(ta.crossover(close, BB_lower), color=color.new(color.blue, 0), style=shape.circle, location=location.belowbar, size=size.auto, title='BB-B')
// plotshape(ta.crossunder(close, BB_upper), color=color.new(color.blue, 0), style=shape.circle, location=location.abovebar, size=size.auto, title='BB-S')

// // Make input options that configure backtest date range
// // ====================================================================================
// //iSDate = input.time(title='Start Date', defval=timestamp('14 Sep 2022 06:00 +0100'), tooltip='Start date and time of backtest')
// //iFDate = input.time(title='End Date', defval=timestamp('16 Sep 2022 16:00 +0100'), tooltip='End date and time of backtest')

// // Look if the close time of the current bar falls inside the date range
// // ====================================================================================
// //inDateRange = (time >= iSDate and time < iFDate)

// // Evaluates conditions to enter short or long
// // ====================================================================================
// //if inDateRange and ConditionLong
// //    strategy.entry('A22.L', strategy.long)

// //if inDateRange and ConditionLong[nBars]
// //    strategy.close('A22.L', comment='A22.L Exit')

// //if inDateRange and ConditionShrt
// //    strategy.entry('A22.S', strategy.short)

// //if inDateRange and ConditionShrt[nBars]
// //    strategy.close('A22.S', comment='A22.S Exit')

// //if not inDateRange
// //    strategy.close_all()



// //This is for 15 mins logic..
// //up15on = input(true, title="15 Minute Opening Range High")
// //down15on = input(true, title="15 Minute Opening Range Low")

// //up30on = input(true, title="30 Minute Opening Range High")
// //down30on = input(true, title="30 Minute Opening Range Low")

// //is_newbar(res) => ta.change(time(res)) != 0 
// //adopt(r, s) => request.security(syminfo.tickerid, r, s) 

// //high_range = ta.valuewhen(is_newbar('D'),high,0)
// //low_range = ta.valuewhen(is_newbar('D'),low,0)

// //high_rangeL = ta.valuewhen(is_newbar('D'),high,0)  
// //low_rangeL = ta.valuewhen(is_newbar('D'),low,0) 

// //up1 = plot(up15on ? adopt('15', high_range):na, color = #0E7A34, style= plot.style_line, linewidth=2, title="15 mins High")
// //down1 = plot(down15on ? adopt('15', low_range): na, color = #DC143C, style= plot.style_line, linewidth=2, title="15 mins Low") 


// //up30 = plot(up30on ? adopt('30', high_range):na, color = #0E7A34, style= plot.style_line, linewidth=2, title="30 mins High")
// //down30 = plot(down30on ? adopt('30', low_range): na, color = #DC143C, style= plot.style_line, linewidth=2, title="30 mins Low") 

// //trans15 = up15on ?  75 : 100
// //trans30 = up30on ?  75 : 100

// //fill(up1, down1, color = #ffffff, transp=trans15)
// //fill(up1, down1, color = #ffffff, transp=trans30)


// //symin1 = input.symbol(title="Symbol", defval='NIFTY', confirm=true)
// t0= timeframe.period
// sym1c = request.security(symin1, t0, close)
// sym1h = request.security(symin1, t0, high)
// sym1l = request.security(symin1, t0, low)   


// RSI_sym = input(14, title='RSI Lenght')
// RSIs1 = ta.rsi(sym1c, RSI_sym)
// //ovr22 = request.security(sym1, t0, close) 
// //nifclose = request.security('NIFTY', '3', close)

// //up1 = ta.change(sym1h)
// //down1 = -ta.change(sym1l)
// //plusDM1 = na(up1) ? na : (up1 > down1 and up1 > 0 ? up1 : 0)
// //minusDM1 = na(down1) ? na : (down1 > up1 and down1 > 0 ? down1 : 0)
// //trur1 = ta.rma(ta.tr, len)
// //plus1 = fixnan(100 * ta.rma(plusDM1, len) / trur1)
// //minus1 = fixnan(100 * ta.rma(minusDM1, len) / trur1)
// //sum1 = plus1 + minus1
// //adx1 = 100 * ta.rma(math.abs(plus1 - minus1) / (sum1 == 0 ? 1 : sum1), lensig)
// //adxlevel = input.int(title="ADX Level", defval=25)

// len1 = input(14)

// TrueRange1 = math.max(math.max(sym1h-sym1l, math.abs(sym1h-nz(sym1c[1]))), math.abs(sym1l-nz(sym1c[1])))
// DirectionalMovementPlus1 = sym1h-nz(sym1h[1]) > nz(sym1l[1])-sym1l ? math.max(sym1h-nz(sym1h[1]), 0): 0
// DirectionalMovementMinus1 = nz(sym1l[1])-sym1l > sym1h-nz(sym1h[1]) ? math.max(nz(sym1l[1])-sym1l, 0): 0

// SmoothedTrueRange1 = 0.0
// SmoothedTrueRange1 := nz(SmoothedTrueRange1[1]) - (nz(SmoothedTrueRange1[1])/len1) + TrueRange1

// SmoothedDirectionalMovementPlus1 = 0.0
// SmoothedDirectionalMovementPlus1 := nz(SmoothedDirectionalMovementPlus1[1]) - (nz(SmoothedDirectionalMovementPlus1[1])/len1) + DirectionalMovementPlus1

// SmoothedDirectionalMovementMinus1 = 0.0
// SmoothedDirectionalMovementMinus1 := nz(SmoothedDirectionalMovementMinus1[1]) - (nz(SmoothedDirectionalMovementMinus1[1])/len1) + DirectionalMovementMinus1

// DIPlus1 = SmoothedDirectionalMovementPlus1 / SmoothedTrueRange1 * 100
// DIMinus1 = SmoothedDirectionalMovementMinus1 / SmoothedTrueRange1 * 100
// DX1 = math.abs(DIPlus1-DIMinus1) / (DIPlus1+DIMinus1)*100
// ADX1 = ta.sma(DX1, len1)


// //symin2 = input.symbol(title="Symbol", defval='BANKNIFTY', confirm=true)
// sym2c = request.security(symin2, t0, close)
// sym2h = request.security(symin2, t0, high)
// sym2l = request.security(symin2, t0, low)


// RSIs2 = ta.rsi(sym2c, RSI_sym)
// //ovr22 = request.security(sym1, t0, close) 
// //nifclose = request.security('NIFTY', '3', close)

// len2 = len1

// TrueRange2 = math.max(math.max(sym2h-sym2l, math.abs(sym2h-nz(sym2c[1]))), math.abs(sym2l-nz(sym2c[1])))
// DirectionalMovementPlus2 = sym2h-nz(sym2h[1]) > nz(sym2l[1])-sym2l ? math.max(sym2h-nz(sym2h[1]), 0): 0
// DirectionalMovementMinus2 = nz(sym2l[1])-sym2l > sym2h-nz(sym2h[1]) ? math.max(nz(sym2l[1])-sym2l, 0): 0

// SmoothedTrueRange2 = 0.0
// SmoothedTrueRange2 := nz(SmoothedTrueRange2[1]) - (nz(SmoothedTrueRange2[1])/len2) + TrueRange2

// SmoothedDirectionalMovementPlus2 = 0.0
// SmoothedDirectionalMovementPlus2 := nz(SmoothedDirectionalMovementPlus2[1]) - (nz(SmoothedDirectionalMovementPlus2[1])/len2) + DirectionalMovementPlus2

// SmoothedDirectionalMovementMinus2 = 0.0
// SmoothedDirectionalMovementMinus2 := nz(SmoothedDirectionalMovementMinus2[1]) - (nz(SmoothedDirectionalMovementMinus2[1])/len2) + DirectionalMovementMinus2

// DIPlus2 = SmoothedDirectionalMovementPlus2 / SmoothedTrueRange2 * 100
// DIMinus2 = SmoothedDirectionalMovementMinus2 / SmoothedTrueRange2 * 100
// DX2 = math.abs(DIPlus2-DIMinus2) / (DIPlus2+DIMinus2)*100
// ADX2 = ta.sma(DX2, len2)




// //symin3 = input.symbol(title="Symbol", defval='HDFCBANK', confirm=true)
// sym3c = request.security(symin3, t0, close)
// sym3h = request.security(symin3, t0, high)
// sym3l = request.security(symin3, t0, low)


// RSIs3 = ta.rsi(sym3c, RSI_sym)
// //ovr22 = request.security(sym1, t0, close) 
// //nifclose = request.security('NIFTY', '3', close)


// len3 = len1

// TrueRange3 = math.max(math.max(sym3h-sym3l, math.abs(sym3h-nz(sym3c[1]))), math.abs(sym3l-nz(sym3c[1])))
// DirectionalMovementPlus3 = sym3h-nz(sym3h[1]) > nz(sym3l[1])-sym3l ? math.max(sym3h-nz(sym3h[1]), 0): 0
// DirectionalMovementMinus3 = nz(sym3l[1])-sym3l > sym3h-nz(sym3h[1]) ? math.max(nz(sym3l[1])-sym3l, 0): 0

// SmoothedTrueRange3 = 0.0
// SmoothedTrueRange3 := nz(SmoothedTrueRange3[1]) - (nz(SmoothedTrueRange3[1])/len3) + TrueRange3

// SmoothedDirectionalMovementPlus3 = 0.0
// SmoothedDirectionalMovementPlus3 := nz(SmoothedDirectionalMovementPlus3[1]) - (nz(SmoothedDirectionalMovementPlus3[1])/len3) + DirectionalMovementPlus3

// SmoothedDirectionalMovementMinus3 = 0.0
// SmoothedDirectionalMovementMinus3 := nz(SmoothedDirectionalMovementMinus3[1]) - (nz(SmoothedDirectionalMovementMinus3[1])/len3) + DirectionalMovementMinus3

// DIPlus3 = SmoothedDirectionalMovementPlus3 / SmoothedTrueRange3 * 100
// DIMinus3 = SmoothedDirectionalMovementMinus3 / SmoothedTrueRange3 * 100
// DX3 = math.abs(DIPlus3-DIMinus3) / (DIPlus3+DIMinus3)*100
// ADX3 = ta.sma(DX3, len3)



// //symin4 = input.symbol(title="Symbol", defval='ICICIBANK', confirm=true)
// sym4c = request.security(symin4, t0, close)
// sym4h = request.security(symin4, t0, high)
// sym4l = request.security(symin4, t0, low)


// RSIs4 = ta.rsi(sym4c, RSI_sym)
// //ovr22 = request.security(sym1, t0, close) 
// //nifclose = request.security('NIFTY', '3', close)


// len4 = len1

// TrueRange4 = math.max(math.max(sym4h-sym4l, math.abs(sym4h-nz(sym4c[1]))), math.abs(sym4l-nz(sym4c[1])))
// DirectionalMovementPlus4 = sym4h-nz(sym4h[1]) > nz(sym4l[1])-sym4l ? math.max(sym4h-nz(sym4h[1]), 0): 0
// DirectionalMovementMinus4 = nz(sym4l[1])-sym4l > sym4h-nz(sym4h[1]) ? math.max(nz(sym4l[1])-sym4l, 0): 0

// SmoothedTrueRange4 = 0.0
// SmoothedTrueRange4 := nz(SmoothedTrueRange4[1]) - (nz(SmoothedTrueRange4[1])/len4) + TrueRange4

// SmoothedDirectionalMovementPlus4 = 0.0
// SmoothedDirectionalMovementPlus4 := nz(SmoothedDirectionalMovementPlus4[1]) - (nz(SmoothedDirectionalMovementPlus4[1])/len4) + DirectionalMovementPlus4

// SmoothedDirectionalMovementMinus4 = 0.0
// SmoothedDirectionalMovementMinus4 := nz(SmoothedDirectionalMovementMinus4[1]) - (nz(SmoothedDirectionalMovementMinus4[1])/len4) + DirectionalMovementMinus4

// DIPlus4 = SmoothedDirectionalMovementPlus4 / SmoothedTrueRange4 * 100
// DIMinus4 = SmoothedDirectionalMovementMinus4 / SmoothedTrueRange4 * 100
// DX4 = math.abs(DIPlus4-DIMinus4) / (DIPlus4+DIMinus4)*100
// ADX4 = ta.sma(DX4, len4)



// //symin5 = input.symbol(title="Symbol", defval='RELIANCE', confirm=true)
// sym5c = request.security(symin5, t0, close)
// sym5h = request.security(symin5, t0, high)
// sym5l = request.security(symin5, t0, low)


// RSIs5 = ta.rsi(sym5c, RSI_sym)
// //ovr22 = request.security(sym1, t0, close) 
// //nifclose = request.security('NIFTY', '3', close)


// len5 = len1

// TrueRange5 = math.max(math.max(sym5h-sym5l, math.abs(sym5h-nz(sym5c[1]))), math.abs(sym5l-nz(sym5c[1])))
// DirectionalMovementPlus5 = sym5h-nz(sym5h[1]) > nz(sym5l[1])-sym5l ? math.max(sym5h-nz(sym5h[1]), 0): 0
// DirectionalMovementMinus5 = nz(sym5l[1])-sym5l > sym5h-nz(sym5h[1]) ? math.max(nz(sym5l[1])-sym5l, 0): 0

// SmoothedTrueRange5 = 0.0
// SmoothedTrueRange5 := nz(SmoothedTrueRange5[1]) - (nz(SmoothedTrueRange5[1])/len5) + TrueRange5

// SmoothedDirectionalMovementPlus5 = 0.0
// SmoothedDirectionalMovementPlus5 := nz(SmoothedDirectionalMovementPlus5[1]) - (nz(SmoothedDirectionalMovementPlus5[1])/len5) + DirectionalMovementPlus5

// SmoothedDirectionalMovementMinus5 = 0.0
// SmoothedDirectionalMovementMinus5 := nz(SmoothedDirectionalMovementMinus5[1]) - (nz(SmoothedDirectionalMovementMinus5[1])/len5) + DirectionalMovementMinus5

// DIPlus5 = SmoothedDirectionalMovementPlus5 / SmoothedTrueRange5 * 100
// DIMinus5 = SmoothedDirectionalMovementMinus5 / SmoothedTrueRange5 * 100
// DX5 = math.abs(DIPlus5-DIMinus5) / (DIPlus5+DIMinus5)*100
// ADX5 = ta.sma(DX5, len5)



// //symin6 = input.symbol(title="Symbol", defval='INFY', confirm=true)
// sym6c = request.security(symin6, t0, close)
// sym6h = request.security(symin6, t0, high)
// sym6l = request.security(symin6, t0, low)


// RSIs6 = ta.rsi(sym6c, RSI_sym)
// //ovr22 = request.security(sym1, t0, close) 
// //nifclose = request.security('NIFTY', '3', close)


// len6 = len1

// TrueRange6 = math.max(math.max(sym6h-sym6l, math.abs(sym6h-nz(sym6c[1]))), math.abs(sym6l-nz(sym6c[1])))
// DirectionalMovementPlus6 = sym6h-nz(sym6h[1]) > nz(sym6l[1])-sym6l ? math.max(sym6h-nz(sym6h[1]), 0): 0
// DirectionalMovementMinus6 = nz(sym6l[1])-sym6l > sym6h-nz(sym6h[1]) ? math.max(nz(sym6l[1])-sym6l, 0): 0

// SmoothedTrueRange6 = 0.0
// SmoothedTrueRange6 := nz(SmoothedTrueRange6[1]) - (nz(SmoothedTrueRange6[1])/len6) + TrueRange6

// SmoothedDirectionalMovementPlus6 = 0.0
// SmoothedDirectionalMovementPlus6 := nz(SmoothedDirectionalMovementPlus6[1]) - (nz(SmoothedDirectionalMovementPlus6[1])/len6) + DirectionalMovementPlus6

// SmoothedDirectionalMovementMinus6 = 0.0
// SmoothedDirectionalMovementMinus6 := nz(SmoothedDirectionalMovementMinus6[1]) - (nz(SmoothedDirectionalMovementMinus6[1])/len6) + DirectionalMovementMinus6

// DIPlus6 = SmoothedDirectionalMovementPlus6 / SmoothedTrueRange6 * 100
// DIMinus6 = SmoothedDirectionalMovementMinus6 / SmoothedTrueRange6 * 100
// DX6 = math.abs(DIPlus6-DIMinus6) / (DIPlus6+DIMinus6)*100
// ADX6 = ta.sma(DX6, len6)



// //symin7 = input.symbol(title="Symbol", defval='SBIN', confirm=true)
// sym7c = request.security(symin7, t0, close)
// sym7h = request.security(symin7, t0, high)
// sym7l = request.security(symin7, t0, low)


// RSIs7 = ta.rsi(sym7c, RSI_sym)
// //ovr22 = request.security(sym1, t0, close) 
// //nifclose = request.security('NIFTY', '3', close)


// len7 = len1

// TrueRange7 = math.max(math.max(sym7h-sym7l, math.abs(sym7h-nz(sym7c[1]))), math.abs(sym7l-nz(sym7c[1])))
// DirectionalMovementPlus7 = sym7h-nz(sym7h[1]) > nz(sym7l[1])-sym7l ? math.max(sym7h-nz(sym7h[1]), 0): 0
// DirectionalMovementMinus7 = nz(sym7l[1])-sym7l > sym7h-nz(sym7h[1]) ? math.max(nz(sym7l[1])-sym7l, 0): 0

// SmoothedTrueRange7 = 0.0
// SmoothedTrueRange7 := nz(SmoothedTrueRange7[1]) - (nz(SmoothedTrueRange7[1])/len7) + TrueRange7

// SmoothedDirectionalMovementPlus7 = 0.0
// SmoothedDirectionalMovementPlus7 := nz(SmoothedDirectionalMovementPlus7[1]) - (nz(SmoothedDirectionalMovementPlus7[1])/len7) + DirectionalMovementPlus7

// SmoothedDirectionalMovementMinus7 = 0.0
// SmoothedDirectionalMovementMinus7 := nz(SmoothedDirectionalMovementMinus7[1]) - (nz(SmoothedDirectionalMovementMinus7[1])/len7) + DirectionalMovementMinus7

// DIPlus7 = SmoothedDirectionalMovementPlus7 / SmoothedTrueRange7 * 100
// DIMinus7 = SmoothedDirectionalMovementMinus7 / SmoothedTrueRange7 * 100
// DX7 = math.abs(DIPlus7-DIMinus7) / (DIPlus7+DIMinus7)*100
// ADX7 = ta.sma(DX7, len7)


// //symin8 = input.symbol(title="Symbol", defval='AXISBANK', confirm=true)
// sym8c = request.security(symin8, t0, close)
// sym8h = request.security(symin8, t0, high)
// sym8l = request.security(symin8, t0, low)


// RSIs8 = ta.rsi(sym8c, RSI_sym)
// //ovr22 = request.security(sym1, t0, close) 
// //nifclose = request.security('NIFTY', '3', close)


// len8 = len1

// TrueRange8 = math.max(math.max(sym8h-sym8l, math.abs(sym8h-nz(sym8c[1]))), math.abs(sym8l-nz(sym8c[1])))
// DirectionalMovementPlus8 = sym8h-nz(sym8h[1]) > nz(sym8l[1])-sym8l ? math.max(sym8h-nz(sym8h[1]), 0): 0
// DirectionalMovementMinus8 = nz(sym8l[1])-sym8l > sym8h-nz(sym8h[1]) ? math.max(nz(sym8l[1])-sym8l, 0): 0

// SmoothedTrueRange8 = 0.0
// SmoothedTrueRange8 := nz(SmoothedTrueRange8[1]) - (nz(SmoothedTrueRange8[1])/len8) + TrueRange8

// SmoothedDirectionalMovementPlus8 = 0.0
// SmoothedDirectionalMovementPlus8 := nz(SmoothedDirectionalMovementPlus8[1]) - (nz(SmoothedDirectionalMovementPlus8[1])/len8) + DirectionalMovementPlus8

// SmoothedDirectionalMovementMinus8 = 0.0
// SmoothedDirectionalMovementMinus8 := nz(SmoothedDirectionalMovementMinus8[1]) - (nz(SmoothedDirectionalMovementMinus8[1])/len8) + DirectionalMovementMinus8

// DIPlus8 = SmoothedDirectionalMovementPlus8 / SmoothedTrueRange8 * 100
// DIMinus8 = SmoothedDirectionalMovementMinus8 / SmoothedTrueRange8 * 100
// DX8 = math.abs(DIPlus8-DIMinus8) / (DIPlus8+DIMinus8)*100
// ADX8 = ta.sma(DX8, len8)


// //symin9 = input.symbol(title="Symbol", defval='LT', confirm=true)
// sym9c = request.security(symin9, t0, close)
// sym9h = request.security(symin9, t0, high)
// sym9l = request.security(symin9, t0, low)


// RSIs9 = ta.rsi(sym9c, RSI_sym)
// //ovr22 = request.security(sym1, t0, close) 
// //nifclose = request.security('NIFTY', '3', close)


// len9 = len1

// TrueRange9 = math.max(math.max(sym9h-sym9l, math.abs(sym9h-nz(sym9c[1]))), math.abs(sym9l-nz(sym9c[1])))
// DirectionalMovementPlus9 = sym9h-nz(sym9h[1]) > nz(sym9l[1])-sym9l ? math.max(sym9h-nz(sym9h[1]), 0): 0
// DirectionalMovementMinus9 = nz(sym9l[1])-sym9l > sym9h-nz(sym9h[1]) ? math.max(nz(sym9l[1])-sym9l, 0): 0

// SmoothedTrueRange9 = 0.0
// SmoothedTrueRange9 := nz(SmoothedTrueRange9[1]) - (nz(SmoothedTrueRange9[1])/len9) + TrueRange9

// SmoothedDirectionalMovementPlus9 = 0.0
// SmoothedDirectionalMovementPlus9 := nz(SmoothedDirectionalMovementPlus9[1]) - (nz(SmoothedDirectionalMovementPlus9[1])/len9) + DirectionalMovementPlus9

// SmoothedDirectionalMovementMinus9 = 0.0
// SmoothedDirectionalMovementMinus9 := nz(SmoothedDirectionalMovementMinus9[1]) - (nz(SmoothedDirectionalMovementMinus9[1])/len9) + DirectionalMovementMinus9

// DIPlus9 = SmoothedDirectionalMovementPlus9 / SmoothedTrueRange9 * 100
// DIMinus9 = SmoothedDirectionalMovementMinus9 / SmoothedTrueRange9 * 100
// DX9 = math.abs(DIPlus9-DIMinus9) / (DIPlus9+DIMinus9)*100
// ADX9 = ta.sma(DX9, len9)


// // symin10 = input.symbol(title="Symbol", defval='TCS', confirm=true)
// // sym10c = request.security(symin10, t0, close)
// // sym10h = request.security(symin10, t0, high)
// // sym10l = request.security(symin10, t0, low)


// // RSIs10 = ta.rsi(sym10c, RSI_sym)
// // //ovr22 = request.security(sym1, t0, close) 
// // //nifclose = request.security('NIFTY', '3', close)


// // len10 = len1

// // TrueRange10 = math.max(math.max(sym10h-sym10l, math.abs(sym10h-nz(sym10c[1]))), math.abs(sym10l-nz(sym10c[1])))
// // DirectionalMovementPlus10 = sym10h-nz(sym10h[1]) > nz(sym10l[1])-sym10l ? math.max(sym10h-nz(sym10h[1]), 0): 0
// // DirectionalMovementMinus10 = nz(sym10l[1])-sym10l > sym10h-nz(sym10h[1]) ? math.max(nz(sym10l[1])-sym10l, 0): 0

// // SmoothedTrueRange10 = 0.0
// // SmoothedTrueRange10 := nz(SmoothedTrueRange10[1]) - (nz(SmoothedTrueRange10[1])/len10) + TrueRange10

// // SmoothedDirectionalMovementPlus10 = 0.0
// // SmoothedDirectionalMovementPlus10 := nz(SmoothedDirectionalMovementPlus10[1]) - (nz(SmoothedDirectionalMovementPlus10[1])/len10) + DirectionalMovementPlus10

// // SmoothedDirectionalMovementMinus10 = 0.0
// // SmoothedDirectionalMovementMinus10 := nz(SmoothedDirectionalMovementMinus10[1]) - (nz(SmoothedDirectionalMovementMinus10[1])/len10) + DirectionalMovementMinus10

// // DIPlus10 = SmoothedDirectionalMovementPlus10 / SmoothedTrueRange10 * 100
// // DIMinus10 = SmoothedDirectionalMovementMinus10 / SmoothedTrueRange10 * 100
// // DX10 = math.abs(DIPlus10-DIMinus10) / (DIPlus10+DIMinus10)*100
// // ADX10 = ta.sma(DX10, len10)


// // symin11 = input.symbol(title="Symbol", defval='BAJFINANCE', confirm=true)
// // sym11c = request.security(symin11, t0, close)
// // sym11h = request.security(symin11, t0, high)
// // sym11l = request.security(symin11, t0, low)


// // RSIs11 = ta.rsi(sym11c, RSI_sym)
// // //ovr22 = request.security(sym1, t0, close) 
// // //nifclose = request.security('NIFTY', '3', close)


// // len11 = input(14)

// // TrueRange11 = math.max(math.max(sym11h-sym11l, math.abs(sym11h-nz(sym11c[1]))), math.abs(sym11l-nz(sym11c[1])))
// // DirectionalMovementPlus11 = sym11h-nz(sym11h[1]) > nz(sym11l[1])-sym11l ? math.max(sym11h-nz(sym11h[1]), 0): 0
// // DirectionalMovementMinus11 = nz(sym11l[1])-sym11l > sym11h-nz(sym11h[1]) ? math.max(nz(sym11l[1])-sym11l, 0): 0

// // SmoothedTrueRange11 = 0.0
// // SmoothedTrueRange11 := nz(SmoothedTrueRange11[1]) - (nz(SmoothedTrueRange11[1])/len11) + TrueRange11

// // SmoothedDirectionalMovementPlus11 = 0.0
// // SmoothedDirectionalMovementPlus11 := nz(SmoothedDirectionalMovementPlus11[1]) - (nz(SmoothedDirectionalMovementPlus11[1])/len11) + DirectionalMovementPlus11

// // SmoothedDirectionalMovementMinus11 = 0.0
// // SmoothedDirectionalMovementMinus11 := nz(SmoothedDirectionalMovementMinus11[1]) - (nz(SmoothedDirectionalMovementMinus11[1])/len11) + DirectionalMovementMinus11

// // DIPlus11 = SmoothedDirectionalMovementPlus11 / SmoothedTrueRange11 * 110
// // DIMinus11 = SmoothedDirectionalMovementMinus11 / SmoothedTrueRange11 * 110
// // DX11 = math.abs(DIPlus11-DIMinus11) / (DIPlus11+DIMinus11)*110
// //ADX11 = ta.sma(DX11, len11)


// // symin12 = input.symbol(title="Symbol", defval='MARUTI', confirm=true)
// // sym12c = request.security(symin12, t0, close)
// // sym12h = request.security(symin12, t0, high)
// // sym12l = request.security(symin12, t0, low)


// // RSIs12 = ta.rsi(sym12c, RSI_sym)
// // //ovr22 = request.security(sym1, t0, close) 
// // //nifclose = request.security('NIFTY', '3', close)


// // len12 = input(14)

// // TrueRange12 = math.max(math.max(sym12h-sym12l, math.abs(sym12h-nz(sym12c[1]))), math.abs(sym12l-nz(sym12c[1])))
// // DirectionalMovementPlus12 = sym12h-nz(sym12h[1]) > nz(sym12l[1])-sym12l ? math.max(sym12h-nz(sym12h[1]), 0): 0
// // DirectionalMovementMinus12 = nz(sym12l[1])-sym12l > sym12h-nz(sym12h[1]) ? math.max(nz(sym12l[1])-sym12l, 0): 0

// // SmoothedTrueRange12 = 0.0
// // SmoothedTrueRange12 := nz(SmoothedTrueRange12[1]) - (nz(SmoothedTrueRange12[1])/len12) + TrueRange12

// // SmoothedDirectionalMovementPlus12 = 0.0
// // SmoothedDirectionalMovementPlus12 := nz(SmoothedDirectionalMovementPlus12[1]) - (nz(SmoothedDirectionalMovementPlus12[1])/len12) + DirectionalMovementPlus12

// // SmoothedDirectionalMovementMinus12 = 0.0
// // SmoothedDirectionalMovementMinus12 := nz(SmoothedDirectionalMovementMinus12[1]) - (nz(SmoothedDirectionalMovementMinus12[1])/len12) + DirectionalMovementMinus12

// // DIPlus12 = SmoothedDirectionalMovementPlus12 / SmoothedTrueRange12 * 120
// // DIMinus12 = SmoothedDirectionalMovementMinus12 / SmoothedTrueRange12 * 120
// // DX12 = math.abs(DIPlus12-DIMinus12) / (DIPlus12+DIMinus12)*120
// // ADX12 = ta.sma(DX12, len12)



// // symin13 = input.symbol(title="Symbol", defval='DRREDDY', confirm=true)
// // sym13c = request.security(symin13, t0, close)
// // sym13h = request.security(symin13, t0, high)
// // sym13l = request.security(symin13, t0, low)


// // RSIs13 = ta.rsi(sym13c, RSI_sym)
// // //ovr22 = request.security(sym1, t0, close) 
// // //nifclose = request.security('NIFTY', '3', close)


// // len13 = input(14)

// // TrueRange13 = math.max(math.max(sym13h-sym13l, math.abs(sym13h-nz(sym13c[1]))), math.abs(sym13l-nz(sym13c[1])))
// // DirectionalMovementPlus13 = sym13h-nz(sym13h[1]) > nz(sym13l[1])-sym13l ? math.max(sym13h-nz(sym13h[1]), 0): 0
// // DirectionalMovementMinus13 = nz(sym13l[1])-sym13l > sym13h-nz(sym13h[1]) ? math.max(nz(sym13l[1])-sym13l, 0): 0

// // SmoothedTrueRange13 = 0.0
// // SmoothedTrueRange13 := nz(SmoothedTrueRange13[1]) - (nz(SmoothedTrueRange13[1])/len13) + TrueRange13

// // SmoothedDirectionalMovementPlus13 = 0.0
// // SmoothedDirectionalMovementPlus13 := nz(SmoothedDirectionalMovementPlus13[1]) - (nz(SmoothedDirectionalMovementPlus13[1])/len13) + DirectionalMovementPlus13

// // SmoothedDirectionalMovementMinus13 = 0.0
// // SmoothedDirectionalMovementMinus13 := nz(SmoothedDirectionalMovementMinus13[1]) - (nz(SmoothedDirectionalMovementMinus13[1])/len13) + DirectionalMovementMinus13

// // DIPlus13 = SmoothedDirectionalMovementPlus13 / SmoothedTrueRange13 * 130
// // DIMinus13 = SmoothedDirectionalMovementMinus13 / SmoothedTrueRange13 * 130
// // DX13 = math.abs(DIPlus13-DIMinus13) / (DIPlus13+DIMinus13)*130
// // ADX13 = ta.sma(DX13, len13)




// //show_table5 = input(true,  "Show Table?")
// tableposition=input.string("top_right",title="Table position", options=["top_left", "top_center", "top_right", "middle_left", "middle_center", "middle_right", "bottom_left", "bottom_center", "bottom_right"])
// tabpos= tableposition== "top_left"? position.top_left:tableposition== "top_center"?position.top_center : tableposition== "top_right"? position.top_right : tableposition== "middle_left"? position.middle_left : tableposition== "middle_center"? position.middle_center : tableposition== "middle_right"? position.middle_right : tableposition== "bottom_left"? position.bottom_left : tableposition== "bottom_center"? position.bottom_center: tableposition== "bottom_right"? position.bottom_right: position.top_right
// //t0= timeframe.period
// var table indicatorTable = table.new(tabpos, columns =  50, rows =  50, frame_color=color.black, frame_width=0,border_color=color.black, border_width=0)
// if barstate.islast and show_table5
// //     symbols
    
//     table.cell(indicatorTable, 0, 0, text="Screener", text_halign=text.align_center, bgcolor=color.black, text_color=color.white, text_size=size.small)
//     table.merge_cells(indicatorTable, 0, 0, 4, 0)
// //    table.cell(indicatorTable, 0, 10, text="Fast EMA", text_halign=text.align_center, bgcolor= color.gray, text_color=color.white, text_size=size.small)
// //    table.cell(indicatorTable, 0, 11, text="Slow EMA", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
// //    table.cell(indicatorTable, 1, 10, str.tostring(math.round(out4,2)), text_halign=text.align_center, bgcolor=out4>out5? color.green:color.red, text_color=color.white, text_size=size.small)
// //    table.cell(indicatorTable, 1, 11, str.tostring(math.round(out5,2)), text_halign=text.align_center, bgcolor=out4>out5? color.green:color.red, text_color=color.white, text_size=size.small)
// //    table.cell(indicatorTable, 0, 5, text="Volume", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
// //    table.cell(indicatorTable, 0, 6, text="5-Day Avg. Volume", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
//  //   table.cell(indicatorTable, 1, 5, str.tostring(math.round(vol,2)), text_halign=text.align_center, bgcolor= vol>vol_sma?color.blue:color.gray, text_color=color.white, text_size=size.small)
//  //   table.cell(indicatorTable, 1, 6, str.tostring(math.round(vol_sma,2)), text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
// //    table.cell(indicatorTable, 0, 3, text="RSI", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
// //    table.cell(indicatorTable, 0, 4, text="ADX", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
// //    table.cell(indicatorTable, 1, 3, str.tostring(math.round(rsi,2)), text_halign=text.align_center, bgcolor= rsi>RSI_High_Trigger? color.green: rsi<RSI_Low_Trigger?color.red:color.gray, text_color=color.white, text_size=size.small)
// //    table.cell(indicatorTable, 1, 4, str.tostring(math.round(sig,2)), text_halign=text.align_center, bgcolor=sig>ADX_Threshold1?color.blue:color.gray, text_color=color.white, text_size=size.small)
// //    table.cell(indicatorTable, 0, 7, text="Close", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
// //    table.cell(indicatorTable, 0, 8, text="5-Candle High", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
// //    table.cell(indicatorTable, 0, 9, text="5-Candle Low", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
// //    table.cell(indicatorTable, 1, 7, str.tostring(math.round(close,2)), text_halign=text.align_center, bgcolor= close>HH ?color.green:close<LL ? color.red:color.gray, text_color=color.white, text_size=size.small)
// //    table.cell(indicatorTable, 1, 8, str.tostring(math.round(HH,2)), text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
// //    table.cell(indicatorTable, 1, 9, str.tostring(math.round(LL,2)), text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
//    // table.cell(indicatorTable, 0, 1, text="ADX Positive", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
//     //table.cell(indicatorTable, 1, 1, str.tostring(math.round(plus,2)), text_halign=text.align_center, bgcolor=plus>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//  //   table.cell(indicatorTable, 2, 1, (plus>adxlevel"Buy"), text_halign=text.align_center, bgcolor=plus>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
    
//     //table.cell(indicatorTable, 0, 2, text="ADX Negative", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
//     //table.cell(indicatorTable, 1, 2, str.tostring(math.round(minus,2)), text_halign=text.align_center, bgcolor=minus>adxlevel?color.red:color.gray, text_color=color.white, text_size=size.small)
//     //table.cell(indicatorTable, 0, 3, text="RSI", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
//     //table.cell(indicatorTable, 1, 3, str.tostring(math.round(RSI2,2)), text_halign=text.align_center, bgcolor= RSI2>RSI_High_Trigger? color.green: RSI2<RSI_Low_Trigger?color.red:color.gray, text_color=color.white, text_size=size.small)
//     //table.cell(indicatorTable, 0, 6, text="RSI (4)", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
//     //table.cell(indicatorTable, 1, 6, str.tostring(math.round(RSI,2)), text_halign=text.align_center, bgcolor= RSI<RSI_overBought? color.green: RSI>RSI_overSold?color.red:color.gray, text_color=color.white, text_size=size.small)
//     //table.cell(indicatorTable, 0, 5, text="ST", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
//     //table.cell(indicatorTable, 1, 5, str.tostring(math.round(supertrend,2)), text_halign=text.align_center, bgcolor= supertrend<close? color.green: supertrend>close?color.red:color.gray, text_color=color.white, text_size=size.small)
//     //table.cell(indicatorTable, 0, 4, text="EMA", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
//     //table.cell(indicatorTable, 1, 4, str.tostring(math.round(out,2)), text_halign=text.align_center, bgcolor= out<close? color.green: out>close?color.red:color.gray, text_color=color.white, text_size=size.small)
//     //table.cell(indicatorTable, 0, 7, text="RSI OB/OS", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
//     //table.cell(indicatorTable, 1, 7, str.tostring(math.round(RSI,2)), text_halign=text.align_center, bgcolor= RSI_overSold2? color.green: RSI_overBought2?color.red:color.gray, text_color=color.white, text_size=size.small)
//     //table.cell(indicatorTable, 0, 8, text="Aroonish OB/OS", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
//     //table.cell(indicatorTable, 1, 8, str.tostring(math.round(Aroonish_ConfVal,2)), text_halign=text.align_center, bgcolor= ArOS? color.green: ArOB?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 0, 1, text="Instruments", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 1, 1, text="RSI", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 2, 1, text="DMI +ve", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 3, 1, text="DMI -ve", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 4, 1, text="ADX", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 0, 2, text=syminfo.ticker , text_halign=text.align_center, bgcolor=((RSI2>RSI_High_Trigger) and (plus>adxlevel))? color.green: ((RSI2<RSI_Low_Trigger) and (minus>adxlevel))?color.red: color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 1, 2, str.tostring(math.round(RSI2,2)), text_halign=text.align_center, bgcolor= RSI2>RSI_High_Trigger? color.green: RSI2<RSI_Low_Trigger?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 2, 2, str.tostring(math.round(plus,2)), text_halign=text.align_center, bgcolor=plus>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 3, 2, str.tostring(math.round(minus,2)), text_halign=text.align_center, bgcolor=minus>adxlevel?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 4, 2, str.tostring(math.round(adx,2)), text_halign=text.align_center, bgcolor=adx>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 0, 3, text=symin1, text_halign=text.align_center, bgcolor=((RSIs1>RSI_High_Trigger) and (DIPlus1>adxlevel))? color.green: ((RSIs1<RSI_Low_Trigger) and (DIMinus1>adxlevel))?color.red: color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 1, 3, str.tostring(math.round(RSIs1,2)), text_halign=text.align_center, bgcolor= RSIs1>RSI_High_Trigger? color.green: RSIs1<RSI_Low_Trigger?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 2, 3, str.tostring(math.round(DIPlus1,2)), text_halign=text.align_center, bgcolor=DIPlus1>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 3, 3, str.tostring(math.round(DIMinus1,2)), text_halign=text.align_center, bgcolor=DIMinus1>adxlevel?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 4, 3, str.tostring(math.round(ADX1,2)), text_halign=text.align_center, bgcolor=ADX1>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 0, 4, text=symin2, text_halign=text.align_center, bgcolor=((RSIs2>RSI_High_Trigger) and (DIPlus2>adxlevel))? color.green: ((RSIs2<RSI_Low_Trigger) and (DIMinus2>adxlevel))?color.red: color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 1, 4, str.tostring(math.round(RSIs2,2)), text_halign=text.align_center, bgcolor= RSIs2>RSI_High_Trigger? color.green: RSIs2<RSI_Low_Trigger?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 2, 4, str.tostring(math.round(DIPlus2,2)), text_halign=text.align_center, bgcolor=DIPlus2>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 3, 4, str.tostring(math.round(DIMinus2,2)), text_halign=text.align_center, bgcolor=DIMinus2>adxlevel?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 4, 4, str.tostring(math.round(ADX2,2)), text_halign=text.align_center, bgcolor=ADX2>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 0, 5, text=symin3, text_halign=text.align_center, bgcolor=((RSIs3>RSI_High_Trigger) and (DIPlus3>adxlevel))? color.green: ((RSIs3<RSI_Low_Trigger) and (DIMinus3>adxlevel))?color.red: color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 1, 5, str.tostring(math.round(RSIs3,2)), text_halign=text.align_center, bgcolor= RSIs3>RSI_High_Trigger? color.green: RSIs3<RSI_Low_Trigger?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 2, 5, str.tostring(math.round(DIPlus3,2)), text_halign=text.align_center, bgcolor=DIPlus3>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 3, 5, str.tostring(math.round(DIMinus3,2)), text_halign=text.align_center, bgcolor=DIMinus3>adxlevel?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 4, 5, str.tostring(math.round(ADX3,2)), text_halign=text.align_center, bgcolor=ADX3>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 0, 6, text=symin4, text_halign=text.align_center, bgcolor=((RSIs4>RSI_High_Trigger) and (DIPlus4>adxlevel))? color.green: ((RSIs4<RSI_Low_Trigger) and (DIMinus4>adxlevel))?color.red: color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 1, 6, str.tostring(math.round(RSIs4,2)), text_halign=text.align_center, bgcolor= RSIs4>RSI_High_Trigger? color.green: RSIs4<RSI_Low_Trigger?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 2, 6, str.tostring(math.round(DIPlus4,2)), text_halign=text.align_center, bgcolor=DIPlus4>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 3, 6, str.tostring(math.round(DIMinus4,2)), text_halign=text.align_center, bgcolor=DIMinus4>adxlevel?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 4, 6, str.tostring(math.round(ADX4,2)), text_halign=text.align_center, bgcolor=ADX4>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 0, 7, text=symin7, text_halign=text.align_center, bgcolor=((RSIs7>RSI_High_Trigger) and (DIPlus7>adxlevel))? color.green: ((RSIs7<RSI_Low_Trigger) and (DIMinus7>adxlevel))?color.red: color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 1, 7, str.tostring(math.round(RSIs7,2)), text_halign=text.align_center, bgcolor= RSIs7>RSI_High_Trigger? color.green: RSIs7<RSI_Low_Trigger?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 2, 7, str.tostring(math.round(DIPlus7,2)), text_halign=text.align_center, bgcolor=DIPlus7>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 3, 7, str.tostring(math.round(DIMinus7,2)), text_halign=text.align_center, bgcolor=DIMinus7>adxlevel?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 4, 7, str.tostring(math.round(ADX7,2)), text_halign=text.align_center, bgcolor=ADX7>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 0, 8, text=symin8, text_halign=text.align_center, bgcolor=((RSIs8>RSI_High_Trigger) and (DIPlus8>adxlevel))? color.green: ((RSIs8<RSI_Low_Trigger) and (DIMinus8>adxlevel))?color.red: color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 1, 8, str.tostring(math.round(RSIs8,2)), text_halign=text.align_center, bgcolor= RSIs8>RSI_High_Trigger? color.green: RSIs8<RSI_Low_Trigger?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 2, 8, str.tostring(math.round(DIPlus8,2)), text_halign=text.align_center, bgcolor=DIPlus8>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 3, 8, str.tostring(math.round(DIMinus8,2)), text_halign=text.align_center, bgcolor=DIMinus8>adxlevel?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 4, 8, str.tostring(math.round(ADX8,2)), text_halign=text.align_center, bgcolor=ADX8>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 0, 9, text=symin5, text_halign=text.align_center, bgcolor=((RSIs5>RSI_High_Trigger) and (DIPlus5>adxlevel))? color.green: ((RSIs5<RSI_Low_Trigger) and (DIMinus5>adxlevel))?color.red: color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 1, 9, str.tostring(math.round(RSIs5,2)), text_halign=text.align_center, bgcolor= RSIs5>RSI_High_Trigger? color.green: RSIs5<RSI_Low_Trigger?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 2, 9, str.tostring(math.round(DIPlus5,2)), text_halign=text.align_center, bgcolor=DIPlus5>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 3, 9, str.tostring(math.round(DIMinus5,2)), text_halign=text.align_center, bgcolor=DIMinus5>adxlevel?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 4, 9, str.tostring(math.round(ADX5,2)), text_halign=text.align_center, bgcolor=ADX5>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 0, 10, text=symin6, text_halign=text.align_center, bgcolor=((RSIs6>RSI_High_Trigger) and (DIPlus6>adxlevel))? color.green: ((RSIs6<RSI_Low_Trigger) and (DIMinus6>adxlevel))?color.red: color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 1, 10, str.tostring(math.round(RSIs6,2)), text_halign=text.align_center, bgcolor= RSIs6>RSI_High_Trigger? color.green: RSIs6<RSI_Low_Trigger?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 2, 10, str.tostring(math.round(DIPlus6,2)), text_halign=text.align_center, bgcolor=DIPlus6>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 3, 10, str.tostring(math.round(DIMinus6,2)), text_halign=text.align_center, bgcolor=DIMinus6>adxlevel?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 4, 10, str.tostring(math.round(ADX6,2)), text_halign=text.align_center, bgcolor=ADX6>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 0, 11, text=symin9, text_halign=text.align_center, bgcolor=((RSIs9>RSI_High_Trigger) and (DIPlus9>adxlevel))? color.green: ((RSIs9<RSI_Low_Trigger) and (DIMinus9>adxlevel))?color.red: color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 1, 11, str.tostring(math.round(RSIs9,2)), text_halign=text.align_center, bgcolor= RSIs9>RSI_High_Trigger? color.green: RSIs9<RSI_Low_Trigger?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 2, 11, str.tostring(math.round(DIPlus9,2)), text_halign=text.align_center, bgcolor=DIPlus9>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 3, 11, str.tostring(math.round(DIMinus9,2)), text_halign=text.align_center, bgcolor=DIMinus9>adxlevel?color.red:color.gray, text_color=color.white, text_size=size.small)
//     table.cell(indicatorTable, 4, 11, str.tostring(math.round(ADX9,2)), text_halign=text.align_center, bgcolor=ADX9>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     // table.cell(indicatorTable, 0, 12, text="TCS", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
//     // table.cell(indicatorTable, 1, 12, str.tostring(math.round(RSIs10,2)), text_halign=text.align_center, bgcolor= RSIs10>RSI_High_Trigger? color.green: RSIs10<RSI_Low_Trigger?color.red:color.gray, text_color=color.white, text_size=size.small)
//     // table.cell(indicatorTable, 2, 12, str.tostring(math.round(DIPlus10,2)), text_halign=text.align_center, bgcolor=DIPlus10>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     // table.cell(indicatorTable, 3, 12, str.tostring(math.round(DIMinus10,2)), text_halign=text.align_center, bgcolor=DIMinus10>adxlevel?color.red:color.gray, text_color=color.white, text_size=size.small)
//     // table.cell(indicatorTable, 4, 12, str.tostring(math.round(ADX10,2)), text_halign=text.align_center, bgcolor=ADX10>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     // table.cell(indicatorTable, 0, 13, text="BAJFINANCE", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
//     // table.cell(indicatorTable, 1, 13, str.tostring(math.round(RSIs11,2)), text_halign=text.align_center, bgcolor= RSIs11>RSI_High_Trigger? color.green: RSIs11<RSI_Low_Trigger?color.red:color.gray, text_color=color.white, text_size=size.small)
//     // table.cell(indicatorTable, 2, 13, str.tostring(math.round(DIPlus11,2)), text_halign=text.align_center, bgcolor=DIPlus11>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     // table.cell(indicatorTable, 3, 13, str.tostring(math.round(DIMinus11,2)), text_halign=text.align_center, bgcolor=DIMinus11>adxlevel?color.red:color.gray, text_color=color.white, text_size=size.small)
//     // table.cell(indicatorTable, 4, 13, str.tostring(math.round(ADX11,2)), text_halign=text.align_center, bgcolor=ADX11>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     // // table.cell(indicatorTable, 0, 14, text="MARUTI", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
//     // table.cell(indicatorTable, 1, 14, str.tostring(math.round(RSIs12,2)), text_halign=text.align_center, bgcolor= RSIs12>RSI_High_Trigger? color.green: RSIs12<RSI_Low_Trigger?color.red:color.gray, text_color=color.white, text_size=size.small)
//     // table.cell(indicatorTable, 2, 14, str.tostring(math.round(DIPlus12,2)), text_halign=text.align_center, bgcolor=DIPlus12>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     // table.cell(indicatorTable, 3, 14, str.tostring(math.round(DIMinus12,2)), text_halign=text.align_center, bgcolor=DIMinus12>adxlevel?color.red:color.gray, text_color=color.white, text_size=size.small)
//     // table.cell(indicatorTable, 4, 14, str.tostring(math.round(ADX12,2)), text_halign=text.align_center, bgcolor=ADX12>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     // table.cell(indicatorTable, 0, 15, text="DRREDDY", text_halign=text.align_center, bgcolor=color.gray, text_color=color.white, text_size=size.small)
//     // table.cell(indicatorTable, 1, 15, str.tostring(math.round(RSIs13,2)), text_halign=text.align_center, bgcolor= RSIs13>RSI_High_Trigger? color.green: RSIs13<RSI_Low_Trigger?color.red:color.gray, text_color=color.white, text_size=size.small)
//     // table.cell(indicatorTable, 2, 15, str.tostring(math.round(DIPlus13,2)), text_halign=text.align_center, bgcolor=DIPlus13>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
//     // table.cell(indicatorTable, 3, 15, str.tostring(math.round(DIMinus13,2)), text_halign=text.align_center, bgcolor=DIMinus13>adxlevel?color.red:color.gray, text_color=color.white, text_size=size.small)
//     // table.cell(indicatorTable, 4, 15, str.tostring(math.round(ADX13,2)), text_halign=text.align_center, bgcolor=ADX13>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
    

//     //table.cell(indicatorTable, 1, 1, str.tostring(math.round(plus,2)), text_halign=text.align_center, bgcolor=plus>adxlevel?color.green:color.gray, text_color=color.white, text_size=size.small)
    

//study(title="DHL Cross over", shorttitle="PDHL Cross Over", overlay=true)
D_High = request.security(syminfo.tickerid, 'D', high[1],lookahead=barmerge.lookahead_on) 
D_Low = request.security(syminfo.tickerid, 'D', low[1],lookahead=barmerge.lookahead_on) 
D_Close =  request.security(syminfo.tickerid, 'D', close[1],lookahead=barmerge.lookahead_on) 
D_Open =  request.security(syminfo.tickerid, 'D', open[1],lookahead=barmerge.lookahead_on)
//len8 = input(14)
cdo = request.security(syminfo.tickerid, 'D', open, lookahead=barmerge.lookahead_on)
//pdc = request.security(syminfo.tickerid, 'D', close[1],lookahead=barmerge.lookahead_on )

//BRange = input.int(20, minval=0, title="Bull Bear Range eg NF25 BNF17")
Range = ((cdo*BRange/10000))

//Range = ((D_High-D_Low)/BRange) + ((D_Open-D_Close)/BRange)

//plot(isintraday ? D_High : na, title="Daily High",style=line, color=blue,linewidth=1) 
//plot(isintraday ? D_Low : na, title="Daily Low",style=line, color=blue,linewidth=1) 

// Written by Robert N.  030615
// Still looking for way to remove from previous days
//study("Open/Close Daily Line", overlay=true)

//odl = input(true, title="Open Daily Line")
//dopen = request.security(syminfo.tickerid, 'D', open[0]) 
//dcolor = close < dopen ? color.red : color.green
//plot(odl and dopen ? dopen :na , title="Daily_Open",style=plot.style_circles, color=dcolor, linewidth=3) 


//cdl = input(true, title="Previous Closing Daily Line")
// dclose = request.security(syminfo.tickerid, 'D', close[1]) 
//dcolor2 = close < dclose ? red : green
//plot(cdl and dclose ? dclose :na , title="Daily_Close",style=circles, color=dcolor2, linewidth=3) 


// daily_open(x) => 
//     trigger = na(time("D")) or ta.change(time("D"))
//     ta.valuewhen(trigger, open, x)

//plot(daily_open(0), "Daily Open", color.blue, 2, plot.style_circles)




//key = if D_Open > D_Close
// key = if cdo < D_Close   
//     ((((((D_High*2)+D_Low+D_Close)/2)-D_High)+((((D_High*2)+D_Low+D_Close)/2)-D_Low))/2)
// else 
//     ((((((D_Low*2)+D_Low+D_Close)/2)-D_High)+((((D_Low*2)+D_Low+D_Close)/2)-D_Low))/2)

//key = (cdo+pdc)/2
key = ((D_High-D_Low)/2) + ((D_Open+D_Close)/2)

//POC = (dopen+dclose)/2




//poc = input(true, title="POC Line")
//dcolor3 = close < POC ? color.red : color.green
//plot(poc and POC ? POC :na , title="POC",style=circles, color=dcolor3, linewidth=3) 
//plot(POC, title="POC",style=plot.style_linebr, color=color.orange,linewidth=3) 
pocplot = plot(title='POC', style=plot.style_linebr,series=timeframe.isintraday ? key : na, linewidth=3, color= color.orange)//series=timeframe.isintraday ? key : na
var label test =na
test :=label.new (x=bar_index, y=key, text="POC/Key level",style = label.style_label_left)
//test :=label.new (x=bar_index, y=key, text="Key level  "+ str.tostring(key, format.mintick),style = label.style_label_left)

label.delete(test[1])
//@version=5
//indicator("My script")

//hline_price_1 = POC
//hline(hline_price_1)
//label hline_label_1 = label.new(bar_index, POC, text = "POC",textalign = text.align_left, style=label.style_none, textcolor=color.black)
//label.delete(hline_label_1[1])

//plotshape(POC, style=shape.labelup, text="POC")
//plot(daily_open(0), title="POC",style=plot.style_line, color=color.black,linewidth=2) 
//plot(dclose, title="POC",style=plot.style_line, color=color.black,linewidth=2) 
//x = position_labels == "Left" ? array.get(arr_time, i) : array.get(arr_time, i + 1)
//label_style = position_labels == "Left" ? label.style_label_right : label.style_label_left
//x = "left"
//var label openLabel = label.new(x=bar_index, y=POC, text="POC/Key level", style = label.style_label_left, color=color.black, xloc=xloc.bar_index, yloc=yloc.price, size=size.small, textcolor = color.orange)
//label.set_xy(openLabel, bar_index+2, POC)
//label.set_style(id=openLabel, style=label.style_label_lower_left)
//label.set_style(openLabel,label.style_label_left)
//x = position_labels == "Left" ? array.get(arr_time, i) : array.get(arr_time, i + 1)
//        array.push(labels, label.new(x = x, y=y, text=display_text, textcolor=txt_color, style=label_style, color=#00000000, xloc=xloc.bar_time))


Bull = cdo+Range
Bear = cdo-Range
plot(Bull, title="Bull",style=plot.style_linebr, color=color.green,linewidth=3) 
plot(Bear, title="Bear",style=plot.style_linebr, color=color.red,linewidth=3) 
//plot(daily_open(0), title="Bear",style=plot.style_circles, color=color.black,linewidth=2) 

//plot(isintraday ? Range : na, title="R",style=line, color=orange,linewidth=2) 
//label_yh = label.new(Bull, text="PDHigh",  style= label.style_none)
//plot(open, title="Bull",style=plot.style_line, color=color.green,linewidth=2) 
var label BullLabel = label.new(x=bar_index, y=Bull, text="Bull Breakout", style = label.style_label_left ,color=color.black, xloc=xloc.bar_index, yloc=yloc.price, size=size.small, textcolor = color.green)
label.set_xy(BullLabel, bar_index +2, Bull)
var label BearLabel = label.new(x=bar_index, y=Bear, text="Bear Breakout", style = label.style_label_left, color=color.black, xloc=xloc.bar_index, yloc=yloc.price, size=size.small, textcolor = color.red)
label.set_xy(BearLabel, bar_index +2, Bear)






// //@version=5
// //indicator("Pivot Points Standard + Alerts V2", "Pivots + Alerts V2", overlay=true, max_lines_count=500, max_labels_count=500)

// AUTO = "Auto"
// DAILY = "Daily"
// WEEKLY = "Weekly"
// MONTHLY = "Monthly"
// QUARTERLY = "Quarterly"
// YEARLY = "Yearly"
// BIYEARLY = "Biyearly"
// TRIYEARLY = "Triyearly"
// QUINQUENNIALLY = "Quinquennially"
// DECENNIALLY = "Decennially"

// TRADITIONAL = "Traditional"
// FIBONACCI = "Fibonacci"
// WOODIE = "Woodie"
// CLASSIC = "Classic"
// DEMARK = "DM"
// CAMARILLA = "Camarilla"

// ALERT_EVERYTIME = "Everytime"
// ALERT_ONCE_PER_BAR = "Once Per Bar"
// ALERT_ONCE_PER_BAR_CLOSE = "Once Per Bar Close"

// kind = input.string(title="Type", defval="Traditional", options=[TRADITIONAL, FIBONACCI, WOODIE, CLASSIC, DEMARK, CAMARILLA])
// pivot_time_frame = input.string(title="Pivots Timeframe", defval=AUTO, options=[AUTO, DAILY, WEEKLY, MONTHLY, QUARTERLY, YEARLY, BIYEARLY, TRIYEARLY, QUINQUENNIALLY, DECENNIALLY])
// look_back = input.int(title="Number of Pivots Back", defval=15, minval=1, maxval=5000)
// is_daily_based = input.bool(title="Use Daily-based Values", defval=true, tooltip="When this option is unchecked, Pivot Points will use intraday data while calculating on intraday charts. If Extended Hours are displayed on the chart, they will be taken into account during the pivot level calculation. If intraday OHLC values are different from daily-based values (normal for stocks), the pivot levels will also differ.")
// show_labels = input.bool(title="Show Labels", defval=true, group="labels")
// show_prices = input.bool(title="Show Prices", defval=false, group="labels")
// position_labels = input.string("Left", "Labels Position", options=["Left", "Right"], group="labels")
// line_width = input.int(title="Line Width", defval=1, minval=1, maxval=100, group="levels")
// // alert_p = input.bool( false, "Alert on reaching P", group = "Alerts Options" )
// // alert_r5 = input.bool( false, "Alert on reaching R5", group = "Alerts Options" )
// // alert_r4 = input.bool( false, "Alert on reaching R4", group = "Alerts Options" )
// // alert_r3 = input.bool( false, "Alert on reaching R3", group = "Alerts Options" )
// // alert_r2 = input.bool( false, "Alert on reaching R2", group = "Alerts Options" )
// // alert_r1 = input.bool( false, "Alert on reaching R1", group = "Alerts Options" )
// // alert_s5 = input.bool( false, "Alert on reaching S5", group = "Alerts Options" )
// // alert_s4 = input.bool( false, "Alert on reaching S4", group = "Alerts Options" )
// // alert_s3 = input.bool( false, "Alert on reaching S3", group = "Alerts Options" )
// // alert_s2 = input.bool( false, "Alert on reaching S2", group = "Alerts Options" )
// // alert_s1 = input.bool( false, "Alert on reaching S1", group = "Alerts Options" )
// // alert_prev_low = input.bool( false, "Alert on Previous Day Low", group = "Alerts Options" )
// // alert_prev_high = input.bool( false, "Alert on Previous Day High", group = "Alerts Options" )
// // alert_freq = input.string(title="Alert Frequency", defval=ALERT_ONCE_PER_BAR, options=[ALERT_EVERYTIME, ALERT_ONCE_PER_BAR, ALERT_ONCE_PER_BAR_CLOSE], group = "Alerts Options")
// // real_alert_frequency = alert_freq == ALERT_ONCE_PER_BAR ? alert.freq_once_per_bar : alert_freq == ALERT_ONCE_PER_BAR_CLOSE ? alert.freq_once_per_bar_close : alert.freq_all

// var daily_high = high
// var daily_low = low
// var prev_day_high = 0.0
// var prev_day_low = 0.0

// var DEF_COLOR = #131313
// var DEF_COLOR1 = #00e804
// var DEF_COLOR2 = #fb0000

// var arr_time = array.new_int()
// var p = array.new_float()
// p_color = input.color(DEF_COLOR, "P‏  ‏  ‏", inline="P", group="levels")
// p_show = input.bool(true, "", inline="P", group="levels")
// var r1 = array.new_float()
// var s1 = array.new_float()
// s1_color = input.color(DEF_COLOR1, "S1", inline="S1/R1" , group="levels")
// s1_show = input.bool(true, "", inline="S1/R1", group="levels")
// r1_color = input.color(DEF_COLOR2, "‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏R1", inline="S1/R1", group="levels")
// r1_show = input.bool(true, "", inline="S1/R1", group="levels")
// var r2 = array.new_float()
// var s2 = array.new_float()
// s2_color = input.color(DEF_COLOR1, "S2", inline="S2/R2", group="levels")
// s2_show = input.bool(true, "", inline="S2/R2", group="levels")
// r2_color = input.color(DEF_COLOR2, "‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏R2", inline="S2/R2", group="levels")
// r2_show = input.bool(true, "", inline="S2/R2", group="levels")
// var r3 = array.new_float()
// var s3 = array.new_float()
// s3_color = input.color(DEF_COLOR1, "S3", inline="S3/R3", group="levels")
// s3_show = input.bool(true, "", inline="S3/R3", group="levels")
// r3_color = input.color(DEF_COLOR2, "‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏R3", inline="S3/R3", group="levels")
// r3_show = input.bool(true, "", inline="S3/R3", group="levels")
// var r4 = array.new_float()
// var s4 = array.new_float()
// s4_color = input.color(DEF_COLOR1, "S4", inline="S4/R4", group="levels")
// s4_show = input.bool(true, "", inline="S4/R4", group="levels")
// r4_color = input.color(DEF_COLOR2, "‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏R4", inline="S4/R4", group="levels")
// r4_show = input.bool(true, "", inline="S4/R4", group="levels")
// var r5 = array.new_float()
// var s5 = array.new_float()
// s5_color = input.color(DEF_COLOR1, "S5", inline="S5/R5", group="levels")
// s5_show = input.bool(true, "", inline="S5/R5", group="levels")
// r5_color = input.color(DEF_COLOR2, "‏  ‏  ‏  ‏  ‏  ‏  ‏  ‏R5", inline="S5/R5", group="levels")
// r5_show = input.bool(true, "", inline="S5/R5", group="levels")
// pivotX_open = float(na)
// pivotX_open := nz(pivotX_open[1], open)
// pivotX_high = float(na)
// pivotX_high := nz(pivotX_high[1], high)
// pivotX_low = float(na)
// pivotX_low := nz(pivotX_low[1], low)
// pivotX_prev_open = float(na)
// pivotX_prev_open := nz(pivotX_prev_open[1])
// pivotX_prev_high = float(na)
// pivotX_prev_high := nz(pivotX_prev_high[1])
// pivotX_prev_low = float(na)
// pivotX_prev_low := nz(pivotX_prev_low[1])
// pivotX_prev_close = float(na)
// pivotX_prev_close := nz(pivotX_prev_close[1])

// get_pivot_resolution() =>
//     resolution = "M"
//     if pivot_time_frame == AUTO
//         if timeframe.isintraday
//             resolution := timeframe.multiplier <= 15 ? "D" : "W"
//         else if timeframe.isweekly or timeframe.ismonthly
//             resolution := "12M"
//     else if pivot_time_frame == DAILY
//         resolution := "D"
//     else if pivot_time_frame == WEEKLY
//         resolution := "W"
//     else if pivot_time_frame == MONTHLY
//         resolution := "M"
//     else if pivot_time_frame == QUARTERLY
//         resolution := "3M"
//     else if pivot_time_frame == YEARLY or pivot_time_frame == BIYEARLY or pivot_time_frame == TRIYEARLY or pivot_time_frame == QUINQUENNIALLY or pivot_time_frame == DECENNIALLY
//         resolution := "12M"
//     resolution

// var lines = array.new_line()
// var labels = array.new_label()

// draw_line(i, pivot, col) =>
//     if array.size(arr_time) > 1
//         array.push(lines, line.new(array.get(arr_time, i), array.get(pivot, i), array.get(arr_time, i + 1), array.get(pivot, i), color=col, xloc=xloc.bar_time, width=line_width))

// draw_label(i, y, txt, txt_color) =>
//     if (show_labels or show_prices) and not na(y)
//         display_text = (show_labels ? txt : "") + (show_prices ? str.format(" ({0})", math.round_to_mintick(y)) : "")
//         label_style = position_labels == "Left" ? label.style_label_right : label.style_label_left
//         x = position_labels == "Left" ? array.get(arr_time, i) : array.get(arr_time, i + 1)
//         array.push(labels, label.new(x = x, y=y, text=display_text, textcolor=txt_color, style=label_style, color=#00000000, xloc=xloc.bar_time))

// traditional() =>
//     pivotX_Median = (pivotX_prev_high + pivotX_prev_low + pivotX_prev_close) / 3
//     array.push(p, pivotX_Median)
//     array.push(r1, pivotX_Median * 2 - pivotX_prev_low)
//     array.push(s1, pivotX_Median * 2 - pivotX_prev_high)
//     array.push(r2, pivotX_Median + 1 * (pivotX_prev_high - pivotX_prev_low))
//     array.push(s2, pivotX_Median - 1 * (pivotX_prev_high - pivotX_prev_low))
//     array.push(r3, pivotX_Median * 2 + (pivotX_prev_high - 2 * pivotX_prev_low))
//     array.push(s3, pivotX_Median * 2 - (2 * pivotX_prev_high - pivotX_prev_low))
//     array.push(r4, pivotX_Median * 3 + (pivotX_prev_high - 3 * pivotX_prev_low))
//     array.push(s4, pivotX_Median * 3 - (3 * pivotX_prev_high - pivotX_prev_low))
//     array.push(r5, pivotX_Median * 4 + (pivotX_prev_high - 4 * pivotX_prev_low))
//     array.push(s5, pivotX_Median * 4 - (4 * pivotX_prev_high - pivotX_prev_low))

// fibonacci() =>
//     pivotX_Median = (pivotX_prev_high + pivotX_prev_low + pivotX_prev_close) / 3
//     pivot_range = pivotX_prev_high - pivotX_prev_low
//     array.push(p, pivotX_Median)
//     array.push(r1, pivotX_Median + 0.382 * pivot_range)
//     array.push(s1, pivotX_Median - 0.382 * pivot_range)
//     array.push(r2, pivotX_Median + 0.618 * pivot_range)
//     array.push(s2, pivotX_Median - 0.618 * pivot_range)
//     array.push(r3, pivotX_Median + 1 * pivot_range)
//     array.push(s3, pivotX_Median - 1 * pivot_range)

// woodie() =>
//     pivotX_Woodie_Median = (pivotX_prev_high + pivotX_prev_low + pivotX_open * 2)/4
//     pivot_range = pivotX_prev_high - pivotX_prev_low
//     array.push(p, pivotX_Woodie_Median)
//     array.push(r1, pivotX_Woodie_Median * 2 - pivotX_prev_low)
//     array.push(s1, pivotX_Woodie_Median * 2 - pivotX_prev_high)
//     array.push(r2, pivotX_Woodie_Median + 1 * pivot_range)
//     array.push(s2, pivotX_Woodie_Median - 1 * pivot_range)

//     pivot_point_r3 = pivotX_prev_high + 2 * (pivotX_Woodie_Median - pivotX_prev_low)
//     pivot_point_s3 = pivotX_prev_low - 2 * (pivotX_prev_high - pivotX_Woodie_Median)
//     array.push(r3, pivot_point_r3)
//     array.push(s3, pivot_point_s3)
//     array.push(r4, pivot_point_r3 + pivot_range)
//     array.push(s4, pivot_point_s3 - pivot_range)

// classic() =>
//     pivotX_Median = (pivotX_prev_high + pivotX_prev_low + pivotX_prev_close)/3
//     pivot_range = pivotX_prev_high - pivotX_prev_low
//     array.push(p, pivotX_Median)
//     array.push(r1, pivotX_Median * 2 - pivotX_prev_low)
//     array.push(s1, pivotX_Median * 2 - pivotX_prev_high)
//     array.push(r2, pivotX_Median + 1 * pivot_range)
//     array.push(s2, pivotX_Median - 1 * pivot_range)
//     array.push(r3, pivotX_Median + 2 * pivot_range)
//     array.push(s3, pivotX_Median - 2 * pivot_range)
//     array.push(r4, pivotX_Median + 3 * pivot_range)
//     array.push(s4, pivotX_Median - 3 * pivot_range)

// demark() =>
//     pivotX_Demark_X = pivotX_prev_high + pivotX_prev_low * 2 + pivotX_prev_close
//     if pivotX_prev_close == pivotX_prev_open
//         pivotX_Demark_X := pivotX_prev_high + pivotX_prev_low + pivotX_prev_close * 2
//     if pivotX_prev_close > pivotX_prev_open
//         pivotX_Demark_X := pivotX_prev_high * 2 + pivotX_prev_low + pivotX_prev_close
//     array.push(p, pivotX_Demark_X / 4)
//     array.push(r1, pivotX_Demark_X / 2 - pivotX_prev_low)
//     array.push(s1, pivotX_Demark_X / 2 - pivotX_prev_high)

// camarilla() =>
//     pivotX_Median = (pivotX_prev_high + pivotX_prev_low + pivotX_prev_close) / 3
//     pivot_range = pivotX_prev_high - pivotX_prev_low
//     array.push(p, pivotX_Median)
//     array.push(r1, pivotX_prev_close + pivot_range * 1.1 / 12.0)
//     array.push(s1, pivotX_prev_close - pivot_range * 1.1 / 12.0)
//     array.push(r2, pivotX_prev_close + pivot_range * 1.1 / 6.0)
//     array.push(s2, pivotX_prev_close - pivot_range * 1.1 / 6.0)
//     array.push(r3, pivotX_prev_close + pivot_range * 1.1 / 4.0)
//     array.push(s3, pivotX_prev_close - pivot_range * 1.1 / 4.0)
//     array.push(r4, pivotX_prev_close + pivot_range * 1.1 / 2.0)
//     array.push(s4, pivotX_prev_close - pivot_range * 1.1 / 2.0)
//     r5_val = pivotX_prev_high / pivotX_prev_low * pivotX_prev_close
// 	array.push(r5, r5_val)
// 	array.push(s5, 2 * pivotX_prev_close - r5_val)

// calc_pivot() =>
//     if kind == TRADITIONAL
//         traditional()
//     else if kind == FIBONACCI
//         fibonacci()
//     else if kind == WOODIE
//         woodie()
//     else if kind == CLASSIC
//         classic()
//     else if kind == DEMARK
//         demark()
//     else if kind == CAMARILLA
//         camarilla()

// resolution = get_pivot_resolution()

// SIMPLE_DIVISOR = -1
// custom_years_divisor = switch pivot_time_frame
// 	BIYEARLY => 2
// 	TRIYEARLY => 3
// 	QUINQUENNIALLY => 5
// 	DECENNIALLY => 10
// 	=> SIMPLE_DIVISOR

// calc_high(prev, curr) =>
//     if na(prev) or na(curr)
//         nz(prev, nz(curr, na))
//     else
//         math.max(prev, curr)
    
// calc_low(prev, curr) =>
//     if not na(prev) and not na(curr)
//         math.min(prev, curr)
//     else
//         nz(prev, nz(curr, na))

// calc_OHLC_for_pivot(custom_years_divisor) =>
//     if custom_years_divisor == SIMPLE_DIVISOR 
//         [open, high, low, close, open[1], high[1], low[1], close[1], time[1], time_close]
//     else
//         var prev_sec_open = float(na)
//         var prev_sec_high = float(na)
//         var prev_sec_low = float(na)
//         var prev_sec_close = float(na)
//         var prev_sec_time = int(na)
//         var curr_sec_open = float(na)
//         var curr_sec_high = float(na)
//         var curr_sec_low = float(na)
//         var curr_sec_close = float(na)
//         if year(time_close) % custom_years_divisor == 0
//         	curr_sec_open := open
// 			curr_sec_high := high
// 			curr_sec_low := low
// 			curr_sec_close := close
//             prev_sec_high := high[1]
//             prev_sec_low := low[1]
//             prev_sec_close := close[1]
//             prev_sec_time := time[1]
//             for i = 2 to custom_years_divisor
//                 prev_sec_open :=  nz(open[i], prev_sec_open)
//                 prev_sec_high := calc_high(prev_sec_high, high[i])
//                 prev_sec_low := calc_low(prev_sec_low, low[i])
//                 prev_sec_time := nz(time[i], prev_sec_time)
//         [curr_sec_open, curr_sec_high, curr_sec_low, curr_sec_close, prev_sec_open, prev_sec_high, prev_sec_low, prev_sec_close, prev_sec_time, time_close]


// // store previous day low and high at next market day start
// if timeframe.change( "1D")
//     prev_day_high := daily_high
//     prev_day_low := daily_low
//     daily_high := high
//     daily_low := low
 
// // update daily high and low
// if high > daily_high
//     daily_high := high

// if low < daily_low
//     daily_low := low

// plot( prev_day_high, "Prev Day High", color.aqua )
// plot( prev_day_low, "Prev Day Low", color.maroon )

// [sec_open, sec_high, sec_low, sec_close, prev_sec_open, prev_sec_high, prev_sec_low, prev_sec_close, prev_sec_time, sec_time] = request.security(syminfo.tickerid, resolution, calc_OHLC_for_pivot(custom_years_divisor), lookahead = barmerge.lookahead_on)
// sec_open_gaps_on = request.security(syminfo.tickerid, resolution, open, gaps = barmerge.gaps_on, lookahead = barmerge.lookahead_on)

// is_change_years = custom_years_divisor > 0 and ta.change(time(resolution)) and year(time_close) % custom_years_divisor == 0

// var is_change = false
// var uses_current_bar = timeframe.isintraday and kind == WOODIE
// var change_time = int(na)
// is_time_change = (ta.change(time(resolution)) and custom_years_divisor == SIMPLE_DIVISOR) or is_change_years
// if is_time_change
//     change_time := time

// var start_time = time
// var was_last_premarket = false
// var start_calculate_in_premarket = false

// is_last_premarket = barstate.islast and session.ispremarket and time_close > sec_time and not was_last_premarket

// if is_last_premarket
//     was_last_premarket := true
//     start_calculate_in_premarket := true
// if session.ismarket
//     was_last_premarket := false
    
// without_time_change = barstate.islast and array.size(arr_time) == 0
// is_can_calc_pivot = (not uses_current_bar and is_time_change and session.ismarket) or (ta.change(sec_open) and not start_calculate_in_premarket) or is_last_premarket or (uses_current_bar and not na(sec_open_gaps_on)) or without_time_change
// enough_bars_for_calculate = prev_sec_time >= start_time or is_daily_based

// if is_can_calc_pivot and enough_bars_for_calculate 
//     if array.size(arr_time) == 0 and is_daily_based
//         pivotX_prev_open := prev_sec_open[1]
//         pivotX_prev_high := prev_sec_high[1]
//         pivotX_prev_low := prev_sec_low[1]
//         pivotX_prev_close := prev_sec_close[1]
//         pivotX_open := sec_open[1]
//         pivotX_high := sec_high[1]
//         pivotX_low := sec_low[1]
//         array.push(arr_time, start_time)
//         calc_pivot()
    
//     if is_daily_based
//     	if is_last_premarket
//             pivotX_prev_open := sec_open
//             pivotX_prev_high := sec_high
//             pivotX_prev_low := sec_low
//             pivotX_prev_close := sec_close
//             pivotX_open := open
//             pivotX_high := high
//             pivotX_low := low
//         else
// 			pivotX_prev_open := prev_sec_open
// 			pivotX_prev_high := prev_sec_high
// 			pivotX_prev_low := prev_sec_low
// 			pivotX_prev_close := prev_sec_close
// 			pivotX_open := sec_open
// 			pivotX_high := sec_high
// 			pivotX_low := sec_low
//     else
//         pivotX_prev_high := pivotX_high
//         pivotX_prev_low := pivotX_low
//         pivotX_prev_open := pivotX_open
//         pivotX_prev_close := close[1]
//         pivotX_open := open
//         pivotX_high := high
//         pivotX_low := low

//     if barstate.islast and not is_change and array.size(arr_time) > 0 and not without_time_change
//         array.set(arr_time, array.size(arr_time) - 1, change_time)
//     else if without_time_change
//         array.push(arr_time, start_time)
//     else
//         array.push(arr_time, nz(change_time, time))

//     calc_pivot()

//     if array.size(arr_time) > look_back
//         if array.size(arr_time) > 0
//             array.shift(arr_time)
//         if array.size(p) > 0 and p_show
//             array.shift(p)
//         if array.size(r1) > 0 and r1_show
//             array.shift(r1)
//         if array.size(s1) > 0 and s1_show
//             array.shift(s1)
//         if array.size(r2) > 0 and r2_show
//             array.shift(r2)
//         if array.size(s2) > 0 and s2_show
//             array.shift(s2)
//         if array.size(r3) > 0 and r3_show
//             array.shift(r3)
//         if array.size(s3) > 0 and s3_show
//             array.shift(s3)
//         if array.size(r4) > 0 and r4_show
//             array.shift(r4)
//         if array.size(s4) > 0 and s4_show
//             array.shift(s4)
//         if array.size(r5) > 0 and r5_show
//             array.shift(r5)
//         if array.size(s5) > 0 and s5_show
//             array.shift(s5)
//     is_change := true
// else if not is_daily_based
//     pivotX_high := math.max(pivotX_high, high)
//     pivotX_low := math.min(pivotX_low, low)

// if barstate.islast and not is_daily_based and array.size(arr_time) == 0 
//     runtime.error("Not enough intraday data to calculate Pivot Points. Lower the Pivots Timeframe or turn on the 'Use Daily-based Values' option in the indicator settings.")

// // if array.size(arr_time) > 0
// //     //label.new(bar_index, high, text = "close " + str.tostring(close) + ",p " + str.tostring( array.get( p, array.size(p) - 1 ) ) + ",s2 " + str.tostring( array.get( s2, array.size(s2) - 1 ) ) + ",s1x " + str.tostring(ta.cross( array.get(s1, array.size(s1) - 1 ), close )), color = color.white, textcolor = color.black )

// //     if array.size(p) > 0
// //         if ta.cross( array.get(p, array.size(p) - 1 ), close ) and alert_p
// //             alert("Price crossed over P level.", real_alert_frequency)

// //     if array.size(s1) > 0
// //         if ta.cross( array.get(s1, array.size(s1) - 1 ), close ) and alert_s1
// //             alert("Price crossed over S1 level.", real_alert_frequency)

// //     if array.size(s2) > 0
// //         if ta.cross( array.get(s2, array.size(s2) - 1 ), close ) and alert_s2
// //             alert("Price crossed over S2 level.", real_alert_frequency)

// //     if array.size(s3) > 0
// //         if ta.cross( array.get(s3, array.size(s3) - 1 ), close ) and alert_s3
// //             alert("Price crossed over S3 level.", real_alert_frequency)

// //     if array.size(s4) > 0
// //         if ta.cross( array.get(s4, array.size(s4) - 1 ), close ) and alert_s4
// //             alert("Price crossed over S4 level.", real_alert_frequency)

// //     if array.size(s5) > 0
// //         if ta.cross( array.get(s5, array.size(s5) - 1 ), close ) and alert_s5
// //             alert("Price crossed over S5 level.", real_alert_frequency)

// //     if array.size(r1) > 0
// //         if ta.cross( array.get(r1, array.size(r1) - 1 ), close ) and alert_r1
// //             alert("Price crossed over R1 level.", real_alert_frequency)

// //     if array.size(r2) > 0
// //         if ta.cross( array.get(r2, array.size(r2) - 1 ), close ) and alert_r2
// //             alert("Price crossed over R2 level.", real_alert_frequency)

// //     if array.size(r3) > 0
// //         if ta.cross( array.get(r3, array.size(r3) - 1 ), close ) and alert_r3
// //             alert("Price crossed over R3 level.", real_alert_frequency)

// //     if array.size(r4) > 0
// //         if ta.cross( array.get(r4, array.size(r4) - 1 ), close ) and alert_r4
// //             alert("Price crossed over R4 level.", real_alert_frequency)

// //     if array.size(r5) > 0
// //         if ta.cross( array.get(r5, array.size(r5) - 1 ), close ) and alert_r5
// //             alert("Price crossed over R5 level.", real_alert_frequency)

// // // previous day high/low
// // if ta.cross( prev_day_low, close ) and alert_prev_low
// //     alert("Price crossed over Previous Day Low.", real_alert_frequency)

// // if ta.cross( prev_day_high, close ) and alert_prev_high
// //     alert("Price crossed over Previous Day High.", real_alert_frequency)

// if barstate.islast and array.size(arr_time) > 0 and is_change
//     is_change := false
//     if custom_years_divisor > 0
//         last_pivot_time = array.get(arr_time, array.size(arr_time) - 1)
//         pivot_timeframe = str.tostring(12 * custom_years_divisor) + "M"
//         estimate_pivot_time = last_pivot_time + timeframe.in_seconds(pivot_timeframe) * 1000
//         array.push(arr_time, estimate_pivot_time)
//     else
//         array.push(arr_time, time_close(resolution))

//     for i = 0 to array.size(lines) - 1
//         if array.size(lines) > 0
//             line.delete(array.shift(lines))
//         if array.size(labels) > 0
//             label.delete(array.shift(labels))

//     for i = 0 to array.size(arr_time) - 2
//         if array.size(p) > 0 and p_show
//             draw_line(i, p, p_color)
//             draw_label(i, array.get(p, i), "P", p_color)
//         if array.size(r1) > 0 and r1_show
//             draw_line(i, r1, r1_color)
//             draw_label(i, array.get(r1, i), "R1", r1_color)
//         if array.size(s1) > 0 and s1_show
//             draw_line(i, s1, s1_color)
//             draw_label(i, array.get(s1, i), "S1", s1_color)
//         if array.size(r2) > 0 and r2_show
//             draw_line(i, r2, r2_color)
//             draw_label(i, array.get(r2, i), "R2", r2_color)
//         if array.size(s2) > 0 and s2_show
//             draw_line(i, s2, s2_color)
//             draw_label(i, array.get(s2, i), "S2", s2_color)
//         if array.size(r3) > 0 and r3_show
//             draw_line(i, r3, r3_color)
//             draw_label(i, array.get(r3, i), "R3", r3_color)
//         if array.size(s3) > 0 and s3_show
//             draw_line(i, s3, s3_color)
//             draw_label(i, array.get(s3, i), "S3", s3_color)
//         if array.size(r4) > 0 and r4_show
//             draw_line(i, r4, r4_color)
//             draw_label(i, array.get(r4, i), "R4", r4_color)
//         if array.size(s4) > 0 and s4_show
//             draw_line(i, s4, s4_color)
//             draw_label(i, array.get(s4, i), "S4", s4_color)
//         if array.size(r5) > 0 and r5_show
//             draw_line(i, r5, r5_color)
//             draw_label(i, array.get(r5, i), "R5", r5_color)
//         if array.size(s5) > 0 and s5_show
//             draw_line(i, s5, s5_color)
//             draw_label(i, array.get(s5, i), "S5", s5_color)




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

//@version=5
//indicator("34 EMA Bands", overlay = true)


//Constants
//EMA_HTF = input.int(33, title="EMA HTF", minval=1, maxval=100)

h_EMA = ta.ema(high, EMA_HTF)
l_EMA = ta.ema(low, EMA_HTF)
EMA = ta.ema(close, EMA_HTF)


//Multi Timeframe
//res = input.timeframe(defval='240', title="Timeframe")

h_EMAres = request.security(syminfo.tickerid, res, h_EMA)
l_EMAres = request.security(syminfo.tickerid, res, l_EMA)
EMAres = request.security(syminfo.tickerid, res, EMA)


// //Lower reversion zone bands
// b_High = ((h_EMAres - EMAres) * math.phi) * math.pi + EMAres
// b_Low = (-(EMAres - l_EMAres) * math.phi) * math.pi + EMAres

// //Lower reversion zone bands smoothed
// b_High_S = ta.wma(b_High, 8)
// b_Low_S = ta.wma(b_Low, 8)


// //Higher reversion zone bands
// phi_High = ((h_EMAres - EMAres) * math.phi) * (math.phi + 4) + EMAres
// phi_Low = (-(EMAres - l_EMAres) * math.phi) * (math.phi + 4) + EMAres

// //Higher reversion zone bands smoothed
// phi_High_S = ta.wma(phi_High, 8)
// phi_Low_S = ta.wma(phi_Low, 8)

// // //====================================================================================================//

// //Median zone bands [plot]
highP1 = plot(h_EMAres, color = color.blue, title = "Top median zone", display=display.none)
lowP1 = plot(l_EMAres, color = color.blue, title = "Bottom median zone", display=display.none)

// //Lower reversion zone bands [plot]
// highP3 = plot(b_High_S, color = color.yellow, title = "Lower sell zone", display=display.none)
// lowP3 = plot(b_Low_S, color = color.teal, title = "Higher buy zone", display=display.none)

// //Higher reversion zone bands [plot]
// phiPlotHigh = plot(phi_High_S, color = color.red, title = "Top sell zone")
// phiPlotLow = plot(phi_Low_S, color = color.green, title = "Bottom buy zone")


// //Sell zone region [fill]
// fill(phiPlotHigh, highP3, color.new(color.red, 95), title = "Sell zone")

// //Buy zone region [fill]
// fill(lowP3, phiPlotLow, color.new(color.green, 95), title = "Buy zone")

//Median zone region [fill]
fill(highP1, lowP1, color.new(color.black, 85), title = "Median zone")




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

// This script plots, and auto-updates, 3 separate VWAPs: a traditional VWAP, a VWAP anchored to a trends high,
// and another anchored to a trends low. Because VWAP is a prominent market maker tool for executing large trades,
// day traders can use it to better anticipate trends, mean reversion, and breakouts.
// More here: https://www.tradingview.com/script/72stxVxI-Anchored-VWAP-Auto-High-Low/

//@version=5 
//indicator("Anchored VWAP (Auto High & Low)", "AVWAP (Auto Hi / Lo)", overlay=true, max_bars_back=0)
max_bars_back(time, 1440)

// Base Colors
none = color.new(color.black, 100), clr1 = color.aqua

// Groups
g1 = "Anchored VWAP", g2 = "Normal VWAP"

// // High Anchor
// shoHiA = input.bool(false, "High Anchor           ", "VWAP anchored and weighted to trend highs.", inline="hi", group=g1, display=display.none)
// hiAnClr = input.color(clr1, "", inline="hi", group=g1, display=display.none)
// hiWidth = input.int(2, "", 1, inline="hi", group=g1, display=display.none)

// // Low Anchor
// shoLoA = input.bool(false, "Low Anchor            ", "VWAP anchored and weighted to trend lows.", inline="lo", group=g1, display=display.none)
// loAnClr = input.color(clr1, "", inline="lo", group=g1, display=display.none)
// loWidth = input.int(2, "", 1, inline="lo", group=g1, display=display.none)

// // Average of Anchors
// shoMid = input.bool(false, "Avg. of Anchors     ", "The mean (average) between the high and low anchored VWAP's", inline="mid", group=g1, display=display.none)
// midClr = input.color(color.new(clr1, 50), "", inline="mid", group=g1, display=display.none)
// mWidth = input.int(2, "", 1, inline="mid", group=g1, display=display.none)

// // Quarter levels
// shoQrt = input.bool(false, "Quarter Values       ", "The median (middle) value between the mean (average) and high / low anchors.", inline="quarter", group=g1, display=display.none)
// qrtClr = input.color(color.new(clr1, 80), "", inline="quarter", group=g1, display=display.none)
// qWidth = input.int(1,"", 1, inline="quarter", group=g1, display=display.none)

// // High eighth levels with fill
// shoHiFil = input.bool(false, "High Interim Bands",  inline="hiFill", group=g1, display=display.none)
// hiEthClr = input.color(color.new(clr1, 93), "", inline="hiFill", group=g1, display=display.none)
// hiFilClr = input.color(color.new(clr1, 97), "", inline="hiFill", group=g1, display=display.none)

// // Low eighth levels with fill
// shoLoFil = input.bool(false, "Low Interim Bands ",  inline="loFill", group=g1, display=display.none)
// loEthClr = input.color(color.new(clr1, 93), "", inline="loFill", group=g1, display=display.none)
// loFilClr = input.color(color.new(clr1, 97), "", inline="loFill", group=g1, display=display.none)

// Smooth Average of Anchors
//shoMA = input.bool(false, "Smooth Average    ", inline="smooth", group=g1, display=display.none)
//maSrc = input.string("SMMA (RMA)", "", ["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "Linear Regression"], inline="smooth", group=g1, display=display.none)
//maLen = input.int(3, "", 1, inline="smooth", group=g1, display=display.none)

// Anchor Reset Settings
sessionLimited = input.bool(false, "Limit anchor session continuation to ", "Defines when a given anchor should be reset if its VWAP has not been broken, measured in sessions.", inline="reset", group=g1, display=display.none)
anchorMax = input.int(1, "", 1, inline="reset", group=g1, display=display.none)
mktHrsOnly = input.bool(false, "Limit anchor resets to only regular trading hours and new sessions.", "Prevents a given VWAP from resetting afhours, overnight, or during the premarket.", group=g1, display=display.none)

// 1 Day VWAP
O = "open", H = "high", L = "low", C = "close", HL2 = "hl2", HLC3 = "hlc3", OHLC4 = "ohlc4", HLCC4 = "hlcc4"
shoVWAP = input.bool(true, "VWAP", "The Volume Weighted Average Price is reset at the beginning of each session.", inline="vwap1", group=g2, display=display.none)
vwap1Clr = input.color(color.blue, "", inline="vwap1", group=g2, display=display.none)
vWidth1 = input.int(2, "", 1, inline="vwap1", group=g2, display=display.none)
source1 = input.string(HLC3, "", [O,H,L,C,HL2, HLC3, OHLC4, HLCC4], inline="vwap1", group=g2, display=display.none)

// 2 Day VWAP
shoDay2 = input.bool(true, "Day 2 ", "Continuation of VWAP into a second day.", inline="vwap2", group=g2, display=display.none)
vwap2Clr = input.color(color.new(color.teal, 25), "", inline="vwap2", group=g2, display=display.none)
vWidth2 = input.int(2, "", 1, inline="vwap2", group=g2, display=display.none)
source2 = input.string(HLC3, "", [O,H,L,C,HL2, HLC3, OHLC4, HLCC4], inline="vwap2", group=g2, display=display.none)

// 3 Day VWAP
shoDay3 = input.bool(true, "Day 3 ", "Continuation of VWAP into a third day.", inline="vwap3", group=g2, display=display.none)
vwap3Clr = input.color(color.new(color.red, 50), "", inline="vwap3", group=g2, display=display.none)
vWidth3 = input.int(2, "", 1, inline="vwap3", group=g2, display=display.none)
source3 = input.string(HLC3, "", [O,H,L,C,HL2, HLC3, OHLC4, HLCC4], inline="vwap3", group=g2, display=display.none)

// Market Hours Settings
mktHrs = input.session("0930-1600", "Start / End Time", tooltip = "A 24 hour format, where 0930 is 9:30 AM, 1600 is 4:00 PM, etc.", group="Market Hours", inline="mkt", display=display.none)
zone = input("America/New_York", "Time Zone          ", "Any IANA time zone ID. Ex: America/New_York\n\nYou can also use \"syminfo.timezone\", which inherits the time zone of the exchange of the chart.", group="Market Hours", inline="zone", display=display.none)
timezone = zone == "syminfo.timezone" ? syminfo.timezone : zone
minutesInDay = "1440", marketHours = time(minutesInDay, mktHrs, timezone)

// Put colors into an array.
//colors = array.from(hiAnClr, loAnClr, midClr, qrtClr, hiEthClr, loEthClr, hiFilClr, loFilClr)

// Define new sessions.
newSession = timeframe.isdwm ? false : session.isfirstbar

// Set core variables.
o = open, h = high, l = low, c = close, index = bar_index

// Define candle patterns and parts.
candleBody = math.abs( c - o )
rising = c > o
falling = o > c
upWick = rising ? h - c : h - o
dnWick = falling ? c - l : o - l
doji = (upWick >= candleBody or dnWick >= candleBody) and (h != o and h != c and l != o and l != c)
higher = o > o[1] and c > c[1]
lower = o < o[1] and c < c[1]
risingHigher = rising and higher
fallingLower = falling and lower

// Anchor variables.
var hi = h, var lo = l, var anchoredHi = h, var anchoredLo = l, var hrH = h, var hrL = l, var lrH = h, var lrL = l
var resetHiAnchor = false, var resetLoAnchor = false, var bullish = false, var bearish = false
var hiSessionCount = 0, var loSessionCount = 0

// Logic for what qualifies as "breaking" the previous anchors.
breakingHigher = rising  and (c > hi or (c[1] > anchoredHi and c > h[1] and o > anchoredHi and not doji))
breakingLower  = falling and (c < lo or (c[1] < anchoredLo and c < l[1] and o < anchoredLo and not doji))

// Logic for when to reset anchors.
if newSession and sessionLimited
    hiSessionCount += 1, loSessionCount += 1
    if hiSessionCount == anchorMax
        hiSessionCount := 0
        hi := h, hrH := h, hrL := l
        resetHiAnchor := true
        bullish := true
    if loSessionCount == anchorMax
        loSessionCount := 0
        lo := l, lrH := h, lrL := l
        resetLoAnchor := true
        bearish := true
else if breakingHigher and (not mktHrsOnly or (marketHours and marketHours))
    hiSessionCount := 0
    hi := h, hrH := h, hrL := l
    resetHiAnchor := true
    bullish := true
else if breakingLower and (not mktHrsOnly or (marketHours and marketHours))
    loSessionCount := 0
    lo := l, lrH := h, lrL := l
    resetLoAnchor := true
    bearish := true
else
    resetHiAnchor := false
    resetLoAnchor := false

// Set the Anchored VWAP, and their average.
anchoredHi := ta.vwap(h, resetHiAnchor)
anchoredLo := ta.vwap(l, resetLoAnchor)
anchoredMean = math.avg(anchoredHi, anchoredLo)

// // Smooth the average according to the traders settings.
// if shoMA
//     anchoredMean := switch maSrc
//         "SMA" => ta.sma(anchoredMean, maLen)
//         "EMA" => ta.ema(anchoredMean, maLen)
//         "SMMA (RMA)" => ta.rma(anchoredMean, maLen)
//         "WMA" => ta.wma(anchoredMean, maLen)
//         "VWMA" => ta.vwma(anchoredMean, maLen)
//         "Linear Regression" => ta.linreg(anchoredMean, maLen, 0)

// // Set the anchored quarter values.
// avg75 = math.avg(anchoredHi, anchoredMean), avg87 = math.avg(anchoredHi, avg75), avg62 = math.avg(anchoredMean, avg75)
// avg25 = math.avg(anchoredLo, anchoredMean), avg12 = math.avg(anchoredLo, avg25), avg42 = math.avg(anchoredMean, avg25)

// Function to get a price.
price(a) =>
    switch a
        O => open
        H => high
        L => low
        C => close
        HL2 => hl2
        HLC3 => hlc3
        OHLC4 => ohlc4
        HLCC4 => hlcc4

// Function to get yesterdays total volume.
ydVol() => request.security(syminfo.tickerid, "1D", volume)

// Initialize variables for the VWAP handoff.
var vwap1D = 0., var handoff = array.new_float(4, 0)

// Handoff VWAP values from the previous day.
if newSession
    handoff.unshift(ydVol()), handoff.pop()
    handoff.unshift(vwap1D), handoff.pop()

// Calc VWAP continuation beyond the first day.
for i = 0 to handoff.size() / 2 - 1
    j = i * 2
    vwap = handoff.get(j)
    src = switch i
        0 => source2
        1 => source3
    vol = volume
    cvol = handoff.get(j + 1)
    newV = cvol + vol
    newP = (vwap * cvol + price(src) * vol) / newV
    handoff.set(j, newP)
    handoff.set(j + 1, newV)

// Set VWAP value.
vwap1D := ta.vwap(price(source1), timeframe.change("1D"))

// Set VWAP colors.
v1Clr = newSession ? none : vwap1Clr
v2Clr = newSession ? none : vwap2Clr
v3Clr = newSession ? none : vwap3Clr

// Plot VWAPs
plot(vwap1D, "1 Day VWAP", v1Clr, vWidth1, plot.style_linebr, false, na, na, na, false, na, shoVWAP  ? display.pane : display.none)
plot(handoff.get(0), "2 Day VWAP", v2Clr, vWidth2, plot.style_linebr, false, na, na, na, false, na, shoDay2 ? display.pane : display.none)
plot(handoff.get(2), "3 Day VWAP", v3Clr, vWidth3, plot.style_linebr, false, na, na, na, false, na, shoDay3 ? display.pane : display.none)

// Function defining abnormal candle move continuation.
// moveCont(d) =>
//     switch d
//         "up" => anchoredHi >= anchoredHi[1] or hlcc4 > avg87
//         "dn" => anchoredLo <= anchoredLo[1] or hlcc4 < avg12

// // Function defining candle consolidation, relative to the previous anchor.
// consolidating(d, resetHi, resetLo) => doji or (o <= resetHi and c <= resetHi and o >= resetLo and c >= resetLo)
    
// // Set the plot and fill colors.
// var clr = array.new_color(colors.size())
// for i = 0 to colors.size() - 1
//     _c_ = colors.get(i)
//     _ifTrue_ = switch i
//         0 => bullish and (resetHiAnchor or moveCont("up") or consolidating("up", hrH, hrL))
//         1 => bearish and (resetLoAnchor or moveCont("dn") or consolidating("dn", lrH, lrL))
//         => false
//     clr.set(i, newSession ? none : _ifTrue_ ? color.new(_c_, 75) : _c_)

// // Toggle the colors reset to be false, if it has been reset back to the user specified color.
// bullish := clr.get(0) == hiAnClr ? false : bullish
// bearish := clr.get(1) == loAnClr ? false : bearish

// // Functions to determine if a plot or fill should display, based on the traders settings.
// displayPlot(ifTrue) => ifTrue ? display.pane : display.none
// displayFill(ifTrue) => ifTrue ? display.all : display.none

// Plot the top, bottom, and average anchor values.
// top = plot(anchoredHi, "High Anchor", clr.get(0), hiWidth, plot.style_linebr, false, na, na, na, false, na, displayPlot(shoHiA))
// bot = plot(anchoredLo, "Low Anchor", clr.get(1), loWidth, plot.style_linebr, false, na, na, na, false, na, displayPlot(shoLoA))
// mid = plot(anchoredMean, "Average of Anchors", clr.get(2), mWidth, plot.style_linebr, false, na, na, na, false, na, displayPlot(shoMid))

// Plot 1/4 values (half way between average and anchor).
// t25 = plot(avg75, "Top 25% Line", clr.get(3), qWidth, plot.style_linebr, false, na, na, na, false, na, displayPlot(shoQrt))
// b25 = plot(avg25, "Bottom 25% Line", clr.get(3), qWidth, plot.style_linebr, false, na, na, na, false, na, displayPlot(shoQrt))

// Plot 1/8 lines.
// showHideTopLine = displayPlot(shoHiFil)
// showHideBotLine = displayPlot(shoLoFil)
// t12  = plot(avg87, "Top 12.5% Line",clr.get(4), 1, plot.style_linebr, false, na, na, na, false, na, showHideTopLine)
// t252 = plot(avg75, "Top 25% Line", clr.get(4), 1, plot.style_linebr, false, na, na, na, false, na, showHideTopLine)
// t42  = plot(avg62, "Top 42.5% Line",clr.get(4), 1, plot.style_linebr, false, na, na, na, false, na, showHideTopLine)
// b42  = plot(avg42, "Bottom 42.5% Line",clr.get(5), 1, plot.style_linebr, false, na, na, na, false, na, showHideBotLine)
// b252 = plot(avg25, "Bottom 25% Line", clr.get(5), 1, plot.style_linebr, false, na, na, na, false, na, showHideBotLine)
// b12  = plot(avg12, "Bottom 12.5% Line",clr.get(5), 1, plot.style_linebr, false, na, na, na, false, na, showHideBotLine)

// Plot 1/8 fills.
// showHideTopFill = displayFill(shoHiFil)
// showHideBotFill = displayFill(shoLoFil)
// fill(top, t12, clr.get(6), "Top 12.5% Fill", false, display = showHideTopFill)
// fill(top, t25, clr.get(6), "Top 25% Fill", false, display = showHideTopFill)
// fill(top, t42, clr.get(6), "Top 42.5% Fill", false, display = showHideTopFill)
// fill(bot, b42, clr.get(7), "Bottom 42.5% Fill", false, display = showHideBotFill)
// fill(bot, b25, clr.get(7), "Bottom 25% Fill", false, display = showHideBotFill)
// fill(bot, b12, clr.get(7), "Bottom 12.5% Fill", false, display = showHideBotFill)


//         SFP Inquisitor
//              1.0
// 
//      coded by Bogdan Vaida

// Code for Swing High, Swing Low and Swing Failure Pattern.

// Note that the number you set in your Swing History variable
// will also be the minimum delay you see until the apples appear.
// This is because we're checking the forward "history" too.

// The SFP will only check for these conditions:
// - high above Swing History high and close below it
// - low below Swing History high and close above it

// In some cases you may see an apple before the SFP that "doesn't fit"
// with the SFP conditions. That's because that apple was drawn later and 
// the SFP actually appeared because of the previous apple.
// 20 candles later.

// Legend:
//   🍏 - swing high
//   🍎 - swing low
//   🍌 - swing failure pattern
//   🍎🍌 - hungry scenario: swing low but also a SFP compared to the last swing
//   🍰 - cake is for stronger SFPs / SFPs in a row (but with a the secret sauce)

// IDEAS:
//  - show potential swing highs/lows (where current bar is < Swing History)
//  - different types of bananas (or kiwi on 1' and a mango on a HTF SFP) (credits: @imal_max)


// Tested on 5' BTCUSDT and 4h ETHUSD

//@version=5

//indicator(title='🍏🍎🍌 Swing Failure Pattern Inquisitor', shorttitle='SFP-I', overlay=true)
// 🔥 Uncomment the lines below for the strategy and revert for the study
// strategy(title="🍏🍎🍌 Swing Failure Pattern Inquisitor", shorttitle="SFP-I", overlay=true, pyramiding=1,
//      process_orders_on_close=true, calc_on_every_tick=true,
//      initial_capital=1000, currency = currency.USD, default_qty_value=10, 
//      default_qty_type=strategy.percent_of_equity,
//      commission_type=strategy.commission.percent, commission_value=0.1, slippage=2)

// swingHistory = input.int(10, title='Swing history:', minval=1)
// plotSwings = input(true, title='Plot swings')
// plotFirstSFPOnly = input(false, title='Plot only first SFP candle')
// plotStrongerSFPs = input.int(10, title='Plot stronger SFPs:', minval=0)

// tradingDirection = input.string(title='Trading Direction: ', group='Strategy', defval='L', options=['L&S', 'L', 'S'])
// stopLoss = input.float(1.0, title='Stop Loss %', group='Strategy') / 100
// takeProfit = input.float(3.0, title='Take Profit %', group='Strategy') / 100
// plotTP = input.bool(true, title='Take Profit?', group='Strategy', inline='plot1')
// plotSL = input.bool(true, title='Stop-Loss?', group='Strategy', inline='plot1')
// plotBgColor = input.bool(true, title='Plot Background Color?', group='Strategy')

// startDate = input.int(title='Start Date', defval=1, minval=1, maxval=31, group='Backtesting range')
// startMonth = input.int(title='Start Month', defval=5, minval=1, maxval=12, group='Backtesting range')
// startYear = input.int(title='Start Year', defval=2021, minval=1800, maxval=2100, group='Backtesting range')
// endDate = input.int(title='End Date', defval=1, minval=1, maxval=31, group='Backtesting range')
// endMonth = input.int(title='End Month', defval=1, minval=1, maxval=12, group='Backtesting range')
// endYear = input.int(title='End Year', defval=2040, minval=1800, maxval=2100, group='Backtesting range')

// // Date range filtering
// inDateRange = time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0) and time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 23, 59)


// // Swing Lows and Swing Highs code
// isSwingHigh = false
// isSwingLow = false
// swingHigh = high[swingHistory]
// swingLow = low[swingHistory]
// range_1 = swingHistory * 2

// for i = 0 to range_1 by 1
//     isSwingHigh := true
//     if i < swingHistory
//         if high[i] > swingHigh
//             isSwingHigh := false
//             break
//     if i > swingHistory
//         if high[i] >= swingHigh
//             isSwingHigh := false
//             break

// for i = 0 to range_1 by 1
//     isSwingLow := true
//     if i < swingHistory
//         if low[i] < swingLow
//             isSwingLow := false
//             break
//     if i > swingHistory
//         if low[i] <= swingLow
//             isSwingLow := false
//             break

// // Swing Failure Pattern
// isSwingHighFailure = false
// isSwingLowFailure = false
// var lastSwingHigh = float(na)
// var lastSwingLow = float(na)

// lastSwingHigh := isSwingHigh ? swingHigh : lastSwingHigh
// lastSwingLow := isSwingLow ? swingLow : lastSwingLow

// strengthOfHighSFP = 0
// if lastSwingHigh < high and lastSwingHigh > close and ta.barssince(lastSwingHigh) <= swingHistory
//     isSwingHighFailure := true
//     if plotFirstSFPOnly
//         lastSwingHigh := na
//     else
//         for i = 0 to plotStrongerSFPs by 1
//             if isSwingHighFailure[i]
//                 strengthOfHighSFP += 1  // it will be at least 1
//             else
//                 if high[i] > high
//                     strengthOfHighSFP -= 1

// strengthOfLowSFP = 0
// if lastSwingLow > low and lastSwingLow < close and ta.barssince(lastSwingLow) <= swingHistory
//     isSwingLowFailure := true
//     if plotFirstSFPOnly
//         lastSwingLow := na
//     else
//         for i = 0 to plotStrongerSFPs by 1
//             if isSwingHighFailure[i]
//                 strengthOfLowSFP += 1  // it will be at least 1
//             else
//                 if low[i] < low
//                     strengthOfLowSFP -= 1


// // Debugging
// plotchar(isSwingHighFailure, 'Swing High Failure', '', location.top)
// plotchar(isSwingLowFailure, 'Swing Low Failure', '', location.top)
// plotchar(lastSwingHigh, 'Last Swing High', '', location.top)
// plotchar(lastSwingLow, 'Last Swing Low', '', location.top)

// // Alerting
// alertcondition(condition=isSwingHighFailure, title='SFP: Swing High Failure', message='Swing High Failure')
// alertcondition(condition=isSwingLowFailure, title='SFP: Swing Low Failure', message='Swing Low Failure')
// alertcondition(condition=isSwingHighFailure or isSwingLowFailure, title='SFP: Any Swing Failure Pattern', message='Any Swing Failure Pattern')

// // Plotting
// // plotchar(series=plotSwings ? isSwingHigh : na, char='🔴', location=location.abovebar, size=size.tiny, offset=-swingHistory)
// // plotchar(series=plotSwings ? isSwingLow : na, char='🟢', location=location.belowbar, size=size.tiny, offset=-swingHistory)
// plotshape(series=plotSwings ? isSwingHigh : na, style= shape.circle, color= color.red, location=location.abovebar, size=size.tiny, offset=-swingHistory)
// plotshape(series=plotSwings ? isSwingLow : na, style= shape.circle, color= color.green, location=location.belowbar, size=size.tiny, offset=-swingHistory)

// //plot(swingHigh, style= plot.style_circles,linewidth = 5, offset=-swingHistory)

// // plotchar(series=isSwingHighFailure and strengthOfHighSFP <= 1, char='🍌', location=location.abovebar, size=size.tiny)
// // plotchar(series=isSwingHighFailure and strengthOfHighSFP >= 3, char='🍰', location=location.abovebar, size=size.tiny)
// // plotchar(series=isSwingLowFailure and strengthOfLowSFP <= 1, char='🍌', location=location.belowbar, size=size.tiny)
// // plotchar(series=isSwingLowFailure and strengthOfLowSFP >= 3, char='🍰', location=location.belowbar, size=size.tiny)


// // 🔥 Uncomment the lines below for the strategy and revert for the study
// // TP = strategy.position_size > 0 ? strategy.position_avg_price *  (1 + takeProfit) :
// //      strategy.position_size < 0 ? strategy.position_avg_price *  (1 - takeProfit) : na
// // SL = strategy.position_size > 0 ? strategy.position_avg_price *  (1 - stopLoss) :
// //      strategy.position_size < 0 ? strategy.position_avg_price *  (1 + stopLoss) : na

// // plot(series = (plotTP and strategy.position_size != 0) ? TP : na, style=plot.style_linebr, color=color.green, title="TP")
// // plot(series = (plotSL and strategy.position_size != 0) ? SL : na, style=plot.style_linebr, color=color.red, title="SL")
// // bgcolor(plotBgColor ? strategy.position_size > 0 ? color.new(color.green, 75) : strategy.position_size < 0 ? color.new(color.red,75) : color(na) : color(na))

// // strategy.risk.allow_entry_in(tradingDirection == "L&S" ? strategy.direction.all :
// //      tradingDirection == "L" ? strategy.direction.long :
// //      tradingDirection == "S" ? strategy.direction.short : na)
// // strategy.entry("SFP", strategy.long, comment="Long", when=isSwingLowFailure)
// // strategy.entry("SFP", strategy.short, comment="Short", when=isSwingHighFailure)
// // strategy.exit(id="TP-SL", from_entry="SFP", limit=TP, stop=SL)
// // if (not inDateRange)
// //     strategy.close_all()

