Reverse Stochastic Momentum Index On Chart

The_Caretaker · study · 147 行 · 点赞 2,286 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//
// 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
// © 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 )
 
//////////////////////////////////////////////////////////////////////////////
// Inputs and global variable declarations
 
i_srcPrice  = input ( close,    "SMI Price Source",         input.source )
i_SMI_len   = input ( 13,       "SMI Length",               input.integer, minval = 1 )
i_smth1     = input ( 25,       "Smooth Length 1",          input.integer, minval = 1 )
i_smth2     = input ( 2,        "Smooth Length 2",          input.integer, minval = 1 )
i_sigLen    = input ( 12,       "Signal Length",            input.integer, minval = 1 )
i_alrtInfo  = input ( true,     "Show Alert Levels Info",   input.bool )
i_alrtHi    = input ( 40,       "Upper Alert Level",        input.float,   minval = -100, maxval = 100)
i_midLine   = input ( 0,        "Midline",                  input.integer, minval = -100, maxval = 100) 
i_alrtLo    = input ( -40,      "Lower Alert Level",        input.float,   minval = -100, maxval = 100)
i_alrtLines = input ( false,    "Show Alert Level Lines",   input.bool )
i_infoBox   = input ( true,     "Show Info Box",            input.bool )
i_decimalP  = input ( 2,        "Prices Decimal Places",    input.integer, minval = 0, maxval = 10)
i_boxOffSet = input ( 5,        "Info Box Offset",          input.integer, minval = 1, maxval = 50)
 
ScaleHi     =  100
ScaleLo     = -100
 
var label  Infobox      = na
 
///////////////////////////////////////////////////////////////////////////////
// Declare Functions
 
f_truncdNum ( Val, DecPl ) => 
    Fact = pow ( 10, DecPl )
    int( Val * Fact) / Fact
     
     // decimal truncation
 
a(x) => 2 / (x + 1)
     
     // exponentially weighted multiplier
 
f_reverse_SMI ( P, U, W, X, Y, Z ) =>
    V = 0.5
    H = highest(W)
    L = lowest(W)
    D = ema (( P - V * ( H + L )), X )[1]
    E = ema ((( a(X)* ( P - V * ( H + L )))+( D -D*a(X))), Y )[1]
    F = ema ( H - L , X )[1]
    G = 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 = 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 = rawReturn > 0 ? rawReturn : 0 
     
     // returns price where Stochastic Momentum Index is equal to input value "U"
     // e.g. f_reverse_SMI ( close, 0, 13, 25, 2, 12 ) 
     // would return the next closing price which would make SMI = 0
     // the user can infer from this that.....
     // closing above this price will cause the Stochastic Momentum Index to cross above the mid line
     // and closing below it will cause the Stochastic Momentum Index to cross below the mid line
     // This may also be used to give the SMI eq price (the price which would make the SMI equal to its prior value)
     // this is done by inputing the prior value of the SMI (SMI[1]) as the "U" value
     // e.g. f_reverse_SMI ( close, SMI[1], 13, 25, 2, 12 ) 
     // The user can infer from this that..... 
     // closing above this price will cause the Stochastic Momentum Index to increase
     // and closing below it will cause the Stochastic Momentum Index to decrease
     // has a Return filter to replace any values below zero with zero
 
f_reverse_SMI_cross ( P, W, X, Y, Z ) =>
    V = 0.5
    H = highest(W)
    L = lowest(W)
    D = ema (( P - V * ( H + L )), X )[1]
    E = ema (((a(X)* ( P - V * ( H + L )))+( D -D*a(X))), Y )[1]
    F = ema ( H - L , X )[1]
    G = 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 = 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 = ( 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))
    return = rawReturn > 0 ? rawReturn : 0
     
     // returns price where Stochastic Momentum Index is equal to the signal line
     // the user can infer from this that.....
     // closing above this price will cause the Stochastic Momentum Index to cross above the signal line
     // and closing below it will cause the Stochastic Momentum Index to cross below the signal line
     // has a Return filter to replace any values below zero with zero
 
f_delta ( P, X ) => X - P > 0
 
