Indicator · Pine Script v4 · Own pane

UCS Stochastic Momentum Index

by amico1234· code credited to amico5

This indicator draws a stochastic momentum oscillator in a separate pane.

View on GitHub

GitHub stars1on the repository
Pine versionv4older, still runs
Lines of code547
Settings18inputs
LicenceNone statedfile: MPL-2.0
Last update2 years ago2 Mar 2024

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

547 lines of Pine Script v4 from 080422/trade_bck.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

trade_bck.pine

/// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © amico5


// # ========================================================================= #
// # script overview
// # ========================================================================= #

// # ========================================================================= #
// 1. simple test case for alerts.
// 2. two alerts: long only basic, long only extended
// 3. further buy, if there is alert on index, fut.
// 4. all alert cond, must be monitored to  buy.
// 5. watch not to buy multiple times on the same subscript.
// 6. backtesting needed.
// 7. test on sandbox broker needed.
// 8. test  3 commodities.
// # ========================================================================= #


// # ========================================================================= #
// # script structure
// # ========================================================================= #

// # ========================================================================= #
// 1. overview
// 2. structure
// 3. main script version number (should b ver' 4. after tested, will b moved to ver' 5)
// 4. main script's: study / inducator (must have only one for all the script)
// 5. sub indicators numbers
// 6. sub indicators title
// 7. input variables
// 8. CALCULATIONS
// 9. plots Entry
// 10. trade definition (for the specific indicator)
// 11. compounding trade definitions (for the indicators, so far)
// 12. alerts Conditions (for the specific indicator)
// 13. compounding alerts Conditions (for the comulative, so far)
// # ========================================================================= #


// # ========================================================================= #
// # main script version number
// # ========================================================================= #

//@version=4

// # ========================================================================= #
// # main script's: study / inducator (must have only one for all the script)
// # ========================================================================= #

// indicator('one_indicator_UCS_Stochastic Momentum Index', shorttitle='oneindicator_UCS_SMI', overlay=false)

study ('one_indicator_UCS_Stochastic Momentum Index', shorttitle='oneindicator_UCS_SMI', overlay=false)

// # ========================================================================= #
// # sub indicators numbers
// # ========================================================================= #

// first indicator 


// # ========================================================================= #
// input variables
// # ========================================================================= #

one_a = input(5, 'Percent K Length')
one_b = input(3, 'Percent D Length')

// # ========================================================================= #
// Range Calculation
// # ========================================================================= #

one_ll = lowest(low, one_a)
one_hh = highest(high, one_a)
one_diff = one_hh - one_ll
one_rdiff = close - (one_hh + one_ll) / 2

// # ========================================================================= #
// Nested Moving Average for smoother curves
// # ========================================================================= #

one_avgrel = ema(ema(one_rdiff, one_b), one_b)
one_avgdiff = ema(ema(one_diff, one_b), one_b)

// # ========================================================================= #
// SMI calculations
// # ========================================================================= #

one_SMI = one_avgdiff != 0 ? one_avgrel / (one_avgdiff / 2) * 100 : 0
one_SMIsignal = ema(one_SMI, one_b)

// # ========================================================================= #
// ob, os, zeroline calc
// # ========================================================================= #

one_ob = 40
one_os = -40
one_zeroline = 0

// # ========================================================================= #
// All PLOTS
// # ========================================================================= #

