RTR Jimmy Momo Candles
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © InarTrades

//@version=4
study("RTR Jimmy Momo Candles",overlay=true)

//These zones are absolutely not "buy here sell here" areas. Context is key as with all levels,indicators, etc. 

//Inputs
bodyAverageRange = input(6, title="Average Body Range")
volumeAverageRange = input(60, title="Volume Average Range")
changeBarColor = input(true, title="Change Bar Color",type=input.bool)
bodyStdDevs = input(2,title="Body Standard Deviations")
hotThreshold = input(2.0,title="Hot Volume Threshold")
showZones = input(true,title="Show Zones",type=input.bool)
showDots = input(true,title="Show Dots",type=input.bool)
zoneLookback = input(60,title="Bar Lookback for Zones")
l=low
h=high
v=volume

volumeAvg= sma(v,volumeAverageRange)
bt = max(close,open)
bb = min(close,open)
gapDown = bb[1] > h ? bb[1]-bt : 0
gapUp = bt[1] < l ? bb-bt[1] : 0

bodyTop = if gapUp > 0
    bt
else if gapDown > 0
    bb[1]
else
    bt
    
bodyBottom = if gapUp > 0
    bt[1]
else if gapDown > 0 
    bb
else
    bb
    
c = if gapUp > 0
    bodyTop
else if gapDown > 0
    bodyBottom
else
    close

o = bodyTop[1] < c ? min(bodyTop[1],open) : open

body = bodyTop - bodyBottom
bn = bar_index

isG = c > o
isR = c < o 

bodySd = stdev(body, bodyAverageRange)

//hot bar logic
isHot = body > bodySd[1] * bodyStdDevs and v > volumeAvg * hotThreshold

//should show
shouldShow = if isG and isHot
    true
else if isR and isHot
    true
else
    false

//dot value logic
dotVal = if shouldShow and isG
    bodyBottom
else if shouldShow and isR
    bodyTop
    
//dot color logic
dotColor = if shouldShow and isG and showDots
    color.new(color.white,0)
else if shouldShow and isR  and showDots
    color.new(color.yellow,0)
else
    color.new(color.white,100)

    

//wick value logic
wickVal = if shouldShow and isG
    bodyTop[1] >= c ? l : min(l[0],l[1])
else if shouldShow and isR 
    bodyBottom[1] <= o ? h : max(h[0],h[1])

boxTopVal = if isG
    dotVal
else if isR
    wickVal

boxBotVal = if isG
    wickVal
else if isR
    dotVal

boxColor = if shouldShow and close > boxTopVal and showZones
    color.new(color.green,50)
else if shouldShow and close < boxBotVal and showZones
    color.new(color.red,50)
else
    color.new(color.gray,100)



//zoneBox=box.new(left=bar_index,top=boxTopVal,right=bar_index+1,bottom=boxBotVal,extend=extend.right,bgcolor=boxColor,border_color=boxColor)
zoneBox = if shouldShow == true
    box.new(left=bar_index,top=boxTopVal,right=bar_index+1,bottom=boxBotVal,extend=extend.right,bgcolor=boxColor,border_color=boxColor)

box.delete(zoneBox[zoneLookback])
barcol = if changeBarColor and shouldShow and isG
    color.lime
else if changeBarColor and shouldShow and isR
    color.orange
else if isG
    color.teal
else if isR
    color.red 
//box.delete(zoneBox[1])
//plot dots
dot = plot(dotVal,style=plot.style_circles,linewidth=3,color=dotColor)

//plot wicks
wick = plot(wickVal,style=plot.style_circles,linewidth=3,color=dotColor)
//highlight important bars
barcolor(barcol)