f_negVal ( X, D ) => X > 0 ? tostring ( f_truncdNum ( X, D )) : "Impossible" 
 
text_eq ( p, x, d ) => p > x ? "Continues Rising Above (eq) : " + tostring(int(x*pow(10,d))/pow(10,d)) : "Continues Falling Below (Eq) :" + tostring(int(x*pow(10,d))/pow(10,d))
 
f_crossText ( P, X, T, D ) => f_delta ( P, X ) ? "Cross Above " + T + " : " + f_negVal ( X, D ) + "\n" : "Cross Below " + T + " : " + f_negVal ( X, D ) + "\n" 
 
//////////////////////////////////////////////////////////////////////////////
// Calculations
 
SMINumerator        = ema ( ema ( i_srcPrice - 0.5 * ( highest (i_SMI_len) + lowest (i_SMI_len)), i_smth1 ), i_smth2 )
SMIDenominator      = 0.5 * ema ( ema ( highest (i_SMI_len) - 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 )
alrtHilineCross     = f_reverse_SMI ( i_srcPrice, i_alrtHi, i_SMI_len, i_smth1, i_smth2, i_sigLen )
zerolineCross       = f_reverse_SMI ( i_srcPrice, 0, i_SMI_len, i_smth1, i_smth2, i_sigLen )
alrtLolineCross     = f_reverse_SMI ( i_srcPrice, i_alrtLo, i_SMI_len, i_smth1, i_smth2, i_sigLen )
signalCross         = f_reverse_SMI_cross ( i_srcPrice, i_SMI_len, i_smth1, i_smth2, i_sigLen )
 
 
///////////////////////////////////////////////////////////////////////////////
// Compute Info Label
 
labelXLoc           = time_close + ( i_boxOffSet * ( time_close - time_close[1] ) )
crossSignalText     = f_crossText ( i_srcPrice, signalCross, "Signal Line", i_decimalP )
SMIeq               = text_eq     ( i_srcPrice, SMI_eq, i_decimalP )
crossZeroText       = f_crossText ( i_srcPrice, zerolineCross, "Zero Line", i_decimalP )
crossAlrtHiText     = f_crossText ( i_srcPrice, alrtHilineCross, "Alert Hi", i_decimalP )
crossAlrtLoText     = f_crossText ( i_srcPrice, alrtLolineCross, "Alert Lo", i_decimalP )
infoBoxText         = i_alrtInfo ? "SMI\n\n" + SMIeq + "\n\n" + crossAlrtHiText + "\n" + crossSignalText + "\n" + crossZeroText + "\n" + crossAlrtLoText : "SMI\n\n" + SMIeq + "\n\n" + crossSignalText + "\n" + crossZeroText
 
///////////////////////////////////////////////////////////////////////////////
// InfoBox Plot
 
if i_infoBox
    Infobox := label.new ( labelXLoc, close, infoBoxText, xloc.bar_time, yloc.price, #000000ff, label.style_label_left, color.white )
 
label.delete ( Infobox[1] )
 
///////////////////////////////////////////////////////////////////////////////
// SMI Plots & Fills
 
p_alrtHiPlot    = plot ( i_alrtLines ? alrtHilineCross : na, "High Alert Level", color.aqua,   1, plot.style_linebr,  transp = 50 )
p_alrtLoPlot    = plot ( i_alrtLines ? alrtLolineCross : na, "Low Alert Level",  color.purple, 1, plot.style_linebr,  transp = 50 )
p_SMI_eqPlot    = plot ( SMI_eq,        "Signal Line Cross", SMI_eq < i_srcPrice ? color.green: color.red,       2, plot.style_linebr, transp=0)
p_smiPlot       = plot ( signalCross,   "Signal Line Cross", signalCross < i_srcPrice ? #0ebb23 : #FF0000,  2, plot.style_linebr, transp=0)
p_MidLinePlot   = plot ( zerolineCross, "Zero Line Cross",   color.white,                                   1, plot.style_linebr, transp=0)
 
///////////////////////////////////////////////////////////////////////////////
// End

← 返回列表