Sum and plot barcount

Shaco

Member
Please help me with this code/thought...
https://usethinkscript.com/threads/rsm-indicator-for-thinkorswim.5407/#post-50775

While looking at a SPY chart, I would like to sum up and plot the UpTrendBarCount from 2 symbols (TSLA and AAPL for example) using the below script. Similarly, sum up and plot the DownTrendBarCount.

Code:
declare lower;
def hi = high;
def lo = low;
def cl = close;

def lengthRSI = 7;
def averageTypeRSI = AverageType.EXPONENTIAL;
def NetChgAvg = MovingAverage(averageTypeRSI, cl - cl[1], lengthRSI);
def TotChgAvg = MovingAverage(averageTypeRSI, AbsValue(cl - cl[1]), lengthRSI);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);

#Stochastic Slow
def over_boughtSt = 80;
def over_soldSt = 20;
def KPeriod = 14;
def DPeriod = 3;
def averageTypeStoch = AverageType.WILDERS;
def SlowK = reference StochasticFull(over_boughtSt,  over_soldSt,  KPeriod,  DPeriod,  hi,  lo,  cl,  3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullK;
def SlowD = reference StochasticFull(over_boughtSt,  over_soldSt,  KPeriod,  DPeriod,  hi,  lo,  cl,  3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullD;

#MACD Calculation
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
def averageTypeMACD = AverageType.WEIGHTED;
def Value = MovingAverage(averageTypeMACD, cl, fastLength) - MovingAverage(averageTypeMACD, cl, slowLength);
def Avg = MovingAverage(averageTypeMACD, Value, MACDLength);
def Diff = Value - Avg;

#SCAN Variables
def UpTrend = if RSI > 50 and SlowK > 50 and Value > Avg then 1 else 0;
def DownTrend = if RSI < 50 and SlowK < 50 and Value < Avg then 1 else 0;

def bnumUp;
def bnumDown;
def closeUpTrendStart;
def closeDownTrendStart;
def UpTrendBarCount;
def DownTrendBarCount;
if UpTrend and (!UpTrend[1] or DownTrend[1]) {
    bnumUp = BarNumber();
    bnumDown = 0;
    closeUpTrendStart = cl;
    closeDownTrendStart = 0;
    UpTrendBarCount = 1;
    DownTrendBarCount = 0;
} else if UpTrend {
    bnumUp = bnumUp[1];
    bnumDown = 0;
    closeUpTrendStart = closeUpTrendStart[1];
    closeDownTrendStart = 0;
    UpTrendBarCount = UpTrendBarCount[1] + 1;
    DownTrendBarCount = 0;
} else if DownTrend and (!DownTrend[1] or UpTrend[1]) {
    bnumUp = 0;
    bnumDown = BarNumber();
    closeDownTrendStart = cl;
    closeUpTrendStart = 0;
    UpTrendBarCount = 0;
    DownTrendBarCount = 1;
} else if DownTrend {
    bnumDown = bnumDown[1];
    closeDownTrendStart = closeDownTrendStart[1];
    DownTrendBarCount = DownTrendBarCount[1] + 1;
    bnumUp = 0;
    closeUpTrendStart = 0;
    UpTrendBarCount = 0;
} else {
    bnumUp = 0;
    bnumDown = 0;
    closeUpTrendStart = 0;
    closeDownTrendStart = 0;
    UpTrendBarCount = 0;
    DownTrendBarCount = 0;
}

# The UpTrendJustStarted and DownTrendJustStarted plots can be used to find stocks that have just started a trend in either direction
def UpTrendJustStartedBool = if RSI > 50 and SlowK > 50 and Value > Avg then 1 else 0;
def DownTrendJustStartedBool = if RSI < 50 and SlowK < 50 and Value < Avg then 1 else 0;
def UpTrendJustStarted = if UpTrendJustStartedBool == 1 and UpTrendJustStartedBool[1] == 0 then 1 else 0;
def DownTrendJustStarted = if DownTrendJustStartedBool == 1 and DownTrendJustStartedBool[1] == 0 then 1 else 0;
def UpTrendJustEnded = if UpTrendJustStartedBool[1] == 1 and UpTrendJustStartedBool == 0 then 1 else 0;
def DownTrendJustEnded = if DownTrendJustStartedBool[1] == 1 and DownTrendJustStartedBool == 0 then 1 else 0;

def GetTrend = if UptrendJustStarted then 3 else if Uptrend then 2 else if UpTrendJustEnded then 1 else if DownTrendJustStarted then -1 else if DownTrend then -2 else if DownTrendJustEnded then -3 else 0;

I want to append the following lines to sum up/plot the UpTrendBarCount and DownTrendBarCount of the 2 symbols:

Code:
plot upSum = if GetTrend == 2  then UpTrendBarCount(symbol="TSLA") + UpTrendBarCount(symbol="AAPL");
plot downSum = if GetTrend == -2 then DownTrendBarCount(symbol="TSLA") + DownTrendBarCount(symbol="AAPL");
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Code:
plot upSum = if GetTrend == 2  then UpTrendBarCount(symbol="TSLA") + UpTrendBarCount(symbol="AAPL");
plot downSum = if GetTrend == -2 then DownTrendBarCount(symbol="TSLA") + DownTrendBarCount(symbol="AAPL");
It's not as simple as that. you would have to modify the whole code to do the Uptrend and/or down trend for each of tickers you want and then add up how many bars each of those tickers are in the Trend.

if you have only 4 of 5 specific tickers, Go ahead and duplicate the code by the number of tickers, and instead of CL = close, you should be calling CL1 = Close("TSLA"); CL2=Close("AAPL") ..... CL5=close("MSFT") and call correspondent CL# for each of the code blocks for RSM calculations, Ofcourse you would have to have different variable names for each of those tickers. Once you do all of this you will have Up/Down trend for each of those tickers, then do what you want with it. Sum just the Uptrends or Uptrends - Downtrends, write your signal accordingly.
 
It's not as simple as that. you would have to modify the whole code to do the Uptrend and/or down trend for each of tickers you want and then add up how many bars each of those tickers are in the Trend.

if you have only 4 of 5 specific tickers, Go ahead and duplicate the code by the number of tickers, and instead of CL = close, you should be calling CL1 = Close("TSLA"); CL2=Close("AAPL") ..... CL5=close("MSFT") and call correspondent CL# for each of the code blocks for RSM calculations, Ofcourse you would have to have different variable names for each of those tickers. Once you do all of this you will have Up/Down trend for each of those tickers, then do what you want with it. Sum just the Uptrends or Uptrends - Downtrends, write your signal accordingly.
thanks for replying to my post. I attempted to code it for the past few days and this is what i have for 4 tickers. I got the amber warning triangle symbol saying this is a complex script and my experience longer load time. I hope someone will be able to condense it for me because I'm a novice coder. Maybe using a subroutine so i can have like 20 tickers and avoid the long load time warning.

Code:
# RSM_WLC
# https://usethinkscript.com/threads/rsi-macd-confluence-indicator-for-thinkorswim.505/page-3

declare lower;
declare once_per_bar;

def showLabels = yes;

input hideHisto_AAPL = yes;
input hideHisto_AMD = yes;
input hideHisto_AMZN = yes;
input hideHisto_TSLA = yes;

def lengthRSI = 7;
def averageTypeRSI = AverageType.EXPONENTIAL;

#Stochastic Slow
def over_boughtSt = 80;
def over_soldSt = 20;
def KPeriod = 14;
def DPeriod = 3;
def averageTypeStoch = AverageType.WILDERS;

#MACD Calculation
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
def averageTypeMACD = AverageType.WEIGHTED;

# -------------------------- AAPL -------------------------------------------
def hi_AAPL = high(symbol="AAPL");
def lo_AAPL = low(symbol="AAPL");
def cl_AAPL = close(symbol="AAPL");

def NetChgAvg_AAPL = MovingAverage(averageTypeRSI, cl_AAPL - cl_AAPL[1], lengthRSI);
def TotChgAvg_AAPL = MovingAverage(averageTypeRSI, AbsValue(cl_AAPL - cl_AAPL[1]), lengthRSI);
def ChgRatio_AAPL = if TotChgAvg_AAPL != 0 then NetChgAvg_AAPL / TotChgAvg_AAPL else 0;
def RSI_AAPL = 50 * (ChgRatio_AAPL + 1);

#Stochastic Slow
def SlowK_AAPL = reference StochasticFull(over_boughtSt,  over_soldSt,  KPeriod,  DPeriod,  hi_AAPL,  lo_AAPL,  cl_AAPL,  3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullK;

#MACD Calculation
def Value_AAPL = MovingAverage(averageTypeMACD, cl_AAPL, fastLength) - MovingAverage(averageTypeMACD, cl_AAPL, slowLength);
def Avg_AAPL = MovingAverage(averageTypeMACD, Value_AAPL, MACDLength);

#SCAN Variables
def UpTrend_AAPL = if RSI_AAPL > 50 and SlowK_AAPL > 50 and Value_AAPL > Avg_AAPL then 1 else 0;
def DownTrend_AAPL = if RSI_AAPL < 50 and SlowK_AAPL < 50 and Value_AAPL < Avg_AAPL then 1 else 0;

def bnumUp_AAPL;
def bnumDown_AAPL;
def closeUpTrendStart_AAPL;
def closeDownTrendStart_AAPL;
def UpTrendBarCount_AAPL;
def DownTrendBarCount_AAPL;
if UpTrend_AAPL and (!UpTrend_AAPL[1] or DownTrend_AAPL[1]) {
    bnumUp_AAPL = BarNumber();
    bnumDown_AAPL = 0;
    closeUpTrendStart_AAPL = cl_AAPL;
    closeDownTrendStart_AAPL = 0;
    UpTrendBarCount_AAPL = 1;
    DownTrendBarCount_AAPL = 0;
} else if UpTrend_AAPL {
    bnumUp_AAPL = bnumUp_AAPL[1];
    bnumDown_AAPL = 0;
    closeUpTrendStart_AAPL = closeUpTrendStart_AAPL[1];
    closeDownTrendStart_AAPL = 0;
    UpTrendBarCount_AAPL = UpTrendBarCount_AAPL[1] + 1;
    DownTrendBarCount_AAPL = 0;
} else if DownTrend_AAPL and (!DownTrend_AAPL[1] or UpTrend_AAPL[1]) {
    bnumUp_AAPL = 0;
    bnumDown_AAPL = BarNumber();
    closeDownTrendStart_AAPL = cl_AAPL;
    closeUpTrendStart_AAPL = 0;
    UpTrendBarCount_AAPL = 0;
    DownTrendBarCount_AAPL = 1;
} else if DownTrend_AAPL {
    bnumDown_AAPL = bnumDown_AAPL[1];
    closeDownTrendStart_AAPL = closeDownTrendStart_AAPL[1];
    DownTrendBarCount_AAPL = DownTrendBarCount_AAPL[1] + 1;
    bnumUp_AAPL = 0;
    closeUpTrendStart_AAPL = 0;
    UpTrendBarCount_AAPL = 0;
} else {
    bnumUp_AAPL = 0;
    bnumDown_AAPL = 0;
    closeUpTrendStart_AAPL = 0;
    closeDownTrendStart_AAPL = 0;
    UpTrendBarCount_AAPL = 0;
    DownTrendBarCount_AAPL = 0;
}

# The UpTrendJustStarted and DownTrendJustStarted plots can be used to find stocks that have just started a trend in either direction
def UpTrendJustStartedBool_AAPL = if RSI_AAPL > 50 and SlowK_AAPL > 50 and Value_AAPL > Avg_AAPL then 1 else 0;
def DownTrendJustStartedBool_AAPL = if RSI_AAPL < 50 and SlowK_AAPL < 50 and Value_AAPL < Avg_AAPL then 1 else 0;
def UpTrendJustStarted_AAPL = if UpTrendJustStartedBool_AAPL == 1 and UpTrendJustStartedBool_AAPL[1] == 0 then 1 else 0;
def DownTrendJustStarted_AAPL = if DownTrendJustStartedBool_AAPL == 1 and DownTrendJustStartedBool_AAPL[1] == 0 then 1 else 0;
def UpTrendJustEnded_AAPL = if UpTrendJustStartedBool_AAPL[1] == 1 and UpTrendJustStartedBool_AAPL == 0 then 1 else 0;
def DownTrendJustEnded_AAPL = if DownTrendJustStartedBool_AAPL[1] == 1 and DownTrendJustStartedBool_AAPL == 0 then 1 else 0;

def GetTrend_AAPL = if UpTrendJustStarted_AAPL then 3 else if UpTrend_AAPL then 2 else if UpTrendJustEnded_AAPL then 1 else if DownTrendJustStarted_AAPL then -1 else if DownTrend_AAPL then -2 else if DownTrendJustEnded_AAPL then -3 else 0;

plot upSum_AAPL = if GetTrend_AAPL == 2 then UpTrendBarCount_AAPL else Double.NaN;
upSum_AAPL.setPaintingStrategy(PaintingStrategy.Histogram);
upSum_AAPL.SetDefaultColor(Color.GREEN);
upSum_AAPL.SetHiding(hideHisto_AAPL);
upSum_AAPL.HideBubble();
upSum_AAPL.hideTitle();

plot downSum_AAPL = if GetTrend_AAPL == -2 then DownTrendBarCount_AAPL else Double.NaN;
downSum_AAPL.setPaintingStrategy(PaintingStrategy.Histogram);
downSum_AAPL.SetDefaultColor(Color.ORANGE);
downSum_AAPL.SetHiding(hideHisto_AAPL);
downSum_AAPL.HideBubble();
downSum_AAPL.hideTitle();

AddLabel (showLabels,
      if GetTrend_AAPL == 3  then "UpTrendStart_AAPL"
 else if GetTrend_AAPL == 2  then "Up_AAPL " + UpTrendBarCount_AAPL
 else if GetTrend_AAPL == 1  then "UpTrendEnd_AAPL"
 else if GetTrend_AAPL == -1 then "DnTrendStart_AAPL"
 else if GetTrend_AAPL == -2 then "Dn_AAPL " + DownTrendBarCount_AAPL
 else if GetTrend_AAPL == -3 then "DnTrendEnd_AAPL"
 else "",

      if GetTrend_AAPL == 3  then Color.CYAN
 else if GetTrend_AAPL == 2  then Color.GREEN
 else if GetTrend_AAPL == 1  then Color.GRAY
 else if GetTrend_AAPL == -1 then Color.PINK
 else if GetTrend_AAPL == -2 then Color.ORANGE
 else if GetTrend_AAPL == -3 then Color.GRAY
 else Color.GRAY);

# -------------------------- AMD -------------------------------------------
def hi_AMD = high(symbol="AMD");
def lo_AMD = low(symbol="AMD");
def cl_AMD = close(symbol="AMD");

def NetChgAvg_AMD = MovingAverage(averageTypeRSI, cl_AMD - cl_AMD[1], lengthRSI);
def TotChgAvg_AMD = MovingAverage(averageTypeRSI, AbsValue(cl_AMD - cl_AMD[1]), lengthRSI);
def ChgRatio_AMD = if TotChgAvg_AMD != 0 then NetChgAvg_AMD / TotChgAvg_AMD else 0;
def RSI_AMD = 50 * (ChgRatio_AMD + 1);

#Stochastic Slow
def SlowK_AMD = reference StochasticFull(over_boughtSt,  over_soldSt,  KPeriod,  DPeriod,  hi_AMD,  lo_AMD,  cl_AMD,  3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullK;

#MACD Calculation
def Value_AMD = MovingAverage(averageTypeMACD, cl_AMD, fastLength) - MovingAverage(averageTypeMACD, cl_AMD, slowLength);
def Avg_AMD = MovingAverage(averageTypeMACD, Value_AMD, MACDLength);

#SCAN Variables
def UpTrend_AMD = if RSI_AMD > 50 and SlowK_AMD > 50 and Value_AMD > Avg_AMD then 1 else 0;
def DownTrend_AMD = if RSI_AMD < 50 and SlowK_AMD < 50 and Value_AMD < Avg_AMD then 1 else 0;

def bnumUp_AMD;
def bnumDown_AMD;
def closeUpTrendStart_AMD;
def closeDownTrendStart_AMD;
def UpTrendBarCount_AMD;
def DownTrendBarCount_AMD;
if UpTrend_AMD and (!UpTrend_AMD[1] or DownTrend_AMD[1]) {
    bnumUp_AMD = BarNumber();
    bnumDown_AMD = 0;
    closeUpTrendStart_AMD = cl_AMD;
    closeDownTrendStart_AMD = 0;
    UpTrendBarCount_AMD = 1;
    DownTrendBarCount_AMD = 0;
} else if UpTrend_AMD {
    bnumUp_AMD = bnumUp_AMD[1];
    bnumDown_AMD = 0;
    closeUpTrendStart_AMD = closeUpTrendStart_AMD[1];
    closeDownTrendStart_AMD = 0;
    UpTrendBarCount_AMD = UpTrendBarCount_AMD[1] + 1;
    DownTrendBarCount_AMD = 0;
} else if DownTrend_AMD and (!DownTrend_AMD[1] or UpTrend_AMD[1]) {
    bnumUp_AMD = 0;
    bnumDown_AMD = BarNumber();
    closeDownTrendStart_AMD = cl_AMD;
    closeUpTrendStart_AMD = 0;
    UpTrendBarCount_AMD = 0;
    DownTrendBarCount_AMD = 1;
} else if DownTrend_AMD {
    bnumDown_AMD = bnumDown_AMD[1];
    closeDownTrendStart_AMD = closeDownTrendStart_AMD[1];
    DownTrendBarCount_AMD = DownTrendBarCount_AMD[1] + 1;
    bnumUp_AMD = 0;
    closeUpTrendStart_AMD = 0;
    UpTrendBarCount_AMD = 0;
} else {
    bnumUp_AMD = 0;
    bnumDown_AMD = 0;
    closeUpTrendStart_AMD = 0;
    closeDownTrendStart_AMD = 0;
    UpTrendBarCount_AMD = 0;
    DownTrendBarCount_AMD = 0;
}

# The UpTrendJustStarted and DownTrendJustStarted plots can be used to find stocks that have just started a trend in either direction
def UpTrendJustStartedBool_AMD = if RSI_AMD > 50 and SlowK_AMD > 50 and Value_AMD > Avg_AMD then 1 else 0;
def DownTrendJustStartedBool_AMD = if RSI_AMD < 50 and SlowK_AMD < 50 and Value_AMD < Avg_AMD then 1 else 0;
def UpTrendJustStarted_AMD = if UpTrendJustStartedBool_AMD == 1 and UpTrendJustStartedBool_AMD[1] == 0 then 1 else 0;
def DownTrendJustStarted_AMD = if DownTrendJustStartedBool_AMD == 1 and DownTrendJustStartedBool_AMD[1] == 0 then 1 else 0;
def UpTrendJustEnded_AMD = if UpTrendJustStartedBool_AMD[1] == 1 and UpTrendJustStartedBool_AMD == 0 then 1 else 0;
def DownTrendJustEnded_AMD = if DownTrendJustStartedBool_AMD[1] == 1 and DownTrendJustStartedBool_AMD == 0 then 1 else 0;

def GetTrend_AMD = if UpTrendJustStarted_AMD then 3 else if UpTrend_AMD then 2 else if UpTrendJustEnded_AMD then 1 else if DownTrendJustStarted_AMD then -1 else if DownTrend_AMD then -2 else if DownTrendJustEnded_AMD then -3 else 0;

plot upSum_AMD = if GetTrend_AMD == 2 then UpTrendBarCount_AMD else Double.NaN;
upSum_AMD.setPaintingStrategy(PaintingStrategy.Histogram);
upSum_AMD.SetDefaultColor(Color.GREEN);
upSum_AMD.SetHiding(hideHisto_AMD);
upSum_AMD.HideBubble();
upSum_AMD.hideTitle();

plot downSum_AMD = if GetTrend_AMD == -2 then DownTrendBarCount_AMD else Double.NaN;
downSum_AMD.setPaintingStrategy(PaintingStrategy.Histogram);
downSum_AMD.SetDefaultColor(Color.ORANGE);
downSum_AMD.SetHiding(hideHisto_AMD);
downSum_AMD.HideBubble();
downSum_AMD.hideTitle();

AddLabel (showLabels,
      if GetTrend_AMD == 3  then "UpTrendStart_AMD"
 else if GetTrend_AMD == 2  then "Up_AMD " + UpTrendBarCount_AMD
 else if GetTrend_AMD == 1  then "UpTrendEnd_AMD"
 else if GetTrend_AMD == -1 then "DnTrendStart_AMD"
 else if GetTrend_AMD == -2 then "Dn_AMD " + DownTrendBarCount_AMD
 else if GetTrend_AMD == -3 then "DnTrendEnd_AMD"
 else "",

      if GetTrend_AMD == 3  then Color.CYAN
 else if GetTrend_AMD == 2  then Color.GREEN
 else if GetTrend_AMD == 1  then Color.GRAY
 else if GetTrend_AMD == -1 then Color.PINK
 else if GetTrend_AMD == -2 then Color.ORANGE
 else if GetTrend_AMD == -3 then Color.GRAY
 else Color.GRAY);

# -------------------------- AMZN -------------------------------------------
def hi_AMZN = high(symbol="AMZN");
def lo_AMZN = low(symbol="AMZN");
def cl_AMZN = close(symbol="AMZN");

def NetChgAvg_AMZN = MovingAverage(averageTypeRSI, cl_AMZN - cl_AMZN[1], lengthRSI);
def TotChgAvg_AMZN = MovingAverage(averageTypeRSI, AbsValue(cl_AMZN - cl_AMZN[1]), lengthRSI);
def ChgRatio_AMZN = if TotChgAvg_AMZN != 0 then NetChgAvg_AMZN / TotChgAvg_AMZN else 0;
def RSI_AMZN = 50 * (ChgRatio_AMZN + 1);

#Stochastic Slow
def SlowK_AMZN = reference StochasticFull(over_boughtSt,  over_soldSt,  KPeriod,  DPeriod,  hi_AMZN,  lo_AMZN,  cl_AMZN,  3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullK;

#MACD Calculation
def Value_AMZN = MovingAverage(averageTypeMACD, cl_AMZN, fastLength) - MovingAverage(averageTypeMACD, cl_AMZN, slowLength);
def Avg_AMZN = MovingAverage(averageTypeMACD, Value_AMZN, MACDLength);

#SCAN Variables
def UpTrend_AMZN = if RSI_AMZN > 50 and SlowK_AMZN > 50 and Value_AMZN > Avg_AMZN then 1 else 0;
def DownTrend_AMZN = if RSI_AMZN < 50 and SlowK_AMZN < 50 and Value_AMZN < Avg_AMZN then 1 else 0;

def bnumUp_AMZN;
def bnumDown_AMZN;
def closeUpTrendStart_AMZN;
def closeDownTrendStart_AMZN;
def UpTrendBarCount_AMZN;
def DownTrendBarCount_AMZN;
if UpTrend_AMZN and (!UpTrend_AMZN[1] or DownTrend_AMZN[1]) {
    bnumUp_AMZN = BarNumber();
    bnumDown_AMZN = 0;
    closeUpTrendStart_AMZN = cl_AMZN;
    closeDownTrendStart_AMZN = 0;
    UpTrendBarCount_AMZN = 1;
    DownTrendBarCount_AMZN = 0;
} else if UpTrend_AMZN {
    bnumUp_AMZN = bnumUp_AMZN[1];
    bnumDown_AMZN = 0;
    closeUpTrendStart_AMZN = closeUpTrendStart_AMZN[1];
    closeDownTrendStart_AMZN = 0;
    UpTrendBarCount_AMZN = UpTrendBarCount_AMZN[1] + 1;
    DownTrendBarCount_AMZN = 0;
} else if DownTrend_AMZN and (!DownTrend_AMZN[1] or UpTrend_AMZN[1]) {
    bnumUp_AMZN = 0;
    bnumDown_AMZN = BarNumber();
    closeDownTrendStart_AMZN = cl_AMZN;
    closeUpTrendStart_AMZN = 0;
    UpTrendBarCount_AMZN = 0;
    DownTrendBarCount_AMZN = 1;
} else if DownTrend_AMZN {
    bnumDown_AMZN = bnumDown_AMZN[1];
    closeDownTrendStart_AMZN = closeDownTrendStart_AMZN[1];
    DownTrendBarCount_AMZN = DownTrendBarCount_AMZN[1] + 1;
    bnumUp_AMZN = 0;
    closeUpTrendStart_AMZN = 0;
    UpTrendBarCount_AMZN = 0;
} else {
    bnumUp_AMZN = 0;
    bnumDown_AMZN = 0;
    closeUpTrendStart_AMZN = 0;
    closeDownTrendStart_AMZN = 0;
    UpTrendBarCount_AMZN = 0;
    DownTrendBarCount_AMZN = 0;
}

# The UpTrendJustStarted and DownTrendJustStarted plots can be used to find stocks that have just started a trend in either direction
def UpTrendJustStartedBool_AMZN = if RSI_AMZN > 50 and SlowK_AMZN > 50 and Value_AMZN > Avg_AMZN then 1 else 0;
def DownTrendJustStartedBool_AMZN = if RSI_AMZN < 50 and SlowK_AMZN < 50 and Value_AMZN < Avg_AMZN then 1 else 0;
def UpTrendJustStarted_AMZN = if UpTrendJustStartedBool_AMZN == 1 and UpTrendJustStartedBool_AMZN[1] == 0 then 1 else 0;
def DownTrendJustStarted_AMZN = if DownTrendJustStartedBool_AMZN == 1 and DownTrendJustStartedBool_AMZN[1] == 0 then 1 else 0;
def UpTrendJustEnded_AMZN = if UpTrendJustStartedBool_AMZN[1] == 1 and UpTrendJustStartedBool_AMZN == 0 then 1 else 0;
def DownTrendJustEnded_AMZN = if DownTrendJustStartedBool_AMZN[1] == 1 and DownTrendJustStartedBool_AMZN == 0 then 1 else 0;

def GetTrend_AMZN = if UpTrendJustStarted_AMZN then 3 else if UpTrend_AMZN then 2 else if UpTrendJustEnded_AMZN then 1 else if DownTrendJustStarted_AMZN then -1 else if DownTrend_AMZN then -2 else if DownTrendJustEnded_AMZN then -3 else 0;

plot upSum_AMZN = if GetTrend_AMZN == 2 then UpTrendBarCount_AMZN else Double.NaN;
upSum_AMZN.setPaintingStrategy(PaintingStrategy.Histogram);
upSum_AMZN.SetDefaultColor(Color.GREEN);
upSum_AMZN.SetHiding(hideHisto_AMZN);
upSum_AMZN.HideBubble();
upSum_AMZN.hideTitle();

plot downSum_AMZN = if GetTrend_AMZN == -2 then DownTrendBarCount_AMZN else Double.NaN;
downSum_AMZN.setPaintingStrategy(PaintingStrategy.Histogram);
downSum_AMZN.SetDefaultColor(Color.ORANGE);
downSum_AMZN.SetHiding(hideHisto_AMZN);
downSum_AMZN.HideBubble();
downSum_AMZN.hideTitle();

AddLabel (showLabels,
      if GetTrend_AMZN == 3  then "UpTrendStart_AMZN"
 else if GetTrend_AMZN == 2  then "Up_AMZN " + UpTrendBarCount_AMZN
 else if GetTrend_AMZN == 1  then "UpTrendEnd_AMZN"
 else if GetTrend_AMZN == -1 then "DnTrendStart_AMZN"
 else if GetTrend_AMZN == -2 then "Dn_AMZN " + DownTrendBarCount_AMZN
 else if GetTrend_AMZN == -3 then "DnTrendEnd_AMZN"
 else "",

      if GetTrend_AMZN == 3  then Color.CYAN
 else if GetTrend_AMZN == 2  then Color.GREEN
 else if GetTrend_AMZN == 1  then Color.GRAY
 else if GetTrend_AMZN == -1 then Color.PINK
 else if GetTrend_AMZN == -2 then Color.ORANGE
 else if GetTrend_AMZN == -3 then Color.GRAY
 else Color.GRAY);

# -------------------------- TSLA -------------------------------------------
def hi_TSLA = high(symbol="TSLA");
def lo_TSLA = low(symbol="TSLA");
def cl_TSLA = close(symbol="TSLA");

def NetChgAvg_TSLA = MovingAverage(averageTypeRSI, cl_TSLA - cl_TSLA[1], lengthRSI);
def TotChgAvg_TSLA = MovingAverage(averageTypeRSI, AbsValue(cl_TSLA - cl_TSLA[1]), lengthRSI);
def ChgRatio_TSLA = if TotChgAvg_TSLA != 0 then NetChgAvg_TSLA / TotChgAvg_TSLA else 0;
def RSI_TSLA = 50 * (ChgRatio_TSLA + 1);

#Stochastic Slow
def SlowK_TSLA = reference StochasticFull(over_boughtSt,  over_soldSt,  KPeriod,  DPeriod,  hi_TSLA,  lo_TSLA,  cl_TSLA,  3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullK;

#MACD Calculation
def Value_TSLA = MovingAverage(averageTypeMACD, cl_TSLA, fastLength) - MovingAverage(averageTypeMACD, cl_TSLA, slowLength);
def Avg_TSLA = MovingAverage(averageTypeMACD, Value_TSLA, MACDLength);

#SCAN Variables
def UpTrend_TSLA = if RSI_TSLA > 50 and SlowK_TSLA > 50 and Value_TSLA > Avg_TSLA then 1 else 0;
def DownTrend_TSLA = if RSI_TSLA < 50 and SlowK_TSLA < 50 and Value_TSLA < Avg_TSLA then 1 else 0;

def bnumUp_TSLA;
def bnumDown_TSLA;
def closeUpTrendStart_TSLA;
def closeDownTrendStart_TSLA;
def UpTrendBarCount_TSLA;
def DownTrendBarCount_TSLA;
if UpTrend_TSLA and (!UpTrend_TSLA[1] or DownTrend_TSLA[1]) {
    bnumUp_TSLA = BarNumber();
    bnumDown_TSLA = 0;
    closeUpTrendStart_TSLA = cl_TSLA;
    closeDownTrendStart_TSLA = 0;
    UpTrendBarCount_TSLA = 1;
    DownTrendBarCount_TSLA = 0;
} else if UpTrend_TSLA {
    bnumUp_TSLA = bnumUp_TSLA[1];
    bnumDown_TSLA = 0;
    closeUpTrendStart_TSLA = closeUpTrendStart_TSLA[1];
    closeDownTrendStart_TSLA = 0;
    UpTrendBarCount_TSLA = UpTrendBarCount_TSLA[1] + 1;
    DownTrendBarCount_TSLA = 0;
} else if DownTrend_TSLA and (!DownTrend_TSLA[1] or UpTrend_TSLA[1]) {
    bnumUp_TSLA = 0;
    bnumDown_TSLA = BarNumber();
    closeDownTrendStart_TSLA = cl_TSLA;
    closeUpTrendStart_TSLA = 0;
    UpTrendBarCount_TSLA = 0;
    DownTrendBarCount_TSLA = 1;
} else if DownTrend_TSLA {
    bnumDown_TSLA = bnumDown_TSLA[1];
    closeDownTrendStart_TSLA = closeDownTrendStart_TSLA[1];
    DownTrendBarCount_TSLA = DownTrendBarCount_TSLA[1] + 1;
    bnumUp_TSLA = 0;
    closeUpTrendStart_TSLA = 0;
    UpTrendBarCount_TSLA = 0;
} else {
    bnumUp_TSLA = 0;
    bnumDown_TSLA = 0;
    closeUpTrendStart_TSLA = 0;
    closeDownTrendStart_TSLA = 0;
    UpTrendBarCount_TSLA = 0;
    DownTrendBarCount_TSLA = 0;
}

# The UpTrendJustStarted and DownTrendJustStarted plots can be used to find stocks that have just started a trend in either direction
def UpTrendJustStartedBool_TSLA = if RSI_TSLA > 50 and SlowK_TSLA > 50 and Value_TSLA > Avg_TSLA then 1 else 0;
def DownTrendJustStartedBool_TSLA = if RSI_TSLA < 50 and SlowK_TSLA < 50 and Value_TSLA < Avg_TSLA then 1 else 0;
def UpTrendJustStarted_TSLA = if UpTrendJustStartedBool_TSLA == 1 and UpTrendJustStartedBool_TSLA[1] == 0 then 1 else 0;
def DownTrendJustStarted_TSLA = if DownTrendJustStartedBool_TSLA == 1 and DownTrendJustStartedBool_TSLA[1] == 0 then 1 else 0;
def UpTrendJustEnded_TSLA = if UpTrendJustStartedBool_TSLA[1] == 1 and UpTrendJustStartedBool_TSLA == 0 then 1 else 0;
def DownTrendJustEnded_TSLA = if DownTrendJustStartedBool_TSLA[1] == 1 and DownTrendJustStartedBool_TSLA == 0 then 1 else 0;

def GetTrend_TSLA = if UpTrendJustStarted_TSLA then 3 else if UpTrend_TSLA then 2 else if UpTrendJustEnded_TSLA then 1 else if DownTrendJustStarted_TSLA then -1 else if DownTrend_TSLA then -2 else if DownTrendJustEnded_TSLA then -3 else 0;

plot upSum_TSLA = if GetTrend_TSLA == 2 then UpTrendBarCount_TSLA else Double.NaN;
upSum_TSLA.setPaintingStrategy(PaintingStrategy.Histogram);
upSum_TSLA.SetDefaultColor(Color.GREEN);
upSum_TSLA.SetHiding(hideHisto_TSLA);
upSum_TSLA.HideBubble();
upSum_TSLA.hideTitle();

plot downSum_TSLA = if GetTrend_TSLA == -2 then DownTrendBarCount_TSLA else Double.NaN;
downSum_TSLA.setPaintingStrategy(PaintingStrategy.Histogram);
downSum_TSLA.SetDefaultColor(Color.ORANGE);
downSum_TSLA.SetHiding(hideHisto_TSLA);
downSum_TSLA.HideBubble();
downSum_TSLA.hideTitle();

AddLabel (showLabels,
      if GetTrend_TSLA == 3  then "UpTrendStart_TSLA"
 else if GetTrend_TSLA == 2  then "Up_TSLA " + UpTrendBarCount_TSLA
 else if GetTrend_TSLA == 1  then "UpTrendEnd_TSLA"
 else if GetTrend_TSLA == -1 then "DnTrendStart_TSLA"
 else if GetTrend_TSLA == -2 then "Dn_TSLA " + DownTrendBarCount_TSLA
 else if GetTrend_TSLA == -3 then "DnTrendEnd_TSLA"
 else "",

      if GetTrend_TSLA == 3  then Color.CYAN
 else if GetTrend_TSLA == 2  then Color.GREEN
 else if GetTrend_TSLA == 1  then Color.GRAY
 else if GetTrend_TSLA == -1 then Color.PINK
 else if GetTrend_TSLA == -2 then Color.ORANGE
 else if GetTrend_TSLA == -3 then Color.GRAY
 else Color.GRAY);

# -------------------------- Combined Trends -------------------------------------------
plot upSum_ALL = if (GetTrend_AAPL == 2) and
                    (GetTrend_AMD  == 2) and
                    (GetTrend_AMZN == 2) and
                    (GetTrend_TSLA == 2)
               then (UpTrendBarCount_AAPL +
                     UpTrendBarCount_AMD  +
                     UpTrendBarCount_AMZN +
                     UpTrendBarCount_TSLA) else Double.NaN;
upSum_ALL.setPaintingStrategy(PaintingStrategy.Histogram);
upSum_ALL.SetDefaultColor(Color.GREEN);
upSum_ALL.SetLineWeight(1);
upSum_ALL.SetHiding();
upSum_ALL.HideBubble();
#upSum_ALL.hideTitle();

plot downSum_ALL = if (GetTrend_AAPL == -2) and
                      (GetTrend_AMD  == -2) and
                      (GetTrend_AMZN == -2) and
                      (GetTrend_TSLA == -2)
                 then (DownTrendBarCount_AAPL +
                       DownTrendBarCount_AMD  +
                       DownTrendBarCount_AMZN +
                       DownTrendBarCount_TSLA) else Double.NaN;
downSum_ALL.setPaintingStrategy(PaintingStrategy.Histogram);
downSum_ALL.SetDefaultColor(Color.ORANGE);
downSum_ALL.SetLineWeight(1);
downSum_ALL.SetHiding();
downSum_ALL.HideBubble();
#downSum_ALL.hideTitle();

AddLabel (yes,
      if (GetTrend_AAPL == 3) and
         (GetTrend_AMD  == 3) and
         (GetTrend_AMZN == 3) and
         (GetTrend_TSLA == 3)
      then "UpTrendStart"
 else if (GetTrend_AAPL == 2) and
         (GetTrend_AMD  == 2) and
         (GetTrend_AMZN == 2) and
         (GetTrend_TSLA == 2)
      then "Up " + (UpTrendBarCount_AAPL +
                    UpTrendBarCount_AMD  +
                    UpTrendBarCount_AMZN +
                    UpTrendBarCount_TSLA)
 else if (GetTrend_AAPL == 1) and
         (GetTrend_AMD  == 1) and
         (GetTrend_AMZN == 1) and
         (GetTrend_TSLA == 1)
      then "UpTrendEnd"
 else if (GetTrend_AAPL == -1) and
         (GetTrend_AMD  == -1) and
         (GetTrend_AMZN == -1) and
         (GetTrend_TSLA == -1)
      then "DnTrendStart"
 else if (GetTrend_AAPL == -2) and
         (GetTrend_AMD  == -2) and
         (GetTrend_AMZN == -2) and
         (GetTrend_TSLA == -2)
      then "Dn " + (DownTrendBarCount_AAPL +
                    DownTrendBarCount_AMD  +
                    DownTrendBarCount_AMZN +
                    DownTrendBarCount_TSLA)
 else if (GetTrend_AAPL == -3) and
         (GetTrend_AMD  == -3) and
         (GetTrend_AMZN == -3) and
         (GetTrend_TSLA == -3)
      then "DnTrendEnd"
 else "NoTrend",

      if (GetTrend_AAPL == 3) and
         (GetTrend_AMD  == 3) and
         (GetTrend_AMZN == 3) and
         (GetTrend_TSLA == 3)
      then Color.CYAN
 else if (GetTrend_AAPL == 2) and
         (GetTrend_AMD  == 2) and
         (GetTrend_AMZN == 2) and
         (GetTrend_TSLA == 2)
      then Color.GREEN
 else if (GetTrend_AAPL == 1) and
         (GetTrend_AMD  == 1) and
         (GetTrend_AMZN == 1) and
         (GetTrend_TSLA == 1)
      then Color.GRAY
 else if (GetTrend_AAPL == -1) and
         (GetTrend_AMD  == -1) and
         (GetTrend_AMZN == -1) and
         (GetTrend_TSLA == -1)
      then Color.PINK
 else if (GetTrend_AAPL == -2) and
         (GetTrend_AMD  == -2) and
         (GetTrend_AMZN == -2) and
         (GetTrend_TSLA == -2)
      then Color.ORANGE
 else if (GetTrend_AAPL == -3) and
         (GetTrend_AMD  == -3) and
         (GetTrend_AMZN == -3) and
         (GetTrend_TSLA == -3)
      then Color.GRAY
 else Color.GRAY);
 
Please help me with this code/thought...
https://usethinkscript.com/threads/rsm-indicator-for-thinkorswim.5407/#post-50775

While looking at a SPY chart, I would like to sum up and plot the UpTrendBarCount from 2 symbols (TSLA and AAPL for example) using the below script. Similarly, sum up and plot the DownTrendBarCount.

Code:
declare lower;
def hi = high;
def lo = low;
def cl = close;

def lengthRSI = 7;
def averageTypeRSI = AverageType.EXPONENTIAL;
def NetChgAvg = MovingAverage(averageTypeRSI, cl - cl[1], lengthRSI);
def TotChgAvg = MovingAverage(averageTypeRSI, AbsValue(cl - cl[1]), lengthRSI);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);

#Stochastic Slow
def over_boughtSt = 80;
def over_soldSt = 20;
def KPeriod = 14;
def DPeriod = 3;
def averageTypeStoch = AverageType.WILDERS;
def SlowK = reference StochasticFull(over_boughtSt,  over_soldSt,  KPeriod,  DPeriod,  hi,  lo,  cl,  3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullK;
def SlowD = reference StochasticFull(over_boughtSt,  over_soldSt,  KPeriod,  DPeriod,  hi,  lo,  cl,  3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullD;

#MACD Calculation
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
def averageTypeMACD = AverageType.WEIGHTED;
def Value = MovingAverage(averageTypeMACD, cl, fastLength) - MovingAverage(averageTypeMACD, cl, slowLength);
def Avg = MovingAverage(averageTypeMACD, Value, MACDLength);
def Diff = Value - Avg;

#SCAN Variables
def UpTrend = if RSI > 50 and SlowK > 50 and Value > Avg then 1 else 0;
def DownTrend = if RSI < 50 and SlowK < 50 and Value < Avg then 1 else 0;

def bnumUp;
def bnumDown;
def closeUpTrendStart;
def closeDownTrendStart;
def UpTrendBarCount;
def DownTrendBarCount;
if UpTrend and (!UpTrend[1] or DownTrend[1]) {
    bnumUp = BarNumber();
    bnumDown = 0;
    closeUpTrendStart = cl;
    closeDownTrendStart = 0;
    UpTrendBarCount = 1;
    DownTrendBarCount = 0;
} else if UpTrend {
    bnumUp = bnumUp[1];
    bnumDown = 0;
    closeUpTrendStart = closeUpTrendStart[1];
    closeDownTrendStart = 0;
    UpTrendBarCount = UpTrendBarCount[1] + 1;
    DownTrendBarCount = 0;
} else if DownTrend and (!DownTrend[1] or UpTrend[1]) {
    bnumUp = 0;
    bnumDown = BarNumber();
    closeDownTrendStart = cl;
    closeUpTrendStart = 0;
    UpTrendBarCount = 0;
    DownTrendBarCount = 1;
} else if DownTrend {
    bnumDown = bnumDown[1];
    closeDownTrendStart = closeDownTrendStart[1];
    DownTrendBarCount = DownTrendBarCount[1] + 1;
    bnumUp = 0;
    closeUpTrendStart = 0;
    UpTrendBarCount = 0;
} else {
    bnumUp = 0;
    bnumDown = 0;
    closeUpTrendStart = 0;
    closeDownTrendStart = 0;
    UpTrendBarCount = 0;
    DownTrendBarCount = 0;
}

# The UpTrendJustStarted and DownTrendJustStarted plots can be used to find stocks that have just started a trend in either direction
def UpTrendJustStartedBool = if RSI > 50 and SlowK > 50 and Value > Avg then 1 else 0;
def DownTrendJustStartedBool = if RSI < 50 and SlowK < 50 and Value < Avg then 1 else 0;
def UpTrendJustStarted = if UpTrendJustStartedBool == 1 and UpTrendJustStartedBool[1] == 0 then 1 else 0;
def DownTrendJustStarted = if DownTrendJustStartedBool == 1 and DownTrendJustStartedBool[1] == 0 then 1 else 0;
def UpTrendJustEnded = if UpTrendJustStartedBool[1] == 1 and UpTrendJustStartedBool == 0 then 1 else 0;
def DownTrendJustEnded = if DownTrendJustStartedBool[1] == 1 and DownTrendJustStartedBool == 0 then 1 else 0;

def GetTrend = if UptrendJustStarted then 3 else if Uptrend then 2 else if UpTrendJustEnded then 1 else if DownTrendJustStarted then -1 else if DownTrend then -2 else if DownTrendJustEnded then -3 else 0;

I want to append the following lines to sum up/plot the UpTrendBarCount and DownTrendBarCount of the 2 symbols:

Code:
plot upSum = if GetTrend == 2  then UpTrendBarCount(symbol="TSLA") + UpTrendBarCount(symbol="AAPL");
plot downSum = if GetTrend == -2 then DownTrendBarCount(symbol="TSLA") + DownTrendBarCount(symbol="AAPL");



what does 'plot the count' mean?
what do you want to see?
.. a lower chart, an increasing line, that represents the count?
.. bubbles on every bar , with a count? a bubble on the last bar on chart? a bubble on last bar of each day? a label that represents something?
..do you want to count during the whole chart? or during a day and resetting each day?


why does post 3 code have a link to a different page ?
is it the same code as mentioned in the post1 link?


=====================================
weird issue with trying to use stochfull()

in the Stochasticslow code,
did you add an if then to pick the type?
i'm guessing it got added automatically..
https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/R-S/StochasticSlow
https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/R-S/StochasticFull

instead of referencing stochfull in stochslow code, just use the stochfull formulas



in stochfull , the type is the 9th parameter

if i remove the 3 as the 8th parameter, the code doesn't auto correct...
if i leave the 3 and remove the type, it doesn't auto correct...
if i try to replace the if then for type, it adds another if then formula for type...?


Code:
def averageTypeStoch = AverageType.WILDERS;
def SlowK = reference StochasticFull(over_boughtSt,  over_soldSt,  KPeriod,  DPeriod,  hi,  lo,  cl,  3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullK;


i tried to replace the if then with averageTypeStoch,
and the editor keeps changing it back, adding in another if then, with this message at the bottom
the script has been automatically updated for code compatability....

i look at the parameters in StochasticFull( ) and you have a 3 for the 8th parameter, instead of a type.
so when i tried to correct the formula, the editor tried to fix it automatically, but did it wrong. it didn't remove the 3, it added an if then.


----------------
the following assumes you wanted an if then, and investigates how to use it,

why not just used the desired value type, that is defined by a def? you didn't use an input, so the type isn't going to change. you set wilders as the type, but in stochfull( ) you aren't using it ??
did you test and verify which type a number 1 is equal to ?

why would you have it as,
. if exponential is picked then simple else exponential ?

averageTypeStoch is not a number, although it can return the index number of the choice, from a list of the choices.

i made a test code to see what values come from the choices.
the numbers seem to match the sequence of choices in the input drop down list of choices.

Code:
input Type = AverageType.SIMPLE;
def u = 1 * type;
addlabel(1, type + "  " + u, color.yellow);

numbers from test code
0 = simple
1 = exp
2 = weighted
3 = wild
4 = hull


list of types in studies settings input drop down list (the same)
simple
exp
wt
wild
hull


https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/AverageType
list on web page (diff)
exp
hull
simple
wt
wild
==================================

added in code for stochfull. change price formula names to o,h,l,c

------------------------

if you want to reuse a section of code many times, you can change it into a script , a sub routine within the study
https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/script

will post something in a bit
 
Please help me with this code/thought...
https://usethinkscript.com/threads/rsm-indicator-for-thinkorswim.5407/#post-50775

While looking at a SPY chart, I would like to sum up and plot the UpTrendBarCount from 2 symbols (TSLA and AAPL for example) using the below script. Similarly, sum up and plot the DownTrendBarCount.

Code:
declare lower;
def hi = high;
def lo = low;
def cl = close;

def lengthRSI = 7;
def averageTypeRSI = AverageType.EXPONENTIAL;
def NetChgAvg = MovingAverage(averageTypeRSI, cl - cl[1], lengthRSI);
def TotChgAvg = MovingAverage(averageTypeRSI, AbsValue(cl - cl[1]), lengthRSI);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);

#Stochastic Slow
def over_boughtSt = 80;
def over_soldSt = 20;
def KPeriod = 14;
def DPeriod = 3;
def averageTypeStoch = AverageType.WILDERS;
def SlowK = reference StochasticFull(over_boughtSt,  over_soldSt,  KPeriod,  DPeriod,  hi,  lo,  cl,  3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullK;
def SlowD = reference StochasticFull(over_boughtSt,  over_soldSt,  KPeriod,  DPeriod,  hi,  lo,  cl,  3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullD;

#MACD Calculation
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
def averageTypeMACD = AverageType.WEIGHTED;
def Value = MovingAverage(averageTypeMACD, cl, fastLength) - MovingAverage(averageTypeMACD, cl, slowLength);
def Avg = MovingAverage(averageTypeMACD, Value, MACDLength);
def Diff = Value - Avg;

#SCAN Variables
def UpTrend = if RSI > 50 and SlowK > 50 and Value > Avg then 1 else 0;
def DownTrend = if RSI < 50 and SlowK < 50 and Value < Avg then 1 else 0;

def bnumUp;
def bnumDown;
def closeUpTrendStart;
def closeDownTrendStart;
def UpTrendBarCount;
def DownTrendBarCount;
if UpTrend and (!UpTrend[1] or DownTrend[1]) {
    bnumUp = BarNumber();
    bnumDown = 0;
    closeUpTrendStart = cl;
    closeDownTrendStart = 0;
    UpTrendBarCount = 1;
    DownTrendBarCount = 0;
} else if UpTrend {
    bnumUp = bnumUp[1];
    bnumDown = 0;
    closeUpTrendStart = closeUpTrendStart[1];
    closeDownTrendStart = 0;
    UpTrendBarCount = UpTrendBarCount[1] + 1;
    DownTrendBarCount = 0;
} else if DownTrend and (!DownTrend[1] or UpTrend[1]) {
    bnumUp = 0;
    bnumDown = BarNumber();
    closeDownTrendStart = cl;
    closeUpTrendStart = 0;
    UpTrendBarCount = 0;
    DownTrendBarCount = 1;
} else if DownTrend {
    bnumDown = bnumDown[1];
    closeDownTrendStart = closeDownTrendStart[1];
    DownTrendBarCount = DownTrendBarCount[1] + 1;
    bnumUp = 0;
    closeUpTrendStart = 0;
    UpTrendBarCount = 0;
} else {
    bnumUp = 0;
    bnumDown = 0;
    closeUpTrendStart = 0;
    closeDownTrendStart = 0;
    UpTrendBarCount = 0;
    DownTrendBarCount = 0;
}

# The UpTrendJustStarted and DownTrendJustStarted plots can be used to find stocks that have just started a trend in either direction
def UpTrendJustStartedBool = if RSI > 50 and SlowK > 50 and Value > Avg then 1 else 0;
def DownTrendJustStartedBool = if RSI < 50 and SlowK < 50 and Value < Avg then 1 else 0;
def UpTrendJustStarted = if UpTrendJustStartedBool == 1 and UpTrendJustStartedBool[1] == 0 then 1 else 0;
def DownTrendJustStarted = if DownTrendJustStartedBool == 1 and DownTrendJustStartedBool[1] == 0 then 1 else 0;
def UpTrendJustEnded = if UpTrendJustStartedBool[1] == 1 and UpTrendJustStartedBool == 0 then 1 else 0;
def DownTrendJustEnded = if DownTrendJustStartedBool[1] == 1 and DownTrendJustStartedBool == 0 then 1 else 0;

def GetTrend = if UptrendJustStarted then 3 else if Uptrend then 2 else if UpTrendJustEnded then 1 else if DownTrendJustStarted then -1 else if DownTrend then -2 else if DownTrendJustEnded then -3 else 0;

I want to append the following lines to sum up/plot the UpTrendBarCount and DownTrendBarCount of the 2 symbols:

Code:
plot upSum = if GetTrend == 2  then UpTrendBarCount(symbol="TSLA") + UpTrendBarCount(symbol="AAPL");
plot downSum = if GetTrend == -2 then DownTrendBarCount(symbol="TSLA") + DownTrendBarCount(symbol="AAPL");


this is a test code - a lower , version 00c

it counts up and down signals, of te symbol on the chart, and displays the count in bubbles, at the highest count.

i changed stochslow code to include stochfull code, so it doesn't reference another study
i standardized the price variables , open, close, high, low,... ( this is prepping for the next version, using a script)


Code:
# sum_plot_counts_for_2stocks_rsm_00c

#https://usethinkscript.com/threads/sum-and-plot-barcount.14386/#post-119602
# Sum and plot barcount
# Shaco  2/7

# Please help me with this code/thought...
# ref1 - in post1 - RSM Indicator
# https://usethinkscript.com/threads/rsm-indicator-for-thinkorswim.5407/#post-50775

# While looking at a SPY chart,
#  sum up and plot the UpTrendBarCount from 2 symbols (TSLA and AAPL for example)
#  sum up and plot the DownTrendBarCount.

# ref2 - in post3 code- RSI + MACD Confluence
# https://usethinkscript.com/threads/rsi-macd-confluence-indicator-for-thinkorswim.505/page-3

#AddLabel(1, "-");

declare lower;

def o = open;
def h = high;
def l = low;
def c = close;

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

# rsm_counts
#----------------------------
# RSI
#input length = 14;
#input over_Bought = 70;
#input over_Sold = 30;
def len_RSI = 7;
#input averageType = AverageType.WILDERS;
def averageTypeRSI = AverageType.EXPONENTIAL;
def NetChgAvg = MovingAverage(averageTypeRSI, c - c[1], len_RSI);
def TotChgAvg = MovingAverage(averageTypeRSI, AbsValue(c - c[1]), len_RSI);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);


#----------------------------
# Stochastic Slow
def over_boughtSt = 80;
def over_soldSt = 20;
#input KPeriod = 10;
#input DPeriod = 10;
def KPeriod = 14;
def DPeriod = 3;
#input averageType = AverageType.SIMPLE;
def averageTypeStoch = AverageType.WILDERS;
#plot SlowK = reference StochasticFull(over_bought,over_sold,KPeriod,DPeriod,priceH,priceL,priceC,3,averageType).FullK;
#plot SlowD = reference StochasticFull(over_bought,over_sold,KPeriod,DPeriod,priceH,priceL,priceC,3,averageType).FullD;

#def SlowK = reference StochasticFull(over_boughtSt,  over_soldSt,  KPeriod,  DPeriod,  hi,  lo,  cl,  3, averageTypeStoch).FullK;
#def SlowD = reference StochasticFull(over_boughtSt,  over_soldSt,  KPeriod,  DPeriod,  hi,  lo,  cl,  3, averageTypeStoch).FullD;

#/////////////////////////
# stochfull
#input over_bought = 80;
#input over_sold = 20;
#input priceH = high;
#input priceL = low;
#input priceC = close;
input slowing_period = 3;
input averageType = AverageType.SIMPLE;
#input showBreakoutSignals = {default "No", "On FullK", "On FullD", "On FullK & FullD"};
def lowest_k = Lowest(l, KPeriod);
def stc1 = c - lowest_k;
def stc2 = Highest(h, KPeriod) - lowest_k;
def FastK = if stc2 != 0 then stc1 / stc2 * 100 else 0;
def FullK = MovingAverage(averageType, FastK, slowing_period);
def FullD = MovingAverage(averageType, FullK, DPeriod);
#/////////////////////////

#def SlowK = reference StochasticFull(over_boughtSt,   over_soldSt,   KPeriod,   DPeriod,   hi,   lo,   cl,   3).FullK;
#def SlowD = reference StochasticFull(over_boughtSt,   over_soldSt,   KPeriod,   DPeriod,   hi,   lo,   cl,  3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullD;
def SlowK = fullk;
def SlowD = fulld;

# test code for type numbers
#input Type = AverageType.SIMPLE;
#def u = 1 * Type;
#AddLabel(1, Type + "  " + u, Color.YELLOW);
# 0 = simple
# 1 = exp
# 2 = weighted
# 3 = wild
# 4 = hull



#----------------------------
# MACD Calculation
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
def averageTypeMACD = AverageType.WEIGHTED;
def Value = MovingAverage(averageTypeMACD, c, fastLength) - MovingAverage(averageTypeMACD, c, slowLength);
def Avg = MovingAverage(averageTypeMACD, Value, MACDLength);
def Diff = Value - Avg;

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

# SCAN Variables
def UpTrend = if RSI > 50 and SlowK > 50 and Value > Avg then 1 else 0;
def DownTrend = if RSI < 50 and SlowK < 50 and Value < Avg then 1 else 0;

def bnumUp;
def bnumDown;
def closeUpTrendStart;
def closeDownTrendStart;
def UpTrendBarCount;
def DownTrendBarCount;
if UpTrend and (!UpTrend[1] or DownTrend[1]) {
    bnumUp = BarNumber();
    bnumDown = 0;
    closeUpTrendStart = c;
    closeDownTrendStart = 0;
    UpTrendBarCount = 1;
    DownTrendBarCount = 0;
} else if UpTrend {
    bnumUp = bnumUp[1];
    bnumDown = 0;
    closeUpTrendStart = closeUpTrendStart[1];
    closeDownTrendStart = 0;
    UpTrendBarCount = UpTrendBarCount[1] + 1;
    DownTrendBarCount = 0;
} else if DownTrend and (!DownTrend[1] or UpTrend[1]) {
    bnumUp = 0;
    bnumDown = BarNumber();
    closeDownTrendStart = c;
    closeUpTrendStart = 0;
    UpTrendBarCount = 0;
    DownTrendBarCount = 1;
} else if DownTrend {
    bnumDown = bnumDown[1];
    closeDownTrendStart = closeDownTrendStart[1];
    DownTrendBarCount = DownTrendBarCount[1] + 1;
    bnumUp = 0;
    closeUpTrendStart = 0;
    UpTrendBarCount = 0;
} else {
    bnumUp = 0;
    bnumDown = 0;
    closeUpTrendStart = 0;
    closeDownTrendStart = 0;
    UpTrendBarCount = 0;
    DownTrendBarCount = 0;
}

# The UpTrendJustStarted and DownTrendJustStarted plots can be used to find stocks that have just started a trend in either direction
def UpTrendJustStartedBool = if RSI > 50 and SlowK > 50 and Value > Avg then 1 else 0;
def DownTrendJustStartedBool = if RSI < 50 and SlowK < 50 and Value < Avg then 1 else 0;
def UpTrendJustStarted = if UpTrendJustStartedBool == 1 and UpTrendJustStartedBool[1] == 0 then 1 else 0;
def DownTrendJustStarted = if DownTrendJustStartedBool == 1 and DownTrendJustStartedBool[1] == 0 then 1 else 0;
def UpTrendJustEnded = if UpTrendJustStartedBool[1] == 1 and UpTrendJustStartedBool == 0 then 1 else 0;
def DownTrendJustEnded = if DownTrendJustStartedBool[1] == 1 and DownTrendJustStartedBool == 0 then 1 else 0;

def GetTrend = if UpTrendJustStarted then 3 else if UpTrend then 2 else if UpTrendJustEnded then 1 else if DownTrendJustStarted then -1 else if DownTrend then -2 else if DownTrendJustEnded then -3 else 0;


#def upcnt = UpTrendBarCount;
#def dwncnt = DownTrendBarCount;
# chg to plots , in a script

plot upcnt = UpTrendBarCount;
plot dwncnt = DownTrendBarCount;
upcnt.setdefaultcolor(color.green);
dwncnt.setdefaultcolor(color.red);

#addlabel(1, upcnt);
#addlabel(1, dwncnt);

def upbub = (upcnt > upcnt[1] and upcnt > upcnt[-1]);
def dwnbub = (dwncnt > dwncnt[1] and dwncnt > dwncnt[-1]);

addchartbubble(upbub, upcnt, upcnt, color.green, yes);
addchartbubble(dwnbub, dwncnt, dwncnt, color.red, yes);


#===================================
#===================================


#I want to append the following lines to sum up/plot the UpTrendBarCount and DownTrendBarCount of the 2 symbols:
#plot upSum = if GetTrend == 2  then UpTrendBarCount(symbol="TSLA") + UpTrendBarCount(symbol="AAPL");
#plot downSum = if GetTrend == -2 then DownTrendBarCount(symbol="TSLA") + DownTrendBarCount(symbol="AAPL");

#

nMWX8fl.jpg
 
Last edited:
Please help me with this code/thought...
https://usethinkscript.com/threads/rsm-indicator-for-thinkorswim.5407/#post-50775

While looking at a SPY chart, I would like to sum up and plot the UpTrendBarCount from 2 symbols (TSLA and AAPL for example) using the below script. Similarly, sum up and plot the DownTrendBarCount.

Code:
declare lower;
def hi = high;
def lo = low;
def cl = close;

def lengthRSI = 7;
def averageTypeRSI = AverageType.EXPONENTIAL;
def NetChgAvg = MovingAverage(averageTypeRSI, cl - cl[1], lengthRSI);
def TotChgAvg = MovingAverage(averageTypeRSI, AbsValue(cl - cl[1]), lengthRSI);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);

#Stochastic Slow
def over_boughtSt = 80;
def over_soldSt = 20;
def KPeriod = 14;
def DPeriod = 3;
def averageTypeStoch = AverageType.WILDERS;
def SlowK = reference StochasticFull(over_boughtSt,  over_soldSt,  KPeriod,  DPeriod,  hi,  lo,  cl,  3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullK;
def SlowD = reference StochasticFull(over_boughtSt,  over_soldSt,  KPeriod,  DPeriod,  hi,  lo,  cl,  3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullD;

#MACD Calculation
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
def averageTypeMACD = AverageType.WEIGHTED;
def Value = MovingAverage(averageTypeMACD, cl, fastLength) - MovingAverage(averageTypeMACD, cl, slowLength);
def Avg = MovingAverage(averageTypeMACD, Value, MACDLength);
def Diff = Value - Avg;

#SCAN Variables
def UpTrend = if RSI > 50 and SlowK > 50 and Value > Avg then 1 else 0;
def DownTrend = if RSI < 50 and SlowK < 50 and Value < Avg then 1 else 0;

def bnumUp;
def bnumDown;
def closeUpTrendStart;
def closeDownTrendStart;
def UpTrendBarCount;
def DownTrendBarCount;
if UpTrend and (!UpTrend[1] or DownTrend[1]) {
    bnumUp = BarNumber();
    bnumDown = 0;
    closeUpTrendStart = cl;
    closeDownTrendStart = 0;
    UpTrendBarCount = 1;
    DownTrendBarCount = 0;
} else if UpTrend {
    bnumUp = bnumUp[1];
    bnumDown = 0;
    closeUpTrendStart = closeUpTrendStart[1];
    closeDownTrendStart = 0;
    UpTrendBarCount = UpTrendBarCount[1] + 1;
    DownTrendBarCount = 0;
} else if DownTrend and (!DownTrend[1] or UpTrend[1]) {
    bnumUp = 0;
    bnumDown = BarNumber();
    closeDownTrendStart = cl;
    closeUpTrendStart = 0;
    UpTrendBarCount = 0;
    DownTrendBarCount = 1;
} else if DownTrend {
    bnumDown = bnumDown[1];
    closeDownTrendStart = closeDownTrendStart[1];
    DownTrendBarCount = DownTrendBarCount[1] + 1;
    bnumUp = 0;
    closeUpTrendStart = 0;
    UpTrendBarCount = 0;
} else {
    bnumUp = 0;
    bnumDown = 0;
    closeUpTrendStart = 0;
    closeDownTrendStart = 0;
    UpTrendBarCount = 0;
    DownTrendBarCount = 0;
}

# The UpTrendJustStarted and DownTrendJustStarted plots can be used to find stocks that have just started a trend in either direction
def UpTrendJustStartedBool = if RSI > 50 and SlowK > 50 and Value > Avg then 1 else 0;
def DownTrendJustStartedBool = if RSI < 50 and SlowK < 50 and Value < Avg then 1 else 0;
def UpTrendJustStarted = if UpTrendJustStartedBool == 1 and UpTrendJustStartedBool[1] == 0 then 1 else 0;
def DownTrendJustStarted = if DownTrendJustStartedBool == 1 and DownTrendJustStartedBool[1] == 0 then 1 else 0;
def UpTrendJustEnded = if UpTrendJustStartedBool[1] == 1 and UpTrendJustStartedBool == 0 then 1 else 0;
def DownTrendJustEnded = if DownTrendJustStartedBool[1] == 1 and DownTrendJustStartedBool == 0 then 1 else 0;

def GetTrend = if UptrendJustStarted then 3 else if Uptrend then 2 else if UpTrendJustEnded then 1 else if DownTrendJustStarted then -1 else if DownTrend then -2 else if DownTrendJustEnded then -3 else 0;

I want to append the following lines to sum up/plot the UpTrendBarCount and DownTrendBarCount of the 2 symbols:

Code:
plot upSum = if GetTrend == 2  then UpTrendBarCount(symbol="TSLA") + UpTrendBarCount(symbol="AAPL");
plot downSum = if GetTrend == -2 then DownTrendBarCount(symbol="TSLA") + DownTrendBarCount(symbol="AAPL");


in this version,
. the user can pick 4 stocks
. it counts the up and down signals for each stock,
... and plots lines for the 4 stocks and draws bubbles at the highest count points


this version has the main code moved inside of a script, a user defined function.
that way that group of code can be used multiple times by 1 code line, instead of copying all that code over and over

this is one of the outputs within the script. they don't draw anything, plots are just used to generate output from a script.
plot upcnt = UpTrendBarCount;

this is a formula calling the user defined script. it uses the script name and parameters for the inputs.
the letters at the end is the plot variable within the script.
def s1up = rsm_counts(01,h1,l1,c1).upcnt;

this will determine how close the lines are
input line_spacing = 35;

i didn't do the final summing of counts from different symbols.
i'm not sure of what combo of symbols you want.
this should get you closer to what you want.


Code:
# sum_plot_counts_for_2stocks_rsm_00d


#https://usethinkscript.com/threads/sum-and-plot-barcount.14386/#post-119602
# Sum and plot barcount
# Shaco  2/7

# Please help me with this code/thought...
# ref1 - in post1 - RSM Indicator
# https://usethinkscript.com/threads/rsm-indicator-for-thinkorswim.5407/#post-50775

# While looking at a SPY chart,
#  sum up and plot the UpTrendBarCount from 2 symbols (TSLA and AAPL for example)
#  sum up and plot the DownTrendBarCount.

# ref2 - in post3 code- RSI + MACD Confluence
# https://usethinkscript.com/threads/rsi-macd-confluence-indicator-for-thinkorswim.505/page-3

#AddLabel(1, "-");

declare lower;

#def o = open;
#def h = high;
#def l = low;
#def c = close;

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

# rsm_counts
script rsm_counts {
#----------------------------

input o = open;
input h = high;
input l = low;
input c = close;


# RSI
#input length = 14;
#input over_Bought = 70;
#input over_Sold = 30;
def len_RSI = 7;
#input averageType = AverageType.WILDERS;
def averageTypeRSI = AverageType.EXPONENTIAL;
def NetChgAvg = MovingAverage(averageTypeRSI, c - c[1], len_RSI);
def TotChgAvg = MovingAverage(averageTypeRSI, AbsValue(c - c[1]), len_RSI);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);


#----------------------------
# Stochastic Slow
def over_boughtSt = 80;
def over_soldSt = 20;
#input KPeriod = 10;
#input DPeriod = 10;
def KPeriod = 14;
def DPeriod = 3;
#input averageType = AverageType.SIMPLE;
def averageTypeStoch = AverageType.WILDERS;
#plot SlowK = reference StochasticFull(over_bought,over_sold,KPeriod,DPeriod,priceH,priceL,priceC,3,averageType).FullK;
#plot SlowD = reference StochasticFull(over_bought,over_sold,KPeriod,DPeriod,priceH,priceL,priceC,3,averageType).FullD;

#def SlowK = reference StochasticFull(over_boughtSt,  over_soldSt,  KPeriod,  DPeriod,  hi,  lo,  cl,  3, averageTypeStoch).FullK;
#def SlowD = reference StochasticFull(over_boughtSt,  over_soldSt,  KPeriod,  DPeriod,  hi,  lo,  cl,  3, averageTypeStoch).FullD;

#/////////////////////////
# stochfull
#input over_bought = 80;
#input over_sold = 20;
#input priceH = high;
#input priceL = low;
#input priceC = close;
def slowing_period = 3;
def averageType = AverageType.SIMPLE;
#input showBreakoutSignals = {default "No", "On FullK", "On FullD", "On FullK & FullD"};
def lowest_k = Lowest(l, KPeriod);
def stc1 = c - lowest_k;
def stc2 = Highest(h, KPeriod) - lowest_k;
def FastK = if stc2 != 0 then stc1 / stc2 * 100 else 0;
def FullK = MovingAverage(averageType, FastK, slowing_period);
def FullD = MovingAverage(averageType, FullK, DPeriod);
#/////////////////////////

#def SlowK = reference StochasticFull(over_boughtSt,   over_soldSt,   KPeriod,   DPeriod,   hi,   lo,   cl,   3).FullK;
#def SlowD = reference StochasticFull(over_boughtSt,   over_soldSt,   KPeriod,   DPeriod,   hi,   lo,   cl,  3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullD;
def SlowK = fullk;
def SlowD = fulld;

# test code for type numbers
#input Type = AverageType.SIMPLE;
#def u = 1 * Type;
#AddLabel(1, Type + "  " + u, Color.YELLOW);
# 0 = simple
# 1 = exp
# 2 = weighted
# 3 = wild
# 4 = hull



#----------------------------
# MACD Calculation
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
def averageTypeMACD = AverageType.WEIGHTED;
def Value = MovingAverage(averageTypeMACD, c, fastLength) - MovingAverage(averageTypeMACD, c, slowLength);
def Avg = MovingAverage(averageTypeMACD, Value, MACDLength);
def Diff = Value - Avg;

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

# SCAN Variables
def UpTrend = if RSI > 50 and SlowK > 50 and Value > Avg then 1 else 0;
def DownTrend = if RSI < 50 and SlowK < 50 and Value < Avg then 1 else 0;

def bnumUp;
def bnumDown;
def closeUpTrendStart;
def closeDownTrendStart;
def UpTrendBarCount;
def DownTrendBarCount;
if UpTrend and (!UpTrend[1] or DownTrend[1]) {
    bnumUp = BarNumber();
    bnumDown = 0;
    closeUpTrendStart = c;
    closeDownTrendStart = 0;
    UpTrendBarCount = 1;
    DownTrendBarCount = 0;
} else if UpTrend {
    bnumUp = bnumUp[1];
    bnumDown = 0;
    closeUpTrendStart = closeUpTrendStart[1];
    closeDownTrendStart = 0;
    UpTrendBarCount = UpTrendBarCount[1] + 1;
    DownTrendBarCount = 0;
} else if DownTrend and (!DownTrend[1] or UpTrend[1]) {
    bnumUp = 0;
    bnumDown = BarNumber();
    closeDownTrendStart = c;
    closeUpTrendStart = 0;
    UpTrendBarCount = 0;
    DownTrendBarCount = 1;
} else if DownTrend {
    bnumDown = bnumDown[1];
    closeDownTrendStart = closeDownTrendStart[1];
    DownTrendBarCount = DownTrendBarCount[1] + 1;
    bnumUp = 0;
    closeUpTrendStart = 0;
    UpTrendBarCount = 0;
} else {
    bnumUp = 0;
    bnumDown = 0;
    closeUpTrendStart = 0;
    closeDownTrendStart = 0;
    UpTrendBarCount = 0;
    DownTrendBarCount = 0;
}

# The UpTrendJustStarted and DownTrendJustStarted plots can be used to find stocks that have just started a trend in either direction
def UpTrendJustStartedBool = if RSI > 50 and SlowK > 50 and Value > Avg then 1 else 0;
def DownTrendJustStartedBool = if RSI < 50 and SlowK < 50 and Value < Avg then 1 else 0;
def UpTrendJustStarted = if UpTrendJustStartedBool == 1 and UpTrendJustStartedBool[1] == 0 then 1 else 0;
def DownTrendJustStarted = if DownTrendJustStartedBool == 1 and DownTrendJustStartedBool[1] == 0 then 1 else 0;
def UpTrendJustEnded = if UpTrendJustStartedBool[1] == 1 and UpTrendJustStartedBool == 0 then 1 else 0;
def DownTrendJustEnded = if DownTrendJustStartedBool[1] == 1 and DownTrendJustStartedBool == 0 then 1 else 0;

def GetTrend = if UpTrendJustStarted then 3 else if UpTrend then 2 else if UpTrendJustEnded then 1 else if DownTrendJustStarted then -1 else if DownTrend then -2 else if DownTrendJustEnded then -3 else 0;


#def upcnt = UpTrendBarCount;
#def dwncnt = DownTrendBarCount;
# chg to plots , in a script

plot upcnt = UpTrendBarCount;
plot dwncnt = DownTrendBarCount;
#upcnt.setdefaultcolor(color.green);
#dwncnt.setdefaultcolor(color.red);
#addlabel(1, upcnt);
#addlabel(1, dwncnt);

}

#def upbub = (upcnt > upcnt[1] and upcnt > upcnt[-1]);
#def dwnbub = (dwncnt > dwncnt[1] and dwncnt > dwncnt[-1]);
#addchartbubble(upbub, upcnt, upcnt, color.green, yes);
#addchartbubble(dwnbub, dwncnt, dwncnt, color.red, yes);


#===================================
#===================================

# start of main code

def na = double.nan;
def bn = barnumber();

#addlabel(1, "-");

input symbol1 = "SPY";
def o1 = open(symbol1);
def h1 = high(symbol1);
def l1 = low(symbol1);
def c1 = close(symbol1);
#input sym1_base = 0;

input symbol2 = "AAPL";
def o2 = open(symbol2);
def h2 = high(symbol2);
def l2 = low(symbol2);
def c2 = close(symbol2);
#input sym2_base = 25;

input symbol3 = "META";
def o3 = open(symbol3);
def h3 = high(symbol3);
def l3 = low(symbol3);
def c3 = close(symbol3);
#input sym3_base = 50;

input symbol4 = "TSLA";
def o4 = open(symbol4);
def h4 = high(symbol4);
def l4 = low(symbol4);
def c4 = close(symbol4);
#input sym4_base = 75;


input line_spacing = 35;
def sym1_base = 0;
def sym2_base = sym1_base + (1 * line_spacing);
def sym3_base = sym1_base + (2 * line_spacing);
def sym4_base = sym1_base + (3 * line_spacing);



def s1up = rsm_counts(01,h1,l1,c1).upcnt;
def s2up = rsm_counts(02,h2,l2,c2).upcnt;
def s3up = rsm_counts(03,h3,l3,c3).upcnt;
def s4up = rsm_counts(04,h4,l4,c4).upcnt;

def s1dwn = rsm_counts(01,h1,l1,c1).dwncnt;
def s2dwn = rsm_counts(02,h2,l2,c2).dwncnt;
def s3dwn = rsm_counts(03,h3,l3,c3).dwncnt;
def s4dwn = rsm_counts(04,h4,l4,c4).dwncnt;


plot s1u = if isnan(close) then na else sym1_base + s1up;
s1u.setdefaultcolor(color.green);
plot s1d = if isnan(close) then na else sym1_base + s1dwn;
s1d.setdefaultcolor(color.red);

plot s2u = if isnan(close) then na else sym2_base + s2up;
s2u.setdefaultcolor(color.green);
plot s2d = if isnan(close) then na else sym2_base + s2dwn;
s2d.setdefaultcolor(color.red);

plot s3u = if isnan(close) then na else sym3_base + s3up;
s3u.setdefaultcolor(color.green);
plot s3d = if isnan(close) then na else sym3_base + s3dwn;
s3d.setdefaultcolor(color.red);

plot s4u = if isnan(close) then na else sym4_base + s4up;
s4u.setdefaultcolor(color.green);
plot s4d = if isnan(close) then na else sym4_base + s4dwn;
s4d.setdefaultcolor(color.red);


input show_bubbles = yes;
def s1upbub = (s1u > s1u[1] and s1u > s1u[-1]);
def s1dwnbub = (s1d > s1d[1] and s1d > s1d[-1]);
addchartbubble(show_bubbles and s1upbub, s1u, s1u, color.green, yes);
addchartbubble(show_bubbles and s1dwnbub, s1d, s1d, color.red, yes);

def s2upbub = (s2u > s2u[1] and s2u > s2u[-1]);
def s2dwnbub = (s2d > s2d[1] and s2d > s2d[-1]);
addchartbubble(show_bubbles and s2upbub, s2u, s2u, color.green, yes);
addchartbubble(show_bubbles and s2dwnbub, s2d, s2d, color.red, yes);

def s3upbub = (s3u > s3u[1] and s3u > s3u[-1]);
def s3dwnbub = (s3d > s3d[1] and s3d > s3d[-1]);
addchartbubble(show_bubbles and s3upbub, s3u, s3u, color.green, yes);
addchartbubble(show_bubbles and s3dwnbub, s3d, s3d, color.red, yes);

def s4upbub = (s4u > s4u[1] and s4u > s4u[-1]);
def s4dwnbub = (s4d > s4d[1] and s4d > s4d[-1]);
addchartbubble(show_bubbles and s4upbub, s4u, s4u, color.green, yes);
addchartbubble(show_bubbles and s4dwnbub, s4d, s4d, color.red, yes);

input bubblex = 3;
def bubx = (!isnan(close[bubblex]) and isnan(close[bubblex-1]));

addchartbubble(bubx, (sym1_base - (line_spacing/2)), symbol1, color.yellow, yes);
addchartbubble(bubx, (sym2_base - (line_spacing/2)), symbol2, color.yellow, yes);
addchartbubble(bubx, (sym3_base - (line_spacing/2)), symbol3, color.yellow, yes);
addchartbubble(bubx, (sym4_base - (line_spacing/2)), symbol4, color.yellow, yes);


#I want to append the following lines to sum up/plot the UpTrendBarCount and DownTrendBarCount of the 2 symbols:
#plot upSum = if GetTrend == 2  then UpTrendBarCount(symbol="TSLA") + UpTrendBarCount(symbol="AAPL");
#plot downSum = if GetTrend == -2 then DownTrendBarCount(symbol="TSLA") + DownTrendBarCount(symbol="AAPL");

#

1Bn11er.jpg
 
thanks for replying to my post. I attempted to code it for the past few days and this is what i have for 4 tickers. I got the amber warning triangle symbol saying this is a complex script and my experience longer load time. I hope someone will be able to condense it for me because I'm a novice coder. Maybe using a subroutine so i can have like 20 tickers and avoid the long load time warning.

Try this, Haven't tested this much, but should work, if you want to add up the Trend counts add do a plot or something, feel free to add up the code.

Ruby:
# RSM_Multi_SPY
#
#CHANGELOG
# 2023.2.12 - V1.0 @ApeX Predetor - RSM Lower Multiple Ticker:
#              Based on @Cos251 RSM_SCAN Code.
#            - Stocks currently in UpTrend
#            - Stocks currently in DownTrend
#            - Stocks where UpTrendJustSarted - first bar of UpTrend for scanend TF
#            - Stocks where DownTrendJustStarted - first bar of DownTrend for scanned TF
#            - Stocks where UpTredJustEnded - first NO Trend bar after UpTrend
#            - Stocks where DownTrendJustEnded - first NO Trend bar after DownTrend
#            - Recommend using default studies for SCANS of RSI, Stochastics or MACD for efficiency
#            
#REQUIREMENTS - RSI Set to 7, EXPONENTIAL
#               Stoch Slow 14 and 3 WILDERS
#               MACD 12,26,9 WEIGHTED
#
#
#CREDITS
# requesed by "@Joseph Patrick 18"
#
#LINK
# https://rockwell-files.s3.amazonaws.com/PXCompanionGuide2ndEd_cover.pdf
# Markus Heikoetter who is the author of the Power X Strategy
# https://usethinkscript.com/threads/mimicking-power-x-strategy-by-markus-heitkoetter.4283/

declare lower;

input Ticker_1 = "AAPL";
input Ticker_2 = "MSFT";
input Ticker_3 = "AMZN";
input Ticker_4 = "TSLA";
input Ticker_5 = "GOOG";
input Ticker_6 = "NVDA";
input Ticker_7 = "AMD";

script RSM_ {
    input Ticker = "";
    # RSI
    def Close = Close(Ticker);
    def  High =  High(Ticker);
    def   Low =   Low(Ticker);
    def lengthRSI = 7;
    def averageTypeRSI = AverageType.EXPONENTIAL;
    # Stochastic
    def over_boughtSt = 80;
    def over_soldSt = 20;
    def KPeriod = 14;
    def DPeriod = 3;
    input averageTypeStoch = AverageType.WILDERS;
    # MACD
    def fastLength = 12;
    def slowLength = 26;
    def MACDLength = 9;
    def averageTypeMACD = AverageType.WEIGHTED;

    ###############################################################
    ##########                 RSI                         #########
    ################################################################

    def NetChgAvg = MovingAverage(averageTypeRSI, Close - Close[1], lengthRSI);
    def TotChgAvg = MovingAverage(averageTypeRSI, AbsValue(Close - Close[1]), lengthRSI);
    def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
    def RSI_ = 50 * (ChgRatio + 1);

    ################################################################
    ##########                 Stochastic Slow             #########
    ################################################################

    def SlowK_ = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, High, Low, Close, 3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullK;
    def SlowD_ = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, High, Low, Close, 3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullD;

    ################################################################
    ##########                 MACD                      ###########
    ################################################################

    def Value_ = MovingAverage(averageTypeMACD, Close, fastLength) - MovingAverage(averageTypeMACD, Close, slowLength);
    def Avg_ = MovingAverage(averageTypeMACD, Value_, MACDLength);
    def Diff_ = Value_ - Avg_;

    #################################################################
    ##########          Trend  & Labels                   ###########
    #################################################################
    def UpTrend_ = if RSI_ >= 50 and SlowK_ >= 50 and Value_ >= Avg_ then 1 else 0;
    def DownTrend_ = if RSI_ <= 50 and SlowK_ <= 50 and Value_ <= Avg_ then 1 else 0;
    plot Trend_ = if UpTrend_ then 1 else if DownTrend_ then -1 else 0;
    def upArrow = if UpTrend_ == 1 and UpTrend_[1] == 0 then low else Double.NaN;
    def downArrow = if DownTrend_ == 1 and DownTrend_[1] == 0 then high else Double.NaN;


    def bnumUp;
    def bnumDown;
    def closeUpTrendStart;
    def closeDownTrendStart;
    def UpTrendBarCount;
    def DownTrendBarCount;
    if UpTrend_ and (!UpTrend_[1] or DownTrend_[1]) {
        bnumUp = BarNumber();
        bnumDown = 0;
        closeUpTrendStart = close;
        closeDownTrendStart = 0;
        UpTrendBarCount = 1;
        DownTrendBarCount = 0;
    } else if UpTrend_ {
        bnumUp = bnumUp[1];
        bnumDown = 0;
        closeUpTrendStart = closeUpTrendStart[1];
        closeDownTrendStart = 0;
        UpTrendBarCount = UpTrendBarCount[1] + 1;
        DownTrendBarCount = 0;
    } else if DownTrend_ and (!DownTrend_[1] or UpTrend_[1]) {
        bnumUp = 0;
        bnumDown = BarNumber();
        closeDownTrendStart = close;
        closeUpTrendStart = 0;
        UpTrendBarCount = 0;
        DownTrendBarCount = 1;
    } else if DownTrend_ {
        bnumDown = bnumDown[1];
        closeDownTrendStart = closeDownTrendStart[1];
        DownTrendBarCount = DownTrendBarCount[1] + 1;
        bnumUp = 0;
        closeUpTrendStart = 0;
        UpTrendBarCount = 0;
    } else {
        bnumUp = 0;
        bnumDown = 0;
        closeUpTrendStart = 0;
        closeDownTrendStart = 0;
        UpTrendBarCount = 0;
        DownTrendBarCount = 0;
    }

    plot Return = if bnumUp > 0 then UpTrendBarCount else if bnumDown > 0 then  DownTrendBarCount else 0;
}

def Ticker_1Trend = RSM_(Ticker_1).Trend_;
def Ticker_1Count = RSM_(Ticker_1).Return;
def Ticker_2Trend = RSM_(Ticker_2).Trend_;
def Ticker_2Count = RSM_(Ticker_2).Return;
def Ticker_3Trend = RSM_(Ticker_3).Trend_;
def Ticker_3Count = RSM_(Ticker_3).Return;
def Ticker_4Trend = RSM_(Ticker_4).Trend_;
def Ticker_4Count = RSM_(Ticker_4).Return;
def Ticker_5Trend = RSM_(Ticker_5).Trend_;
def Ticker_5Count = RSM_(Ticker_5).Return;
def Ticker_6Trend = RSM_(Ticker_6).Trend_;
def Ticker_6Count = RSM_(Ticker_6).Return;
def Ticker_7Trend = RSM_(Ticker_7).Trend_;
def Ticker_7Count = RSM_(Ticker_7).Return;

AddLabel(Yes,Ticker_1 + " "+ if Ticker_1Trend == 1 then "Up" else if Ticker_1Trend == -1 then "Dn" else "NT" +" " + Ticker_1Count ,if Ticker_1Trend == 1 then Color.Green else if Ticker_1Trend == -1 then Color.RED else Color.GRAY);
AddLabel(Yes,Ticker_2 + " "+ if Ticker_2Trend == 1 then "Up" else if Ticker_2Trend == -1 then "Dn" else "NT" +" " + Ticker_2Count ,if Ticker_2Trend == 1 then Color.Green else if Ticker_2Trend == -1 then Color.RED else Color.GRAY);
AddLabel(Yes,Ticker_3 + " "+ if Ticker_3Trend == 1 then "Up" else if Ticker_3Trend == -1 then "Dn" else "NT" +" " + Ticker_3Count ,if Ticker_3Trend == 1 then Color.Green else if Ticker_3Trend == -1 then Color.RED else Color.GRAY);
AddLabel(Yes,Ticker_4 + " "+ if Ticker_4Trend == 1 then "Up" else if Ticker_4Trend == -1 then "Dn" else "NT" +" " + Ticker_4Count ,if Ticker_4Trend == 1 then Color.Green else if Ticker_4Trend == -1 then Color.RED else Color.GRAY);
AddLabel(Yes,Ticker_5 + " "+ if Ticker_5Trend == 1 then "Up" else if Ticker_5Trend == -1 then "Dn" else "NT" +" " + Ticker_5Count ,if Ticker_5Trend == 1 then Color.Green else if Ticker_5Trend == -1 then Color.RED else Color.GRAY);
AddLabel(Yes,Ticker_6 + " "+ if Ticker_6Trend == 1 then "Up" else if Ticker_6Trend == -1 then "Dn" else "NT" +" " + Ticker_6Count ,if Ticker_6Trend == 1 then Color.Green else if Ticker_6Trend == -1 then Color.RED else Color.GRAY);
AddLabel(Yes,Ticker_7 + " "+ if Ticker_7Trend == 1 then "Up" else if Ticker_7Trend == -1 then "Dn" else "NT" +" " + Ticker_7Count ,if Ticker_7Trend == 1 then Color.Green else if Ticker_7Trend == -1 then Color.RED else Color.GRAY);
 
Last edited:
in this version,
. the user can pick 4 stocks
. it counts the up and down signals for each stock,
... and plots lines for the 4 stocks and draws bubbles at the highest count points


this version has the main code moved inside of a script, a user defined function.
that way that group of code can be used multiple times by 1 code line, instead of copying all that code over and over

this is one of the outputs within the script. they don't draw anything, plots are just used to generate output from a script.
plot upcnt = UpTrendBarCount;

this is a formula calling the user defined script. it uses the script name and parameters for the inputs.
the letters at the end is the plot variable within the script.
def s1up = rsm_counts(01,h1,l1,c1).upcnt;

this will determine how close the lines are
input line_spacing = 35;

i didn't do the final summing of counts from different symbols.
i'm not sure of what combo of symbols you want.
this should get you closer to what you want.


Code:
# sum_plot_counts_for_2stocks_rsm_00d


#https://usethinkscript.com/threads/sum-and-plot-barcount.14386/#post-119602
# Sum and plot barcount
# Shaco  2/7

# Please help me with this code/thought...
# ref1 - in post1 - RSM Indicator
# https://usethinkscript.com/threads/rsm-indicator-for-thinkorswim.5407/#post-50775

# While looking at a SPY chart,
#  sum up and plot the UpTrendBarCount from 2 symbols (TSLA and AAPL for example)
#  sum up and plot the DownTrendBarCount.

# ref2 - in post3 code- RSI + MACD Confluence
# https://usethinkscript.com/threads/rsi-macd-confluence-indicator-for-thinkorswim.505/page-3

#AddLabel(1, "-");

declare lower;

#def o = open;
#def h = high;
#def l = low;
#def c = close;

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

# rsm_counts
script rsm_counts {
#----------------------------

input o = open;
input h = high;
input l = low;
input c = close;


# RSI
#input length = 14;
#input over_Bought = 70;
#input over_Sold = 30;
def len_RSI = 7;
#input averageType = AverageType.WILDERS;
def averageTypeRSI = AverageType.EXPONENTIAL;
def NetChgAvg = MovingAverage(averageTypeRSI, c - c[1], len_RSI);
def TotChgAvg = MovingAverage(averageTypeRSI, AbsValue(c - c[1]), len_RSI);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);


#----------------------------
# Stochastic Slow
def over_boughtSt = 80;
def over_soldSt = 20;
#input KPeriod = 10;
#input DPeriod = 10;
def KPeriod = 14;
def DPeriod = 3;
#input averageType = AverageType.SIMPLE;
def averageTypeStoch = AverageType.WILDERS;
#plot SlowK = reference StochasticFull(over_bought,over_sold,KPeriod,DPeriod,priceH,priceL,priceC,3,averageType).FullK;
#plot SlowD = reference StochasticFull(over_bought,over_sold,KPeriod,DPeriod,priceH,priceL,priceC,3,averageType).FullD;

#def SlowK = reference StochasticFull(over_boughtSt,  over_soldSt,  KPeriod,  DPeriod,  hi,  lo,  cl,  3, averageTypeStoch).FullK;
#def SlowD = reference StochasticFull(over_boughtSt,  over_soldSt,  KPeriod,  DPeriod,  hi,  lo,  cl,  3, averageTypeStoch).FullD;

#/////////////////////////
# stochfull
#input over_bought = 80;
#input over_sold = 20;
#input priceH = high;
#input priceL = low;
#input priceC = close;
def slowing_period = 3;
def averageType = AverageType.SIMPLE;
#input showBreakoutSignals = {default "No", "On FullK", "On FullD", "On FullK & FullD"};
def lowest_k = Lowest(l, KPeriod);
def stc1 = c - lowest_k;
def stc2 = Highest(h, KPeriod) - lowest_k;
def FastK = if stc2 != 0 then stc1 / stc2 * 100 else 0;
def FullK = MovingAverage(averageType, FastK, slowing_period);
def FullD = MovingAverage(averageType, FullK, DPeriod);
#/////////////////////////

#def SlowK = reference StochasticFull(over_boughtSt,   over_soldSt,   KPeriod,   DPeriod,   hi,   lo,   cl,   3).FullK;
#def SlowD = reference StochasticFull(over_boughtSt,   over_soldSt,   KPeriod,   DPeriod,   hi,   lo,   cl,  3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullD;
def SlowK = fullk;
def SlowD = fulld;

# test code for type numbers
#input Type = AverageType.SIMPLE;
#def u = 1 * Type;
#AddLabel(1, Type + "  " + u, Color.YELLOW);
# 0 = simple
# 1 = exp
# 2 = weighted
# 3 = wild
# 4 = hull



#----------------------------
# MACD Calculation
def fastLength = 12;
def slowLength = 26;
def MACDLength = 9;
def averageTypeMACD = AverageType.WEIGHTED;
def Value = MovingAverage(averageTypeMACD, c, fastLength) - MovingAverage(averageTypeMACD, c, slowLength);
def Avg = MovingAverage(averageTypeMACD, Value, MACDLength);
def Diff = Value - Avg;

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

# SCAN Variables
def UpTrend = if RSI > 50 and SlowK > 50 and Value > Avg then 1 else 0;
def DownTrend = if RSI < 50 and SlowK < 50 and Value < Avg then 1 else 0;

def bnumUp;
def bnumDown;
def closeUpTrendStart;
def closeDownTrendStart;
def UpTrendBarCount;
def DownTrendBarCount;
if UpTrend and (!UpTrend[1] or DownTrend[1]) {
    bnumUp = BarNumber();
    bnumDown = 0;
    closeUpTrendStart = c;
    closeDownTrendStart = 0;
    UpTrendBarCount = 1;
    DownTrendBarCount = 0;
} else if UpTrend {
    bnumUp = bnumUp[1];
    bnumDown = 0;
    closeUpTrendStart = closeUpTrendStart[1];
    closeDownTrendStart = 0;
    UpTrendBarCount = UpTrendBarCount[1] + 1;
    DownTrendBarCount = 0;
} else if DownTrend and (!DownTrend[1] or UpTrend[1]) {
    bnumUp = 0;
    bnumDown = BarNumber();
    closeDownTrendStart = c;
    closeUpTrendStart = 0;
    UpTrendBarCount = 0;
    DownTrendBarCount = 1;
} else if DownTrend {
    bnumDown = bnumDown[1];
    closeDownTrendStart = closeDownTrendStart[1];
    DownTrendBarCount = DownTrendBarCount[1] + 1;
    bnumUp = 0;
    closeUpTrendStart = 0;
    UpTrendBarCount = 0;
} else {
    bnumUp = 0;
    bnumDown = 0;
    closeUpTrendStart = 0;
    closeDownTrendStart = 0;
    UpTrendBarCount = 0;
    DownTrendBarCount = 0;
}

# The UpTrendJustStarted and DownTrendJustStarted plots can be used to find stocks that have just started a trend in either direction
def UpTrendJustStartedBool = if RSI > 50 and SlowK > 50 and Value > Avg then 1 else 0;
def DownTrendJustStartedBool = if RSI < 50 and SlowK < 50 and Value < Avg then 1 else 0;
def UpTrendJustStarted = if UpTrendJustStartedBool == 1 and UpTrendJustStartedBool[1] == 0 then 1 else 0;
def DownTrendJustStarted = if DownTrendJustStartedBool == 1 and DownTrendJustStartedBool[1] == 0 then 1 else 0;
def UpTrendJustEnded = if UpTrendJustStartedBool[1] == 1 and UpTrendJustStartedBool == 0 then 1 else 0;
def DownTrendJustEnded = if DownTrendJustStartedBool[1] == 1 and DownTrendJustStartedBool == 0 then 1 else 0;

def GetTrend = if UpTrendJustStarted then 3 else if UpTrend then 2 else if UpTrendJustEnded then 1 else if DownTrendJustStarted then -1 else if DownTrend then -2 else if DownTrendJustEnded then -3 else 0;


#def upcnt = UpTrendBarCount;
#def dwncnt = DownTrendBarCount;
# chg to plots , in a script

plot upcnt = UpTrendBarCount;
plot dwncnt = DownTrendBarCount;
#upcnt.setdefaultcolor(color.green);
#dwncnt.setdefaultcolor(color.red);
#addlabel(1, upcnt);
#addlabel(1, dwncnt);

}

#def upbub = (upcnt > upcnt[1] and upcnt > upcnt[-1]);
#def dwnbub = (dwncnt > dwncnt[1] and dwncnt > dwncnt[-1]);
#addchartbubble(upbub, upcnt, upcnt, color.green, yes);
#addchartbubble(dwnbub, dwncnt, dwncnt, color.red, yes);


#===================================
#===================================

# start of main code

def na = double.nan;
def bn = barnumber();

#addlabel(1, "-");

input symbol1 = "SPY";
def o1 = open(symbol1);
def h1 = high(symbol1);
def l1 = low(symbol1);
def c1 = close(symbol1);
#input sym1_base = 0;

input symbol2 = "AAPL";
def o2 = open(symbol2);
def h2 = high(symbol2);
def l2 = low(symbol2);
def c2 = close(symbol2);
#input sym2_base = 25;

input symbol3 = "META";
def o3 = open(symbol3);
def h3 = high(symbol3);
def l3 = low(symbol3);
def c3 = close(symbol3);
#input sym3_base = 50;

input symbol4 = "TSLA";
def o4 = open(symbol4);
def h4 = high(symbol4);
def l4 = low(symbol4);
def c4 = close(symbol4);
#input sym4_base = 75;


input line_spacing = 35;
def sym1_base = 0;
def sym2_base = sym1_base + (1 * line_spacing);
def sym3_base = sym1_base + (2 * line_spacing);
def sym4_base = sym1_base + (3 * line_spacing);



def s1up = rsm_counts(01,h1,l1,c1).upcnt;
def s2up = rsm_counts(02,h2,l2,c2).upcnt;
def s3up = rsm_counts(03,h3,l3,c3).upcnt;
def s4up = rsm_counts(04,h4,l4,c4).upcnt;

def s1dwn = rsm_counts(01,h1,l1,c1).dwncnt;
def s2dwn = rsm_counts(02,h2,l2,c2).dwncnt;
def s3dwn = rsm_counts(03,h3,l3,c3).dwncnt;
def s4dwn = rsm_counts(04,h4,l4,c4).dwncnt;


plot s1u = if isnan(close) then na else sym1_base + s1up;
s1u.setdefaultcolor(color.green);
plot s1d = if isnan(close) then na else sym1_base + s1dwn;
s1d.setdefaultcolor(color.red);

plot s2u = if isnan(close) then na else sym2_base + s2up;
s2u.setdefaultcolor(color.green);
plot s2d = if isnan(close) then na else sym2_base + s2dwn;
s2d.setdefaultcolor(color.red);

plot s3u = if isnan(close) then na else sym3_base + s3up;
s3u.setdefaultcolor(color.green);
plot s3d = if isnan(close) then na else sym3_base + s3dwn;
s3d.setdefaultcolor(color.red);

plot s4u = if isnan(close) then na else sym4_base + s4up;
s4u.setdefaultcolor(color.green);
plot s4d = if isnan(close) then na else sym4_base + s4dwn;
s4d.setdefaultcolor(color.red);


input show_bubbles = yes;
def s1upbub = (s1u > s1u[1] and s1u > s1u[-1]);
def s1dwnbub = (s1d > s1d[1] and s1d > s1d[-1]);
addchartbubble(show_bubbles and s1upbub, s1u, s1u, color.green, yes);
addchartbubble(show_bubbles and s1dwnbub, s1d, s1d, color.red, yes);

def s2upbub = (s2u > s2u[1] and s2u > s2u[-1]);
def s2dwnbub = (s2d > s2d[1] and s2d > s2d[-1]);
addchartbubble(show_bubbles and s2upbub, s2u, s2u, color.green, yes);
addchartbubble(show_bubbles and s2dwnbub, s2d, s2d, color.red, yes);

def s3upbub = (s3u > s3u[1] and s3u > s3u[-1]);
def s3dwnbub = (s3d > s3d[1] and s3d > s3d[-1]);
addchartbubble(show_bubbles and s3upbub, s3u, s3u, color.green, yes);
addchartbubble(show_bubbles and s3dwnbub, s3d, s3d, color.red, yes);

def s4upbub = (s4u > s4u[1] and s4u > s4u[-1]);
def s4dwnbub = (s4d > s4d[1] and s4d > s4d[-1]);
addchartbubble(show_bubbles and s4upbub, s4u, s4u, color.green, yes);
addchartbubble(show_bubbles and s4dwnbub, s4d, s4d, color.red, yes);

input bubblex = 3;
def bubx = (!isnan(close[bubblex]) and isnan(close[bubblex-1]));

addchartbubble(bubx, (sym1_base - (line_spacing/2)), symbol1, color.yellow, yes);
addchartbubble(bubx, (sym2_base - (line_spacing/2)), symbol2, color.yellow, yes);
addchartbubble(bubx, (sym3_base - (line_spacing/2)), symbol3, color.yellow, yes);
addchartbubble(bubx, (sym4_base - (line_spacing/2)), symbol4, color.yellow, yes);


#I want to append the following lines to sum up/plot the UpTrendBarCount and DownTrendBarCount of the 2 symbols:
#plot upSum = if GetTrend == 2  then UpTrendBarCount(symbol="TSLA") + UpTrendBarCount(symbol="AAPL");
#plot downSum = if GetTrend == -2 then DownTrendBarCount(symbol="TSLA") + DownTrendBarCount(symbol="AAPL");

#

1Bn11er.jpg
wow! thank you so much @halcyonguy for your time and codes. It is very complex and beyond my coding knowledge. I really appreciate your help. I "frankestein" my codes together using others' codes that I found on here throughout the months/years so I dont exactly remember who/where to give credits. But your code is a great start for me to dissect and leverage.
 
Try this, Haven't tested this much, but should work, if you want to add up the Trend counts add do a plot or something, feel free to add up the code.

Ruby:
# RSM_Multi_SPY
#
#CHANGELOG
# 2023.2.12 - V1.0 @ApeX Predetor - RSM Lower Multiple Ticker:
#              Based on @Cos251 RSM_SCAN Code.
#            - Stocks currently in UpTrend
#            - Stocks currently in DownTrend
#            - Stocks where UpTrendJustSarted - first bar of UpTrend for scanend TF
#            - Stocks where DownTrendJustStarted - first bar of DownTrend for scanned TF
#            - Stocks where UpTredJustEnded - first NO Trend bar after UpTrend
#            - Stocks where DownTrendJustEnded - first NO Trend bar after DownTrend
#            - Recommend using default studies for SCANS of RSI, Stochastics or MACD for efficiency
#           
#REQUIREMENTS - RSI Set to 7, EXPONENTIAL
#               Stoch Slow 14 and 3 WILDERS
#               MACD 12,26,9 WEIGHTED
#
#
#CREDITS
# requesed by "@Joseph Patrick 18"
#
#LINK
# https://rockwell-files.s3.amazonaws.com/PXCompanionGuide2ndEd_cover.pdf
# Markus Heikoetter who is the author of the Power X Strategy
# https://usethinkscript.com/threads/mimicking-power-x-strategy-by-markus-heitkoetter.4283/

declare lower;

input Ticker_1 = "AAPL";
input Ticker_2 = "MSFT";
input Ticker_3 = "AMZN";
input Ticker_4 = "TSLA";
input Ticker_5 = "GOOG";
input Ticker_6 = "NVDA";
input Ticker_7 = "AMD";

script RSM_ {
    input Ticker = "";
    # RSI
    def Close = Close(Ticker);
    def  High =  High(Ticker);
    def   Low =   Low(Ticker);
    def lengthRSI = 7;
    def averageTypeRSI = AverageType.EXPONENTIAL;
    # Stochastic
    def over_boughtSt = 80;
    def over_soldSt = 20;
    def KPeriod = 14;
    def DPeriod = 3;
    input averageTypeStoch = AverageType.WILDERS;
    # MACD
    def fastLength = 12;
    def slowLength = 26;
    def MACDLength = 9;
    def averageTypeMACD = AverageType.WEIGHTED;

    ###############################################################
    ##########                 RSI                         #########
    ################################################################

    def NetChgAvg = MovingAverage(averageTypeRSI, Close - Close[1], lengthRSI);
    def TotChgAvg = MovingAverage(averageTypeRSI, AbsValue(Close - Close[1]), lengthRSI);
    def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
    def RSI_ = 50 * (ChgRatio + 1);

    ################################################################
    ##########                 Stochastic Slow             #########
    ################################################################

    def SlowK_ = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, High, Low, Close, 3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullK;
    def SlowD_ = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, High, Low, Close, 3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullD;

    ################################################################
    ##########                 MACD                      ###########
    ################################################################

    def Value_ = MovingAverage(averageTypeMACD, Close, fastLength) - MovingAverage(averageTypeMACD, Close, slowLength);
    def Avg_ = MovingAverage(averageTypeMACD, Value_, MACDLength);
    def Diff_ = Value_ - Avg_;

    #################################################################
    ##########          Trend  & Labels                   ###########
    #################################################################
    def UpTrend_ = if RSI_ >= 50 and SlowK_ >= 50 and Value_ >= Avg_ then 1 else 0;
    def DownTrend_ = if RSI_ <= 50 and SlowK_ <= 50 and Value_ <= Avg_ then 1 else 0;
    plot Trend_ = if UpTrend_ then 1 else if DownTrend_ then -1 else 0;
    def upArrow = if UpTrend_ == 1 and UpTrend_[1] == 0 then low else Double.NaN;
    def downArrow = if DownTrend_ == 1 and DownTrend_[1] == 0 then high else Double.NaN;


    def bnumUp;
    def bnumDown;
    def closeUpTrendStart;
    def closeDownTrendStart;
    def UpTrendBarCount;
    def DownTrendBarCount;
    if UpTrend_ and (!UpTrend_[1] or DownTrend_[1]) {
        bnumUp = BarNumber();
        bnumDown = 0;
        closeUpTrendStart = close;
        closeDownTrendStart = 0;
        UpTrendBarCount = 1;
        DownTrendBarCount = 0;
    } else if UpTrend_ {
        bnumUp = bnumUp[1];
        bnumDown = 0;
        closeUpTrendStart = closeUpTrendStart[1];
        closeDownTrendStart = 0;
        UpTrendBarCount = UpTrendBarCount[1] + 1;
        DownTrendBarCount = 0;
    } else if DownTrend_ and (!DownTrend_[1] or UpTrend_[1]) {
        bnumUp = 0;
        bnumDown = BarNumber();
        closeDownTrendStart = close;
        closeUpTrendStart = 0;
        UpTrendBarCount = 0;
        DownTrendBarCount = 1;
    } else if DownTrend_ {
        bnumDown = bnumDown[1];
        closeDownTrendStart = closeDownTrendStart[1];
        DownTrendBarCount = DownTrendBarCount[1] + 1;
        bnumUp = 0;
        closeUpTrendStart = 0;
        UpTrendBarCount = 0;
    } else {
        bnumUp = 0;
        bnumDown = 0;
        closeUpTrendStart = 0;
        closeDownTrendStart = 0;
        UpTrendBarCount = 0;
        DownTrendBarCount = 0;
    }

    plot Return = if bnumUp > 0 then UpTrendBarCount else if bnumDown > 0 then  DownTrendBarCount else 0;
}

def Ticker_1Trend = RSM_(Ticker_1).Trend_;
def Ticker_1Count = RSM_(Ticker_1).Return;
def Ticker_2Trend = RSM_(Ticker_2).Trend_;
def Ticker_2Count = RSM_(Ticker_2).Return;
def Ticker_3Trend = RSM_(Ticker_3).Trend_;
def Ticker_3Count = RSM_(Ticker_3).Return;
def Ticker_4Trend = RSM_(Ticker_4).Trend_;
def Ticker_4Count = RSM_(Ticker_4).Return;
def Ticker_5Trend = RSM_(Ticker_5).Trend_;
def Ticker_5Count = RSM_(Ticker_5).Return;
def Ticker_6Trend = RSM_(Ticker_6).Trend_;
def Ticker_6Count = RSM_(Ticker_6).Return;
def Ticker_7Trend = RSM_(Ticker_7).Trend_;
def Ticker_7Count = RSM_(Ticker_7).Return;

AddLabel(Yes,Ticker_1 + " "+ if Ticker_1Trend == 1 then "Up" else if Ticker_1Trend == -1 then "Dn" else "NT" +" " + Ticker_1Count ,if Ticker_1Trend == 1 then Color.Green else if Ticker_1Trend == -1 then Color.RED else Color.GRAY);
AddLabel(Yes,Ticker_2 + " "+ if Ticker_2Trend == 1 then "Up" else if Ticker_2Trend == -1 then "Dn" else "NT" +" " + Ticker_2Count ,if Ticker_2Trend == 1 then Color.Green else if Ticker_2Trend == -1 then Color.RED else Color.GRAY);
AddLabel(Yes,Ticker_3 + " "+ if Ticker_3Trend == 1 then "Up" else if Ticker_3Trend == -1 then "Dn" else "NT" +" " + Ticker_3Count ,if Ticker_3Trend == 1 then Color.Green else if Ticker_3Trend == -1 then Color.RED else Color.GRAY);
AddLabel(Yes,Ticker_4 + " "+ if Ticker_4Trend == 1 then "Up" else if Ticker_4Trend == -1 then "Dn" else "NT" +" " + Ticker_4Count ,if Ticker_4Trend == 1 then Color.Green else if Ticker_4Trend == -1 then Color.RED else Color.GRAY);
AddLabel(Yes,Ticker_5 + " "+ if Ticker_5Trend == 1 then "Up" else if Ticker_5Trend == -1 then "Dn" else "NT" +" " + Ticker_5Count ,if Ticker_5Trend == 1 then Color.Green else if Ticker_5Trend == -1 then Color.RED else Color.GRAY);
AddLabel(Yes,Ticker_6 + " "+ if Ticker_6Trend == 1 then "Up" else if Ticker_6Trend == -1 then "Dn" else "NT" +" " + Ticker_6Count ,if Ticker_6Trend == 1 then Color.Green else if Ticker_6Trend == -1 then Color.RED else Color.GRAY);
AddLabel(Yes,Ticker_7 + " "+ if Ticker_7Trend == 1 then "Up" else if Ticker_7Trend == -1 then "Dn" else "NT" +" " + Ticker_7Count ,if Ticker_7Trend == 1 then Color.Green else if Ticker_7Trend == -1 then Color.RED else Color.GRAY);
Thank you very much @ApeX Predator. I didnt even know this post existed. This is very helpful!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
390 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