1 over 10 monthly label

ProJazz

New member
VIP
Can someone help me clean up this code I pieced together? I am trying to have a label that tell me if the underlying 1 month moving average is above the 10 month moving average. I haven't done that much coding in a long time. I was playing around with labels to help me tell if a stock is on an uptrend or downtrend at a glance. I came across a mobius code for TMO with higher aggregate for using a different time frame that what is displayed. http://tos.mx/ABHh7kw

Code:
#1over 10 month
input length = 10;
input calcLength = 1;
input smoothLength = 1;
input agg = AggregationPeriod.monTH;

def o = open(period = agg);
def c = close(period = agg);

def data = fold i = 0 to length
           with s
           do s + (if c > GetValue(o, i)
                   then 1
                   else if c < GetValue(o, i)
                        then - 1
                        else 0);

def EMA5 = ExpAverage(data, calcLength);
plot Main = ExpAverage(EMA5, smoothLength);
plot Signal = ExpAverage(Main, smoothLength);

Main.AssignValueColor(if Main > Signal
                           then Color.GREEN
                           else Color.RED);

Signal.AssignValueColor(if Main > Signal
                             then Color.GREEN
                             else Color.RED);

Signal.HideBubble();
Signal.HideTitle();


def bullish = simplemovingAvg(1)>simpleMovingAvg(10);
def bearish = simpleMovingAvg(1)<simplemovingAvg(10);

AddLabel(yes, if bullish then "1>10 Monthly" else "1<10 Monthly", if bullish then Color.light_GREEN else Color.red);
 
Last edited by a moderator:
Solution
@ProJazz
Is this what you're wanting?
Ruby:
# 1 month over 10 month moving average trend label
# if 1 mo > 10 month SMA the stock is in an uptrend
# if 1 mo < 10 month SMA the stock is in a downtrend

input Period = AggregationPeriod.MONTH;
input AvgType = AverageType.SIMPLE;
input Length_1 = 1;
input Length_2 =10;
def ma1 = MovingAverage(AvgType, close(period = Period), Length_1);
def ma2 = MovingAverage(AvgType, close(period = Period), Length_2);

AddLabel(1, if ma1 > ma2 then " 1mo > 10mo " else if ma1 == ma2
    then " 1ma = 10mo " else " 1mo < 10mo", if ma1 > ma2 then Color.GREEN
    else if ma1 == ma2 then Color.GRAY else Color.RED);
Can someone help me clean up this code I pieced together? I am trying to have a label that tell me if the underlying 1 month moving average is above the 10 month moving average. I haven't done that much coding in a long time. I was playing around with labels to help me tell if a stock is on an uptrend or downtrend at a glance. I came across a mobius code for TMO with higher aggregate for using a different time frame that what is displayed. http://tos.mx/ABHh7kw
I am not sure what you are trying to do.
A moving average, by definition is an average of x number of bars. A moving average of one bar would be the same as just saying the close of the current candle????

So you want a label that says that current month close is over/under the 10 month EMA?
Code:
input agg = AggregationPeriod.monTH;
def c = close(period = agg);
def ema= ExpAverage(c, 10);
def trend = c > ema ;
Addlabel(yes, if trend then "1>10 Monthly" else "1<10 Monthly", 
                      if trend then Color.light_GREEN else Color.red);
 
I am not sure what you are trying to do.
A moving average, by definition is an average of x number of bars. A moving average of one bar would be the same as just saying the close of the current candle????

So you want a label that says that current month close is over/under the 10 month EMA?
Code:
input agg = AggregationPeriod.monTH;
def c = close(period = agg);
def ema= ExpAverage(c, 10);
def trend = c > ema ;
Addlabel(yes, if trend then "1>10 Monthly" else "1<10 Monthly",
                      if trend then Color.light_GREEN else Color.red);
Yes but have it persist across time frames. So while I’m looking at a 15 minute or 1 hour chart it shows the 1 above/below the 10 MONTH label of the same ticker
 
that's an interesting idea, a1cturner. My goal is to use a screen that tracks different data and on the same stock so I can see an overview and specifics so I can greenlight/redlight stocks without looking too deep. My timeframes vary a lot. I need to be a better rules based trader and I am trying to learn more disciple.

I am trying to understand more how thinkscript works and no better way than learning by doing. I just don't know the syntax very well. Any help is appreciated. I sometimes sell long term stocks when the underlying crosses the 1<10 mo moving average and don't buy again until the 1 is over the 10.

