MTF/SMA

woulf1004

Active member
VIP
Can someone help code this script so it can plot as ShowonExpansion with labels that show price and also changes colors to green when price is above the SMA50 and red when price is below SMA50?

plot SMAD50 = SimpleMovingAvg (close (period = AggregationPeriod.Day), 50) ;
plot SMA4H50 = SimpleMovingAvg (close (period = AggregationPeriod.Four_Hours), 50) ;
plot SMA1H50 = SimpleMovingAvg (close (period = AggregationPeriod.Hour), 50) ;
 
Last edited:
Solution
Can someone help code this script so it can plot as ShowonExpansion with labels that show price and also changes colors to green when price is above the SMA50 and red when price is below SMA50?

plot SMAD50 = SimpleMovingAvg (close (period = AggregationPeriod.Day), 50) ;
plot SMA4H50 = SimpleMovingAvg (close (period = AggregationPeriod.Four_Hours), 50) ;
plot SMA1H50 = SimpleMovingAvg (close (period = AggregationPeriod.Hour), 50) ;

Try this

Capture.jpg
Ruby:
input showonexpansion = yes;
def SMA50D   = if IsNaN(close)
               then SMA50D[1]
               else SimpleMovingAvg (close (period = AggregationPeriod.DAY), 50) ;
plot SMAD50  = if showonexpansion and !IsNaN(close)
               then Double.NaN...
Can someone help code this script so it can plot as ShowonExpansion with labels that show price and also changes colors to green when price is above the SMA50 and red when price is below SMA50?

plot SMAD50 = SimpleMovingAvg (close (period = AggregationPeriod.Day), 50) ;
plot SMA4H50 = SimpleMovingAvg (close (period = AggregationPeriod.Four_Hours), 50) ;
plot SMA1H50 = SimpleMovingAvg (close (period = AggregationPeriod.Hour), 50) ;

Try this

Capture.jpg
Ruby:
input showonexpansion = yes;
def SMA50D   = if IsNaN(close)
               then SMA50D[1]
               else SimpleMovingAvg (close (period = AggregationPeriod.DAY), 50) ;
plot SMAD50  = if showonexpansion and !IsNaN(close)
               then Double.NaN
               else SMA50D ;

def SMA504H  = if IsNaN(close)
               then SMA504H[1]
               else SimpleMovingAvg (close (period = AggregationPeriod.FOUR_HOURS), 50) ;
plot SMA4H50 = if showonexpansion and !IsNaN(close)
               then Double.NaN
               else SMA504H ;

def SMA501H   = if IsNaN(close)
               then SMA501H[1]
               else SimpleMovingAvg (close (period = AggregationPeriod.HOUR), 50) ;
plot SMA1H50 = if showonexpansion and !IsNaN(close)
               then Double.NaN
               else SMA501H ;

input bubbles     = yes;
input bubblemover = 5;
def   bm          = bubblemover;
def   bm1         = bm + 1;
AddChartBubble(bubbles and !IsNaN(close[bm1]) and IsNaN(close[bm]),
               SMAD50[bm], "SMA50 D",
               SMAD50.TakeValueColor());
AddChartBubble(bubbles and !IsNaN(close[bm1]) and IsNaN(close[bm]),
               SMA4H50[bm], "SMA50 4H",
               SMA4H50.TakeValueColor());
AddChartBubble(bubbles and !IsNaN(close[bm1]) and IsNaN(close[bm]),
               SMA1H50[bm], "SMA50 1H",
               SMA1H50.TakeValueColor());
 
Solution

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Thanks for the support as usual SleepyZ. Can you also make the labels that can show price and also changes colors to green when price is above the SMA50 or red when price is below SMA50 for each perspective timeframe?

SMA50_D $3956 (COLOR GREEN IF PRICE IS ABOVE SMA50), SMA50_4HR $4164.78 (COLOR GREEN IF PRICE IS ABOVE SMA50) AND SMA50_1HR $42227.12 (COLOR GREEN IF PRICE IS ABOVE SMA50)

Please refer to the attached for your reference.

 
Thanks for the support as usual SleepyZ. Can you also make the labels that can show price and also changes colors to green when price is above the SMA50 or red when price is below SMA50 for each perspective timeframe?

SMA50_D $3956 (COLOR GREEN IF PRICE IS ABOVE SMA50), SMA50_4HR $4164.78 (COLOR GREEN IF PRICE IS ABOVE SMA50) AND SMA50_1HR $42227.12 (COLOR GREEN IF PRICE IS ABOVE SMA50)

Please refer to the attached for your reference.


Try this

