Horizontal line Moving Average


Category:
0
0

Pete, Would you help me to write the code on TOS chart. “straight line moving average”. Iwould like have a horizontal line at the top of the chart that changes color to match the changing color of the DoubleMA.

Thank you

Chung

Edited by Moderator 1/27/17 19:30 MT

declare upper;
input displace = 0;
input MA1_length = 8;
input MA2_length = 21;
input price = close;
DefineGlobalColor("RisingMA", Color.Blue);
DefineGlobalColor("FallingMA", Color.Light_RED);
input movingAverageType1 = {default Simple, Exponential, Weighted, Hull, Variable};
input movingAverageType2 = {default Variable, Simple, Exponential, Weighted, Hull};
def data1;
switch (movingAverageType1) {
case Simple:
data1 = CompoundValue(1, Average(price[-displace], MA1_length), price);
case Exponential:
data1 = CompoundValue(1, ExpAverage(price[-displace], MA1_length), price);
case Weighted:
data1 = CompoundValue(1, WMA(price[-displace], MA1_length), price);
case Hull:
data1 = CompoundValue(1, HullMovingAvg(price[-displace], MA1_length), price);
case Variable:
data1 = CompoundValue(1, VariableMA(price = price, length = MA1_length), price);
}
plot DoubleMA;
switch (movingAverageType2) {
case Simple:
DoubleMA = CompoundValue(1, Average(data1[-displace], MA2_length), data1);
case Exponential:
DoubleMA = CompoundValue(1, ExpAverage(data1[-displace], MA2_length), data1);
case Weighted:
DoubleMA = CompoundValue(1, WMA(data1[-displace], MA2_length), data1);
case Hull:
DoubleMA = CompoundValue(1, HullMovingAvg(data1[-displace], MA2_length), data1);
case Variable:
DoubleMA = CompoundValue(1, VariableMA( data1, MA2_length), data1);
}
DoubleMA.SetLineWeight(4);
DoubleMA.AssignValueColor(if DoubleMA > DoubleMA[1] then GlobalColor("RisingMA") else GlobalColor("FallingMA"));
DoubleMA.HideBubble();

RESOLVED
Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on January 27, 2017 2:36 pm
1655 views
0

I edited your post to include the code you originally sent to my email. I wanted to make sure the post contained the complete context of what you are trying to accomplish. I will provide a response to this question tomorrow.

( at January 27, 2017 7:38 pm)
0
Private answer

We can do this, but there is a catch. We can plot the line at the top or the bottom of the chart. But only if the chart is zoomed out so that every single bar is visible. As soon as you start to zoom in, you will loose sight of at least one of the lines, most likely both.

These are the statements you add to your code to plot the lines as shown on the attached:

#------------- get the chart's range based of viewable candles
plot chartRangeHigh = HighestAll(high);
chartRangeHigh.AssignValueColor(if DoubleMA > DoubleMA[1] then GlobalColor("RisingMA") else GlobalColor("FallingMA"));
plot chartRangeLow = LowestAll(low);
chartRangeLow.AssignValueColor(if DoubleMA > DoubleMA[1] then GlobalColor("RisingMA") else GlobalColor("FallingMA"));

A more stable solution would be to plot this horizontal line as a lower study. And to save space further, you could add it to a modified version of a lower study you already use. For instance you could add it as the Zero line of a momentum oscillator. Or you could add it as the 0% or 100% line of the RSI or Stochastic.

Here is you code, modified to plot the color coded horizontal line in a lower subgraph. You can add this to the code of a lower study you are already using, and adjust the value of plot chartRangeHigh = 0; to whatever level you want it to plot.

declare lower;
input displace = 0;
input MA1_length = 8;
input MA2_length = 21;
input price = close;
DefineGlobalColor("RisingMA", Color.Blue);
DefineGlobalColor("FallingMA", Color.Light_RED);
input movingAverageType1 = {default Simple, Exponential, Weighted, Hull, Variable};
input movingAverageType2 = {default Variable, Simple, Exponential, Weighted, Hull};
def data1;
switch (movingAverageType1) {
case Simple: data1 = CompoundValue(1, Average(price[-displace], MA1_length), price);
case Exponential: data1 = CompoundValue(1, ExpAverage(price[-displace], MA1_length), price);
case Weighted: data1 = CompoundValue(1, WMA(price[-displace], MA1_length), price);
case Hull: data1 = CompoundValue(1, HullMovingAvg(price[-displace], MA1_length), price);
case Variable: data1 = CompoundValue(1, VariableMA(price = price, length = MA1_length), price);
}
def DoubleMA;
switch (movingAverageType2) { case Simple: DoubleMA = CompoundValue(1, Average(data1[-displace], MA2_length), data1);
case Exponential: DoubleMA = CompoundValue(1, ExpAverage(data1[-displace], MA2_length), data1);
case Weighted: DoubleMA = CompoundValue(1, WMA(data1[-displace], MA2_length), data1);
case Hull: DoubleMA = CompoundValue(1, HullMovingAvg(data1[-displace], MA2_length), data1);
case Variable: DoubleMA = CompoundValue(1, VariableMA( data1, MA2_length), data1);
}
#------------- get the chart's range based of viewable candles
plot chartRangeHigh = 0;
chartRangeHigh.AssignValueColor(if DoubleMA > DoubleMA[1] then GlobalColor("RisingMA") else GlobalColor("FallingMA"));

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on January 28, 2017 2:23 pm
0

Pete Hahn, how can I make it so that my 28 EMA is blue when it is going up and red when it is going down like in the picture above?

( at February 19, 2017 8:16 am)
0

Use the AssignValueColor() style statement to the plot. Details here: http://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AssignValueColor.html
So you have some plot on the chart:
plot myEMA = ExpAverage(close, 28);
then you style it:
myEMA.AssignValueColor(if myEMA[1] < myEMA then Color.BLUE else Color.RED);

( at February 19, 2017 9:43 am)