Welcome to the second episode of “How to Thinkscript”. We are TOSIndicators.com, home of the Volatility Box, the most robust ThinkOrSwim indicator based on statistical models built for large institutions and hedge funds.

Today’s video is going to be an answer to a user’s (shoutout Carlos) question, regarding how to build an Anchored cumulative advance decline line for a group of stocks, which we turn into a trading system.


If you’d like to skip the tutorial and start playing with the indicator right away, it’s available to download for free below.

Original Raw Code Provided by Carlos:

 
declare lower;
 FB
 def fb = close("FB");
 def fb1 = close("FB")[1];
 def fba = fb > fb1;
 def fbb = fb < fb1;
 AMZN
 def amzn = close("AMZN");
 def amzn1 = close("AMZN")[1];
 def amzna = amzn > amzn1;
 def amznb = amzn < amzn1;
 AAPL
 def aapl = close("AAPL");
 def aapl1 = close("AAPL")[1];
 def aapla = aapl > aapl1;
 def aaplb = aapl < aapl1;
 NFLX
 def nflx = close("NFLX");
 def nflx1 = close("NFLX")[1];
 def nflxa = nflx > nflx1;
 def nflxb = nflx < nflx1;
 GOOG
 def goog = close("GOOG");
 def goog1 = close("GOOG")[1];
 def googa = goog > goog1;
 def googb = goog < goog1;
 MSFT
 def msft = close("MSFT");
 def msft1 = close("MSFT")[1];
 def msfta = nflx > nflx1;
 def msftb = nflx < nflx1;
 input startDateYyyyMmDd = 20120518;
 def beyondStartDate = if GetYYYYMMDD() >= startDateYyyyMmDd then 1 else 0;
 plot gang =  if beyondStartDate then TotalSum((fba - fbb)+(amzna-amznb)+(aapla-aaplb)+(nflxa-nflxb)+(googa-googb)+(msfta-msftb)) else totalsum(0);

Clean Code With New Variable Names:

#Credit to /r/BuckyJackson36 for commenting out the symbol code

declare lower;
 input symbol_1 = "FB";
 input symbol_2 = "AMZN";
 input symbol_3 = "AAPL";
 input symbol_4 = "NFLX";
 input symbol_5 = "GOOG";
 input symbol_6 = "MSFT";
 #FB
 def symbol1 = close(symbol_1);
 def symbol11 = close(symbol_1)[1];
 def symbol1a = symbol1 > symbol11;
 def symbol1b = symbol1 < symbol11;
 #AMZN
 def symbol2 = close(symbol_2);
 def symbol21 = close(symbol_2)[1];
 def symbol2a = symbol2 > symbol21;
 def symbol2b = symbol2 < symbol21;
 #AAPL
 def symbol3 = close(symbol_3);
 def symbol31 = close(symbol_3)[1];
 def symbol3a = symbol3 > symbol31;
 def symbol3b = symbol3 < symbol31;
 #NFLX
 def symbol4 = close(symbol_4);
 def symbol41 = close(symbol_4)[1];
 def symbol4a = symbol4 > symbol41;
 def symbol4b = symbol4 < symbol41;
 #GOOG
 def symbol5 = close(symbol_5);
 def symbol51 = close(symbol_5)[1];
 def symbol5a = symbol5 > symbol51;
 def symbol5b = symbol5 < symbol51;
 #MSFT
 def symbol6 = close(symbol_6);
 def symbol61 = close(symbol_6)[1];
 def symbol6a = symbol4 > symbol41;
 def symbol6b = symbol4 < symbol41;
 input startDateYyyyMmDd = 20120518;
 def beyondStartDate = if GetYYYYMMDD() >= startDateYyyyMmDd then 1 else 0;
 plot advDecLine =  if beyondStartDate then TotalSum((symbol1a - symbol1b)+(symbol2a-symbol2b)+(symbol3a-symbol3b)+(symbol4a-symbol4b)+(symbol5a-symbol5b)+(symbol6a-symbol6b)) else totalsum(0);

Click the button below to download the Anchored Cumulative Advance Decline Trading Indicator