# Reverse Stochastic Momentum Index On Chart For ThinkOrSwim

Hi, I am looking for some help converting following Tradingview code for Thinkorswim. Here is the original link: https://www.tradingview.com/script/rCfo5HHf-Reverse-Stochastic-Momentum-Index-On-Chart/

I have modified the code to only show the Reverse Stochastic Momentum Index midline. I can't figure out how to code "f_reverse_SMI" function. Thanks for any help!!

//
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @version = 4
// @author = The_Caretaker
//
// Much respect to Alex Orekhov (everget) for sharing the Stochastic Momentum Index script which I based this indicator on.
// Greatly inspired by the original paper from William Blau, the inventor of the Stochastic Momentum Index
// and the prior works of Giorgos Siligardos, Dimitris Tsokakis and Johny Dough in reverse engineering momentum oscillators.
//
// Feel free to reuse or develop this script further, please drop me a note below if you find it useful.
//

study ( "Reverse Stochastic Momentum Index On Chart", "RSMI OC", true )

indicator('Reset Every (Price)', overlay=true, max_bars_back=1000)

// Inputs and global variable declarations
i_srcPrice = input(close, 'SMI Price Source')
i_SMI_len = input.int(10, 'SMI Length', minval=1)
i_smth1 = input.int(3, 'Smooth Length 1', minval=1)
i_smth2 = input.int(3, 'Smooth Length 2', minval=1)
i_sigLen = input.int(10, 'Signal Length', minval=1)

a(x) =>
2 / (x + 1)

f_reverse_SMI(P, U, W, X, Y, Z) =>
V = 0.5
H = ta.highest(W)
L = ta.lowest(W)
D = ta.ema(P - V * (H + L), X)[1]
E = ta.ema(a(X) * (P - V * (H + L)) + D - D * a(X), Y)[1]
F = ta.ema(H - L, X)[1]
G = ta.ema(a(X) * (H - L) + F * (1 - a(X)), Y)[1]
J = 100 * (a(Y) * (a(X) * (P - V * (H + L)) + D - D * a(X)) + E * (1 - a(Y))) / (V * (a(Y) * (a(X) * (H - L) + F * (1 - a(X))) + G * (1 - a(Y))))[1]
K = ta.ema(100 * (a(Y) * (a(X) * (P - V * (H + L)) + D - D * a(X)) + E * (1 - a(Y))) / (V * (a(Y) * (a(X) * (H - L) + F * (1 - a(X))) + G * (1 - a(Y)))), Z)[1]
rawReturn = (V * U * (a(Y) * a(X) * H - a(Y) * a(X) * L - a(Y) * F * a(X) + a(Y) * F - G * a(Y) + G) + 100 * (a(Y) * a(X) * V * H + a(Y) * a(X) * V * L - a(Y) * D + a(Y) * D * a(X) + E * a(Y) - E)) / (100 * a(Y) * a(X))
return_1 = rawReturn > 0 ? rawReturn : 0
return_1

SMINumerator = ta.ema(ta.ema(i_srcPrice - 0.5 * (ta.highest(i_SMI_len) + ta.lowest(i_SMI_len)), i_smth1), i_smth2)
SMIDenominator = 0.5 * ta.ema(ta.ema(ta.highest(i_SMI_len) - ta.lowest(i_SMI_len), i_smth1), i_smth2)
SMI = 100 * SMINumerator / SMIDenominator
SMI_eq = f_reverse_SMI(i_srcPrice, SMI[1], i_SMI_len, i_smth1, i_smth2, i_sigLen)

p_SMI_eqPlot = plot(SMI_eq, 'SMI EQ Price', color.rgb(3, 49, 87), 2, plot.style_linebr, transp=0)[/CODE]
try this

CSS:
``````#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// Much respect to Alex Orekhov (everget) for sharing the Stochastic Momentum Index script which I based this indicator on.
#// Greatly inspired by the original paper from William Blau, the inventor of the Stochastic Momentum Index
#// and the prior works of Giorgos Siligardos, Dimitris Tsokakis and Johny Dough in reverse engineering momentum oscillators.
#study ( "Reverse Stochastic Momentum Index On Chart", "RSMI OC", true )
# Converted by [email protected] - 01/2023 - request from UseThinkScript.com member