Ruby:
def SMAD50 = SimpleMovingAvg (close (period = AggregationPeriod.DAY), 50) ;
def SMA4H50 = SimpleMovingAvg (close (period = AggregationPeriod.FOUR_HOURS), 50) ;
def SMA1H50 = SimpleMovingAvg (close (period = AggregationPeriod.HOUR), 50) ;

input showlabels = yes;
AddLabel(showlabels, "SMAD50 " + AsDollars(SMAD50), if close > SMAD50 then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA4H0 " + AsDollars(SMA4H50), if close > SMA4H50 then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA1H50 " + AsDollars(SMA1H50), if close > SMA1H50 then Color.GREEN else Color.RED);
 
Hi SleepyZ,

I have added a couple of more periods to the script so I can use them as reference but this particular script works only on the 5mns time frame. The chart keeps notifying a message saying that the secondary period cannot be less than the primary, and I need this to also work on different time frames but specifically on the daily time frame. Could you take a look and see what needs to be fixed?


def SMA50_5MN = SimpleMovingAvg (close (period = AggregationPeriod.FIVE_MIN), 50) ;
def SMA50_15MN = SimpleMovingAvg (close (period = AggregationPeriod.FIFTEEN_MIN), 50) ;
def SMA50_30MN = SimpleMovingAvg (close (period = AggregationPeriod.THIRTY_MIN), 50) ;
def SMA50_1HR = SimpleMovingAvg (close (period = AggregationPeriod.HOUR), 50) ;
def SMA50_4HR = SimpleMovingAvg (close (period = AggregationPeriod.FOUR_HOURS), 50) ;
def SMA50_D = SimpleMovingAvg (close (period = AggregationPeriod.DAY), 50) ;

input showlabels = yes;