plot(one_SMI, title='Stochastic Momentum Index')
plot(one_SMIsignal, color=color.new(#FF0000, 0), title='SMI Signal Line')

one_ob_plot = plot(40, color=color.new(#FFCDD2, 0), title='one_Over_Bought_plot')
one_os_plot = plot(-40, color=color.new(#008000, 0), title='one_Over_Sold_plot')
one_zeroline_plot = plot(0, color=color.new(#0094ff, 0), title='one_Zero_Line_plot')

// # ========================================================================= #
// trade definition (for the specific indicator)
// # ========================================================================= #

// one_Buy = crossover (one_SMI, one_os)
// one_Sell = crossunder(one_SMI, one_ob)

one_Buy_5 = one_SMI > one_os 
one_Sell_5 = one_SMI < one_ob 

one_Buy_7_maybe = one_SMI > one_SMIsignal
one_Sell_7_maybe = one_SMI < one_SMIsignal


one_SMI_stat_less_zero = one_SMI < one_zeroline // buy
one_SMI_stat_above_zero = one_SMI > one_zeroline // sell


// second option //
//
// one_Buy = if one_SMI > -40
//     close
// one_Sell = if one_SMI < 40
//     close

// with other option than above / less //

// one_sell_test = crossunder(one_SMI, one_SMIsignal)
// one_buy_test = crossover(one_SMI, one_SMIsignal)


// # ========================================================================= #
// compounding trade definitions (for the indicators, so far)
// # ========================================================================= #


// compoundAlertBuy = one_Buy_5 and one_Buy_7_maybe 
// compoundAlertSell = one_Sell_5 and one_Sell_7_maybe

// with only zero
one_compoundAlertBuyExtraZero = one_Buy_5 and one_Buy_7_maybe and one_SMI_stat_less_zero
one_compoundAlertSellExtraZero = one_Sell_5 and one_Sell_7_maybe and one_SMI_stat_above_zero

// # ========================================================================= #
// alerts Conditions (for the specific indicator)
// # ========================================================================= #

// should be only alerting one time //


// alertcondition(one_Buy, title='one_Buy Signal', message='one_Buy')
// alertcondition(one_Sell, title='not_one_Sell_Signal', message='not_one_Sell')

// alertcondition (one_Sell_5, title= "one_sell_5", message ="one_sell_5")
// alertcondition (one_Buy_5, title= "one_buy_5", message ="one_buy_5")
// alertcondition (one_Sell_7_maybe, title= "one_Sell_7_maybe", message ="one_Sell_7_maybe")
// alertcondition (one_Buy_7_maybe, title= "one_Buy_7_maybe", message ="one_Buy_7_maybe")


// # ========================================================================= #
// compound alerts condition
// # ========================================================================= #

// alertcondition (compoundAlertBuy, title= "compoundAlertBuy", message ="ocompoundAlertBuy")

// with extra zero
alertcondition (one_compoundAlertBuyExtraZero, title= "one_compoundAlertBuyExtraZero", message ="one_compoundAlertBuyExtraZero")
alertcondition (one_compoundAlertSellExtraZero, title= "one_compoundAlertSellExtraZero", message ="one_compoundAlertSellExtraZero")


///////////////////////////////////////////////////////////////////////////


// --second indicator-- //

// ---- //
//      //
//      //
// -  - //
//      //
//      //
// ---- //




// # ========================================================================= #
// # indicator number
// # ========================================================================= #

// 2nd indicator

// # ========================================================================= #
// # indicator title
// # ========================================================================= #

// Overbought oversold

//

two_n = input(5, title="Length")
two_Barcolor=input(true,title="Barcolor")
two_ys1 = (high + low + close * 2) / 4
two_rk3 = ema(two_ys1, two_n)
two_rk4 = stdev(two_ys1, two_n)
two_rk5 = (two_ys1 - two_rk3) * 100 / two_rk4
two_rk6 = ema(two_rk5, two_n)
two_up = ema(two_rk6, two_n)
two_down = ema(two_up, two_n)

two_Oo = iff(two_up < two_down, two_up, two_down)
two_Hh = two_Oo
two_Ll = iff(two_up < two_down, two_down, two_up)
two_Cc = two_Ll

two_b_color = iff(two_Oo[1] < two_Oo and two_Cc < two_Cc[1], #FFFF00, iff(two_up > two_down, #008000, #FF0000))
barcolor(two_Barcolor ? two_b_color : na)

hline(0)
plot(two_Oo, style=plot.style_histogram, color=two_b_color)
plot(two_Ll, style=plot.style_histogram, color=two_b_color)

plot(two_up, color=two_b_color,title="Up")
plot(two_down, color=two_b_color,title="Down")

// # ========================================================================= #
// trade definition (for the specific indicator)
// # ========================================================================= #


// Buy = crossover(up, down)
// Sell = crossunder(up, down)

two_Buy = two_up > two_down
two_Sell = two_up < two_down


// # ========================================================================= #
// Compound Trade Definition (for the specific indicator)
// # ========================================================================= #


two_compoundAlertBuy = one_Buy_5 and one_Buy_7_maybe and one_SMI_stat_less_zero and two_Buy
two_compoundAlertSell = one_Sell_5 and one_Sell_7_maybe and one_SMI_stat_above_zero and two_Sell






//
plotshape(two_Buy, title="two_Buy", color=#008000, style=shape.triangleup, location=location.bottom, text="Buy",size=size.tiny)
plotshape(two_Sell, title="two_Sell", color=#FF0000, style=shape.triangledown, location=location.top, text="Sell",size=size.tiny)
//

// # ========================================================================= #
// alerts condition
// # ========================================================================= #

// alertcondition(two_Buy, title="two_Buy Signal", message="two_Buy")
// alertcondition(two_Sell, title="two_Sell Signal", message="two_Sell")


// # ========================================================================= #
// compound alerts condition
// # ========================================================================= #

alertcondition (two_compoundAlertBuy, title= "two_compoundAlertBuy", message ="two_ocompoundAlertBuy")
alertcondition (two_compoundAlertSell, title= "two_compoundAlertSell", message ="wo_compoundAlertSell")


// 3rd indicator //


///////////////////////////////////////////////////////////////////////////


three_n1 = input(10, "three_Channel Length")
three_n2 = input(21, "three_Average Length")
three_obLevel1 = input(60, "three_Over Bought Level 1")
three_obLevel2 = input(53, "three_Over Bought Level 2")
three_osLevel1 = input(-60, "three_Over Sold Level 1")
three_osLevel2 = input(-53, "three_Over Sold Level 2")
 
three_ap = hlc3 
three_esa = ema(three_ap, three_n1)
three_d = ema(abs(three_ap - three_esa), three_n1)
three_ci = (three_ap - three_esa) / (0.015 * three_d)
three_tci = ema(three_ci, three_n2)
 
three_wt1 = three_tci
three_wt2 = sma(three_wt1,4)

plot(0, color=#7b8da6)
three_p55 = plot(three_obLevel1, color=#ff2c55)
three_p5 = plot(three_osLevel1, color=#99cc33)
three_p555 = plot(three_obLevel2, color=#f49f0a, style=3)
three_p7 = plot(three_osLevel2, color= #ba9588, style=3)

three_shortrig = plot(three_wt1, color=#6b5d52, title="three_shortrig")
three_longsig = plot(three_wt2, color= #f5cff2, style=3, title = "three_longsig")
three_p77 = plot(three_wt1-three_wt2, color= #ffd680, style= 3, transp=80)


// # ========================================================================= #
// buy, sell Trade Definition
// # ========================================================================= #

three_Buy = three_wt1 > three_wt2
three_Sell = three_wt1 < three_wt2

// # ========================================================================= #
// Compound Trade Definition
// # ========================================================================= #


three_compoundAlertBuy = one_Buy_5 and one_Buy_7_maybe and two_Buy and three_Buy
three_compoundAlertSell = one_Sell_5 and one_Sell_7_maybe and two_Sell and three_Sell


// # ========================================================================= #
// compound alerts condition
// # ========================================================================= #

alertcondition (three_compoundAlertBuy, title= "three_compoundAlertBuy", message ="three_ocompoundAlertBuy")


//////////////////////////////////////////////////////////////

// 4th indicator



// Getting inputs
four_fast_length = input(title="four_Fast Length", type=input.integer, defval=8)
four_slow_length = input(title="four_Slow Length", type=input.integer, defval=21)
four_First_R = input(title="four_First R", type=input.integer, defval = 8)
four_Second_S = input(title="four_Second S", type=input.integer, defval=5)
four_src = input(title="four_Source", type=input.source, defval=close)
four_signal_length = input(title="four_Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
four_sma_signal = input(title="four_Simple MA(Signal Line)", type=input.bool, defval=false)


// Plot colors
four_col_grow_above = #26A69A
four_col_grow_below = #FFCDD2
four_col_fall_above = #B2DFDB
four_col_fall_below = #EF5350
four_col_macd = #0094ff
four_col_signal = #00ff00
four_col_mirror_macd = #ff0000

// Calculating
four_fast_ma =  ema(four_src, four_fast_length)
four_slow_ma =  ema(four_src, four_slow_length)
four_macd = four_fast_ma - four_slow_ma
/////

four_MTM = four_macd - four_macd[1]
four_ABSMTM = abs(four_MTM)

four_EMA_MTM = ema(four_MTM, four_First_R)
four_EMA_ABSMTM = ema(four_ABSMTM, four_First_R)

four_EMA2_MTM = ema(four_EMA_MTM, four_Second_S)
four_EMA2_ABSMTM = ema(four_EMA_ABSMTM, four_Second_S)

four_TSI = 100.0*four_EMA2_MTM/four_EMA2_ABSMTM
four_Mirror_TSI = -100.0*four_EMA2_MTM/four_EMA2_ABSMTM

four_signal = four_sma_signal ? sma(four_TSI, four_signal_length) : ema(four_TSI, four_signal_length)

plotshape(crossover(four_TSI,four_Mirror_TSI), color = color.green, style = shape.cross, location = location.top)
plotshape(crossover(four_Mirror_TSI,four_TSI), color = color.red, style = shape.cross, location = location.top)




// 7. trade definition (for the specific indicator)
// 8. compounding trade definitions (for the indicators, so far)
// 9. alerts Conditions (for the specific indicator)
// 10. compounding alerts Conditions (for the comulative, so far)






hline(0)
plot(four_TSI, title="four_TSI_MACD", color=four_col_macd, transp=0)
plot(four_signal, title="four_Signal", color=four_col_signal, transp=0)
plot(four_Mirror_TSI, title="four_TSI_Mirror", color=four_col_mirror_macd)





///////////

//5th indicator


// The author of this indicator is Veronique Valcu. The z-score (z) for a data 
// item x measures the distance (in standard deviations StdDev) and direction 
// of the item from its mean (U):
//     z = (x-StdDev) / U
// A value of zero indicates that the data item x is equal to the mean U, while 
// positive or negative values show that the data item is above (x>U) or below 
// (x Values of +2 and -2 show that the data item is two standard deviations 
// above or below the chosen mean, respectively, and over 95.5% of all data 
// items are contained within these two horizontal references (see Figure 1).
// We substitute x with the closing price C, the mean U with simple moving 
// average (SMA) of n periods (n), and StdDev with the standard deviation of 
// closing prices for n periods, the above formula becomes:
//     Z_score = (C - SMA(n)) / StdDev(C,n)
// The z-score indicator is not new, but its use can be seen as a supplement to 
// Bollinger bands. It offers a simple way to assess the position of the price 
// vis-a-vis its resistance and support levels expressed by the Bollinger Bands. 
// In addition, crossings of z-score averages may signal the start or the end of 
// a tradable trend. Traders may take a step further and look for stronger signals 
// by identifying common crossing points of z-score, its average, and average of average. 
////////////////////////////////////////////////////////////



//study(title="Z-Score", shorttitle="Z-Score")



five_Period = input(20, minval=1)
hline(0, color=#d575b6)
five_xStdDev = stdev(close, five_Period)
five_xMA = sma(close, five_Period)
five_nRes = (close - five_xMA) / five_xStdDev
plot(five_nRes, color=#f2e2ce, title="five_Z-Score")





// 7. trade definition (for the specific indicator)
// 8. compounding trade definitions (for the indicators, so far)
// 9. alerts Conditions (for the specific indicator)
// 10. compounding alerts Conditions (for the comulative, so far)







//////////////


// 6th indicator

// argotic tsi


// r - Length of first EMA smoothing of 1 day momentum        4
// s - Length of second EMA smoothing of 1 day smoothing      8    
// u- Length of third EMA smoothing of 1 day momentum         6  
// Length of EMA signal line                                  3
// Source of Ergotic TSI                                      Close
//
// This is one of the techniques described by William Blau in his book "Momentum,
// Direction and Divergence" (1995). If you like to learn more, we advise you to 
// read this book. His book focuses on three key aspects of trading: momentum, 
// direction and divergence. Blau, who was an electrical engineer before becoming 
// a trader, thoroughly examines the relationship between price and momentum in 
// step-by-step examples. From this grounding, he then looks at the deficiencies 
// in other oscillators and introduces some innovative techniques, including a 
// fresh twist on Stochastics. On directional issues, he analyzes the intricacies 
// of ADX and offers a unique approach to help define trending and non-trending periods. 
// You can use in the xPrice any series: Open, High, Low, Close, HL2, HLC3, OHLC4 and ect...
////////////////////////////////////////////////////////////
//study(title="Ergotic TSI Strategy")




// r = input(4, minval=1)
// s = input(8, minval=1)
// u = input(6, minval=1)
// SmthLen = input(3, minval=1)
// hline(0, color=blue, linestyle=line)
// xPrice = close
// xPrice1 = xPrice - xPrice[1]
// xPrice2 = abs(xPrice - xPrice[1])
// xSMA_R = ema(ema(ema(xPrice1,r), s),u)
// xSMA_aR = ema(ema(ema(xPrice2, r), s),u)
// Val1 = 100 * xSMA_R
// Val2 = xSMA_aR
// xTSI = iff (Val2 != 0, Val1 / Val2, 0)
// xEMA_TSI = ema(xTSI, SmthLen)
// pos =    iff(xTSI > xEMA_TSI, 1,
//         iff(xTSI < xEMA_TSI, -1, nz(pos[1], 0))) 
// barcolor(pos == -1 ? red: pos == 1 ? green : blue )
// plot(xTSI, color=green, title="Ergotic TSI")
// plot(xEMA_TSI, color=red, title="SigLin")



// // 7th indicator

// // lrs

// // Simple linear regression slope - Good way see if the trend is accelarating or decelarating

// // study(title="UCSGEARS - Linear Regression Slope", shorttitle="UCS-LRS", overlay=false)

// src = close
// //Input
// clen = input (defval = 50, minval = 1, title = "Curve Length")
// slen = input(defval=5, minval=1, title="Slope Length")
// glen = input(defval=13, minval=1, title="Signal Length")

// //Linear Regression Curve
// lrc = linreg(src, clen, 0)
// //Linear Regression Slope
// lrs = (lrc-lrc[1])/1
// //Smooth Linear Regression Slope
// slrs = ema(lrs, slen)
// //Signal Linear Regression Slope
// alrs = sma(slrs, glen)
// //loalrs = sma(slrs, (glen*5))

// uacce = lrs > alrs and lrs > 0 
// dacce = lrs < alrs and lrs < 0 

// scolor = uacce ? green : dacce ? red : blue

// plot(0, title = "Zero Line", color = gray)
// plot(slrs, color = scolor, title = "Linear Regression Slope", style = histogram, linewidth = 4)
// plot(alrs, color = gray, title = "Average Slope")

Overview

UCS Stochastic Momentum Index measures how far the close sits from the midpoint of the highest high and lowest low over the Percent K length, then double-smooths that relative distance and the full range with nested exponential moving averages using the Percent D length. Further inputs cover channel and average lengths, overbought and oversold thresholds, fast and slow lengths, signal smoothing, bar color, and a period. It is not a strategy and shows no entries, exits, or stop loss in the supplied code.

Details

Published by

amico1234 on GitHub

Code credit

amico5, as written in the script's header

Type

Indicator, Pine Script v4

Draws

Separate pane below the chart

Category

Oscillators & momentum

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Percent K Lengthint5one_a
Percent D Lengthint3one_b
Lengthint5two_n
Barcolorbooltruetwo_Barcolor
three_Channel Lengthint10three_n1
three_Average Lengthint21three_n2
three_Over Bought Level 1int60three_obLevel1
three_Over Bought Level 2int53three_obLevel2
three_Over Sold Level 1int-60three_osLevel1
three_Over Sold Level 2int-53three_osLevel2
four_Fast Lengthint8four_fast_length
four_Slow Lengthint21four_slow_length
Show all 18 settings
SettingTypeDefaultVariable
four_First Rint8four_First_R
four_Second Sint5four_Second_S
four_Sourcesourceclosefour_src
four_Signal Smoothingint9four_signal_length
four_Simple MA(Signal Line)boolfalsefour_sma_signal
five_Periodint20five_Period
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Asymmetrical RSI

by everget
948 stars

Asymmetrical RSI is an indicator that computes a custom relative strength index from the rate of change of a user-selected source. It plots the resulting ARSI line in a separate pane with color coding that turns green above the overbought level and red below t

Indicator v4 Oscillators & momentum
Updated 10 days ago · GPL-3.0 GitHub repository of Asymmetrical RSI

This indicator calculates momentum setups based on the Relative Strength Index (RSI) and plots in a separate pane. It offers multiple scanning modes such as Continuous Break, Continuous Retracement, Flip Break, Flip Retracement, and both regular and hidden div

Indicator v6 Oscillators & momentumDivergence Repaints
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - RSI Directional Momentum Indicator

Beep Boop

by magic8bot
409 stars

This indicator calculates a modified MACD oscillator and signal line alongside a trend EMA filter, plotting the output as a stylized column histogram in a separate pane. Instead of plotting true MACD distance, it quantifies positive and negative histogram mome

Indicator v4 Oscillators & momentumTrend
Updated 3 years ago · MIT GitHub repository of Beep Boop
261 stars

This indicator demonstrates an experimental graph-style scatter visualization within a separate pane using Pine Script arrays. It tracks two features across recent bars: the single-bar price change and a scaled 14-period Relative Strength Index. Values are loa

Indicator v4 Oscillators & momentum
Updated 4 years ago · None stated GitHub repository of Example: Graph Style - 4th Dimension RSI

KD指標

by TaichiS
219 stars

This script calculates and plots a Stochastic Oscillator (KD indicator) in a separate chart pane. It determines the fast stochastic value over a 9-period lookback, then applies a 3-period Simple Moving Average (SMA) to create the %K line, and a second 3-period

Indicator v4 Oscillators & momentum
Updated 5 years ago · None stated GitHub repository of KD指標

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers