Makuchaku's Trade Tools - Pivots/Fractals & Crossovers
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © makuchaku

//@version=4
study("Makuchaku's Trade Tools - Pivots & Crossovers", overlay=true)
pivotLookup  = input(title="pivotLookup", type=input.integer, defval=1, group="Pivots")
boxLength  = input(title="boxLength", type=input.integer, defval=3, group="General", inline="General")
boxTransparency  = input(title="boxTransparency", type=input.integer, defval=95, group="General", inline="General")


isUp(index) =>
    close[index] > open[index]
    
isDown(index) =>
    close[index] < open[index]


isObUp(index) =>
    isDown(index+1) and isUp(index) and (close[index] > high[index+1])
    

isObDown(index) =>
    isUp(index+1) and isDown(index) and (close[index] < low[index+1])



//////////////////// Pivots //////////////////// 
plotPivots = true
hih = pivothigh(high, pivotLookup, pivotLookup)
lol = pivotlow (low , pivotLookup, pivotLookup)
top = valuewhen(hih, high[pivotLookup], 0)
bottom = valuewhen(lol, low [pivotLookup], 0)
plot(top, offset=-pivotLookup, linewidth=2, color=(top != top[1] ? na : (plotPivots ? color.silver : na)), title="pivotTop")
plot(bottom, offset=-pivotLookup, linewidth=2, color=(bottom != bottom[1] ? na : (plotPivots ? color.silver : na)), title="pivotBottom")

if crossover(close, top)
    box.new(left=bar_index, top=top, right=bar_index+boxLength, bottom=bottom, bgcolor=color.new(color.green, boxTransparency), border_color=color.new(color.green, 80))
    
if crossunder(close, bottom)
    box.new(left=bar_index, top=top, right=bar_index+boxLength, bottom=bottom, bgcolor=color.new(color.red, boxTransparency), border_color=color.new(color.red, 80))
//////////////////// Pivots //////////////////// 