Momentum Ratio Oscillator [Loxx] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
VFk6aIk.png

Author Message:
The theory behind this indicator involves utilizing a sequence of exponential moving average ( EMA ) calculations to achieve a smoother value of momentum ratio, which compares the current value to the previous one. Although this results in an outcome similar to that of some pre-existing indicators (such as volume zone or price zone oscillators), the use of EMA for smoothing is what sets it apart. EMA produces a smooth step-like output when values undergo sudden changes, whereas the mathematics used for those other indicators are completely distinct.

CODE:

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © loxx
#indicator("Momentum Ratio Oscillator [Loxx]"
# converted and mod by Sam4Cok@Samer800        - 04/2023
declare lower;
input srcin = Close;                # "Source",
input Period = 50;                  # "Period"
input sigtype = {default "Middle", "Levels", "Signal"};    # "Signal Type"
input upper_Level = 0.8;            # "Upper Level"
input lower_Level = 0.2;            # "Lower Level"
input colorBars = yes;              # "Color bars?"
input showSigs = yes;               # "Show Signals?"

def na = Double.NaN;
def last = isNaN(Close);
def Middle = sigtype==sigtype."Middle";
def Signal = sigtype==sigtype."Signal";
def haClose = ohlc4;
def haOpen = CompoundValue(1, (haOpen[1] + haClose[1]) / 2, (open + close) / 2);
def haHigh = Max(high, Max(haOpen, haClose));
def haLow  = Min(low, Min(haOpen, haClose));
def hamedian   = (hahigh + halow) / 2;
def hatypical  = (hahigh + halow + haclose) / 3;
def haweighted =  (hahigh + halow + haclose + haclose)/4;
def haaverage  =  (haopen + hahigh + halow + haclose)/4;


def alpha = 2.0 / (1.0 * Period);
def ema = CompoundValue(1, ema[1] + alpha * (srcin - ema[1]), srcin[1] + alpha * (srcin - srcin[1]));
def ratioa = ema/ema[1];

def emaa = CompoundValue(1, emaa[1] + alpha * ((if(ratioa < 1.0,ratioa,0)) - emaa[1]), alpha * if(ratioa < 1.0,ratioa,0));
def emab = CompoundValue(1, emab[1] + alpha * ((if(ratioa > 1.0,ratioa,0)) - emab[1]), alpha * if(ratioa > 1.0,ratioa,0));

def ratiob = ratioa / (ratioa + emab);

def val = 2.0 * ratioa / (ratioa + ratiob * emaa) - 1.0;
def sig = val[1];

def mid = 0.5;
def colorout = if Middle then if val > mid then 1 else -1 else
               if Signal then if val > sig then 1 else -1 else
               if val > upper_Level then 1 else if val < lower_Level then -1 else 0;

plot ValLine = val;
plot MidLine = if last then na else mid;
def UpperLevel = if last then na else upper_Level;#, "Upper Level"
def LowerLevel = if last then na else lower_Level;#, "Lower Level"

ValLine.SetLineWeight(2);
ValLine.AssignValueColor(if colorout > 0 then Color.GREEN else
                         if colorout < 0 then Color.RED else Color.GRAY);
MidLine.SetDefaultColor(Color.GRAY);
MidLine.SetPaintingStrategy(PaintingStrategy.DASHES);


AssignPriceColor(if !colorbars then Color.CURRENT else
                 if colorout > 0 then Color.GREEN else
                 if colorout < 0 then Color.RED else Color.GRAY);

#--- Signals
def goLong =  if Middle then (val Crosses above mid) else
              if Signal then (val Crosses above sig) else (val Crosses above upper_Level);
def goShort = if Middle then (val Crosses below mid) else
              if Signal then (val Crosses below sig) else (val Crosses below upper_Level);

plot SigUp = if showSigs and goLong then lower_Level else 0;     # "Long"
plot SigDn = if showSigs and goShort then upper_Level else 1;    # "Short"

SigUp.SetDefaultColor(Color.DARK_GREEN);
SigDn.SetDefaultColor(Color.DARK_RED);

AddCloud(SigDn, upper_Level, Color.DARK_RED, Color.DARK_RED, yes);
AddCloud(lower_Level, SigUp, Color.DARK_GREEN, Color.DARK_GREEN, yes);

#--- END of COde
 
Last edited by a moderator:

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

