Join useThinkScript to post your question to a community of 21,000+ developers and traders.
#BRANCH INVESTMENT GROUP (BIG) ------------------------------------------------
#NOVEMBER 3, 2020
#MOVING AVERAGES-----------------------------------------------------------------
input length2 = 2;
input length5 = 5;
input length8 = 8;
input length10 = 10;
input length13 = 13;
input length21 = 21;
input length35 = 35;
input length50 = 50;
input length100 = 100;
input length200 = 200;
input over_Bought = 80;
input over_Sold = 20;
input price = close;
input averageType = AverageType.SIMPLE;
input showBreakoutSignals = no;
input averageType1 = AverageType.SIMPLE;
input crossingType = {default above, below};
def NetChgAvg = MovingAverage(averageType, price - price[1], length10);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length10);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def avg2 = MovingAverage(averageType1, price, length2);
def avg5 = MovingAverage(averageType1, price, length5);
def avg8 = MovingAverage(averageType1, price, length8);
def avg10 = MovingAverage(averageType1, price, length10);
def avg13 = MovingAverage(averageType1, price, length13);
def avg21 = MovingAverage(averageType1, price, length21);
def avg35 = MovingAverage(averageType1, price, length35);
def avg50 = MovingAverage(averageType1, price, length50);
def avg100 = MovingAverage(averageType1, price, length100);
def avg200 = MovingAverage(averageType1, price, length200);
#MOVING AVERGERS LABELS-----------------------------------------------------------------------------------
AddLabel (yes, if close then " [MOVING AVERAGES]" else "", Color.CYAN);
AddLabel(yes, if close < avg2 then " (2) " + Average (close, 2) else "", Color.RED);
AddLabel (yes, if close > avg2 then " (2) " + Average (close, 2) else "", Color.GREEN);
AddLabel(yes, if close < avg5 then " (5) " + Average (close, 5) else "", Color.RED);
AddLabel (yes, if close > avg5 then " (5) " + Average (close, 5) else "", Color.GREEN);
AddLabel(yes, if close < avg8 then " (8) " + Average (close, 8) else "", Color.RED);
AddLabel (yes, if close > avg8 then " (8) " + Average (close, 8) else "", Color.GREEN);
AddLabel(yes, if close < avg10 then " (10) " + Average (close, 10) else "", Color.RED);
AddLabel (yes, if close > avg10 then " (10) " + Average (close, 10) else "", Color.GREEN);
AddLabel(yes, if close < avg13 then " (13) " + Average (close, 13) else "", Color.RED);
AddLabel (yes, if close > avg13 then " (13) " + Average (close, 13) else "", Color.GREEN);
AddLabel (yes, if close < avg21 then " (21) " + Average (close, 21) else "", Color.RED);
AddLabel (yes, if close > avg21 then " (21) " + Average (close, 21) else "", Color.GREEN);
AddLabel (yes, if close < avg35 then " (35) " + Average (close, 35) else "", Color.RED);
AddLabel (yes, if close > avg35 then " (35) " + Average (close, 35) else "", Color.GREEN);
AddLabel (yes, if close < avg50 then " (50) " + Average (close, 50) else "", Color.RED);
AddLabel (yes, if close > avg50 then " (50) " + Average (close, 50) else "", Color.GREEN);
AddLabel (yes, if close < avg100 then " (100) " + Average (close, 100) else "", Color.RED);
AddLabel (yes, if close > avg100 then " (100) " + Average (close, 100) else "", Color.GREEN);
AddLabel (yes, if close < avg200 then " (200) " + Average (close, 200) else "", Color.RED);
AddLabel (yes, if close > avg200 then " (200) " + Average (close, 200) else "", Color.GREEN);
#THE END------------------------------------------------------------------------------------------------------------
input numDevDn = -2.0;
input numDevUp = 2.0;
input timeFrame = {default DAY, WEEK, MONTH};
def cap = getAggregationPeriod();
def errorInAggregation =
timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");
def yyyyMmDd = getYyyyMmDd();
def periodIndx;
switch (timeFrame) {
case DAY:
periodIndx = yyyyMmDd;
case WEEK:
periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 7);
case MONTH:
periodIndx = roundDown(yyyyMmDd / 100, 0);
}
def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);
def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;
if (isPeriodRolled) {
volumeSum = volume;
volumeVwapSum = volume * vwap;
volumeVwap2Sum = volume * Sqr(vwap);
} else {
volumeSum = compoundValue(1, volumeSum[1] + volume, volume);
volumeVwapSum = compoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
volumeVwap2Sum = compoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}
def price = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));
plot VWAP = price;
plot UpperBand = price + numDevUp * deviation;
plot LowerBand = price + numDevDn * deviation;
VWAP.setDefaultColor(Color.Yellow);
VWAP.setLineWeight(2);
VWAP.hide();
UpperBand.setDefaultColor(getColor(2));
UpperBand.hide();
LowerBand.setDefaultColor(getColor(4));
LowerBand.hide();
AddLabel(yes," VWAP: "+AsText(VWAP, NumberFormat.TWO_DECIMAL_PLACES),Color.Dark_Orange);
plot EMA12 = ExpAverage(HLC3,12);#8
EMA12.setDefaultColor(Getcolor(1));
EMA12.setLineWeight(2);
plot SMA26 = simplemovingAvg(close,26);#20
SMA26.setDefaultColor(Getcolor(6));
SMA26.setLineWeight(2);
plot SMA50 = expAverage(close,50);
sMA50.setDefaultColor(createColor(255,155,21));
sMA50.setLineWeight(2);
plot SMA200 = simplemovingAvg(low,200);
SMA200.setDefaultColor(Getcolor(5));
SMA200.setLineWeight(2);
AddLabel(yes," 12 EMA: "+AsText(ExpAverage(HLC3,12), NumberFormat.TWO_DECIMAL_PLACES)+ " ",Color.Cyan);
AddLabel(yes," 26 SMA: "+AsText(simplemovingAvg(close,26), NumberFormat.TWO_DECIMAL_PLACES)+ " ",Color.Green);
AddLabel(yes," 50sMA: "+AsText(expAverage(close,50), NumberFormat.TWO_DECIMAL_PLACES),createColor(255,155,21));
AddLabel(yes," 200SMA: "+AsText(simpleMovingAvg(close,200), NumberFormat.TWO_DECIMAL_PLACES),CreateColor(255,90,51));
Input fastLength = 12;#8
Input slowLength = 26;#20
Input MACDLength = 50;#8
Input averageType = AverageType.EXPONENTIAL;
Def MACD1 = MACD(fastLength, slowLength, MACDLength, averageType).Diff;
Def MACD = round(MACD1,3);
Def bullish = MACD > 0;
AddLabel (yes, “ MACD: “ + MACD, if bullish then color.Green else if !bullish then color.Dark_Green else color.RED);
Hi, I was trying to remove ALL decimals from the MA labels by putting , 0, but keep encountering errors. Can anyone advise how to modify the script properly please? Thanks!@islandcowgirl I only have a minute. Try the below. This is for learning.
This should be tried where ever MA_1... is located. Try one thing at a time.
Change the "ohlc4" to "close".
Change "MA_1_length); to "MA_1_length, 2)"
Lastly, put a ", 2 " Before and/or after "color.cyan" )
Sorry, on mobile.
Hi, I was trying to remove ALL decimals from the MA labels by putting , 0, but keep encountering errors. Can anyone advise how to modify the script properly please? Thanks!
input price_SMA = close;
input length_10 = 10;
input length_20 = 20;
input length_50 = 50;
input length_100 = 100;
input length_200 = 200;
input displace = 0;
#10 day SMA
def SMA_10 = Average(price_SMA[-displace], length_10);
DefineGlobalColor("SMA10", CreateColor(255, 0, 0)) ;
def SMA_10 = Average(price_SMA[-displace], length_10);
DefineGlobalColor("SMA10", CreateColor(255, 0, 0)) ;
addlabel( yes, "SMA(10) " + SMA_10, globalcolor("SMA10"));
input price_SMA = close;
input length_10 = 10;
input length_20 = 20;
input length_50 = 50;
input length_100 = 100;
input length_200 = 200;
input displace = 0;
#---------------
#10 day SMA
def SMA_10 = Average(price_SMA[-displace], length_10);
DefineGlobalColor("SMA10", CreateColor(255, 0, 0)) ;
addlabel( yes, "SMA(10) " + SMA_10, globalcolor("SMA10"));
#20 day SMA
def SMA_20 = Average(price_SMA[-displace], length_20);
DefineGlobalColor("SMA20", CreateColor(0, 197, 49)) ;
addlabel( yes, "SMA(20) " + SMA_20, globalcolor("SMA20"));
#50 day SMA
def SMA_50 = Average(price_SMA[-displace], length_50);
DefineGlobalColor("SMA50", CreateColor(0, 255, 255)) ;
addlabel( yes, "SMA(50) " + SMA_50, globalcolor("SMA50"));
# 100 day SMA
def SMA_100 = Average(price_SMA[-displace], length_100);
DefineGlobalColor("SMA100", CreateColor(255, 255, 51)) ;
addlabel( yes, "SMA(100) " + SMA_100, globalcolor("SMA100"));
#200 day SMA
def SMA_200 = Average(price_SMA[-displace], length_200);
DefineGlobalColor("SMA200", CreateColor(255, 126, 156)) ;
addlabel( yes, "SMA(200) " + SMA_200, globalcolor("SMA200"));
I’ve been trying to figure how to get the label to indicate whether or not the current price is above or below the moving average...would anyone know how to add that?@mourningwood4521 I'm glad to see that you got the code working as desired... Keep at it and you'll get more and more proficient with Thinkscript...
DefineGlobalColor("Labeloverbought", CreateColor(204, 0, 0)) ;
DefineGlobalColor("Labelnormal", CreateColor(225, 225, 225));
DefineGlobalColor("LabelOversold", CreateColor(0, 204, 0));
DefineGlobalColor("labelslightred", CreateColor(225, 51, 51));
DefineGlobalColor("labelslightgreen", CreateColor(0, 153, 0));
AddLabel(yes,
if RSI5 is true then "RSI(5) " + RSI5 else "",
if RSI5 > over_Bought_70 then GlobalColor("Labeloverbought") else
if RSI5 < over_Sold_70 then GlobalColor("Labeloversold") else
if RSI5 > 55 and RSI5 < over_Bought_70 then GlobalColor("labelslightred") else
if RSI5 < 45 and RSI5 > over_Sold_70 then GlobalColor("labelslightgreen") else GlobalColor("Labelnormal"));
AddLabel(yes, if Bulltrigger then "👍" else if BearTrigger then "👎" else " ", if BearTrigger then Color.dark_red else if Bulltrigger then Color.dark_GREEN else Color.ORANGE);
# ########################################################
# Moving Averages
def avg1 = MovingAverage(AverageType.eXPONENTIAL, HL2, 8);
def avg2 = MovingAverage(AverageType.eXPONENTIAL, HL2, 50);
def diff = avg2 - avg1 ;
AddLabel(yes, "ema diff: " +round(diff,2), if diff>0 then color.green else color.red);
# ########################################################
Thank you works perfectly!!!@earlyinout You are asking for a dollar difference? You do realize that if a $10 stock has a difference of $5 that would mean something totally different than if a $500 stock has a $5 difference. That's why you will see most strategies using percentages or standard deviations to determine if a difference is statistically significant. You certainly can do what you asking and below is the code written to your specifications. But you might want to reconsider if this is really what you want.
HTHRuby:# ######################################################## # Moving Averages def avg1 = MovingAverage(AverageType.eXPONENTIAL, HL2, 8); def avg2 = MovingAverage(AverageType.eXPONENTIAL, HL2, 50); def diff = avg2 - avg1 ; AddLabel(yes, "ema diff: " +round(diff,2), if diff>0 then color.green else color.red); # ########################################################
Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|
L | A Volume Pressure Moving Average For ThinkOrSwim | Indicators | 15 | |
T | Mega Moving Average For ThinkOrSwim | Indicators | 26 | |
S | Moving Average Golden Cross - Death Cross For ThinkOrSwim | Indicators | 14 | |
D | Repaints MTF Moving Average Slope Histogram For ThinkOrSwim | Indicators | 20 | |
Stacked Moving Averages For ThinkOrSwim | Indicators | 88 |
Start a new thread and receive assistance from our community.
useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.
We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.
If you are new, or just looking for guidance, here are some helpful links to get you started.