hello can someone help me plotshape whenever a MACD high is broken like this?
this is my code
//@version=5
indicator("break high", overlay = true)
// === MACD ===
fastMA = input.int(title="Fast moving average", defval = 12, minval = 7)
slowMA = input.int(title="Slow moving average", defval = 26, minval = 7)
signalLength = input.int(9, minval=1)
MacdControl = input(true, title="MACD/Histogram Control")
[currMacd,,] = ta.macd(close[0], fastMA, slowMA, signalLength)
[prevMacd,,] = ta.macd(close[1], fastMA, slowMA, signalLength)
signal = ta.ema(currMacd, signalLength)
[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9) //Zero Line
crossoverbull = ta.crossover(macdLine, signalLine)
crossoverbear = ta.crossover(signalLine, macdLine)
var can_find = false
condstart = crossoverbull
condstop = crossoverbear
can_find := condstop ? false : condstart ? true : can_find
var float highesthigh = na
highesthigh := if (condstart)
high
else if (can_find)
if (high > highesthigh)
high
else
highesthigh
else
na
plot(highesthigh, color=color.blue, style=plot.style_linebr)
I dont know hot to store the revious high to be referenced when a cande breaks it.
You can leverage few functions to achieve this.
First, let's declare a variable that will store previous highesthigh
and use the var
keyword so that it's value won't reset between bars/executions:
var float lastHighestHigh = na
Second, we'll want to reset this variable each time we get into new highesthigh
calculations so we'll need a bool
variable to check for this condition. We can simply check if current bar is not na
and if previous bar had na
value for highesthigh
:
firstBarAfterHH = not na(highesthigh) and na(highesthigh[1])
Third, we'll check for the value of highesthigh
last time it wasn't na
using the ta.valuewhen()
function. We'll have to call it on each bar for consistency:
valueOfLastHighestHigh = ta.valuewhen(not na(highesthigh), highesthigh, 1)
Fourth, we'll set the value of our lastHighestHigh
variable so it will get the value of valueOfLastHighestHigh
each time we firstBarAfterHH
is true
:
if firstBarAfterHH
lastHighestHigh := valueOfLastHighestHigh
At this point, we have 2 variables we can work with:
highesthigh
- which will store the current bar highest high. lastHighestHigh
- which will store our previous highesthigh
. So we can compare them using ta.crossover()
function to get the first bar when one value is over the other. For example:
plotshape(ta.crossover(highesthigh, lastHighestHigh))
Full code:
var float lastHighestHigh = na
firstBarAfterHH = not na(highesthigh) and na(highesthigh[1])
valueOfLastHighestHigh = ta.valuewhen(not na(highesthigh), highesthigh, 1)
if firstBarAfterHH
lastHighestHigh := valueOfLastHighestHigh
plotshape(ta.crossover(highesthigh, lastHighestHigh))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.