Repaints Plot up to 10 MTF averages For ThinkOrSwim

Repaints

halcyonguy

Moderator - Expert
VIP
Lifetime
plot up to 10 MTF averages

each average can choose,
..to use a MTF time
....if 1 minute is picked, then the average will be at chart time.
....( if a time is picked that is >1 and < chart time, then nothing will be plotted)
..average type
..length
..displacement
..line color

set a length to 1 to disable an average

average defaults are,
..day and simple
..6 lengths have numbers - 5, 9, 21, 50, 100, 200

labels with some stats,
..line color, length, agg minutes


created for this request
https://usethinkscript.com/threads/daily-sma-multiple-vs-individual.14759/



Code:
# avgs_mtf_10x_00d

def na = double.nan;
def bn = barnumber();
def chartagg = getaggregationperiod();
def chartmin = chartagg/(1000*60);

# averages ---------------------------------------
#  5,9,21,50,100,200
def w = 1;

input avg1_len = 5;
#hint avg1_len: set length to 1 to disable an average
input displace1 = 0;
input avg1_type =  AverageType.simple;
input avg1_agg = AggregationPeriod.day;
#hint avg1_agg: choose 1 minute, to use chart time
input Color1 = {default "magenta", "cyan", "pink", "gray", "orange", "red", "green", "dark_gray", "yellow", "white"};
def avg1_min = avg1_agg/60000;
def avg1_en = avg1_len > w;
#def avg1_pr = if avg1_min == 1 then close[displace1] else close(period = avg1_agg)[displace1];
def avg1_pr;
 if avg1_agg == AggregationPeriod.min then {
avg1_pr = close[displace1];
} else{
avg1_pr = close(period = avg1_agg)[displace1];
}
def avg1 = MovingAverage(avg1_type, avg1_pr, avg1_len);


input avg2_len = 9;
input displace2 = 0;
input avg2_type =  AverageType.simple;
input avg2_agg = AggregationPeriod.day;
input Color2 = { "magenta", default "cyan", "pink", "gray", "orange", "red", "green", "dark_gray", "yellow", "white"};
def avg2_min = avg2_agg/60000;
def avg2_en = avg2_len > w;
#def avg2_pr = close(period = avg2_agg)[displace2];
def avg2_pr;
 if avg2_agg == AggregationPeriod.min then {
avg2_pr = close[displace2];
} else{
avg2_pr = close(period = avg2_agg)[displace2];
}
def avg2 = MovingAverage(avg2_type, avg2_pr, avg2_len);


input avg3_len = 21;
input displace3 = 0;
input avg3_type =  AverageType.simple;
input avg3_agg = AggregationPeriod.day;
input Color3 = { "magenta", "cyan", default "pink", "gray", "orange", "red", "green", "dark_gray", "yellow", "white"};
def avg3_min = avg3_agg/60000;
def avg3_en = avg3_len > w;
#def avg3_pr = close(period = avg3_agg)[displace3];
def avg3_pr;
 if avg3_agg == AggregationPeriod.min then {
avg3_pr = close[displace3];
} else{
avg3_pr = close(period = avg3_agg)[displace3];
}
def avg3 = MovingAverage(avg3_type, avg3_pr, avg3_len);


input avg4_len = 50;
input displace4 = 0;
input avg4_type =  AverageType.simple;
input avg4_agg = AggregationPeriod.day;
input Color4 = { "magenta", "cyan", "pink", default "gray", "orange", "red", "green", "dark_gray", "yellow", "white"};
def avg4_min = avg4_agg/60000;
def avg4_en = avg4_len > w;
#def avg4_pr = close(period = avg4_agg)[displace4];
def avg4_pr;
 if avg4_agg == AggregationPeriod.min then {
avg4_pr = close[displace4];
} else{
avg4_pr = close(period = avg4_agg)[displace4];
}
def avg4 = MovingAverage(avg4_type, avg4_pr, avg4_len);


