Gatekeeper
Member
- Platform
-
- Thinkorswim
Code:
#Posted on UseThinkScript page 16
#START OF RSI/Stochastic/MACD Confluence Strategy for ThinkOrSwim
# RSM_MTF_Labels
#
#CHANGELOG
# 2020.12.30 V2.1 @SuryaKiranC - Fork from @cos251 Version, to reduce the number of lines in code and optimize for performance.
#
# 2021.07.24 V1.2 @speedydooo - Updated script to work with all timeframes without
# - setting aggregation period. Shows RSM Labels for all
# - timeframes highter than the current chart
# 2020.12.11 V1.1 @cos251 - Added 2D, 3D, 4D, 1WK, 1MNTH Agg Period Labels
#
# 2020.12.02 V1.0 @cos251 - Added RSM signal calculation for following timeframes:
# - 1m, 2m, 5m, 15m, 30m, 1h, 2h, 4h, D
# - Label will be added to top of chart for every allowed TF
# - Label will read "TF:L(Long):S(Short):I(Idle)"
# - Label Color will be green, red or gray accordingly
#
#
#REQUIREMENTS - RSI Set to 7, EXPONENTIAL
# Stoch Slow 14 and 3 WILDERS
# MACD 12,26,9 WEIGHTED
#
#ORIGINAL REQUEST - @Joseph Patrick 18
# - Link: https://usethinkscript.com/threads/mimicking-power-x-strategy-by-markus-heitkoetter.4283/
#
#
DefineGlobalColor("UpTrend", Color.Green);
DefineGlobalColor("DownTrend", Color.Dark_RED);
DefineGlobalColor("NoTrend", Color.Dark_GRAY);
script RSM_ {
input aP = AggregationPeriod.DAY;
# RSI
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(period = aP) - close(period = aP)[1], lengthRSI);
def TotChgAvg = MovingAverage(averageTypeRSI, AbsValue(close(period = aP) - close(period = aP)[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(period = aP), low(period = aP), close(period = aP), 3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullK;
def SlowD_ = reference StochasticFull(over_boughtSt, over_soldSt, KPeriod, DPeriod, high(period = aP), low(period = aP), close(period = aP), 3, if (averageTypeStoch == 1) then AverageType.SIMPLE else AverageType.EXPONENTIAL).FullD;
################################################################
########## MACD ###########
################################################################
def Value_ = MovingAverage(averageTypeMACD, close(period = aP), fastLength) - MovingAverage(averageTypeMACD, close(period = aP), 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 0 else -1;
}
def currentPeriod = GetAggregationPeriod();
#def RSM;
def monthRSM;
def monthAggregationPeriod;
if GetAggregationPeriod() <= AggregationPeriod.Month {
monthRSM = RSM_(aP = AggregationPeriod.Month);
monthAggregationPeriod = 1;
}
else {
monthRSM = Double.NaN;
monthAggregationPeriod = 0;
}
AddLabel(monthRSM and monthAggregationPeriod, "RSM-M", if monthRSM == 1 then GlobalColor("UpTrend") else if monthRSM == 0 then GlobalColor("DownTrend") else GlobalColor("NoTrend"));
def weekRSM;
def WeekAggregationPeriod;
if GetAggregationPeriod() <= AggregationPeriod.week {
weekRSM = RSM_(aP = AggregationPeriod.Week);
weekAggregationPeriod = 1;
}
else {
weekRSM = Double.NaN;
weekAggregationPeriod = 0;
}
AddLabel(weekRSM and weekAggregationPeriod, "RSM-W", if weekRSM == 1 then GlobalColor("UpTrend") else if weekRSM == 0 then GlobalColor("DownTrend") else GlobalColor("NoTrend"));
def fourdaysRSM;
def fourdaysAggregationPeriod;
if GetAggregationPeriod() <= AggregationPeriod.FOUR_DAYS {
fourdaysRSM = RSM_(aP = AggregationPeriod.FOUR_DAYS);
fourdaysAggregationPeriod = 1;
}
else {
fourdaysRSM = Double.NaN;
fourdaysAggregationPeriod = 0;
}
AddLabel(fourdaysRSM and fourdaysAggregationPeriod, "RSM-4D", if fourdaysRSM == 1 then GlobalColor("UpTrend") else if fourdaysRSM == 0 then GlobalColor("DownTrend") else GlobalColor("NoTrend"));
def threedaysRSM;
def threedaysAggregationPeriod;
if GetAggregationPeriod() <= AggregationPeriod.THREE_DAYS {
threedaysRSM = RSM_(aP = AggregationPeriod.THREE_DAYS);
threedaysAggregationPeriod = 1;
}
else {
threedaysRSM = Double.NaN;
threedaysAggregationPeriod = 0;
}
AddLabel(threedaysRSM and threedaysAggregationPeriod, "RSM-3D", if threedaysRSM == 1 then GlobalColor("UpTrend") else if threedaysRSM == 0 then GlobalColor("DownTrend") else GlobalColor("NoTrend"));
def twodaysRSM;
def twodaysAggregationPeriod;
if GetAggregationPeriod() <= AggregationPeriod.TWO_DAYS {
twodaysRSM = RSM_(aP = AggregationPeriod.TWO_DAYS);
twodaysAggregationPeriod = 1;
}
else {
twodaysRSM = Double.NaN;
twodaysAggregationPeriod = 0;
}
AddLabel(twodaysRSM and twodaysAggregationPeriod, "RSM-2D", if twodaysRSM == 1 then GlobalColor("UpTrend") else if twodaysRSM == 0 then GlobalColor("DownTrend") else GlobalColor("NoTrend"));
def dayRSM;
def dayAggregationPeriod;
if GetAggregationPeriod() <= AggregationPeriod.DAY {
dayRSM = RSM_(aP = AggregationPeriod.DAY);
dayAggregationPeriod = 1;
}
else {
dayRSM = Double.NaN;
dayAggregationPeriod = 0;
}
AddLabel(dayRSM and dayAggregationPeriod, "RSM-D", if dayRSM == 1 then GlobalColor("UpTrend") else if dayRSM == 0 then GlobalColor("DownTrend") else GlobalColor("NoTrend"));
def fourhoursRSM;
def fourhoursAggregationPeriod;
if GetAggregationPeriod() <= AggregationPeriod.FOUR_HOURS {
fourhoursRSM = RSM_(aP = AggregationPeriod.FOUR_HOURS);
fourhoursAggregationPeriod = 1;
}
else {
fourhoursRSM = Double.NaN;
fourhoursAggregationPeriod = 0;
}
AddLabel(fourhoursRSM and fourhoursAggregationPeriod, "RSM-4H", if fourhoursRSM == 1 then GlobalColor("UpTrend") else if fourhoursRSM == 0 then GlobalColor("DownTrend") else GlobalColor("NoTrend"));
def twohoursRSM;
def twohoursAggregationPeriod;
if GetAggregationPeriod() <= AggregationPeriod.TWO_HOURS {
twohoursRSM = RSM_(aP = AggregationPeriod.TWO_HOURS);
twohoursAggregationPeriod = 1;
}
else {
twohoursRSM = Double.NaN;
twohoursAggregationPeriod = 0;
}
AddLabel(twohoursRSM and twohoursAggregationPeriod, "RSM-2H", if twohoursRSM == 1 then GlobalColor("UpTrend") else if twohoursRSM == 0 then GlobalColor("DownTrend") else GlobalColor("NoTrend"));
def hourRSM;
def hourAggregationPeriod;
if GetAggregationPeriod() <= AggregationPeriod.HOUR {
hourRSM = RSM_(aP = AggregationPeriod.HOUR);
hourAggregationPeriod = 1;
}
else {
hourRSM = Double.NaN;
hourAggregationPeriod = 0;
}
AddLabel(hourRSM and hourAggregationPeriod, "RSM-1H", if hourRSM == 1 then GlobalColor("UpTrend") else if hourRSM == 0 then GlobalColor("DownTrend") else GlobalColor("NoTrend"));
def thirtyminsRSM;
def thirtyminAggregationPeriod;
if GetAggregationPeriod() <= AggregationPeriod.THIRTY_MIN {
thirtyminsRSM = RSM_(aP = AggregationPeriod.THIRTY_MIN);
thirtyminAggregationPeriod = 1;
}
else {
thirtyminsRSM = Double.NaN;
thirtyminAggregationPeriod = 0;
}
AddLabel(thirtyminsRSM and thirtyminAggregationPeriod, "RSM-30m", if thirtyminsRSM == 1 then GlobalColor("UpTrend") else if thirtyminsRSM == 0 then GlobalColor("DownTrend") else GlobalColor("NoTrend"));
def twentyminsRSM;
def twentyminAggregationPeriod;
if GetAggregationPeriod() <= AggregationPeriod.TWENTY_MIN {
twentyminsRSM = RSM_(aP = AggregationPeriod.TWENTY_MIN);
twentyminAggregationPeriod = 1;
}
else {
twentyminsRSM = Double.NaN;
twentyminAggregationPeriod = 0;
}
AddLabel(twentyminsRSM and twentyminAggregationPeriod, "RSM-20m", if twentyminsRSM == 1 then GlobalColor("UpTrend") else if twentyminsRSM == 0 then GlobalColor("DownTrend") else GlobalColor("NoTrend"));
def tenminsRSM;
def tenminAggregationPeriod;
if GetAggregationPeriod() <= AggregationPeriod.TEN_MIN {
tenminsRSM = RSM_(aP = AggregationPeriod.TEN_MIN);
tenminAggregationPeriod = 1;
}
else {
tenminsRSM = Double.NaN;
tenminAggregationPeriod = 0;
}
AddLabel(tenminsRSM and tenminAggregationPeriod, "RSM-10m", if tenminsRSM == 1 then GlobalColor("UpTrend") else if tenminsRSM == 0 then GlobalColor("DownTrend") else GlobalColor("NoTrend"));
def fiveminsRSM;
def fiveminAggregationPeriod;
if GetAggregationPeriod() <= AggregationPeriod.FIVE_MIN {
fiveminsRSM = RSM_(aP = AggregationPeriod.FIVE_MIN);
fiveminAggregationPeriod = 1;
}
else {
fiveminsRSM = Double.NaN;
fiveminAggregationPeriod = 0;
}
AddLabel(fiveminsRSM and fiveminAggregationPeriod, "RSM-5m", if fiveminsRSM == 1 then GlobalColor("UpTrend") else if fiveminsRSM == 0 then GlobalColor("DownTrend") else GlobalColor("NoTrend"));
def fourminsRSM;
def fourminAggregationPeriod;
if GetAggregationPeriod() <= AggregationPeriod.FOUR_MIN {
fourminsRSM = RSM_(aP = AggregationPeriod.FOUR_MIN);
fourminAggregationPeriod = 1;
}
else {
fourminsRSM = Double.NaN;
fourminAggregationPeriod = 0;
}
AddLabel(fourminsRSM and fourminAggregationPeriod, "RSM-4m", if fourminsRSM == 1 then GlobalColor("UpTrend") else if fourminsRSM == 0 then GlobalColor("DownTrend") else GlobalColor("NoTrend"));
def threeminsRSM;
def threeminAggregationPeriod;
if GetAggregationPeriod() <= AggregationPeriod.THREE_MIN {
threeminsRSM = RSM_(aP = AggregationPeriod.THREE_MIN);
threeminAggregationPeriod = 1;
}
else {
threeminsRSM = Double.NaN;
threeminAggregationPeriod = 0;
}
AddLabel(threeminsRSM and threeminAggregationPeriod, "RSM-3m", if threeminsRSM == 1 then GlobalColor("UpTrend") else if threeminsRSM == 0 then GlobalColor("DownTrend") else GlobalColor("NoTrend"));
def twominsRSM;
def twominAggregationPeriod;
if GetAggregationPeriod() <= AggregationPeriod.TWO_MIN {
twominsRSM = RSM_(aP = AggregationPeriod.TWO_MIN);
twominAggregationPeriod = 1;
}
else {
twominsRSM = Double.NaN;
twominAggregationPeriod = 0;
}
AddLabel(twominsRSM and twominAggregationPeriod, "RSM-2m", if twominsRSM == 1 then GlobalColor("UpTrend") else if twominsRSM == 0 then GlobalColor("DownTrend") else GlobalColor("NoTrend"));
def oneminsRSM;
def oneminAggregationPeriod;
if GetAggregationPeriod() <= AggregationPeriod.MIN {
oneminsRSM = RSM_(aP = AggregationPeriod.MIN);
oneminAggregationPeriod = 1;
}
else {
oneminsRSM = Double.NaN;
oneminAggregationPeriod = 0;
}
AddLabel(oneminsRSM and oneminAggregationPeriod, "RSM-1m", if oneminsRSM == 1 then GlobalColor("UpTrend") else if oneminsRSM == 0 then GlobalColor("DownTrend") else GlobalColor("NoTrend"));