After a lot more reading, I was thinking it should be something like this:
Code:
# 1 month over 10 month moving average trend label
# if 1 mo > 10 month SMA the stock is in an uptrend
# if 1 mo < 10 month SMA the stock is in a downtrend

input agg = aggregationperiod.month;

def ma1 = simplemovingavg(close(period = agg), 1);

def ma2 = simplemovingavg(close(period = agg), 10);

AddLabel(1, if ma1 > ma2 then " 1mo > 10mo " else if ma1 == ma2 then " 1ma = 10mo " else " 1mo < 10mo", if ma1 > ma2 then color.green else if ma1 == ma2 then color.gray else color.red);

But I can't FIGURE OUT can I get the moving average Type as a variable, so I can add it to the code to reuse it, in case I want a different type of moving average cross.

for example:

input matype = AverageType.SIMPLE;

def ma1 = matype(close(period = agg), 1);

etc

http://tos.mx/1Irqe8h
 
Last edited:
@ProJazz
Is this what you're wanting?
Ruby:
# 1 month over 10 month moving average trend label
# if 1 mo > 10 month SMA the stock is in an uptrend
# if 1 mo < 10 month SMA the stock is in a downtrend

input Period = AggregationPeriod.MONTH;
input AvgType = AverageType.SIMPLE;
input Length_1 = 1;
input Length_2 =10;
def ma1 = MovingAverage(AvgType, close(period = Period), Length_1);
def ma2 = MovingAverage(AvgType, close(period = Period), Length_2);

AddLabel(1, if ma1 > ma2 then " 1mo > 10mo " else if ma1 == ma2
    then " 1ma = 10mo " else " 1mo < 10mo", if ma1 > ma2 then Color.GREEN
    else if ma1 == ma2 then Color.GRAY else Color.RED);
 
Last edited:
Solution
@Svanoy has your answer above. I am also new to ThinkScript but you will learn quick.

What I do is decide what I want to be able to change later. Those are “input ##### =“. I then define my MACD, RSI, MA etc with “def ##### =“ Then I plot or plot scan what I want the output to be with “plot ##### =“ or “plotscan ##### =“ or in your case “AddLabel, (yes, #####);”

Sometimes I just have to draw out a flow chart on paper to wrap my mind around what I want and then tweak it after I have a rough code typed up.

Check out the resource page. It helps a lot.
 
I tried what I originally told you about the 10 and 100 and it is a little bit off. I tried it on an EMA study I have that I use on the 1 minute and 10 minute time frame. On the 10 minute I use the 5EMA, 12EMA, 34EMA, and 50EMA. On the 1 minute I tried the 50EMA, 120EMA, 340EMA, and 500EMA. It was close but not close enough for my particular study. (EMA crossovers we’re 10-15 minutes apart between the 1 minute and 10 minute charts)

Since the EMA uses the close of the candle I understand why the above solution is not correct. I wonder if it’s possible to define the candle in which it pulls the close price from?

I tried close[10] and that didn’t work. I’m sure there is a fairly simple solution. Anyone know of one?

I think this would help multiple studies on this forum with repainting issues from MTFs that we get.
 
Last edited:
But I can't FIGURE OUT can I get the moving average Type as a variable, so I can add it to the code to reuse it, in case I want a different type of moving average cross.

I don't think you can change the average type within the program.

If you want multiple average types, then create multiple formulas and then use the appropriate variable.
pick the desired variable with if-then or switch-case.

If you keep thinking about what your program might need, it will never be finished and will be a 1000 lines long.


Code:
input avg1_type = AverageType.SIMPLE;
input avg1_price = close;
input avg1_len = 9;
def ma1 = MovingAverage(avg1_type, avg1_price, avg1_len);

input avg2_type = AverageType.exponential;
input avg2_price = close;
input avg2_len = 22;
def ma2 = MovingAverage(avg2_type, avg2_price, avg2_len);


https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/AverageType
...
EXPONENTIAL
HULL
SIMPLE
WEIGHTED
WILDERS
 
You guys are awesome. Thanks. Naturally, I wasn't interested trading based on any label, rather only to quickly see a few parameters at a glance as I scan lots of charts for patterns - sure I have scanners but I like to know if the stock is up/down overall compared to the market to trade against the trend. I am a contrarian by nature but momentum trading with the trend is fun too. I have 3 monitors and I use one for data while I look at daily charts.
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
540 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