MACD & RSI Overlay (Expo) for ThinkOrSwim


Moderator - Expert

Creator Message:

#// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
#// © Zeiierman
#indicator('MACD & RSI Overlay (Expo)', overlay=true)
# Converted by Sam4Cok@Samer800     - 03/2023
#// ~~ Input variables {
input showLabel    = yes;        # "Show Trend Label"
input macd_rsi     = {default "MACD", "RSI-14", "Both"};    # "MACD/RSI"
input SignalType   = {Default indicator, Price, None};
input ShowIndicator = yes;
input macd_rsi_smooth = 1;      # "Smooth"
input signal_line_Length = 14;  # "Signal Length"
input rsiOverBought = 70;       # "RSI OB/OS"
input rsiOverSold = 30;         # "RSI OB/OS"
input trendFactor   = 30;       # "Trend Factor"
input scalingFactor = 2.5;      # "Scaling Factor"

def na = Double.NaN;
def macdType = macd_rsi==macd_rsi."MACD";
def rsiType  = macd_rsi==macd_rsi."RSI-14";
def bothType = macd_rsi==macd_rsi."Both";
def SigInd   = SignalType==SignalType.indicator;
def SigNon   = SignalType==SignalType.None;
#--- Color
DefineGlobalColor("macd"   , CreateColor(0,188,212));
DefineGlobalColor("rsi"    , CreateColor(19,69,238));
DefineGlobalColor("signal" , GetColor(8));
DefineGlobalColor("ob"     , CreateColor(0,230,118));
DefineGlobalColor("os"     , CreateColor(255,82,82));
#// ~~ Moving Averages {
def priceMa = Average(close, 2);
def trendMa = Average(close, trendFactor);

#//Calculate Scaled Adaptive Moving Average
def error    = AbsValue(priceMa - trendMa);
def errorSum = Sum(error, 10);
def ama      = if errorSum == 0 then close[1] else priceMa * (error / errorSum) + trendMa * (1 - error / errorSum);
def pma      = scalingFactor * close + (1.0 - scalingFactor) * ama[1];

def macd   = WMA(WildersAverage(pma, 5), macd_rsi_smooth);
def rsi    = WMA(WildersAverage(pma, 1), macd_rsi_smooth);
def signal = Average(pma, signal_line_Length);

#//OB/OS color
def rsi_ = RSI(PRICE = close, LENGTH = 14);
def rsi_col = if rsi_ > rsiOverBought then 1 else if rsi_ < rsiOverSold then -1 else 0;

plot macdLine = if ShowIndicator then if macdType or BothType then macd else na else na;  # 'MACD Line'
plot rsiLine  = if ShowIndicator then if rsiType or BothType  then rsi else na else na;    # 'RSI Line'
plot SigLine  = signal;          # 'Signal Line'
rsiLine.AssignValueColor(if rsi_col>0 then GlobalColor("ob") else
                         if rsi_col<0 then GlobalColor("os") else GlobalColor("rsi"));

AddLabel(showLabel and macdType or bothType, if macd>close then "MACD: Bullish" else  "MACD: Bearish",
                               if macd>close then Color.GREEN else Color.RED);
AddLabel(showLabel and rsiType or bothType, if close<rsi then "RSI: Bullish" else  "RSI: Bearish",
                              if close<rsi then Color.GREEN else Color.RED);

#// ~~ Alerts {
#indicatorcross(indicator) =>
Script indicatorcross {
input indicator = close;
input signal    = close;
    def over_  = crosses(indicator,signal, CrossingDirection.ABOVE);
    def under_ = crosses(indicator,signal, CrossingDirection.BELOW);
    plot over = over_;
    plot under = under_;
#srccross(src,indicator) =>
Script srccross {
input src = close;
input indicator = close;
    def over_  = crosses(src,indicator, CrossingDirection.ABOVE);
    def under_ = crosses(src,indicator, CrossingDirection.BELOW);
    plot over = over_;
    plot under = under_;

def over_macd  = indicatorcross(macd, signal).over;
def under_macd = indicatorcross(macd, signal).under;
def over_rsi   = indicatorcross(rsi, signal).over;
def under_rsi  = indicatorcross(rsi, signal).under;

def over_macd_price  = srccross(close,macd).over;
def under_macd_price = srccross(close,macd).under;

def over_rsi_price   = srccross(close,rsi).over;
def under_rsi_price  = srccross(close,rsi).under;

#def over_rsi_macd    = srccross(rsi,macd).over;
#def under_rsi_macd   = srccross(rsi,macd).under;

plot macdIndUp = if !SigNon and macdType and SigInd and over_macd then macd else na;#,  "B", Color.GREEN, no);
plot macdIndDn = if !SigNon and macdType and SigInd and under_macd then macd else na;#,"S", Color.RED , yes);

plot macdPrUp = if !SigNon and macdType and !SigInd and over_macd_price then low else na;#, "B", Color.GREEN, no);
plot macdPrDn = if !SigNon and macdType and !SigInd and under_macd_price then high else na;#,"S", Color.RED, yes);

plot rsiIndUp = if !SigNon and rsiType and SigInd and over_rsi then rsi else na;#,  "B", Color.GREEN, no);
plot rsiIndDn = if !SigNon and rsiType and SigInd and under_rsi then rsi else na;#,"S", Color.RED , yes);

plot rsiPrUp = if !SigNon and rsiType and !SigInd and over_rsi_price then low else na;#, "B", Color.GREEN, no);
plot rsiPrDn = if !SigNon and rsiType and !SigInd and under_rsi_price then high else na;#,"S", Color.RED, yes);

#plot bothIndUp = if !SigNon and bothType and over_rsi_macd then low else na;#, "B", Color.GREEN, no);
#plot bothIndDn = if !SigNon and bothType and under_rsi_macd then high else na;#,"S", Color.RED, yes);

Last edited by a moderator:

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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