Moving average is X percent away or greater from another Moving average


Category:
0
0

Hi Pete,

I really find these Q&A very helpful building my custom studies and indicator, specially the condition wizard tutorials. and I want to say thank you. But there is one thing that I could not find as a feature in condition wizard or in these forums is to detect a true condition if  Moving average is  X percent away or greater from another Moving Average. I am currently building a custom alert and using only “cross above” condition sometime gives me false signal. Thank you.

Marked as spam
Posted by (Questions: 2, Answers: 1)
Asked on November 8, 2019 2:12 pm
1201 views
0
Yes, it is possible. However unless you provide specific details or an example we cannot even begin to solve this. Details.
( at November 8, 2019 2:23 pm)
0
Lets say, 8 Exponential Mov Avg is at least 3% above 15 Exponential Mov Avg. Thanks
( at November 8, 2019 7:31 pm)
0
Private answer

The author of the post provided some details so we can now provide a specific solution. I have a request from another post for just the opposite of this. So I took the time to build a solution that would be flexible enough to work for the entire audience.

This covers all four potential conditions:

  1. ma one above ma two while less than x percent away
  2. ma one above ma two while more than x percent away
  3. ma one below ma two while less than x percent away
  4. ma one below ma two while more than x percent away

The two ma's are plotted on the chart study. Dots are plotted for every bar on the chart to show the current relationship between the two moving averages. From the bullet point list above, the dots are colored as follows:

  1. dark green
  2. light green
  3. dark red
  4. light red

You can turn plots off as needed and change any color through the edit studies window. You can also adjust the length, type and price for each moving average. Here is the code and the screenshot below shows the result:

input lengthOne = 8;
input lengthTwo = 15;
input maTypeOne = AverageType.EXPONENTIAL;
input maTypeTwo = AverageType.EXPONENTIAL;
input maPriceOne = close;
input maPriceTwo = close;
input percentDiff = 3.0;
plot maOne = MovingAverage(maTypeOne, maPriceOne, lengthOne);
maOne.SetDefaultColor(Color.WHITE);
plot maTwo = MovingAverage(maTypeTwo, maPriceTwo, lengthTwo);
maTwo.SetDefaultColor(Color.YELLOW);
plot maAboveMoreThanPercent = if maOne > maTwo * (1 + percentDiff * 0.01) then maOne else Double.NaN;
maAboveMoreThanPercent.SetPaintingStrategy(PaintingStrategy.POINTS);
maAboveMoreThanPercent.SetLineWeight(3);
maAboveMoreThanPercent.SetDefaultColor(Color.LIGHT_GREEN);
plot maAboveLessThanPercent = if maOne > maTwo and maOne < maTwo * (1 + percentDiff * 0.01) then maOne else Double.NaN;
maAboveLessThanPercent.SetPaintingStrategy(PaintingStrategy.POINTS);
maAboveLessThanPercent.SetLineWeight(3);
maAboveLessThanPercent.SetDefaultColor(Color.DARK_GREEN);
plot maBelowMoreThanPercent = if maOne < maTwo * (1 - percentDiff * 0.01) then maOne else Double.NaN;
maBelowMoreThanPercent.SetPaintingStrategy(PaintingStrategy.POINTS);
maBelowMoreThanPercent.SetLineWeight(3);
maBelowMoreThanPercent.SetDefaultColor(Color.RED);
plot maBelowLessThanPercent = if maOne < maTwo and maOne > maTwo * (1 - percentDiff * 0.01) then maOne else Double.NaN;
maBelowLessThanPercent.SetPaintingStrategy(PaintingStrategy.POINTS);
maBelowLessThanPercent.SetLineWeight(3);
maBelowLessThanPercent.SetDefaultColor(Color.DARK_RED);

I understand that some might like to run this as a scan. So I'll also include the code you can use to scan for these conditions:

input lengthOne = 8;
input lengthTwo = 15;
input maTypeOne = AverageType.EXPONENTIAL;
input maTypeTwo = AverageType.EXPONENTIAL;
input maPriceOne = close;
input maPriceTwo = close;
input percentDiff = 3.0;
def maOne = MovingAverage(maTypeOne, maPriceOne, lengthOne);
def maTwo = MovingAverage(maTypeTwo, maPriceTwo, lengthTwo);
# scan for ma one above ma two while less than x percent away
plot maAboveMoreThanPercent = maOne > maTwo * (1 + percentDiff * 0.01);
# scan for ma one above ma two while more than x percent away
#plot maAboveLessThanPercent = maOne > maTwo and maOne < maTwo * (1 + percentDiff * 0.01);
# scan for ma one below ma two while less than x percent away
#plot maBelowMoreThanPercent = maOne < maTwo * (1 - percentDiff * 0.01);
# scan for ma one below ma two while more than x percent away
#plot maBelowLessThanPercent = maOne < maTwo and maOne > maTwo * (1 - percentDiff * 0.01);

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on November 12, 2019 2:55 pm