
I created this indicator with multiple oscillator option, background color as per the selected osc and divergence. try it
CODE - Updated with Bar color option
CSS:
#indicator("Heikin-Ashi oscillator for many with Background Color and div)
# Created by [email protected] - 11/2022
# Updated: Color Bar option added as requsted from useThinkScript.com member
declare lower;
input enableBackgroundColor = yes;
input ColorBar = yes;
input oscillatorSource = ohlc4;
input smoothMode = yes; #Hint smoothMode: This option smoothes the RSI in a manner similar to HA.
input oscillatorType = {default RSI, CCI, MACD, ChandeMomOsc, STOCH, StochRSI, MFI, Williams, TSI, WaveTrend};
input smoothing = 3;
input HeikinAshiLength = 14;
input ShowOscLine = no;
input OscLineLength = 7;
input overboughtOversoldLookckback = 100;
input alerts = yes;
input sound = {default "NoSound", "Ding", "Bell", "Chimes", "Ring"};
input DivBull = yes; # "Plot Bullish"
input DivBear = yes; # "Plot Bearish"
input DivHiddenBull = no; # "Plot Hidden Bullish"
input DivHiddenBear = no; # "Plot Hidden Bearish"
#--- Calc
def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
#---Colors
DefineGlobalColor("UPTICK" , CreateColor(0, 147, 197));
DefineGlobalColor("UP" , CreateColor(0, 90, 121));
DefineGlobalColor("DOWNTICK", CreateColor(255, 197, 19));
DefineGlobalColor("DOWN" , CreateColor(148, 111, 0));
DefineGlobalColor("osc" , CreateColor(204, 0, 204));
DefineGlobalColor("bgUP" , CreateColor(1, 25, 16));
DefineGlobalColor("bgDn" , CreateColor(38, 0, 0));
DefineGlobalColor("ob" , CreateColor(154, 0, 0));
DefineGlobalColor("os" , CreateColor(0, 154, 0));
#---- Scripts
script nz {
input data = close;
input repl = 0;
def ret_val = if IsNaN(data) then repl else data;
plot return = ret_val;
}
# stoch(source, high, low, length) =>
script stoch {
input src = close;
input h = high;
input l = low;
input len = 20;
def stoch = 100 * (src - Lowest(l, len)) / (Highest(h, len) - Lowest(l, len));
plot return = stoch;
}
#MACD(src)
script mac {
input src = close;
def fast_length = 12;
def slow_length = 26;
def signal_length = 9;
def fast_ma = ExpAverage(src, fast_length);
def slow_ma = ExpAverage(src, slow_length);
def macd = fast_ma - slow_ma;
def signal = ExpAverage(macd, signal_length);
def hist = macd - signal;
plot return = hist;
}
script osc {
input osc1TypeInput = "RSI";
input osc1SourceInput = close;
input osc1LengthInput = 14;
def osc1Type;
def nRSI1 = RSI(Price = osc1SourceInput, Length = osc1LengthInput);
def srcEMA = ExpAverage(osc1SourceInput, osc1LengthInput);
def diff = AbsValue(osc1SourceInput - srcEMA);
def diffEMA = ExpAverage(diff, osc1LengthInput);
def ci = (osc1SourceInput - srcEMA) / (0.015 * diffEMA);
def WT = ExpAverage(ci, osc1LengthInput * 2);
osc1Type =
if osc1TypeInput == "RSI" then
nRSI1 - 50 else
if osc1TypeInput == "CCI" then
CCI(Length = osc1LengthInput) else
if osc1TypeInput == "MACD" then
MAC(osc1SourceInput) else
if osc1TypeInput == "ChandeMomOsc" then
ChandeMomentumOscillator(osc1LengthInput) else
if osc1TypeInput == "STOCH" then
stoch(osc1SourceInput, high, low, osc1LengthInput) - 50 else
if osc1TypeInput == "StochRSI" then
Average(stoch(nRSI1, nRSI1, nRSI1, osc1LengthInput), 3) - 50 else
if osc1TypeInput == "MFI" then
MoneyFlowIndex(Length = osc1LengthInput) - 50 else
if osc1TypeInput == "Williams" then
WilliamsPercentR(Length = osc1LengthInput) + 50 else
if osc1TypeInput == "WaveTrend" then
Average(WT, 4) else
if osc1TypeInput == "TSI" then
TrueStrengthIndex(longLength = osc1LengthInput * 2, shortLength = osc1LengthInput) else Double.NaN;
plot return = osc1Type;
}
script f_rsiHeikinAshi {
input _highIn = high;
input _lowIn = low;
input _closeIn = close;
input _length = 14;
input _smoothing = 5;
def _closeOsc = _closeIn;
def _openOsc = nz( _closeOsc[1], _closeOsc );
def _highOsc_raw = _highIn;
def _lowOsc_raw = _lowIn;
def _highOsc = Max( _highOsc_raw, _lowOsc_raw );
def _lowOsc = Min( _highOsc_raw, _lowOsc_raw );
def _close = ( _openOsc + _highOsc + _lowOsc + _closeOsc ) / 4;
def _open;
_open = if IsNaN(_open[1]) then ( _openOsc + _closeOsc ) / 2 else
CompoundValue(1, (( _open[1] * _smoothing ) + _close[1] ) / ( _smoothing + 1 ), _close);
def _high = Max( _highOsc, Max( _open, _close ) );
def _low = Min( _lowOsc, Min( _open, _close ) );
plot high = _high;
plot low = _low;
plot close = _close;
plot open = _open;
}
def oscType = osc(oscillatorType, oscillatorSource, OscLineLength);
plot osc_Line = if ShowOscLine then oscType else na;
osc_Line.SetDefaultColor(GlobalColor("osc"));
osc_Line.SetLineWeight(2);
#----
def srcHigh1 = osc(oscillatorType, high, HeikinAshiLength);
def srcLow1 = osc(oscillatorType, low, HeikinAshiLength);
def srcClose1 = osc(oscillatorType, close, HeikinAshiLength);
def srcOpen1 = osc(oscillatorType, open, HeikinAshiLength);
def srcHigh = if !smoothMode then srcHigh1 else
if IsNaN(srcHigh[1]) then srcHigh1 else (srcHigh[1] + srcHigh1 ) / 2;
def srcLow = if !smoothMode then srcLow1 else
if IsNaN(srcLow[1]) then srcLow1 else (srcLow[1] + srcLow1 ) / 2;
def srcClose = if !smoothMode then srcClose1 else
if IsNaN(srcClose[1]) then srcClose1 else (srcClose[1] + srcClose1 ) / 2;
def srcOpen = if !smoothMode then srcOpen1 else
if IsNaN(srcOpen[1]) then srcOpen1 else (srcOpen[1] + srcOpen1 ) / 2;
def hao = f_rsiHeikinAshi(srcHigh, srcLow, srcClose, HeikinAshiLength, smoothing).open;
def hah = f_rsiHeikinAshi(srcHigh, srcLow, srcClose, HeikinAshiLength, smoothing).high;
def hal = f_rsiHeikinAshi(srcHigh, srcLow, srcClose, HeikinAshiLength, smoothing).low;
def hac = f_rsiHeikinAshi(srcHigh, srcLow, srcClose, HeikinAshiLength, smoothing).close;
#-- Lines plot
def bandSrc = (hac + hao + hal + hah) / 4;
plot zeroLine = if IsNaN(close) then na else 0;
zeroLine.SetDefaultColor(Color.GRAY);
zeroLine.SetStyle(Curve.MEDIUM_DASH);
plot HiLine = if IsNaN(close) then na else HighestAll(Average(bandSrc, overboughtOversoldLookckback)[1]);
HiLine.SetDefaultColor(Color.GRAY);
HiLine.SetStyle(Curve.LONG_DASH);
plot LoLine = if IsNaN(close) then na else LowestAll(Average(bandSrc, overboughtOversoldLookckback)[1]);
LoLine.SetDefaultColor(Color.GRAY);
LoLine.SetStyle(Curve.LONG_DASH);
AddCloud(pos, HiLine, GlobalColor("ob"));
AddCloud(LoLine, neg, GlobalColor("os"));
#---- Ha Candle Plot
def haOpen = hao;
def haHigh = hah;
def haLow = hal;
def haClose = hac;
def candelUp = haClose > haOpen;
def isExp = AbsValue(haClose - haOpen) > AbsValue( haClose[1] - haOpen[1]);
# Plot UP candle
def UpO;
def UpH;
def UpL;
def UpC;
if candelUp
then {
UpO = haOpen ;
UpH = haHigh ;
UpL = haLow ;
UpC = haClose;
} else
{
UpO = na;
UpH = na;
UpL = na;
UpC = na;
}
# Plot DOWN candle
def DnO;
def DnH;
def DnL;
def DnC;
if !candelUp
then {
DnO = haOpen ;
DnH = haHigh ;
DnL = haLow ;
DnC = haClose;
} else
{
DnO = na;
DnH = na;
DnL = na;
DnC = na;
}
# Plot the new Chart
AddChart(high = if isExp then UpH else na , low = UpL , open = UpC, close = UpO,
type = ChartType.CANDLE, growcolor = GlobalColor("UPTICK"));
AddChart(high = if !isExp then UpH else na , low = UpL , open = UpC, close = UpO,
type = ChartType.CANDLE, growcolor = GlobalColor("UP"));
AddChart(high = if isExp then DnH else na, low = DnL , open = DnO, close = DnC,
type = ChartType.CANDLE, growcolor = GlobalColor("DOWNTICK"));
AddChart(high = if !isExp then DnH else na, low = DnL , open = DnO, close = DnC,
type = ChartType.CANDLE, growcolor = GlobalColor("DOWN"));
#--- Bar Color--
AssignPriceColor(if !ColorBar then Color.CURRENT else
if candelUp and isExp then GlobalColor("UPTICK") else
if candelUp then GlobalColor("UP") else
if !candelUp and !isExp then GlobalColor("DOWN") else
if !candelUp then GlobalColor("DOWN") else Color.GRAY);
#----Div-----------
def LookBackRight = 5; # "Pivot Lookback Right"
def LookBackLeft = 5; # "Pivot Lookback Left"
def MaxLookback = 60; # "Max of Lookback Range"
def MinLookback = 5; # "Min of Lookback Range"
def divSrc = bandSrc;
def h = high;
def l = low;
script FindPivots {
input dat = close; # default data or study being evaluated
input HL = 0; # default high or low pivot designation, -1 low, +1 high
input lbL = 5; # default Pivot Lookback Left
input lbR = 1; # default Pivot Lookback Right
##############
def _nan; # used for non-number returns
def _BN; # the current barnumber
def _VStop; # confirms that the lookforward period continues the pivot trend
def _V; # the Value at the actual pivot point
##############
_BN = BarNumber();
_nan = Double.NaN;
_VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
fold a = 1 to lbR + 1 with b=1 while b do
if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
if (HL > 0) {
_V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
then dat else _nan;
} else {
_V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
then dat else _nan;
}
plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
#valuewhen (Cond, source, lbr, occurrence)
script valuewhen {
input cond = 0;
input src = close;
input lbr = 0;
input occurrence = 0;
def n = occurrence + 1;
def offset = fold j = 0 to 200 with p=1 while p < n + 1
do p + ( if p == n then j - n else if cond[j] == yes then 1 else 0 );
plot price = GetValue(src[lbr], offset - 1);
}
#_inRange(cond) =>
script _inRange {
input cond = yes;
input rangeUpper = 60;
input rangeLower = 5;
def bars = if cond then 0 else bars[1] + 1;
def inrange = (rangeLower <= bars) and (bars <= rangeUpper);
plot retrun = inrange;
}
def pl = findpivots(divSrc, -1, LookBackLeft, LookBackRight);
def ph = findpivots(divSrc, 1, LookBackLeft, LookBackRight);
def plFound = if !IsNaN(pl) then 1 else 0;
def phFound = if !IsNaN(ph) then 1 else 0;
def vlFound = valuewhen(plFound, divSrc, 0, 1);
def vhFound = valuewhen(phFound, divSrc, 0, 1);
def plPrice = valuewhen(plFound, l, 0, 1);
def phPrice = valuewhen(phFound, h, 0, 1);
#// Regular Bullish
def oscHL = divSrc > vlFound and _inRange(plFound[1], MaxLookback, MinLookback);
def priceLL = l < plPrice;
def bullCond = DivBull and plFound and oscHL and priceLL;
#// Hidden Bullish
def oscLL = divSrc[LookBackRight] < vlFound and _inRange(plFound[1], MaxLookback, MinLookback);
def priceHL = l[LookBackRight] > plPrice;
def hiddenBullCond = DivHiddenBull and plFound and oscLL and priceHL;
#// Regular Bearish
def oscLH = divSrc < vhFound and _inRange(phFound[1], MaxLookback, MinLookback);
def priceHH = h > phPrice;
def bearCond = DivBear and phFound and oscLH and priceHH;
#// Hidden Bearish
def oscHH = divSrc > vhFound and _inRange(phFound[1], MaxLookback, MinLookback);
def priceLH = h < phPrice;
def hiddenBearCond = DivHiddenBear and phFound and oscHH and priceLH;
#------ Bubbles
AddChartBubble(bullCond, divSrc, "R", Color.GREEN, no);
AddChartBubble(bearCond, divSrc, "R", CreateColor(156, 39, 176), yes);
AddChartBubble(hiddenBullCond, divSrc, "H", Color.DARK_GREEN, no);
AddChartBubble(hiddenBearCond, divSrc, "H", Color.DARK_RED, yes);
#---- Alerts
Alert(alerts and bullCond, "Regular Bull Div", Alert.BAR, sound);
Alert(alerts and bearCond, "Regular Bear Div", Alert.BAR, sound);
Alert(alerts and hiddenBullCond, "Hidden Bull Div", Alert.BAR, sound);
Alert(alerts and hiddenBearCond, "Hidden Bear Div", Alert.BAR, sound);
#--- BackGround Color
AssignBackgroundColor(if !enableBackgroundColor then Color.CURRENT else if candelUp then GlobalColor("bgUP")
else if !candelUp then GlobalColor("bgDn") else Color.CURRENT);
#--- END Code
Last edited: