Custom Moving Average


Category:
0
0

Hello Pete,
I have a script that where I want to add the Triangular Moving Average. The thinkorswim name is MovAvgTriangular. I believe the abbreviation may be AvgTri. With this script I have, how would I add the triangular MA ….or even replace MA1 with AvgTri ? I would want the colors to change based on the direction of the Triangular MA.  Thanks

plot Data = close;declare upper;

input price1 = close;
input length1 = 10;
input displace1 = 0;
input averageType1 = AverageType.SIMPLE;

plot MA1 = MovingAverage(averageType1, price1[-displace1], length1);
MA1.SetDefaultColor(GetColor(4));
MA1.SetLineWeight(2);
MA1.SetStyle(Curve.FIRM);
MA1.DefineColor(“Up”, GetColor(6));
MA1.DefineColor(“Down”, GetColor(5));
MA1.AssignValueColor(if MA1 > MA1[1] then MA1.color(“Up” ) else MA1.color(“Down” ));

 

Marked as spam
Posted by (Questions: 11, Answers: 10)
Asked on March 24, 2018 12:42 pm
201 views
0
Private answer

All we need to keep from the original code are the style statements:

MA1.SetDefaultColor(GetColor(4));
MA1.SetLineWeight(2);
MA1.SetStyle(Curve.FIRM);
MA1.DefineColor(“Up”, GetColor(6));
MA1.DefineColor(“Down”, GetColor(5));
MA1.AssignValueColor(if MA1 > MA1[1] then MA1.color(“Up” ) else MA1.color(“Down” ));

Then we create a new plot based on the MovAvgTriangular study:

input price = close;
input length = 9;
input displace = 0;
plot avgTriangular = MovAvgTriangular(price, length, displace).AvgTri;

All we need to complete this is to replace every occurrence of MA1 with the new plot, avgTriangular

Here is the entire code, completed:

input price = close;
input length = 9;
input displace = 0;
plot avgTriangular = MovAvgTriangular(price, length, displace).AvgTri;
avgTriangular.SetDefaultColor(GetColor(4));
avgTriangular.SetLineWeight(2);
avgTriangular.SetStyle(Curve.FIRM);
avgTriangular.DefineColor(“Up”, GetColor(6));
avgTriangular.DefineColor(“Down”, GetColor(5));
avgTriangular.AssignValueColor(if avgTriangular > avgTriangular[1] then avgTriangular.color(“Up” ) else avgTriangular.color(“Down” ));

Attached screenshot shows the end result.

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on March 24, 2018 2:12 pm
0

Excellent work Pete. I was trying to add reversal alerts to it for when the MA line color changes from up to down & down to up. I was trying to come up with one. How could I add this feature? Will something like this work?
def bullReversal = upTrend and downTrend[1] == yes;
Alert(bullReversal, “Bullish Trend”, Alert.BAR, Sound.RING);
def bearReversal = downTrend and upTrend[1] == yes;
Alert(bearReversal, “Bearish Trend”, Alert.BAR, Sound.RING);

( at March 25, 2018 10:11 am)
0

Where have you defined the upTrend and downTrend variables? Need to define those or you will get errors. And you don’t need the “== yes” at the end. They should already evaluate to a true/false condition.

( at March 25, 2018 10:26 am)
0

Not quite sure…just messing around trying to figure it out with my limited coding skillz. If you figure it out I’ll be glad to make a contribution. thx

( at March 25, 2018 11:32 am)
0

“….if you figure it out…”. Well that is already given. I have already demonstrated this in dozens of posts on this Q&A forum. So yes, I have already figured it out. The question is, did you want to take some time to work it out? Hint: You may want to search in the “Alerts and Notifications” topics for examples….

I don’t do this for contributions. I do this to serve the audience. I monetize the site with adds. But voluntary contributions are always appreciated.

( at March 25, 2018 11:57 am)