input ShowLabel  = yes;#        "Show Alert Levels Info"
input srcPrice  = close;#       "SMI Price Source"
input SMI_len   = 13;#          "SMI Length"
input SmoothLength1  = 25;#     "Smooth Length 1"
input SmoothLength2  = 2;#      "Smooth Length 2"
input sigLen     = 12;#         "Signal Length"
input PricesDecimal  = 2;#      "Prices Decimal Places"

def na = Double.NaN;
def ScaleHi     =  100;
def ScaleLo     = -100;

#// Declare Functions
#f_truncdNum ( Val, DecPl ) =>
script f_truncdNum {
input Val = close;
input DecPl = 2;
def Fact = Power( 10, DecPl );
def f_truncdNum = Floor( Val * Fact) / Fact;
plot return = f_truncdNum;
}
# // decimal truncation

script a {
input x = 10;
def a = 2 / (x + 1);
plot return = a;
}
# // exponentially weighted multiplier
#f_reverse_SMI ( P, U, W, X, Y, Z ) =>
script f_reverse_SMI {
input P = 0;
input U = 0;
input W = 0;
input X = 0;
input Y = 0;
input Z = 0;
def V = 0.5;
def H = Highest(high, W);
def L = Lowest(low, W);
def D = ExpAverage(( P - V * ( H + L )), X )[1];
def E = ExpAverage((( a(X) * ( P - V * ( H + L ))) + ( D - D * a(X))), Y )[1];
def F = ExpAverage( H - L , X )[1];
def G = ExpAverage((( a(X) * ( H - L ) +  F * ( 1 - a(X)))), Y )[1];
def J1 = (( a(Y) * ( ( a(X) * ( P - V * ( H + L ))) + ( D - D * a(X)))) + ( E * ( 1 - a(Y)) )) / ( V * (a(Y) * ((a(X) * ( H - L ) +  F * ( 1 - a(X)))) + ( G * ( 1 - a(Y)))));
def J = 100 * J1[1];
def K = ExpAverage( ( 100 * (( a(Y) * ( ( a(X) * ( P - V * ( H + L ))) + ( D - D * a(X)))) + ( E * ( 1 - a(Y)) )) / ( V * (a(Y) * ((a(X) * ( H - L ) +  F * ( 1 - a(X)))) + ( G * ( 1 - a(Y)))))), Z )[1];
def rawReturn = ( V * U * (a(Y) * a(X) * H - a(Y) * a(X) * L - a(Y) * F * a(X) + a(Y) * F - G * a(Y) + G) + 100 * (a(Y) * a(X) * V * H + a(Y) * a(X) * V * L - a(Y) * D + a(Y) * D * a(X) + E * a(Y) - E)) / ( 100 * a(Y) * a(X));
plot return = if rawReturn > 0 then rawReturn else 0;
}
#f_reverse_SMI_cross ( P, W, X, Y, Z ) =>
script f_reverse_SMI_cross {
input P = 0;
input W = 0;
input X = 0;
input Y = 0;
input Z = 0;
def V = 0.5;
def H = Highest(high, W);
def L = Lowest(low, W);
def D = ExpAverage(( P - V * ( H + L )), X )[1];
def E = ExpAverage(((a(X) * ( P - V * ( H + L ))) + ( D - D * a(X))), Y )[1];
def F = ExpAverage( H - L , X )[1];
def G = ExpAverage(((a(X) * ( H - L ) +  F * ( 1 - a(X)))), Y )[1];
def J1 = (( a(Y) * ( ( a(X) * ( P - V * ( H + L ))) + ( D - D * a(X)))) + ( E * ( 1 - a(Y)) )) / ( V * (a(Y) * ((a(X) * ( H - L ) +  F * ( 1 - a(X)))) + ( G * ( 1 - a(Y)))));
def J = 100 * J1[1];
def K = ExpAverage( ( 100 * (( a(Y) * ( ( a(X) * ( P - V * ( H + L ))) + ( D - D * a(X)))) + ( E * ( 1 - a(Y)) )) / ( V * (a(Y) * ((a(X) * ( H - L ) +  F * ( 1 - a(X)))) + ( G * ( 1 - a(Y)))))), Z )[1];
def rawReturn = ( a(Y) * (100 * ( a(Z) * (-a(X) * V * H - a(X) * V * L + D - D * a(X) - a(X) - E) + a(X) * V * H + a(X) * V * L - D + D * a(X) + E) + V * K * (a(X) * (-H * a(Z) + H + L * a(Z) - L + F * a(Z) - F) - F * a(Z) + F + G * a(Z) - G)) + 100 * (a(Z) * E - E) - V * K * G * a(Z) + V * K * G) / (100 * a(Y) * a(X) * (-a(Z) + 1));
plot return = if rawReturn > 0 then rawReturn else 0;
}
#f_negVal ( X, D ) =>
script f_negVal {
input X = close;
input D = 0;
def f_negVal = if X > 0 then f_truncdNum ( X, D ) else Double.NaN;
plot return = f_negVal;
}
#// Calculations