input avg5_len = 100;
input displace5 = 0;
input avg5_type =  AverageType.simple;
input avg5_agg = AggregationPeriod.day;
input Color5 = { "magenta", "cyan", "pink", "gray", default "orange", "red", "green", "dark_gray", "yellow", "white"};
def avg5_min = avg5_agg/60000;
def avg5_en = avg5_len > w;
#def avg5_pr = close(period = avg5_agg)[displace5];
def avg5_pr;
 if avg5_agg == AggregationPeriod.min then {
avg5_pr = close[displace5];
} else{
avg5_pr = close(period = avg5_agg)[displace5];
}
def avg5 = MovingAverage(avg5_type, avg5_pr, avg5_len);


input avg6_len = 200;
input displace6 = 0;
input avg6_type =  AverageType.simple;
input avg6_agg = AggregationPeriod.day;
input Color6 = { "magenta", "cyan", "pink", "gray", "orange", default "red", "green", "dark_gray", "yellow", "white"};
def avg6_min = avg6_agg/60000;
def avg6_en = avg6_len > w;
#def avg6_pr = close(period = avg6_agg)[displace6];
def avg6_pr;
 if avg6_agg == AggregationPeriod.min then {
avg6_pr = close[displace6];
} else{
avg6_pr = close(period = avg6_agg)[displace6];
}
def avg6 = MovingAverage(avg6_type, avg6_pr, avg6_len);


input avg7_len = 1;
input displace7 = 0;
input avg7_type =  AverageType.simple;
input avg7_agg = AggregationPeriod.day;
input Color7 = { "magenta", "cyan", "pink", "gray", "orange", "red", default "green", "dark_gray", "yellow", "white"};
def avg7_min = avg7_agg/60000;
def avg7_en = avg7_len > w;
#def avg7_pr = close(period = avg7_agg)[displace7];
def avg7_pr;
 if avg7_agg == AggregationPeriod.min then {
avg7_pr = close[displace7];
} else{
avg7_pr = close(period = avg7_agg)[displace7];
}
def avg7 = MovingAverage(avg7_type, avg7_pr, avg7_len);


input avg8_len = 1;
input displace8 = 0;
input avg8_type =  AverageType.simple;
input avg8_agg = AggregationPeriod.day;
input Color8 = { "magenta", "cyan", "pink", "gray", "orange", "red", "green", default "dark_gray", "yellow", "white"};
def avg8_min = avg8_agg/60000;
def avg8_en = avg8_len > w;
#def avg8_pr = close(period = avg8_agg)[displace8];
def avg8_pr;
 if avg8_agg == AggregationPeriod.min then {
avg8_pr = close[displace8];
} else{
avg8_pr = close(period = avg8_agg)[displace8];
}
def avg8 = MovingAverage(avg8_type, avg8_pr, avg8_len);


input avg9_len = 1;
input displace9 = 0;
input avg9_type =  AverageType.simple;
input avg9_agg = AggregationPeriod.day;
input Color9 = { "magenta", "cyan", "pink", "gray", "orange", "red", "green", "dark_gray", default "yellow", "white"};
def avg9_min = avg9_agg/60000;
def avg9_en = avg9_len > w;
#def avg9_pr = close(period = avg9_agg)[displace9];
def avg9_pr;
 if avg9_agg == AggregationPeriod.min then {
avg9_pr = close[displace9];
} else{
avg9_pr = close(period = avg9_agg)[displace9];
}
def avg9 = MovingAverage(avg9_type, avg9_pr, avg9_len);


input avg10_len = 1;
input displace10 = 0;
input avg10_type =  AverageType.simple;
input avg10_agg = AggregationPeriod.day;
input Color10 = { "magenta", "cyan", "pink", "gray", "orange", "red", "green", "dark_gray", "yellow", default "white"};
def avg10_min = avg10_agg/60000;
def avg10_en = avg10_len > w;
#def avg10_pr = close(period = avg10_agg)[displace10];
def avg10_pr;
 if avg10_agg == AggregationPeriod.min then {
avg10_pr = close[displace10];
} else{
avg10_pr = close(period = avg10_agg)[displace10];
}
def avg10 = MovingAverage(avg10_type, avg10_pr, avg10_len);



# -----------------------------------------------------

input show_only_last_period = no;
input line_thickness = 1;


