Count how many crossovers within a session?


0
0

Say i do a MovingAvg10 and MovingAvg20 on a 1min chart.

How do i calculate how many MA cross-up and MA cross-down in the whole trading session (0930 – 1600hr) ?

Addlabel on the chart – Num of Cross-up and Num of Cross-down

Marked as spam
Posted by (Questions: 1, Answers: 0)
Asked on July 1, 2020 11:33 pm
99 views
0
Private answer

This code needs to be applied to an intraday chart without extended hour data included. Otherwise it will count all the crossovers from midnight to midnight.

Screenshot below shows the result.

input maLengthOne = 10;
input maTypeOne = AverageType.SIMPLE;
input maPriceOne = close;
input maLengthTwo = 20;
input maTypeTwo = AverageType.SIMPLE;
input maPriceTwo = close;
plot maOne = MovingAverage(maTypeOne, maPriceOne, maLengthOne);
plot maTwo = MovingAverage(maTypeTwo, maPriceTwo, maLengthTwo);
plot crossAbove = maOne[1] < maTwo[1] and maOne > maTwo;
crossAbove.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
plot crossBelow = maOne[1] > maTwo[1] and maOne < maTwo;
crossBelow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
def newDay = GetDay() <> GetDay()[1];
rec countCrossAbove = if newDay then 0 else if crossAbove then countCrossAbove[1] + 1 else countCrossAbove[1];
rec countCrossBelow = if newDay then 0 else if crossBelow then countCrossBelow[1] + 1 else countCrossBelow[1];
AddLabel(yes, Concat("X Above: ", countCrossAbove), Color.GREEN);
AddLabel(yes, Concat("X Below: ", countCrossBelow), Color.RED);

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on July 2, 2020 10:53 am