Heikin Ashi RSI Oscillator for TOS

sam4cok

Member
Platform
  1. Thinkorswim
further details here. - Not exact TV version.

#// This source code is free to use, copy, and alter in any way you choose.
#// ...but credit is always nice :)
#//@version=4
#//@author=JayRogers
#https://www.tradingview.com/script/1o4oWbEx-Heikin-Ashi-RSI-Oscillator/
#study( "Heikin Ashi RSI Oscillator", "HARSI •", false, format.price, 2 )
# Converted by SAM4COK@ 06/2022 - Not exact code

declare lower;
def na = Double.NaN;
########## Colors ########
DefineGlobalColor("UPTICK" , CreateColor( 0,120,120));
DefineGlobalColor("UP" , CreateColor( 0,80 , 80));
DefineGlobalColor("DOWNTICK" , CreateColor(173, 0, 20));
DefineGlobalColor("DOWN" , CreateColor(117, 0, 14));
DefineGlobalColor("RSI" , CreateColor(250, 200, 50));
#DefineGlobalColor("UPTICK" , CreateColor( 0,166, 50));
#DefineGlobalColor("UP" , CreateColor( 0,110, 33));
#////////////////////////////////////////////////////////////////////////////////
#// //
#// ====== INPUTS ====== //
#// //
#////////////////////////////////////////////////////////////////////////////////
#// -- Candle config
input i_lenHARSI = 14; #"Length RSI calculations"
input i_smoothing = 1; #"Open Smoothing"

#// -- RSI plot config
input i_source = ohlc4; # "Source",
input i_lenRSI = 7; # "Length",
input i_mode = yes; # "Smoothed Mode RSI?"
input i_showPlot = yes; # "Show RSI Plot?"
input i_showHist = yes; # "Show RSI Histogram?"

#// -- Channel OB/OS config
input i_upper = 20; # "OB"
input i_upperx = 30; # "OB Extreme"
input i_lower = -20; # "OS"
input i_lowerx = -30; # "OS Extreme"

#////////////////////////////////////////////////////////////////////////////////
#// ====== FUNCTIONS ====== //
#////////////////////////////////////////////////////////////////////////////////

#// zero median rsi helper function, just subtracts 50.
script f_zrsi {
input _source = ohlc4;
input _length = 0;
def RSI = RSI(PRICE = _source, LENGTH = _length ) - 50;
plot return = RSI;
}

script f_rsi { #f_rsi( i_source, i_lenRSI, i_mode )
input _source = ohlc4;
input _length = 0;
input _mode = yes;
def _zrsi = f_zrsi( _source, _length );
def _smoothed = if IsNaN( _smoothed[1] ) then _zrsi else ( _smoothed[1] + _zrsi ) / 2;
def f_rsi = if _mode then _smoothed else _zrsi;
plot return = f_rsi;
}

#// RSI Heikin-Ashi generation function

script nz {
input data = 0;
input replacement = 0;
def ret_val = if IsNaN(data) then replacement else data;
plot return = ret_val;
}
script f_Close { #f_Close(i_lenHARSI)
input _length = 0;
def _closeRSI = f_zrsi( close, _length);
def _openRSI = nz( _closeRSI[1], _closeRSI );
def _highRSI_raw = f_zrsi( high, _length );
def _lowRSI_raw = f_zrsi( low, _length );
def _highRSI = Max( _highRSI_raw, _lowRSI_raw );
def _lowRSI = Min( _highRSI_raw, _lowRSI_raw );
def _close = ( _openRSI + _highRSI + _lowRSI + _closeRSI ) / 4;
plot return = _close;
}
script f_Open { #f_Open(i_lenHARSI, i_smoothing)
input _length = 0;
input _smoothing = 1;
def _closeRSI = f_zrsi( close, _length);
def _openRSI = nz( _closeRSI[1], _closeRSI );
def _open = if IsNaN ( _open[_smoothing]) then ( _openRSI + _closeRSI ) / 2 else
(( _open[1] * _smoothing ) + f_Close(_length)[1]) / (_smoothing + 1 );
plot return = _open;
}
script f_High { #f_High(i_lenHARSI , i_smoothing)
input _length = 0;
input _smoothing = 1;
def _highRSI_raw = f_zrsi( high, _length );
def _lowRSI_raw = f_zrsi( low, _length );
def _highRSI = Max( _highRSI_raw, _lowRSI_raw );
def _high = Max( _highRSI, Max(f_Open(_length, _smoothing), f_Close(_length)));
plot return = _high;
}
script f_Low { #f_Low(i_lenHARSI , i_smoothing)
input _length = 0;
input _smoothing = 1;
def _highRSI_raw = f_zrsi( high, _length );
def _lowRSI_raw = f_zrsi( low, _length );
def _lowRSI = Min( _highRSI_raw, _lowRSI_raw );
def _low = Min( _lowRSI, Min(f_Open(_length, _smoothing), f_Close(_length)));
plot return = _low ;
}

#////////////////////////////////////////////////////////////////////////////////
#// ====== SERIES, LINES and LABELS ====== //
#////////////////////////////////////////////////////////////////////////////////

#// standard, or ha smoothed rsi for the line plot and/or histogram
def RSI = f_rsi( i_source, i_lenRSI, i_mode );

