I want to create a Moving Average for the average closing price of 10 specific stocks. Is it possible to do this? And if so, can it be made so you can switch the stocks used in and out with other ones?
I want to create a Moving Average for the average closing price of 10 specific stocks. Is it possible to do this? And if so, can it be made so you can switch the stocks used in and out with other ones?
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
I tried a few different things, lines 27- 44 ended up working for a MA but after experimenting with different types of MA's and lengths within the code and using the gear icon it gave me the same line that had a starting point. Can you see what i did wrong? And Is there any way to have it plot as candles or a histogram?take a look at this
https://usethinkscript.com/threads/getting-a-hma-value-of-a-different-symbol.12722/#post-108365
can assign a variable to a stock symbol.
input s1 = "SPY";
then use that variable as an input parameter to close() , to read price from that stock.
p1 = close(s1);
repeat this 10x, then add them up and divide by 10.
def p_avg = ( p1+p2+p3+... +p10 )/10;
plot z = p_avg;
.
I tried a few different things, lines 27- 44 ended up working for a MA but after experimenting with different types of MA's and lengths within the code and using the gear icon it gave me the same line that had a starting point. Can you see what i did wrong? And Is there any way to have it plot as candles or a histogram?
# multiple_symbols1_01_line
# https://usethinkscript.com/threads/creating-a-custom-index.12756/
# create a Moving Average for the average closing price of 10 specific stocks.
# ref
#https://usethinkscript.com/threads/getting-a-hma-value-of-a-different-symbol.12722/#post-108365
# LIDAR INDEX
declare lower;
def na = double.nan;
def bn = barnumber();
input price_data = { default chart_time , second_agg };
input Agg = AggregationPeriod.DAY;
input s1 = "LAZR";
input s2 = "VLDR";
input s3 = "MVIS";
input s4 = "INDI";
input s5 = "AEVA";
input s6 = "QNGY";
input s7 = "CPTN";
input s8 = "INVZ";
input s9 = "LIDR";
def p1;
def p2;
def p3;
def p4;
def p5;
def p6;
def p7;
def p8;
def p9;
switch (price_data) {
case chart_time:
p1 = if isnan(close(s1)) then close(s1)[1] else close(s1);
p2 = if isnan(close(s2)) then close(s1)[1] else close(s2);
p3 = if isnan(close(s3)) then close(s1)[1] else close(s3);
p4 = if isnan(close(s4)) then close(s1)[1] else close(s4);
p5 = if isnan(close(s5)) then close(s1)[1] else close(s5);
p6 = if isnan(close(s6)) then close(s1)[1] else close(s6);
p7 = if isnan(close(s7)) then close(s1)[1] else close(s7);
p8 = if isnan(close(s8)) then close(s1)[1] else close(s8);
p9 = if isnan(close(s9)) then close(s1)[1] else close(s9);
case second_agg:
p1 = if isnan(close(s1, agg)) then close(s1, agg)[1] else close(s1, agg);
p2 = if isnan(close(s2, agg)) then close(s1, agg)[1] else close(s2, agg);
p3 = if isnan(close(s3, agg)) then close(s1, agg)[1] else close(s3, agg);
p4 = if isnan(close(s4, agg)) then close(s1, agg)[1] else close(s4, agg);
p5 = if isnan(close(s5, agg)) then close(s1, agg)[1] else close(s5, agg);
p6 = if isnan(close(s6, agg)) then close(s1, agg)[1] else close(s6, agg);
p7 = if isnan(close(s7, agg)) then close(s1, agg)[1] else close(s7, agg);
p8 = if isnan(close(s8, agg)) then close(s1, agg)[1] else close(s8, agg);
p9 = if isnan(close(s9, agg)) then close(s1, agg)[1] else close(s9, agg);
}
input qty = 9;
def x = if !isnan(close(s1)) then 1 else 0;
plot Avg = if !x then na else (p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9)/qty;
avg.setdefaultcolor(color.yellow);
#--------------------------------
input show_price_lines = no;
plot z1 = if (show_price_lines and x) then p1 else na;
z1.setdefaultcolor(getcolor(0));
z1.SetStyle(Curve.MEDIUM_DASH);
plot z2 = if (show_price_lines and x) then p2 else na;
z2.setdefaultcolor(getcolor(1));
z2.SetStyle(Curve.MEDIUM_DASH);
plot z3 = if (show_price_lines and x) then p3 else na;
z3.setdefaultcolor(getcolor(2));
z3.SetStyle(Curve.MEDIUM_DASH);
plot z4 = if (show_price_lines and x) then p4 else na;
z4.setdefaultcolor(getcolor(3));
z4.SetStyle(Curve.MEDIUM_DASH);
plot z5 = if (show_price_lines and x) then p5 else na;
z5.setdefaultcolor(getcolor(4));
z5.SetStyle(Curve.MEDIUM_DASH);
plot z6 = if (show_price_lines and x) then p5 else na;
z6.setdefaultcolor(getcolor(5));
z6.SetStyle(Curve.MEDIUM_DASH);
plot z7 = if (show_price_lines and x) then p7 else na;
z7.setdefaultcolor(getcolor(6));
z7.SetStyle(Curve.MEDIUM_DASH);
plot z8 = if (show_price_lines and x) then p8 else na;
z8.setdefaultcolor(getcolor(7));
z8.SetStyle(Curve.MEDIUM_DASH);
plot z9 = if (show_price_lines and x) then p9 else na;
z9.setdefaultcolor(getcolor(8));
z9.SetStyle(Curve.MEDIUM_DASH);
#
# multiple_symbols1_02_histo
# https://usethinkscript.com/threads/creating-a-custom-index.12756/
# create a Moving Average for the average closing price of 10 specific stocks.
# ref
#https://usethinkscript.com/threads/getting-a-hma-value-of-a-different-symbol.12722/#post-108365
# LIDAR INDEX
declare lower;
def na = double.nan;
def bn = barnumber();
input price_data = { default chart_time , second_agg };
input Agg = AggregationPeriod.DAY;
input s1 = "LAZR";
input s2 = "VLDR";
input s3 = "MVIS";
input s4 = "INDI";
input s5 = "AEVA";
input s6 = "QNGY";
input s7 = "CPTN";
input s8 = "INVZ";
input s9 = "LIDR";
def p1;
def p2;
def p3;
def p4;
def p5;
def p6;
def p7;
def p8;
def p9;
switch (price_data) {
case chart_time:
p1 = if isnan(close(s1)) then close(s1)[1] else close(s1);
p2 = if isnan(close(s2)) then close(s1)[1] else close(s2);
p3 = if isnan(close(s3)) then close(s1)[1] else close(s3);
p4 = if isnan(close(s4)) then close(s1)[1] else close(s4);
p5 = if isnan(close(s5)) then close(s1)[1] else close(s5);
p6 = if isnan(close(s6)) then close(s1)[1] else close(s6);
p7 = if isnan(close(s7)) then close(s1)[1] else close(s7);
p8 = if isnan(close(s8)) then close(s1)[1] else close(s8);
p9 = if isnan(close(s9)) then close(s1)[1] else close(s9);
case second_agg:
p1 = if isnan(close(s1, agg)) then close(s1, agg)[1] else close(s1, agg);
p2 = if isnan(close(s2, agg)) then close(s1, agg)[1] else close(s2, agg);
p3 = if isnan(close(s3, agg)) then close(s1, agg)[1] else close(s3, agg);
p4 = if isnan(close(s4, agg)) then close(s1, agg)[1] else close(s4, agg);
p5 = if isnan(close(s5, agg)) then close(s1, agg)[1] else close(s5, agg);
p6 = if isnan(close(s6, agg)) then close(s1, agg)[1] else close(s6, agg);
p7 = if isnan(close(s7, agg)) then close(s1, agg)[1] else close(s7, agg);
p8 = if isnan(close(s8, agg)) then close(s1, agg)[1] else close(s8, agg);
p9 = if isnan(close(s9, agg)) then close(s1, agg)[1] else close(s9, agg);
}
input qty = 9;
input lvl1 = 3.5;
input lvl2 = 4.0;
def x = if !isnan(close(s1)) then 1 else 0;
plot Avg = if !x then na else (p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9)/qty;
#avg.setdefaultcolor(color.yellow);
avg.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
avg.AssignValueColor(if avg >= lvl2 then color.green else if avg >= lvl1 then color.cyan else color.blue);
#
#
I modified the histo to change color based on increase/decrease of previous bari don't know what you mean by this,
..it gave me the same line that had a starting point.
the study isn't reading data from a chart. it is reading the same 9 stock prices on whatever chart you look at, so the line will always be the same, no matter what stock is loaded in the main chart.
i should have mentioned to make it a lower study
it is possible to make this an upper study and turn off the main price candles. but for me, the chart was still scaling as if the main plot was drawing. the code below will turn off main candles.
if you use this, it can only be used in 1 study that is loaded on a chart. and if it is in a lower study, it will still affect the candles in the main chart.
# turn off price bars
input HidePrice = YES;
HidePricePlot(HidePrice);
-------------------
i created 2 new versions, a line chart and a histogram.
each of them,
..read prices from 9 stocks. changeable in settings , from an input
..can pick to use chart time or use a second aggregation time.
....default time is chart time. default agg is day.
..the line version can also plot dashed lines for the 9 stocks
..the histogram has 3 colors of bars, depending on 2 price levels
default is,
input lvl1 = 3.5
input lvl2 = 4.0
------------------
line study
Ruby:# multiple_symbols1_01_line # https://usethinkscript.com/threads/creating-a-custom-index.12756/ # create a Moving Average for the average closing price of 10 specific stocks. # ref #https://usethinkscript.com/threads/getting-a-hma-value-of-a-different-symbol.12722/#post-108365 # LIDAR INDEX declare lower; def na = double.nan; def bn = barnumber(); input price_data = { default chart_time , second_agg }; input Agg = AggregationPeriod.DAY; input s1 = "LAZR"; input s2 = "VLDR"; input s3 = "MVIS"; input s4 = "INDI"; input s5 = "AEVA"; input s6 = "QNGY"; input s7 = "CPTN"; input s8 = "INVZ"; input s9 = "LIDR"; def p1; def p2; def p3; def p4; def p5; def p6; def p7; def p8; def p9; switch (price_data) { case chart_time: p1 = if isnan(close(s1)) then close(s1)[1] else close(s1); p2 = if isnan(close(s2)) then close(s1)[1] else close(s2); p3 = if isnan(close(s3)) then close(s1)[1] else close(s3); p4 = if isnan(close(s4)) then close(s1)[1] else close(s4); p5 = if isnan(close(s5)) then close(s1)[1] else close(s5); p6 = if isnan(close(s6)) then close(s1)[1] else close(s6); p7 = if isnan(close(s7)) then close(s1)[1] else close(s7); p8 = if isnan(close(s8)) then close(s1)[1] else close(s8); p9 = if isnan(close(s9)) then close(s1)[1] else close(s9); case second_agg: p1 = if isnan(close(s1, agg)) then close(s1, agg)[1] else close(s1, agg); p2 = if isnan(close(s2, agg)) then close(s1, agg)[1] else close(s2, agg); p3 = if isnan(close(s3, agg)) then close(s1, agg)[1] else close(s3, agg); p4 = if isnan(close(s4, agg)) then close(s1, agg)[1] else close(s4, agg); p5 = if isnan(close(s5, agg)) then close(s1, agg)[1] else close(s5, agg); p6 = if isnan(close(s6, agg)) then close(s1, agg)[1] else close(s6, agg); p7 = if isnan(close(s7, agg)) then close(s1, agg)[1] else close(s7, agg); p8 = if isnan(close(s8, agg)) then close(s1, agg)[1] else close(s8, agg); p9 = if isnan(close(s9, agg)) then close(s1, agg)[1] else close(s9, agg); } input qty = 9; def x = if !isnan(close(s1)) then 1 else 0; plot Avg = if !x then na else (p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9)/qty; avg.setdefaultcolor(color.yellow); #-------------------------------- input show_price_lines = no; plot z1 = if (show_price_lines and x) then p1 else na; z1.setdefaultcolor(getcolor(0)); z1.SetStyle(Curve.MEDIUM_DASH); plot z2 = if (show_price_lines and x) then p2 else na; z2.setdefaultcolor(getcolor(1)); z2.SetStyle(Curve.MEDIUM_DASH); plot z3 = if (show_price_lines and x) then p3 else na; z3.setdefaultcolor(getcolor(2)); z3.SetStyle(Curve.MEDIUM_DASH); plot z4 = if (show_price_lines and x) then p4 else na; z4.setdefaultcolor(getcolor(3)); z4.SetStyle(Curve.MEDIUM_DASH); plot z5 = if (show_price_lines and x) then p5 else na; z5.setdefaultcolor(getcolor(4)); z5.SetStyle(Curve.MEDIUM_DASH); plot z6 = if (show_price_lines and x) then p5 else na; z6.setdefaultcolor(getcolor(5)); z6.SetStyle(Curve.MEDIUM_DASH); plot z7 = if (show_price_lines and x) then p7 else na; z7.setdefaultcolor(getcolor(6)); z7.SetStyle(Curve.MEDIUM_DASH); plot z8 = if (show_price_lines and x) then p8 else na; z8.setdefaultcolor(getcolor(7)); z8.SetStyle(Curve.MEDIUM_DASH); plot z9 = if (show_price_lines and x) then p9 else na; z9.setdefaultcolor(getcolor(8)); z9.SetStyle(Curve.MEDIUM_DASH); #
------------------
histogram study
Ruby:# multiple_symbols1_02_histo # https://usethinkscript.com/threads/creating-a-custom-index.12756/ # create a Moving Average for the average closing price of 10 specific stocks. # ref #https://usethinkscript.com/threads/getting-a-hma-value-of-a-different-symbol.12722/#post-108365 # LIDAR INDEX declare lower; def na = double.nan; def bn = barnumber(); input price_data = { default chart_time , second_agg }; input Agg = AggregationPeriod.DAY; input s1 = "LAZR"; input s2 = "VLDR"; input s3 = "MVIS"; input s4 = "INDI"; input s5 = "AEVA"; input s6 = "QNGY"; input s7 = "CPTN"; input s8 = "INVZ"; input s9 = "LIDR"; def p1; def p2; def p3; def p4; def p5; def p6; def p7; def p8; def p9; switch (price_data) { case chart_time: p1 = if isnan(close(s1)) then close(s1)[1] else close(s1); p2 = if isnan(close(s2)) then close(s1)[1] else close(s2); p3 = if isnan(close(s3)) then close(s1)[1] else close(s3); p4 = if isnan(close(s4)) then close(s1)[1] else close(s4); p5 = if isnan(close(s5)) then close(s1)[1] else close(s5); p6 = if isnan(close(s6)) then close(s1)[1] else close(s6); p7 = if isnan(close(s7)) then close(s1)[1] else close(s7); p8 = if isnan(close(s8)) then close(s1)[1] else close(s8); p9 = if isnan(close(s9)) then close(s1)[1] else close(s9); case second_agg: p1 = if isnan(close(s1, agg)) then close(s1, agg)[1] else close(s1, agg); p2 = if isnan(close(s2, agg)) then close(s1, agg)[1] else close(s2, agg); p3 = if isnan(close(s3, agg)) then close(s1, agg)[1] else close(s3, agg); p4 = if isnan(close(s4, agg)) then close(s1, agg)[1] else close(s4, agg); p5 = if isnan(close(s5, agg)) then close(s1, agg)[1] else close(s5, agg); p6 = if isnan(close(s6, agg)) then close(s1, agg)[1] else close(s6, agg); p7 = if isnan(close(s7, agg)) then close(s1, agg)[1] else close(s7, agg); p8 = if isnan(close(s8, agg)) then close(s1, agg)[1] else close(s8, agg); p9 = if isnan(close(s9, agg)) then close(s1, agg)[1] else close(s9, agg); } input qty = 9; input lvl1 = 3.5; input lvl2 = 4.0; def x = if !isnan(close(s1)) then 1 else 0; plot Avg = if !x then na else (p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9)/qty; #avg.setdefaultcolor(color.yellow); avg.SetPaintingStrategy(PaintingStrategy.HISTOGRAM); avg.AssignValueColor(if avg >= lvl2 then color.green else if avg >= lvl1 then color.cyan else color.blue); # #
This is what I mean by giving me a starting point and the same line (The link). It wouldnt let me post the picture for some reason but regardless of the length input of the MA the line remained the same (Seems to be fixed now). My end goal is to have the chart candlesticks turned off and have the study register as candlesticks instead of a line and to run it side by side with the stock im trading so essentially it would look like just another stock to anyone looking at it.
https://1drv.ms/u/s!Ai-lRKVY6qfrhkmEtbjImSqiwid-?e=Jq4BPV
In its current version (the Line study you posted) why is the line fixed? It doesnt adjust on the chart like a typical MA. If its changed from a line to candlesticks will it adjust?
I understand it is calculating from the 9 stocks. The blue line in the pic has a starting point and an end point, and wouldnt change when i adjusted the aggregation period. Image link below, hopfully it works correctly, l followed the instructions from the link. Like I said, its working fine now, it was an error from my attempt not yours.what do you mean by 'fixed' ?
if you are wondering why the line looks the same when you load a different symbol on the chart,
reread my previous post,
the study isn't reading data from a chart. it is reading the same 9 stock prices on whatever chart you look at, so the line will always be the same, no matter what stock is loaded in the main chart.
you can't post images on this forum. need to copy a link to an image , and paste that. the answer is explained in a link, in sentences #3 , at the top of the questions group.
https://usethinkscript.com/forums/questions.6/
Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|
L | creating alert for custom candle colors | Questions | 1 | |
J | Please help creating a custom alert based on this.... | Questions | 1 | |
C | Creating an alert for custom study? | Questions | 1 | |
S | Creating a custom scan? | Questions | 2 | |
A | Creating a custom step limit order for options trading | Questions | 1 |
Start a new thread and receive assistance from our community.
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.
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.