Reference Different Symbol for MACD

Solution
Well, this question has lingered for some time so I'll take a swing. I'm not able to take the time to re-write your code for you with that many secondary symbols, but the basics of referencing a secondary (or tertiary) symbol are covered here:

https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Others/GetSymbol

Toward the bottom of their example is a line which calculates the difference between the current symbol and IBM. This reference to IBM is how you want to get the data for all of your composite symbol pieces.

As for how you want to go about constructing your composite I leave that to you. If it's a simple addition of all the pieces you may be able to just do a sum on a bunch of other things... if...
Well, this question has lingered for some time so I'll take a swing. I'm not able to take the time to re-write your code for you with that many secondary symbols, but the basics of referencing a secondary (or tertiary) symbol are covered here:

https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Others/GetSymbol

Toward the bottom of their example is a line which calculates the difference between the current symbol and IBM. This reference to IBM is how you want to get the data for all of your composite symbol pieces.

As for how you want to go about constructing your composite I leave that to you. If it's a simple addition of all the pieces you may be able to just do a sum on a bunch of other things... if you want the sum of some function on the current prices for each component, it gets more involved. But hopefully with this bit of info you can take a stab at it yourself.

-mashume
 
Solution
I use a simplified version of Lazybear's Impulse MACD. Would it possible for it to reference a composite symbol different from the chart? In this case the symbol I would like is "AAPL+MSFT+NVDA+GOOGL+META+AMZN+TSLA".

In this script:
https://usethinkscript.com/threads/impulse-macd-lazybear-for-thinkorswim.13333/
Enjoy!


Code:
#// @author LazyBear
#// List of my public indicators: http://bit.ly/1LQaPK8
#// List of my app-store indicators: http://blog.tradingview.com/?p=970
#study("Impulse MACD [LazyBear]", shorttitle="IMACD_LB", overlay=false)
# Converted by Sam4Cok@Samer800 - 11/2022
# Added multiple foreign stock symbols by whoDAT - 4/7/2025

declare lower;
input BarColor = no;
input Symbols = "AAPL+MSFT+NVDA+GOOGL+META+AMZN+TSLA";
input lengthMA = 34;
input lengthSignal = 9;
#input src = hlc3;

def na = Double.NaN;

#calc_smma(src, len) =>
script calc_smma {
input src = close;
input len = 34;
    def smma = if isNaN(smma[1]) then SimpleMovingAvg(src, len) else (smma[1] * (len - 1) + src) / len;
    plot return = smma;
}

#calc_zlema(src, length) =>
script calc_zlema {
input src = close;
input length = 34;
    def ema1 = ExpAverage(src, length);
    def ema2 = ExpAverage(ema1, length);
    def d    = ema1-ema2;
    def zelma = ema1+d;
 plot return = zelma;
}

def sym_high = high(Symbols);
def sym_low = low(Symbols);
def sym_src = hlc3(Symbols);

def hi=calc_smma(sym_high, lengthMA);
def lo=calc_smma(sym_low, lengthMA);
def mi=calc_zlema(sym_src, lengthMA);

def md = if (mi>hi)then (mi-hi) else if (mi<lo) then (mi - lo) else 0;
def sb = SimpleMovingAvg(md, lengthSignal);
def sh=md-sb;
def mdc = if sym_src>mi then if sym_src>hi then 2 else 1 else if sym_src<lo then -2 else -1;

#--- PLots
plot ImpulseSignal = sb;
ImpulseSignal.SetDefaultColor(Color.WHITE);
ImpulseSignal.SetLineWeight(2);

plot ImpulseHisto = sh;
ImpulseHisto.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
ImpulseHisto.SetDefaultColor(Color.BLUE);

plot ImpulseMACD = md;
ImpulseMACD.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
ImpulseMACD.AssignValueColor(if mdc==2 then Color.GREEN else
                             if mdc==1 then Color.DARK_GREEN else
                             if mdc==-2 then Color.RED else Color.DARK_RED);

plot "0" = if isNaN(close) then na else 0;
"0".SetDefaultColor(Color.DARK_GRAY);

#--- Bar Color
AssignPriceColor(if !BarColor then Color.CURRENT else
                 if mdc==2 then Color.GREEN else
                 if mdc==1 then Color.DARK_GREEN else
                 if mdc==-2 then Color.RED else Color.DARK_RED);


#--- END Code
 

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