plot RSI_OL = if i_showPlot then RSI else na;
RSI_OL.setDefaultColor(GlobalColor("RSI"));

def o = f_Open(i_lenHARSI, i_smoothing);
def h = f_High(i_lenHARSI , i_smoothing);
def l = f_Low(i_lenHARSI , i_smoothing);
def c = f_Close(i_lenHARSI);

def isExp = AbsValue( c - o ) >= AbsValue( c[1] - o[1]);

# Plot UP candle with isBull only
def UpO1; def UpH1; def UpL1; def UpC1;
if o < c then {UpO1 = o ; UpH1 = h ; UpL1 = l ; UpC1 = c; } else
{UpO1 = na; UpH1 = na; UpL1 = na; UpC1 = na;}
# Plot UP candle with isBull and isExp
def UpO; def UpH; def UpL; def UpC;
if o < c and isExp then {UpO = o ; UpH = h ; UpL = l ; UpC = c; } else
{UpO = na; UpH = na; UpL = na; UpC = na;}
# Plot DOWN candle
def DnO; def DnH; def DnL; def DnC;
if o > c and !isExp then {DnO = o ; DnH = h ; DnL = l ; DnC = c; } else
{DnO = na; DnH = na; DnL = na; DnC = na;}
# Plot DOWN candle with !isBull and !isExp
def DnO1; def DnH1; def DnL1; def DnC1;
if o > c then {DnO1 = o ; DnH1 = h ; DnL1 = l ; DnC1 = c; } else
{DnO1 = na; DnH1 = na; DnL1 = na; DnC1 = na;}
# Plot the new Chart

AddChart(high = UpH1, low = UpL1, open = UpC1, close = UpO1,
type = ChartType.CANDLE, growcolor = GlobalColor("UPTICK"));
AddChart(high = UpH , low = UpL , open = UpC, close = UpO,
type = ChartType.CANDLE, growcolor = GlobalColor("UP"));

AddChart(high = DnH1, low = DnL1, open = DnO1, close = DnC1,
type = ChartType.CANDLE, growcolor = GlobalColor("DOWNTICK"));
AddChart(high = DnH , low = DnL , open = DnO, close = DnC,
type = ChartType.CANDLE, growcolor = GlobalColor("DOWN"));

#####

def upperx = i_upperx;
#upperx.SetDefaultColor(Color.DARK_RED);
#upperx.SetStyle(Curve.FIRM);

def upper = i_upper;
#upper.SetDefaultColor(Color.DARK_RED);
#upper.SetStyle(Curve.SHORT_DASH);

plot median = 0;
#median.SetDefaultColor(Color.DARK_GRAY);
#median.SetStyle(Curve.LONG_DASH);

def lower = i_lower;
#lower.SetDefaultColor(Color.DARK_GREEN);
#lower.SetStyle(Curve.SHORT_DASH);

def lowerx = i_lowerx;
#lowerx.SetDefaultColor(Color.DARK_GREEN);
#lowerx.SetStyle(Curve.FIRM);

addcloud (upperx, upper, color.dark_red,color.dark_red,no);
addcloud (lower, lowerx, color.dark_green,color.dark_green,no);
 
@sam4cok thank you some much for share this great system with this group... a quick question do you think it is possible to convert the BREAK OUT FINDER to TOS? that one look great!! thank you in advance.
 

BrusselsAffair

New member
Is it possible to do a version of this using Mobius' TMO (True Momentum Oscillator) instead of RSI? I might give it a shot but my code fu is weak.

Code:
# TMO ((T)rue (M)omentum (O)scilator)
# Mobius
# V01.05.2018
# hint: TMO calculates momentum using the delta of price. Giving a much better picture of trend, tend reversals and divergence than momentum oscillators using price.

declare Lower;

input length = 14;
input calcLength = 5;
input smoothLength = 3;

def o = open;
def c = close;
def data = fold i = 0 to length
           with s
           do s + (if c > getValue(o, i)
                   then 1
                   else if c < getValue(o, i)
                        then - 1
                        else 0);
def EMA5 = ExpAverage(data, calcLength);
plot Main = ExpAverage(EMA5, smoothLength);
plot Signal = ExpAverage(Main, smoothLength);
     Main.AssignValueColor(if Main > Signal
                           then color.green
                           else color.red);
     Signal.AssignValueColor(if Main > Signal
                             then color.green
                             else color.red);
     Signal.HideBubble();
     Signal.HideTitle();
addCloud(Main, Signal, color.green, color.red);
plot zero = if isNaN(c) then double.nan else 0;
     zero.SetDefaultColor(Color.gray);
     zero.hideBubble();
     zero.hideTitle();
plot ob = if isNaN(c) then double.nan else round(length * .7);
     ob.SetDefaultColor(Color.gray);
     ob.HideBubble();
     ob.HideTitle();
plot os = if isNaN(c) then double.nan else -round(length * .7);
     os.SetDefaultColor(Color.gray);
     os.HideBubble();
     os.HideTitle();
addCloud(ob, length, color.light_red, color.light_red, no);
addCloud(-length, os, color.light_green, color.light_green);
# End Code TMO
 
Top