RSI Divergence (Custom Command Indicator)
stableDescription
This anti-trend indicator is based mainly on human-like algorithm of reading RSI Divergence. The script works in 2 modes:
Mode 1:
Sends a a buy signal during an RSI Bullish Divergence (2nd consecutive bottom on the plot, with lower price but higher RSI). Of course it also sends a sell signal for Bearish Divergence. This mode is not very useful by itself because it tends to give many early signals that are either too early and sometimes may not even be confirmed, if the RSI keeps going lower for example and goes below the previous bottom's RSI.
This mode should only be used together with another trading logic/indicator.
Mode 2:
Sends a buy signal when an RSI Bullish Divergence has occurred on the graph AND the price started rising again. Heiken Ashi candle closing as green is used as a confirmation of the turn. Sell signal will be sent after an RSI Bearish Divergence with Heikin Ashe confirmation happens. This mode even with default parameters usually tends to look impressive and give good results, but in the wrong conditions it can have lots of false signals.
IMPORTANT: This is an countertrend script, it tries to catch tops and bottoms early (much, much earlier than trend-based indicators). It means you will be able to snipe some tops and bottoms with an surprising accuracy, however, if the signal is wrong and the price keeps falling, you will NOT get a stop-loss/buy back from this indicator, so make sure to use either a stop-loss or a trend-based indicator to get you out when things go south.
HaasScript
-- RSI Divergence aka HARD Top/Bottom Seeker by Prof Ghibli
-- When Red (Bearish Divergence) or Green (Bullish Divergence) line goes above white line (value 6), then a Sell or Buy signal is issued.
-- Define command
DefineCommand('RSI_Divergence_smart', '2 modes of RSI Divergence along with Heiken Ashi confirmation')
-- Define command parameters.
local chartIndex = DefineParameter(NumberType, 'chartIndex', 'The index on which to chart', false, 1, 'Number')
local name = DefineParameter(StringType, 'name', 'Unique name of the indicator.', false, '', 'Text')
local interval = DefineParameter(NumberType, 'interval', 'Used interval for price data. Default is 0 and the main interval will be used.', false, 0, 'Number,InputInterval')
DefineIntervalOptimization(interval)
-- Input fields for the indicator. Add the name in front.
local type = Input(name..'type', 2)
local atrx = Input(name..'ATR multiplier', 3)
-- Calculate the Indicator
-- Assigning array cell numbers to a, b, c (makes it easy to change all these if ever needed)
local a = 1
local b = 2
local c = 3
-- RSI Length - can be changed but since most traders use the default value of 14, it's advised to leave this at 14
local rsilen = 14
-- Reset Down - when finding bearish divergence, if after the 1st peak the RSI goes below this value, the said peak is forgotten and is not counted as peak 1
local resetdown = 45
-- Reset Up - same as Reset Down, but for bullish divergence
local resetup = 55
-- Threshold - how low does the RSI must go after peak 1 before we can start looking for peak 2 (the depth of the gap between 2 peaks)
local threshold = 15
-- lowrsi - Only use bullish divergence under this RSI value
local lowrsi = 30
-- hirsi - Only use bearish divergence above this RSI level
local hirsi = 70
-------------------------------------------- BEARISH PART ----------------------------------------------
-- Getting Heikin Ashe prices
local ha_close = HeikenClosePrices()
local ha_open = HeikinOpenPrices()
-- Getting HLC prices
local p = ClosePrices()
local phi = HighPrices()
local plo = LowPrices()
-- Assigning RSI to x
local x = RSI(p, rsilen)
-- lx = last x
local lx = x[a]
local aytir = ATR(phi, plo, p, 3)
local aytr = atrx*aytir[a]
local ztate = Load('ztate', 0)
local burusai = Load('burusai', x[a])
local burais = Load('burais', 0)
local newhip = Load('newhip', phi[a])
local cond2 = 0
if (x[a] <= resetdown) then
ztate = 0
end
if (ztate == 0) then
burusai = x[a]
burais = phi[a]
ztate = 1
end
local thresh2 = burusai - threshold
if x[a] >= burusai then
ztate=1
burusai = x[a]
burais = phi[a]
end
if (ztate == 1 and x[a] > burusai) then
burusai = x[a]
end
if (ztate == 1 and phi[a] > burais) then
burais = phi[a]
end
-------------------
if (ztate == 6 and ha_close[a] < ha_open[a]) then
cond2 = 1
ztate = 0
burais = newhip
end
if (ztate == 5 and p[a] < p[b] and x[a] < burusai and type == 2 and x[b] > hirsi) then
ztate = 6
end
if ztate == 4 and phi[a] > burais then
newhip = phi[a]
end
if (ztate == 4 and burusai > hirsi and type == 1) then
cond2 = 1
end
if (ztate == 4 and p[a] < p[b] and x[a] < burusai and type == 1) then --this one needs some work for sure
cond2 = 1
ztate = 0
end
if (ztate == 4 and phi[a] > burais and type == 2 and burusai>x[a]) then --alternative can be burusai-x[a]>2
ztate = 5
end
if (ztate == 3 and p[a] > (burais-aytr)) then
ztate = 4
end
if (ztate == 2 and x[a] < thresh2 and x[a] < 70) then
ztate = 3
end
if (ztate == 1 and x[a] < burusai and burusai > 70) then
ztate = 2
end
if (ztate == 2 and phi[a] > burais) then
burais = phi[a]
end
Save('ztate', ztate)
Save('burusai', burusai)
Save('burais', burais)
Save('newhip', newhip)
-------------------------------------------- BULLISH PART ----------------------------------------------
local plow = LowPrices()
local state = Load('state', 0)
local urusai = Load('urusai', x[a])
local purais = Load('purais', 0)
local newlowp = Load('newlowp', plow[a])
local cond = 0
if x[a] >= resetup then
state = 0
end
if (state == 0) then
urusai = x[a]
purais = plow[a]
state = 1
end
local thresh = threshold + urusai
if x[a] <= urusai then
state = 1
urusai = x[a]
purais = plow[a]
end
if (state == 1 and x[a] < urusai) then
urusai = x[a]
end
if (state == 1 and plow[a] < purais) then
purais = plow[a]
end
if (state == 6 and ha_close[a] > ha_open[a]) then
cond = 1
state = 0
purais = newlowp
end
if (state == 5 and p[a]>p[b] and x[a]>urusai and type == 2 and x[b] < lowrsi and (x[b]-urusai) >= 2) then
state = 6
end
if (state == 4 and p[a] > p[b] and x[a]>urusai and type == 1) then --this line can be improved
cond = 1
state = 0
end
if (state == 4 and plow[a] < purais and type == 2) then
state = 5
end
if state == 4 and plow[a] < purais then
newlowp = plow[a]
end
if (state == 4 and urusai < lowrsi and type == 1) then
cond = 1
end
if (state == 3 and p[a] < (purais-aytr)) then
state = 4
end
if (state == 2 and x[a] > thresh) then
state = 3
end
if (state == 2 and plow[a] > purais) then
purais = plow[a]
end
if (state == 1 and x[a] > urusai and urusai < 28) then
state = 2
end
Save('state', state)
Save('urusai', urusai)
Save('purais', purais)
Save('newlowp', newlowp)
-- Determine the signal.
local signal = SignalNone
if (cond == 1) then
signal = SignalLong
elseif (cond2 == 1) then
signal = SignalShort
end
-- Plot the indicator Use the chartIndex parameter.
Plot(chartIndex + 1, 'Bearish Div State', ztate, Red)
Plot(chartIndex + 1, 'Threshold', 5, White)
Plot(chartIndex, 'Overbought', 30, Gray)
Plot(chartIndex, 'Oversold', 70, Gray)
Plot(chartIndex, 'RSI', x[a], Blue)
Plot(chartIndex + 1, 'Bullish Div State', state, Green)
--Return the custom signal.
DefineOutput(EnumType, signal, 'Signal result', 'TradeBotContainer, IndicatorContainer, Signal Helpers')
2 Comments
Sign in to leave a comment.
Im trying to implement this in a basic trading script
Any chance you could give an example of usage?
thanks
Update: figured it out :)