def SMINumerator = ExpAverage(ExpAverage(srcPrice-0.5*(Highest(high,SMI_len)+Lowest(low,SMI_len)),SmoothLength1),SmoothLength2);
def SMIDenominator = 0.5*ExpAverage(ExpAverage(Highest(high, SMI_len) - Lowest(low, SMI_len), SmoothLength1 ), SmoothLength2 );
def SMI             = 100 * SMINumerator / SMIDenominator;
def SMI_eq          = f_reverse_SMI (srcPrice, SMI[1], SMI_len, SmoothLength1, SmoothLength2, sigLen );
def alrtHilineCross = f_reverse_SMI (srcPrice, AlertHi, SMI_len, SmoothLength1, SmoothLength2, sigLen );
def zerolineCross   = f_reverse_SMI (srcPrice, 0, SMI_len, SmoothLength1, SmoothLength2, sigLen );
def alrtLolineCross = f_reverse_SMI (srcPrice, AlertLo, SMI_len, SmoothLength1, SmoothLength2, sigLen );
def signalCross     = f_reverse_SMI_cross (srcPrice, SMI_len, SmoothLength1, SmoothLength2, sigLen );

#// SMI Plots & Fills

plot p_alrtHiPlot    = if AlertLines then alrtHilineCross else na;    # "High Alert Level"
p_alrtHiPlot.SetDefaultColor(Color.CYAN);
plot p_alrtLoPlot    = if AlertLines then alrtLolineCross else na;    # "Low Alert Level"
p_alrtLoPlot.SetDefaultColor(Color.MAGENTA);

plot p_SMI_eqPlot    = SMI_eq;            # "SMI EQ Price"
p_SMI_eqPlot.SetLineWeight(2);
p_SMI_eqPlot.AssignValueColor(if SMI_eq < srcPrice then color.DARK_GREEN else color.DARK_RED);

plot p_smiPlot       = signalCross;       # "Signal Line Cross"
p_smiPlot.SetLineWeight(2);
p_smiPlot.AssignValueColor(if signalCross < srcPrice then CreateColor(14,187,35) else Color.RED);
plot p_MidLinePlot   = zerolineCross;     # "Zero Line Cross"
p_MidLinePlot.SetDefaultColor(Color.WHITE);

def text_eq      = srcPrice > SMI_eq;
def Signal_delta = signalCross - srcPrice > 0;
def Zero_delta   = zerolineCross - srcPrice > 0;
def Hi_delta     = alrtHilineCross - srcPrice > 0;
def Lo_delta     = alrtLolineCross - srcPrice > 0;

def SMIeq = floor(SMI_eq * power(10,PricesDecimal))/power(10,PricesDecimal);
def crossSignal = f_negVal(signalCross, PricesDecimal);
def crossZero   = f_negVal(zerolineCross, PricesDecimal);
def crossAlrtHi = f_negVal(alrtHilineCross, PricesDecimal);
def crossAlrtLo = f_negVal(alrtLolineCross, PricesDecimal);

AddLaBel(ShowLabel and text_eq,  "Continues Rising Above (Eq): " + SMIeq, Color.LIGHT_GREEN);
AddLaBel(ShowLabel and !text_eq, "Continues Falling Below (Eq): " + SMIeq, Color.PINK);

AddLaBel(ShowLabel and Signal_delta,  "Cross Above Signal Line: " + crossSignal, Color.LIGHT_GREEN);
AddLaBel(ShowLabel and !Signal_delta, "Cross Below Signal Line: " + crossSignal, Color.PINK);

AddLaBel(ShowLabel and Zero_delta,  "Cross Above Zero Line: " + crossZero, Color.LIGHT_GREEN);
AddLaBel(ShowLabel and !Zero_delta, "Cross Below Zero Line: " + crossZero, Color.PINK);

#-- END CODE``````

Thanks a lot Samer800!! Exactly what i needed. Appreciate the help!!

87k+ Posts
177 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?