VFk6aIk.png

Author Message:
The theory behind this indicator involves utilizing a sequence of exponential moving average ( EMA ) calculations to achieve a smoother value of momentum ratio, which compares the current value to the previous one. Although this results in an outcome similar to that of some pre-existing indicators (such as volume zone or price zone oscillators), the use of EMA for smoothing is what sets it apart. EMA produces a smooth step-like output when values undergo sudden changes, whereas the mathematics used for those other indicators are completely distinct.

CODE:

CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © loxx
#indicator("Momentum Ratio Oscillator [Loxx]"
# converted and mod by Sam4Cok@Samer800        - 04/2023
declare lower;
input srcin = Close;                # "Source",
input Period = 50;                  # "Period"
input sigtype = {default "Middle", "Levels", "Signal"};    # "Signal Type"
input upper_Level = 0.8;            # "Upper Level"
input lower_Level = 0.2;            # "Lower Level"
input colorBars = yes;              # "Color bars?"
input showSigs = yes;               # "Show Signals?"

def na = Double.NaN;
def last = isNaN(Close);
def Middle = sigtype==sigtype."Middle";
def Signal = sigtype==sigtype."Signal";
def haClose = ohlc4;
def haOpen = CompoundValue(1, (haOpen[1] + haClose[1]) / 2, (open + close) / 2);
def haHigh = Max(high, Max(haOpen, haClose));
def haLow  = Min(low, Min(haOpen, haClose));
def hamedian   = (hahigh + halow) / 2;
def hatypical  = (hahigh + halow + haclose) / 3;
def haweighted =  (hahigh + halow + haclose + haclose)/4;
def haaverage  =  (haopen + hahigh + halow + haclose)/4;


def alpha = 2.0 / (1.0 * Period);
def ema = CompoundValue(1, ema[1] + alpha * (srcin - ema[1]), srcin[1] + alpha * (srcin - srcin[1]));
def ratioa = ema/ema[1];

def emaa = CompoundValue(1, emaa[1] + alpha * ((if(ratioa < 1.0,ratioa,0)) - emaa[1]), alpha * if(ratioa < 1.0,ratioa,0));
def emab = CompoundValue(1, emab[1] + alpha * ((if(ratioa > 1.0,ratioa,0)) - emab[1]), alpha * if(ratioa > 1.0,ratioa,0));

def ratiob = ratioa / (ratioa + emab);

def val = 2.0 * ratioa / (ratioa + ratiob * emaa) - 1.0;
def sig = val[1];

def mid = 0.5;
def colorout = if Middle then if val > mid then 1 else -1 else
               if Signal then if val > sig then 1 else -1 else
               if val > upper_Level then 1 else if val < lower_Level then -1 else 0;

plot ValLine = val;
plot MidLine = if last then na else mid;
def UpperLevel = if last then na else upper_Level;#, "Upper Level"
def LowerLevel = if last then na else lower_Level;#, "Lower Level"

ValLine.SetLineWeight(2);
ValLine.AssignValueColor(if colorout > 0 then Color.GREEN else
                         if colorout < 0 then Color.RED else Color.GRAY);
MidLine.SetDefaultColor(Color.GRAY);
MidLine.SetPaintingStrategy(PaintingStrategy.DASHES);


AssignPriceColor(if !colorbars then Color.CURRENT else
                 if colorout > 0 then Color.GREEN else
                 if colorout < 0 then Color.RED else Color.GRAY);

#--- Signals
def goLong =  if Middle then (val Crosses above mid) else
              if Signal then (val Crosses above sig) else (val Crosses above upper_Level);
def goShort = if Middle then (val Crosses below mid) else
              if Signal then (val Crosses below sig) else (val Crosses below upper_Level);

plot SigUp = if showSigs and goLong then lower_Level else 0;     # "Long"
plot SigDn = if showSigs and goShort then upper_Level else 1;    # "Short"

SigUp.SetDefaultColor(Color.DARK_GREEN);
SigDn.SetDefaultColor(Color.DARK_RED);

AddCloud(SigDn, upper_Level, Color.DARK_RED, Color.DARK_RED, yes);
AddCloud(lower_Level, SigUp, Color.DARK_GREEN, Color.DARK_GREEN, yes);

#--- END of COde
Very nice study indeed! Thank you for posting it!
 
  • Thumbs Up
Reactions: IPA

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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