Show number of candles since moving average cross in watchlist


Category:
0
0

Hello Pete I have a watch list that shows the color change when the 8 ema crosses the 34 ema but I would like it to show the number of candles showing when it happened.

Thank you

Dan

Marked as spam
Posted by (Questions: 1, Answers: 0)
Asked on March 2, 2020 11:42 am
58 views
0
Private answer

We are going to borrow some code from a previous post that counts the number of bars since the close crossed above or below the Hull Moving average. Link to that previous post is here:

https://www.hahn-tech.com/ans/display-bars-since-cross-above-hma/

Here is the code from that post, modified to apply two moving averages crossing:

input price = close;
input lengthOne = 8;
input lengthTwo = 34;
input typeOne = AverageType.EXPONENTIAL;
input typeTwo = AverageType.EXPONENTIAL;
def maOne = MovingAverage(typeOne, price, lengthOne);
def maTwo = MovingAverage(typeTwo, price, lengthTwo);
def crossAbove = maOne[1] < maTwo[1] and maOne > maTwo;
def crossBelow = maOne[1] > maTwo[1] and maOne < maTwo; rec countAbove = if crossAbove then 1 else if maOne > maTwo then countAbove[1] + 1 else 0;
rec countBelow = if crossBelow then -1 else if maOne < maTwo then countBelow[1] - 1 else 0; plot data = if countAbove > AbsValue(countBelow) then countAbove else countBelow;
data.AssignValueColor(if data == 0 then Color.CURRENT else Color.BLACK);
AssignBackgroundColor(if data > 0 then Color.GREEN else if data < 0 then Color.RED else Color.CURRENT);

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on March 2, 2020 4:15 pm