plot z1 = if (show_only_last_period and !IsNaN(avg1_pr[-1])) or !avg1_en then na else avg1;
#z1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#z1.SetPaintingStrategy(PaintingStrategy.line);
z1.SetDefaultColor(GetColor(Color1));
z1.setlineweight(line_thickness);
z1.hidebubble();

plot z2 = if (show_only_last_period and !IsNaN(avg2_pr[-1])) or !avg2_en then na else avg2;
#z2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z2.SetDefaultColor(GetColor(Color2));
z2.setlineweight(line_thickness);
z2.hidebubble();

plot z3 = if (show_only_last_period and !IsNaN(avg3_pr[-1])) or !avg3_en then na else avg3;
#z3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z3.SetDefaultColor(GetColor(Color3));
z3.setlineweight(line_thickness);
z3.hidebubble();

plot z4 = if (show_only_last_period and !IsNaN(avg4_pr[-1])) or !avg4_en then na else avg4;
#z4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z4.SetDefaultColor(GetColor(Color4));
z4.setlineweight(line_thickness);
z4.hidebubble();

plot z5 = if (show_only_last_period and !IsNaN(avg5_pr[-1])) or !avg5_en then na else avg5;
#z5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z5.SetDefaultColor(GetColor(Color5));
z5.setlineweight(line_thickness);
z5.hidebubble();

plot z6 = if (show_only_last_period and !IsNaN(avg6_pr[-1])) or !avg6_en then na else avg6;
#z6.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z6.SetDefaultColor(GetColor(Color6));
z6.setlineweight(line_thickness);
z6.hidebubble();

plot z7 = if (show_only_last_period and !IsNaN(avg7_pr[-1])) or !avg7_en then na else avg7;
#z7.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z7.SetDefaultColor(GetColor(Color7));
z7.setlineweight(line_thickness);
z7.hidebubble();

plot z8 = if (show_only_last_period and !IsNaN(avg8_pr[-1])) or !avg8_en then na else avg8;
#z8.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z8.SetDefaultColor(GetColor(Color8));
z8.setlineweight(line_thickness);
z8.hidebubble();

plot z9 = if (show_only_last_period and !IsNaN(avg9_pr[-1])) or !avg9_en then na else avg9;
#z9.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z9.SetDefaultColor(GetColor(Color9));
z9.setlineweight(line_thickness);
z9.hidebubble();

plot z10 = if (show_only_last_period and !IsNaN(avg10_pr[-1])) or !avg10_en then na else avg10;
#z10.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z10.SetDefaultColor(GetColor(Color10));
z10.setlineweight(line_thickness);
z10.hidebubble();


#------------------------------

input show_labels = yes;
addlabel(show_labels and avg1_en, "Avg1  " + avg1_len + " / " + avg1_min + " min", z1.TakeValueColor());
addlabel(show_labels and avg2_en, "Avg2  " + avg2_len + " / " + avg2_min + " min" , z2.TakeValueColor());
addlabel(show_labels and avg3_en, "Avg3  " + avg3_len + " / " + avg3_min + " min" , z3.TakeValueColor());
addlabel(show_labels and avg4_en, "Avg4  " + avg4_len + " / " + avg4_min + " min" , z4.TakeValueColor());
addlabel(show_labels and avg5_en, "Avg5  " + avg5_len + " / " + avg5_min + " min" , z5.TakeValueColor());
addlabel(show_labels and avg6_en, "Avg6  " + avg6_len + " / " + avg6_min + " min" , z6.TakeValueColor());
addlabel(show_labels and avg7_en, "Avg7  " + avg7_len + " / " + avg7_min + " min" , z7.TakeValueColor());
addlabel(show_labels and avg8_en, "Avg8  " + avg8_len + " / " + avg8_min + " min" , z8.TakeValueColor());
addlabel(show_labels and avg9_en, "Avg9  " + avg9_len + " / " + avg9_min + " min" , z9.TakeValueColor());
addlabel(show_labels and avg10_en, "Avg10  " + avg10_len + " / " + avg10_min + " min" , z10.TakeValueColor());
#
#

f3w2zkI.jpg
 

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