I am new to pine coding and trying to combine the (high or highest high) or (low or lowest low) with MACD histogram bars. I am looking to find the highest high when the histogram is up and should find the high by last green bar of the histogram or should find low at teh last red bar of the histogram.Is it possible to achieve.Any help is much appreciated.
lb = input.int(21, title='Left Bars', minval=0)
rb = input.int(0, title='Right Bars', minval=0)
ph1 = ta.pivothigh(lb, rb)
pl1 = ta.pivotlow(lb, rb)
iff_1 = pl1 ? -1 : na // Trend direction
hl = ph1 ? 1 : iff_1
iff_2 = pl1 ? pl1 : na
zz = ph1 ? ph1 : iff_2
valuewhen_1 = ta.valuewhen(hl, hl, 1)
valuewhen_2 = ta.valuewhen(zz, zz, 1)
zz := pl1 and hl == -1 and valuewhen_1 == -1 and pl1 > valuewhen_2 ? na : zz
valuewhen_3 = ta.valuewhen(hl, hl, 1)
valuewhen_4 = ta.valuewhen(zz, zz, 1)
zz := ph1 and hl == 1 and valuewhen_3 == 1 and ph1 < valuewhen_4 ? na : zz
valuewhen_5 = ta.valuewhen(hl, hl, 1)
valuewhen_6 = ta.valuewhen(zz, zz, 1)
hl := hl == -1 and valuewhen_5 == 1 and zz > valuewhen_6 ? na : hl
valuewhen_7 = ta.valuewhen(hl, hl, 1)
valuewhen_8 = ta.valuewhen(zz, zz, 1)
hl := hl == 1 and valuewhen_7 == -1 and zz < valuewhen_8 ? na : hl
zz := na(hl) ? na : zz
findprevious() => // finds previous three points (b, c, d, e)
ehl = hl == 1 ? -1 : 1
loc1 = 0.0
loc2 = 0.0
loc3 = 0.0
loc4 = 0.0
xx = 0
for x = 1 to 1000 by 1
if hl[x] == ehl and not na(zz[x])
loc1 := zz[x]
xx := x + 1
break
ehl := hl
for x = xx to 1000 by 1
if hl[x] == ehl and not na(zz[x])
loc2 := zz[x]
xx := x + 1
break
ehl := hl == 1 ? -1 : 1
for x = xx to 1000 by 1
if hl[x] == ehl and not na(zz[x])
loc3 := zz[x]
xx := x + 1
break
ehl := hl
for x = xx to 1000 by 1
if hl[x] == ehl and not na(zz[x])
loc4 := zz[x]
break
[loc1, loc2, loc3, loc4]
float a = na
float b = na
float c = na
float d = na
float e = na
if not na(hl)
[loc1, loc2, loc3, loc4] = findprevious()
a := zz
b := loc1
c := loc2
d := loc3
e := loc4
fast_length = input.int(title="Fast Length", defval=24)
slow_length = input.int(title="Slow Length", defval=52)
signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 999, defval = 18)
sma_source = input.string(title="Oscillator MA Type", defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])
// Alerts T/F Inputs
alert_Long_A = input.bool(false, title = "MACD Cross Up & > 0", group = "Alerts", inline="Alert20")
alert_Short_B = input.bool(false, title = "MACD Cross Dn & < 0", group = "Alerts", inline="Alert20")
// Calculating
fast_ma = request.security(syminfo.tickerid, res, sma_source == "EMA" ? ta.ema(src, fast_length) : ta.ema(src, fast_length))
slow_ma = request.security(syminfo.tickerid, res, sma_source == "EMA" ? ta.ema(src, slow_length) : ta.ema(src, slow_length))
macd = fast_ma - slow_ma
signal = request.security(syminfo.tickerid, res, sma_signal == "EMA" ? ta.ema(macd, signal_length) : ta.ema(macd, signal_length))
hist = macd - signal
// Determine if the current histogram bar is up or down
histUp = hist > 0
histDown = hist < 0
// MACD Trend and Cross Up/Down conditions
trend_up = macd > signal
trend_dn = macd < signal
cross_UP = signal[1] >= macd[1] and signal < macd
cross_DN = signal[1] <= macd[1] and signal > macd
cross_UP_A = (signal[1] >= macd[1] and signal < macd) and macd > 0
cross_DN_B = (signal[1] <= macd[1] and signal > macd) and macd < 0
// Condition that changes Color of MACD Line if Show Trend is turned on..
//Var Statements for Histogram Color Change
var bool histA_IsUp = false
var bool histA_IsDown = false
var bool histB_IsDown = false
var bool histB_IsUp = false
histA_IsUp := hist == hist[1] ? histA_IsUp[1] : hist > hist[1] and hist > 0
histA_IsDown := hist == hist[1] ? histA_IsDown[1] : hist < hist[1] and hist > 0
histB_IsDown := hist == hist[1] ? histB_IsDown[1] : hist < hist[1] and hist <= 0
histB_IsUp := hist == hist[1] ? histB_IsUp[1] : hist > hist[1] and hist <= 0
// // Plot Statements
// Determine if the histogram bar color has changed from the previous bar
colorChangeUp = hist > hist[1] and hist[1] <= hist[2]
colorChangeDown = hist < hist[1] and hist[1] >= hist[2]
// Logic to find the last histogram bar that is up before color change
var float lastHistUp = na
if histUp and not colorChangeUp
lastHistUp := hist
else
lastHistUp := na
// Logic to find the last histogram bar that is down before color change
var float lastHistDown = na
if histDown and not colorChangeDown
lastHistDown := hist
else
lastHistDown := na
////////////////////////////////////////////////////////////
firstHistADown = hist < hist[1] and hist[1] > hist[2]
firstHistBUp = hist > hist[1] and hist[1] < hist[2]
lasthistAup = hist > hist[1] and hist[1] < hist[2]
lastHistBDown = hist < hist[1] and hist[1] > hist[2]
_hh = (zz and a > b and a > c and c > b and c > d ) and lasthistAup
_ll = (zz and a < b and a < c and c < b and c < d ) and lasthistBDown
plotshape (_hh , text='HH', title='Higher High', style=shape.labeldown, color=color.new(color.lime, 0), textcolor=color.new(color.black, 0), location=location.abovebar)
plotshape(_ll , text='LL', title='Lower Low', style=shape.labelup, color=color.new(color.red, 0), textcolor=color.new(color.white, 0), location=location.belowbar)