Does TOS allow you to assign an aggregation period to plots for custom moving averages?

Mr_Wheeler

Active member
I was successful with standard moving average types supplied by TOS but I'm running into problems with a custom triangle study.

https://tos.mx/tEyrGZg - that's my completed custom MTF label study.

This is the line of code I'm having issues with.

Code:
addlabel(one_minute_label, if price >= tmaline then "Golden Cross " + barssince( crossup_1m) + " bars ago" else if price <= tmaline then "Death Cross " + barssince(crossdn_1m) + " bars ago" else "", if price >= tmaline then Color.GREEN else if price <= tmaline then Color.WHITE else Color.GRAY);

specifically

Code:
barssince( crossup_1m)

&

Code:
barssince(crossdn_1m)

Here's my full code.

Code:
## Golden and Death Cross indicator
## Coded by Chemmy at usethinkscript.com

input src = close;
input price = close;
input length = 200;
input one_minute_label = yes;
input two_minute_label = yes;
input five_minute_label = yes;
input ten_minute_label = yes;
input fifteen_minute_label = yes;
input twenty_minute_label = yes;
input thirty_minute_label = no;
input one_hour_label = no;
input two_hour_label = no;
input four_hour_label = no;
input one_day_label = no;


############################################################ Triangle
# Calculate TMA
def tma = MovingAverage(AverageType.WEIGHTED, MovingAverage(AverageType.WEIGHTED, MovingAverage(AverageType.WEIGHTED, price, length / 2), length / 2), length);

# Plot TMA
plot tmaLine = tma;
tmaLine.AssignValueColor(if tma > tma[1] then Color.GREEN else if tma < tma[1] then Color.RED else Color.YELLOW);
tmaLine.SetLineWeight(2);

# Define alert conditions for green, red, and yellow plot line color changes
def isGreen = tmaLine > tmaLine[1];
def isRed = tmaLine < tmaLine[1];
def up = close > tma;
def down = close < tma;
def Hold_Up_Value = if up then 1 else if !down then Hold_Up_Value[1] else 0;
def Hold_Dn_Value = if down then 1 else if !up then Hold_Dn_Value[1] else 0;
plot Signal_Up = if up and Hold_Dn_Value[1] == 1 then low else Double.NaN;
Signal_Up.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
plot Signal_Dn = if down and Hold_Up_Value[1] == 1 then high else Double.NaN;
Signal_Dn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);


# Define the barssince script
def bn = barnumber();
script barssince {
    input Condition = 0;
    def barssince = if Condition then 1 else barssince[1] + 1;
    plot return = barssince;
}

def crossup_1m =  price crosses above tmaline;
def crossdn_1m =  price crosses below tmaline;


###################################################################################################
###################################################################################################

AddLabel(one_minute_label, "triangle", Color.LIME);
AddLabel(one_minute_label, "2 minute", Color.ORANGE);

addlabel(one_minute_label, if price >= tmaline then "Golden Cross " + barssince( crossup_1m) + " bars ago" else if price <= tmaline then "Death Cross " + barssince(crossdn_1m) + " bars ago" else "", if price >= tmaline then Color.GREEN else if price <= tmaline then Color.WHITE else Color.GRAY);
 
Solution
I was successful with standard moving average types supplied by TOS but I'm running into problems with a custom triangle study.

https://tos.mx/tEyrGZg - that's my completed custom MTF label study.

This is the line of code I'm having issues with.

Code:
addlabel(one_minute_label, if price >= tmaline then "Golden Cross " + barssince( crossup_1m) + " bars ago" else if price <= tmaline then "Death Cross " + barssince(crossdn_1m) + " bars ago" else "", if price >= tmaline then Color.GREEN else if price <= tmaline then Color.WHITE else Color.GRAY);

specifically

Code:
barssince( crossup_1m)

&

Code:
barssince(crossdn_1m)

Here's my full code.

Code:
## Golden and Death Cross indicator
## Coded by Chemmy at usethinkscript.com...
I was successful with standard moving average types supplied by TOS but I'm running into problems with a custom triangle study.

https://tos.mx/tEyrGZg - that's my completed custom MTF label study.

This is the line of code I'm having issues with.

Code:
addlabel(one_minute_label, if price >= tmaline then "Golden Cross " + barssince( crossup_1m) + " bars ago" else if price <= tmaline then "Death Cross " + barssince(crossdn_1m) + " bars ago" else "", if price >= tmaline then Color.GREEN else if price <= tmaline then Color.WHITE else Color.GRAY);

specifically

Code:
barssince( crossup_1m)

&

Code:
barssince(crossdn_1m)

Here's my full code.

Code:
## Golden and Death Cross indicator
## Coded by Chemmy at usethinkscript.com

input src = close;
input price = close;
input length = 200;
input one_minute_label = yes;
input two_minute_label = yes;
input five_minute_label = yes;
input ten_minute_label = yes;
input fifteen_minute_label = yes;
input twenty_minute_label = yes;
input thirty_minute_label = no;
input one_hour_label = no;
input two_hour_label = no;
input four_hour_label = no;
input one_day_label = no;


############################################################ Triangle
# Calculate TMA
def tma = MovingAverage(AverageType.WEIGHTED, MovingAverage(AverageType.WEIGHTED, MovingAverage(AverageType.WEIGHTED, price, length / 2), length / 2), length);

# Plot TMA
plot tmaLine = tma;
tmaLine.AssignValueColor(if tma > tma[1] then Color.GREEN else if tma < tma[1] then Color.RED else Color.YELLOW);
tmaLine.SetLineWeight(2);

# Define alert conditions for green, red, and yellow plot line color changes
def isGreen = tmaLine > tmaLine[1];
def isRed = tmaLine < tmaLine[1];
def up = close > tma;
def down = close < tma;
def Hold_Up_Value = if up then 1 else if !down then Hold_Up_Value[1] else 0;
def Hold_Dn_Value = if down then 1 else if !up then Hold_Dn_Value[1] else 0;
plot Signal_Up = if up and Hold_Dn_Value[1] == 1 then low else Double.NaN;
Signal_Up.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
plot Signal_Dn = if down and Hold_Up_Value[1] == 1 then high else Double.NaN;
Signal_Dn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);


# Define the barssince script
def bn = barnumber();
script barssince {
    input Condition = 0;
    def barssince = if Condition then 1 else barssince[1] + 1;
    plot return = barssince;
}

def crossup_1m =  price crosses above tmaline;
def crossdn_1m =  price crosses below tmaline;


###################################################################################################
###################################################################################################

AddLabel(one_minute_label, "triangle", Color.LIME);
AddLabel(one_minute_label, "2 minute", Color.ORANGE);

addlabel(one_minute_label, if price >= tmaline then "Golden Cross " + barssince( crossup_1m) + " bars ago" else if price <= tmaline then "Death Cross " + barssince(crossdn_1m) + " bars ago" else "", if price >= tmaline then Color.GREEN else if price <= tmaline then Color.WHITE else Color.GRAY);

I assume that you are finding N/A in some of your labels. If so, it is likely that you do not have a large enough timeframe defined for those to make the comparisons. In the image below is your code with a 30day 1min chart with all labels populating.

Screenshot 2023-08-15 093707.png
 
Last edited:
Solution

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
223 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