AddLabel(showlabels, "SMA50_D " + AsDollars(SMA50_D), if close > SMA50_D then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_4HR " + AsDollars(SMA50_4HR), if close > SMA50_4HR then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_1HR " + AsDollars(SMA50_1HR), if close > SMA50_1HR then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_30MN " + AsDollars(SMA50_30MN), if close > SMA50_30MN then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_15MN " + AsDollars(SMA50_15MN), if close > SMA50_15MN then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_5MN " + AsDollars(SMA50_5MN), if close > SMA50_5MN then Color.GREEN else Color.RED);

AddLabel(showlabels, "|", Color.YELLOW);

 
Hi SleepyZ,

I have added a couple of more periods to the script so I can use them as reference but this particular script works only on the 5mns time frame. The chart keeps notifying a message saying that the secondary period cannot be less than the primary, and I need this to also work on different time frames but specifically on the daily time frame. Could you take a look and see what needs to be fixed?


def SMA50_5MN = SimpleMovingAvg (close (period = AggregationPeriod.FIVE_MIN), 50) ;
def SMA50_15MN = SimpleMovingAvg (close (period = AggregationPeriod.FIFTEEN_MIN), 50) ;
def SMA50_30MN = SimpleMovingAvg (close (period = AggregationPeriod.THIRTY_MIN), 50) ;
def SMA50_1HR = SimpleMovingAvg (close (period = AggregationPeriod.HOUR), 50) ;
def SMA50_4HR = SimpleMovingAvg (close (period = AggregationPeriod.FOUR_HOURS), 50) ;
def SMA50_D = SimpleMovingAvg (close (period = AggregationPeriod.DAY), 50) ;

input showlabels = yes;

AddLabel(showlabels, "SMA50_D " + AsDollars(SMA50_D), if close > SMA50_D then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_4HR " + AsDollars(SMA50_4HR), if close > SMA50_4HR then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_1HR " + AsDollars(SMA50_1HR), if close > SMA50_1HR then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_30MN " + AsDollars(SMA50_30MN), if close > SMA50_30MN then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_15MN " + AsDollars(SMA50_15MN), if close > SMA50_15MN then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_5MN " + AsDollars(SMA50_5MN), if close > SMA50_5MN then Color.GREEN else Color.RED);

AddLabel(showlabels, "|", Color.YELLOW);

TOS will not display aggregations lower than the chart timeframe. The only one of these that will display on a Daily chart is the SMA50_D.

One alternative would be is to use a grid of 2 charts, 5m and Daily (or higher than 5m chart). The 5m chart has the label code in the lower pane of the upper chart in the image with the price subgraph not showing. Here is a link to the grid. http://tos.mx/ZvYuypC

 
I see. This means I should be able create a separate label for each respective time frame. Thanks again!

Yes. I had some time and put it all in one script that will display labels for all timeframes daily and below charts with the bubbles that can be displayed based upon the chart's aggregation

Ruby:
def SMA50_5MN;
def SMA50_15MN;
def SMA50_30MN;
def SMA50_1HR;
def SMA50_4HR;
def SMA50_D;

if GetAggregationPeriod() <= AggregationPeriod.FIVE_MIN {
    SMA50_5MN = SimpleMovingAvg (close (period = AggregationPeriod.FIVE_MIN), 50);
} else {
    SMA50_5MN = Double.NaN;
}

if GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN {
    SMA50_15MN = SimpleMovingAvg (close (period = AggregationPeriod.FIFTEEN_MIN), 50) ;
} else {
    SMA50_15MN = Double.NaN;
}

if GetAggregationPeriod() <= AggregationPeriod.THIRTY_MIN {
    SMA50_30MN = SimpleMovingAvg (close (period = AggregationPeriod.THIRTY_MIN), 50) ;
} else {
    SMA50_30MN = Double.NaN;
}

if GetAggregationPeriod() <= AggregationPeriod.HOUR {
    SMA50_1HR = SimpleMovingAvg (close (period = AggregationPeriod.HOUR), 50);
} else {
    SMA50_1HR = Double.NaN;
}

if GetAggregationPeriod() <= AggregationPeriod.FOUR_HOURS {
    SMA50_4HR = SimpleMovingAvg (close (period = AggregationPeriod.FOUR_HOURS), 50) ;
} else {
    SMA50_4HR = Double.NaN;
}

if GetAggregationPeriod() <= AggregationPeriod.DAY {
    SMA50_D = SimpleMovingAvg (close (period = AggregationPeriod.DAY), 50) ;
} else {
    SMA50_D = Double.NaN;
}

input showlabels = yes;

AddLabel(showlabels and getaggregationPeriod()<=aggregationPeriod.DAY, "SMA50_D " + AsDollars(SMA50_D), if close > SMA50_D then Color.GREEN else Color.RED);

AddLabel(showlabels and getaggregationPeriod()<=aggregationPeriod.FOUR_HOURS, "SMA50_4HR " + AsDollars(SMA50_4HR), if close > SMA50_4HR then Color.GREEN else Color.RED);

AddLabel(showlabels and getaggregationPeriod()<=aggregationPeriod.HOUR, "SMA50_1HR " + AsDollars(SMA50_1HR), if close > SMA50_1HR then Color.GREEN else Color.RED);

AddLabel(showlabels and getaggregationPeriod()<=aggregationPeriod.THIRTY_MIN, "SMA50_30MN " + AsDollars(SMA50_30MN), if close > SMA50_30MN then Color.GREEN else Color.RED);

AddLabel(showlabels and GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN, "SMA50_15MN " + AsDollars(SMA50_15MN), if close > SMA50_15MN then Color.GREEN else Color.RED);

AddLabel(showlabels and GetAggregationPeriod() <= AggregationPeriod.FIVE_MIN, "SMA50_5MN " + AsDollars(SMA50_5MN), if close > SMA50_5MN then Color.GREEN else Color.RED);


AddLabel(showlabels, "|", Color.YELLOW);
 
Hi SleepyZ,

I have added a couple of more periods to the script so I can use them as reference but this particular script works only on the 5mns time frame. The chart keeps notifying a message saying that the secondary period cannot be less than the primary, and I need this to also work on different time frames but specifically on the daily time frame. Could you take a look and see what needs to be fixed?


def SMA50_5MN = SimpleMovingAvg (close (period = AggregationPeriod.FIVE_MIN), 50) ;
def SMA50_15MN = SimpleMovingAvg (close (period = AggregationPeriod.FIFTEEN_MIN), 50) ;
def SMA50_30MN = SimpleMovingAvg (close (period = AggregationPeriod.THIRTY_MIN), 50) ;
def SMA50_1HR = SimpleMovingAvg (close (period = AggregationPeriod.HOUR), 50) ;
def SMA50_4HR = SimpleMovingAvg (close (period = AggregationPeriod.FOUR_HOURS), 50) ;
def SMA50_D = SimpleMovingAvg (close (period = AggregationPeriod.DAY), 50) ;

input showlabels = yes;

AddLabel(showlabels, "SMA50_D " + AsDollars(SMA50_D), if close > SMA50_D then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_4HR " + AsDollars(SMA50_4HR), if close > SMA50_4HR then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_1HR " + AsDollars(SMA50_1HR), if close > SMA50_1HR then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_30MN " + AsDollars(SMA50_30MN), if close > SMA50_30MN then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_15MN " + AsDollars(SMA50_15MN), if close > SMA50_15MN then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_5MN " + AsDollars(SMA50_5MN), if close > SMA50_5MN then Color.GREEN else Color.RED);

AddLabel(showlabels, "|", Color.YELLOW);

Is there a way to plot each of these in the Price chart?
 
Is there a way to plot each of these in the Price chart?

Yes, this plots whatever aggregations are greater than or equal to the chart aggregation. There are movable bubbles added.

Capture.jpg
Ruby:
plot SMA50_5MN;
plot SMA50_15MN;
plot SMA50_30MN;
plot SMA50_1HR;
plot SMA50_4HR;
plot SMA50_D;


if GetAggregationPeriod() <= AggregationPeriod.FIVE_MIN {
    SMA50_5MN = SimpleMovingAvg (close (period = AggregationPeriod.FIVE_MIN), 50) ;
} else {
    SMA50_5MN = Double.NaN;
}

if GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN {
    SMA50_15MN = SimpleMovingAvg (close (period = AggregationPeriod.FIFTEEN_MIN), 50) ;
} else {
    SMA50_15MN = Double.NaN;
}

if GetAggregationPeriod() <= AggregationPeriod.THIRTY_MIN {
    SMA50_30MN = SimpleMovingAvg (close (period = AggregationPeriod.THIRTY_MIN), 50) ;
} else {
    SMA50_30MN = Double.NaN;
}

if GetAggregationPeriod() <= AggregationPeriod.HOUR {
    SMA50_1HR = SimpleMovingAvg (close (period = AggregationPeriod.HOUR), 50) ;
} else {
    SMA50_1HR = Double.NaN;
}

if GetAggregationPeriod() <= AggregationPeriod.FOUR_HOURS {
    SMA50_4HR = SimpleMovingAvg (close (period = AggregationPeriod.FOUR_HOURS), 50) ;
} else {
    SMA50_4HR = Double.NaN;
}

if GetAggregationPeriod() <= AggregationPeriod.DAY {
    SMA50_D = SimpleMovingAvg (close (period = AggregationPeriod.DAY), 50) ;
} else {
    SMA50_D = Double.NaN;
}

input showlabels = yes;

AddLabel(showlabels, "SMA50_D " + AsDollars(SMA50_D), if close > SMA50_D then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_4HR " + AsDollars(SMA50_4HR), if close > SMA50_4HR then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_1HR " + AsDollars(SMA50_1HR), if close > SMA50_1HR then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_30MN " + AsDollars(SMA50_30MN), if close > SMA50_30MN then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_15MN " + AsDollars(SMA50_15MN), if close > SMA50_15MN then Color.GREEN else Color.RED);
AddLabel(showlabels, "SMA50_5MN " + AsDollars(SMA50_5MN), if close > SMA50_5MN then Color.GREEN else Color.RED);

input showbubbles = yes;
input bubblemover = 2;
def b = bubblemover;
def bm = b + 1;
AddChartBubble(showbubbles and IsNaN(close[b]) and !IsNaN(close[bm]), SMA50_D[b], "SMA50_D " + AsDollars(SMA50_D[bm]), if close[bm] > SMA50_D[bm] then Color.GREEN else Color.RED);
AddChartBubble(showbubbles and IsNaN(close[b]) and !IsNaN(close[bm]), SMA50_4HR[b], "SMA50_4HR " + AsDollars(SMA50_4HR[bm]), if close[bm] > SMA50_4HR[bm] then Color.GREEN else Color.RED);
AddChartBubble(showbubbles and IsNaN(close[b]) and !IsNaN(close[bm]), SMA50_1HR[b], "SMA50_1HR " + AsDollars(SMA50_1HR[bm]), if close[bm] > SMA50_1HR[bm] then Color.GREEN else Color.RED);
AddChartBubble(showbubbles and IsNaN(close[b]) and !IsNaN(close[bm]), SMA50_30MN[b], "SMA50_30MN " + AsDollars(SMA50_30MN[bm]), if close[bm] > SMA50_30MN[bm] then Color.GREEN else Color.RED);
AddChartBubble(showbubbles and IsNaN(close[b]) and !IsNaN(close[bm]), SMA50_15MN[b], "SMA50_15MN " + AsDollars(SMA50_15MN[bm]), if close[bm] > SMA50_15MN[bm] then Color.GREEN else Color.RED);
AddChartBubble(showbubbles and IsNaN(close[b]) and !IsNaN(close[bm]), SMA50_5MN[bm], "SMA50_5MN " + AsDollars(SMA50_5MN[bm]), if close[bm] > SMA50_5MN[bm] then Color.GREEN else Color.RED);
 
Last edited:
Thread starter Similar threads Forum Replies Date
B MTF Stochastic Questions 1
E MTF Stacked Moving Averages Questions 3
M MTF AMA Questions 2
C MTF EMA cloud Questions 1
V MTF Marubozu signals Questions 0

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
446 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top