Leg/Base & GAP Candles
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © india TradeLegend By AnkurMaheshwari

//@version=4
study(title="Leg/Base & GAP Candles",overlay=true)
//Leg and Base Candle Input
var legThreshold  = input(title="Leg Candle Close-Open to Low-High Range Ratio Between 0 to 1", type=input.float, minval = 0.01, maxval = 0.99, defval=0.75)
var baseThreshold  = input(title="Base Candle Body to Range Ratio 0 to 1", type=input.float, minval = 0.01, maxval = 0.99, defval=0.5)
var allowLegCandleColorChange = input(title="Colour Change on Leg Candles",type=input.bool, defval = true)
var allowBaseCandleColorChange = input(title="Colour Change on Base Candles",type=input.bool, defval = false)
var labelAllow = input(title="Label Close Price? ",type=input.bool, defval = false)
var redrawCandle = input(title="Redraw all intraday candles?",type=input.bool, defval = false)

//MINUTE & HOUR LegCandle and Base Candle boolean based on previous values
//legCandle = iff(float((abs(close-iff(hour(time)==9 and minute(time)==15,close[1],open))/(high-low)))>=float(legThreshold) and timeframe.isminutes and allowLegCandleColorChange,1,0)
//baseCandle = iff(float((abs(close-iff(hour(time)==9 and minute(time)==15,close[1],open))/(high-low)))<=float(baseThreshold) and timeframe.isminutes and allowBaseCandleColorChange,1,0)
legCandle = iff(timeframe.isminutes, float((abs(close-iff(hour(time)==9 and minute(time)==15,close[1],open))/(high-low)))>=float(legThreshold) and allowLegCandleColorChange?1:0, float((abs(close-close[1])/(high-low)))>=float(legThreshold) and allowLegCandleColorChange?1:0)
baseCandle = iff(timeframe.isminutes, float((abs(close-iff(hour(time)==9 and minute(time)==15,close[1],open))/(high-low)))<=float(baseThreshold) and allowBaseCandleColorChange?1:0, float((abs(close-close[1])/(high-low)))<=float(baseThreshold) and allowBaseCandleColorChange?1:0)
firstCandle = iff(hour(time)==9 and minute(time)==15,1,0)
//Leg Candles and Base Candles Bar Colour changes
barcolor(legCandle and firstCandle==0 ? close >= iff(timeframe.isminutes,hour(time)==9 and minute(time)==15?close[1]:open,close[1]) ? color.yellow : color.fuchsia : na, title="Leg Candle Color Marking")
barcolor(baseCandle and firstCandle==0 ? close >= iff(timeframe.isminutes,hour(time)==9 and minute(time)==15?close[1]:open,close[1]) ? color.black : color.black : na, title="Base Candle Color Marking")



//barcolor(legCandle ? close >= iff(hour(time)==9 and minute(time)==15,close[1],open) ? color.yellow : color.fuchsia : na, title="Leg Candle Color Marking")
//barcolor(baseCandle ? close >= iff(hour(time)==9 and minute(time)==15,close[1],open) ? color.black : color.black : na, title="Base Candle Color Marking")
//Shaping on Leg candles     
plotshape(legCandle  ? close < iff(timeframe.isminutes,hour(time)==9 and minute(time)==15?close[1]:open,close[1]) ? 1:0:0, style=shape.arrowdown, color = color.purple, location = location.top)
plotshape(legCandle  ? close > iff(timeframe.isminutes,hour(time)==9 and minute(time)==15?close[1]:open,close[1]) ? 1:0:0, style=shape.arrowup, color = color.yellow, location = location.top)


//Minute Redraw Candle on the Basis of Previous Close

//Only NSE 9:15 candle redraw intraday
//plotcandle(iff(hour(time)==9 and minute(time)==15 and timeframe.isintraday,close[1],na), iff(hour(time)==9 and minute(time)==15 and timeframe.isintraday,high,na), iff(hour(time)==9 and minute(time)==15 and timeframe.isintraday,low,na), iff(hour(time)==9 and minute(time)==15 and timeframe.isintraday,close,na), color = iff(legCandle,close[1]<=close?color.yellow:color.fuchsia,close[1]<=close?color.teal:color.red), wickcolor = iff(legCandle,close[1]<=close?color.yellow:color.fuchsia,close[1]<=close?color.black:color.black), bordercolor = color.black, title="Minute/Hourly Redraw Candle")     

plotcandle(iff(timeframe.isintraday and redrawCandle,close[1],na), iff(timeframe.isintraday and redrawCandle,high,na), iff(timeframe.isintraday and redrawCandle,low,na), iff(timeframe.isintraday and redrawCandle,close,na), color = iff(legCandle,close[1]<=close?color.yellow:color.fuchsia,close[1]<=close?color.teal:color.red), wickcolor = iff(legCandle,close[1]<=close?color.yellow:color.fuchsia,close[1]<=close?color.black:color.black), bordercolor = color.black, title="Minute/Hourly Redraw Candle")     
if (barstate.islast and labelAllow and redrawCandle)
    l = label.new(bar_index, high, text=tostring(close))
    label.delete(l[1])
    if(close<open)
        label.set_y(l,low)
        label.set_style(l,label.style_label_up)

plotcandle(iff(hour(time)==9 and minute(time)==15 and timeframe.isintraday and not redrawCandle,close[1],na), iff(hour(time)==9 and minute(time)==15 and timeframe.isintraday and not redrawCandle,high,na), iff(hour(time)==9 and minute(time)==15 and timeframe.isintraday and not redrawCandle,low,na), iff(hour(time)==9 and minute(time)==15 and timeframe.isintraday and not redrawCandle,close,na), color = iff(legCandle,close[1]<=close?color.yellow:color.fuchsia,close[1]<=close?color.teal:color.red), wickcolor = iff(legCandle,close[1]<=close?color.yellow:color.fuchsia,close[1]<=close?color.black:color.black), bordercolor = color.black, title="Minute/Hourly Redraw Candle")     


//Daily Weekly Monthly Redraw Candles on the Basis of Previous Close
plotcandle(iff(timeframe.isdwm,close[1],na), iff(timeframe.isdwm,high,na), iff(timeframe.isdwm,low,na), iff(timeframe.isdwm,close,na), color = iff(legCandle,close[1]<=close?color.yellow:color.fuchsia,close[1]<=close?color.teal:color.red), wickcolor = iff(legCandle,close[1]<=close?color.yellow:color.fuchsia,close[1]<=close?color.teal:color.red), bordercolor = color.black,title="Daily, Weekly, Monthly Redraw Candle")     


//plotchar(baseCandle,text=tostring(float((high-low)/close)),char="x")
//if(float((abs(close-iff(hour(time)==9 and minute(time)==15,close[1],open))/(high-low)))<=float(0.5)?float((high-low)/close)<float(0.003)?1:1:0)
  //  label.new(bar_index, high, tostring(float((high-low)/close)), yloc = yloc.abovebar, color = color.red, textcolor = color.black)