Assumed 6 days ago and 12 days ago there were MACD crossover. How can I get or determine their barIndex, so I can get the value of the MACD Line?
Use BarsSince (https://www.prorealcode.com/documentation/barssince/):
CrossOver = MACD[12,26,9](close) crosses over 0
CossingBar = BarsSince(CrossOver)
CossingBar contains the bar ID where the crossover occurred.
robertogozzi wrote: Use BarsSince (https://www.prorealcode.com/documentation/barssince/): CrossOver = MACD[12,26,9](close) crosses over 0 CossingBar = BarsSince(CrossOver) Copy CossingBar contains the bar ID where the crossover occurred. thomas2004ch thanked this post
Many thanks!
I simply use the example code here: https://www.prorealcode.com/documentation/barssince/
But how can I understand the values? Here is a screenshot. The “previous occurrence” is 5 and the “last occurrence” is 0. By the second screenshot, the “previous occurrence” is 8 and the “last occurrence” is still 0
I can’t figure out why your data seem to be wrong.
Mine are correct as you can see from my attached picture.
I remind you that 0 is the current bar, 1 is the first bar preceding the current bar, …. 7 is the seventh bar prior to the current one. So 0 means the crossover occurred on the CURRENT bar.
robertogozzi wrote: I can’t figure out why your data seem to be wrong. Mine are correct as you can see from my attached picture. I remind you that 0 is the current bar, 1 is the first bar preceding the current bar, …. 7 is the seventh bar prior to the current one. So 0 means the crossover occurred on the CURRENT bar.
Ok, now I got it. Many thanks!
Now I want print the value on the cross point. But I got error.
here is my code.
MyMACDLine = MACDline[12,26,9](close)
MyMACDSignal = MACDSignal[12,26,9](close)
MACDCrossOverSignal = MyMACDLine crosses over MyMACDSignal
testMACDCrossOverSignalFirst = BarsSince(MACDCrossOverSignal)
div1 = MyMACDLine[testMACDCrossOverSignalFirst]
//return testMACDCrossOverSignalFirst COLOURED(255,0,0) as "first occurrence"
return div1
Try this version to avoid the negative integer error:
MyMACDLine = MACDline[12,26,9](close)
MyMACDSignal = MACDSignal[12,26,9](close)
MACDCrossOverSignal = MyMACDLine crosses over MyMACDSignal
testMACDCrossOverSignalFirst = max(0,BarsSince(MACDCrossOverSignal))
div1 = MyMACDLine[testMACDCrossOverSignalFirst]
//return testMACDCrossOverSignalFirst COLOURED(255,0,0) as "first occurrence"
return div1
BarsSince: If the condition has never been true, the function returns -1.