#### netarchitech

##### Well-known member

My first TradingView port...Looking forward to thoughts, feedback, backtests, etc...

Code:

```
# filename: _TnB_Candle_Identifier_
# source: https://www.tradingview.com/script/wkUMNJQm-UCS-Top-Bottom-Candle/
# Original idea and execution:
# Short Term Top and Bottom Candle Identifier
# Code by UCSgears
# initial port by netarchitech
# 2019.11.04
declare lower;
input percentKlength = 5;
input percentDlength = 3;
# Range Calculation
def ll = lowest (low, percentKlength);
def hh = highest (high, percentKlength);
def diff = hh - ll;
def rdiff = close - (hh+ll)/2;
# Nested Moving Average for smoother curves
def avgrel = expaverage(expaverage(rdiff,percentDlength),percentDlength);
def avgdiff = expaverage(expaverage(diff,percentDlength),percentDlength);
# Momentum
def mom = ((close - close[percentDlength])/close[percentDlength])*1000;
# SMI (Stochastic Momentum Index) calculations
plot SMI = if avgdiff != 0 then (avgrel/(avgdiff/2)*100) else 0;
SMI.SetDefaultColor(Color.GREEN);
SMI.SetLineWeight(2);
plot SMIsignal = expaverage(SMI,percentDlength);
SMIsignal.SetDefaultColor(Color.RED);
SMIsignal.SetLineWeight(2);
#UI enhancements
plot ZeroLine = 0;
Zeroline.SetDefaultColor(GetColor(4));
def over_bought = 40;
plot overbought = over_bought;
overbought.SetDefaultColor(GetColor(4));
overbought.HideTitle();
def over_sold = -40;
plot oversold = over_sold;
oversold.SetDefaultColor(GetColor(4));
oversold.HideTitle();
AddCloud(if SMI >= 40 then SMI else Double.NaN, over_bought, Color.RED, Color.RED);
AddCloud(if SMI <= -40 then SMI else Double.NaN, over_sold, Color.GREEN, Color.GREEN);
AddCloud(Zeroline, over_bought, Color.DARK_RED, Color.DARK_RED);
AddCloud(over_sold, Zeroline, Color.DARK_GREEN, Color.DARK_GREEN);
# Strategy Signals
#def long = if SMI < 35 and mom > 0 and mom[1] < 0 then Color.GREEN else Double.NaN;
def long = SMI < 35 and mom > 0 and mom[1] < 0;
#def short = if SMI > 35 and mom < 0 and mom[1] > 0 then Color.RED else Double.NaN;
def short = SMI > 35 and mom < 0 and mom[1] > 0;
AssignPriceColor(if long
then Color.GREEN
else if short
then Color.RED
else Color.GRAY);
input showBreakoutSignals = yes;
# plot the Breakout Signals
plot UpSignal = if SMI crosses above OverSold then OverSold else Double.NaN;
UpSignal.SetHiding(!showBreakoutSignals);
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UpSignal.HideTitle();
plot DownSignal = if SMI crosses below OverBought then OverBought else Double.NaN;
DownSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DownSignal.HideTitle();
```

Good Luck and Good Trading

Last edited: