Indicator · Pine Script v4 · Own pane

UCS Stochastic Momentum Index

by amico1234· code credited to amico5

This indicator calculates a Stochastic Momentum Index and is set to draw in a separate pane.

View on GitHub

GitHub stars1on the repository
Pine versionv4older, still runs
Lines of code326
Settings20inputs
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

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

trade.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. version number (should b ver' 4. after tested, will b moved to ver' 5)
// 2. study / inducator (must have only one for all the script)
// 3. indicators numbers, indicator title
// 4. input variables
// 5. CALCULATIONS
// 6. plots Entry
// 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)
// # ========================================================================= #




// # ========================================================================= #
// # script version
// # ========================================================================= #

//@version=4

// # ========================================================================= #
// # script study or indicator
// # ========================================================================= #

// # ========================================================================= #
// "study" lione should be for all the script. one "study" for all indicators.
// # ========================================================================= #

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


// # ========================================================================= #
// 1st 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 = #FF0000 , title = "SMI Signal Line")

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

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

one_Buy = crossover (one_SMI, one_os) // greater (only once)
one_Sell = crossunder(one_SMI, one_ob) // less than (only once)

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

alertcondition(one_Buy, title="one_Buy Signal", message="one_Buy")
alertcondition(one_Sell, title="not_one_Sell Signal", message="not_one_Sell")







// 2nd indicator

// study("Overbought Oversold Indicator")

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")

// the actual definition of the alerts to trade

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

two_Buy = two_up > two_down
two_Sell = two_up < two_down




//
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)
//
alertcondition(two_Buy, title="two_Buy Signal", message="two_Buy")
alertcondition(two_Sell, title="two_Sell Signal", message="two_Sell")



// 3rd indicator

// study(title="WaveTrend [LazyBear] copy", shorttitle="WT_LB_copy")
n1 = input(10, "Channel Length")
n2 = input(21, "Average Length")
obLevel1 = input(60, "Over Bought Level 1")
obLevel2 = input(53, "Over Bought Level 2")
osLevel1 = input(-60, "Over Sold Level 1")
osLevel2 = input(-53, "Over Sold Level 2")
 
ap = hlc3 
esa = ema(ap, n1)
d = ema(abs(ap - esa), n1)
ci = (ap - esa) / (0.015 * d)
tci = ema(ci, n2)
 
wt1 = tci
wt2 = sma(wt1,4)

plot(0, color=gray)
p55 = plot(obLevel1, color=red)
p5 = plot(osLevel1, color=green)
p555 = plot(obLevel2, color= orange, style=3)
p7 = plot(osLevel2, color= green, style=3)

shortrig = plot(wt1, color= blue, title="shortrig")
longsig = plot(wt2, color= white, style=3, title = "longsig")
p77 = plot(wt1-wt2, color= orange, style=area, transp=80)



// what is BUY?, SELL ?  ("the actual definition of the alerts to trade")





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

// advanced  trade sample

//MA Distance plots the price's distance from a given MA in percent. It's color coded green (positive) or red (negative).

//MA types are:
// 1 - simple
// 2 - exponential
// 3 - Hull
// 4 - weighted
// 5 - volume weighted

// study(title = "Distance from MA", shorttitle="MA_dist")


length = input(50, minval=1, title="Length")
src = input(close, title="Source")
matype = input(1, minval=1, maxval=5, title="AvgType")

simplema = sma(src,length)
exponentialma = ema(src,length)
hullma = wma(2*wma(src, length/2)-wma(src, length), round(sqrt(length)))
weightedma = wma(src, length)
volweightedma = vwma(src, length)

avgval = matype==1 ? simplema : matype==2 ? exponentialma : matype==3 ? hullma : matype==4 ? weightedma : matype==5 ? volweightedma : na

distance=((close/avgval)-1)*100

Pcolor = distance > 0 ? color.green : color.red
// Pcolor = avgval > avgval[1] ? color.green : color.red

plot(distance,title='Color MA', color=Pcolor, linewidth=4)




// what is BUY?, SELL ?  ("the actual definition of the alerts to trade")








////////////////////////////////////////////////////////////////////////////////////////////////////////
study(title="Mirror TSI-MACD by Trader Jay", shorttitle="Mirror_TSI_MACD")

// Getting inputs
fast_length = input(title="Fast Length", type=input.integer, defval=8)
slow_length = input(title="Slow Length", type=input.integer, defval=21)
First_R = input(title="First R", type=input.integer, defval = 8)
Second_S = input(title="Second S", type=input.integer, defval=5)
src = input(title="Source", type=input.source, defval=close)
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
sma_signal = input(title="Simple MA(Signal Line)", type=input.bool, defval=false)


// Plot colors
col_grow_above = #26A69A
col_grow_below = #FFCDD2
col_fall_above = #B2DFDB
col_fall_below = #EF5350
col_macd = #0094ff
col_signal = #00ff00
col_mirror_macd = #ff0000

// Calculating
fast_ma =  ema(src, fast_length)
slow_ma =  ema(src, slow_length)
macd = fast_ma - slow_ma
/////

MTM = macd - macd[1]
ABSMTM = abs(MTM)

EMA_MTM = ema(MTM, First_R)
EMA_ABSMTM = ema(ABSMTM, First_R)

EMA2_MTM = ema(EMA_MTM, Second_S)
EMA2_ABSMTM = ema(EMA_ABSMTM, Second_S)

TSI = 100.0*EMA2_MTM/EMA2_ABSMTM
Mirror_TSI = -100.0*EMA2_MTM/EMA2_ABSMTM

signal = sma_signal ? sma(TSI, signal_length) : ema(TSI, signal_length)

plotshape(crossover(TSI,Mirror_TSI), color = color.green, style = shape.cross, location = location.top)
plotshape(crossover(Mirror_TSI,TSI), color = color.red, style = shape.cross, location = location.top)



hline(0)
plot(TSI, title="TSI_MACD", color=col_macd, transp=0)
plot(signal, title="Signal", color=col_signal, transp=0)
plot(Mirror_TSI, title="TSI_Mirror", color=col_mirror_macd)


// what is BUY?, SELL ?  ("the actual definition of the alerts to trade")




// # ========================================================================= #
//  add z-score ?, add two tsi
// # ========================================================================= #

Overview

UCS Stochastic Momentum Index finds the highest high and lowest low over the Percent K length, measures the close against the midpoint of that range, and smooths both the relative distance and the range with a nested exponential moving average. Percent K and Percent D lengths change the lookback and smoothing. Other inputs set overbought and oversold levels, channel and average lengths, and fast and slow periods. The shown code has no entry, exit, or stop rules.

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
Channel Lengthint10n1
Average Lengthint21n2
Over Bought Level 1int60obLevel1
Over Bought Level 2int53obLevel2
Over Sold Level 1int-60osLevel1
Over Sold Level 2int-53osLevel2
Lengthint50length
Sourcesourceclosesrc
Show all 20 settings
SettingTypeDefaultVariable
AvgTypeint1matype
Fast Lengthint8fast_length
Slow Lengthint21slow_length
First Rint8First_R
Second Sint5Second_S
Sourcesourceclosesrc
Signal Smoothingint9signal_length
Simple MA(Signal Line)boolfalsesma_signal
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