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.
I want to append the following lines to sum up/plot the UpTrendBarCount and DownTrendBarCount of the 2 symbols:
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: