This CRSI uses Heikin Ashi definitions for ohlc and is filtered using a triple exponential moving average. The pine-script code is as follows -
I need to code it in AmiBroker Formula Language. Please help me to do so. Thanks in advance. Regards.
Code:
//@version=5
strategy(title="Filtered Connors RSI", shorttitle="Filtered-CRSI", overlay=false, format=format.price, precision=2, timeframe="", timeframe_gaps = true)
candle = input.bool(false, "Enable Candle")
ma_length = input.int(14, "MA Length", 1, 2000, 1, group="MA Settings")
c_length = input.int(3, "CRSI Length", 1, 2000, 1, group="RSI Settings")
ud_length = input.int(2, "CRSI Up Down Length", 1, 2000, 1, group="RSI Settings")
roc_length = input.int(100,"CRSI ROC Length", 1, 2000, 1, group="RSI Settings")
input = input.string("Close", "Source", ["Open", "Close", "High", "Low", "HL2", "HLC3", "OHLC4", "HLCC4"])
ema(src, len) =>
alpha = 2 / (len + 1)
sum = 0.0
sum := na(sum[1]) ? src : alpha * src + (1 - alpha) * nz(sum[1])
tma(src, int len) =>
ema1 = ema(src, len)
ema2 = ema(ema1, len)
ema3 = ema(ema2, len)
tma = 3 * (ema1 - ema2) + ema3
rsi(float input = close, int length = 14) =>
up = ta.rma(math.max(ta.change(input), 0), length)
down = ta.rma(-math.min(ta.change(input), 0), length)
out = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
updown(float s) =>
isEqual = s == s[1]
isGrowing = s > s[1]
ud = 0.0
ud := isEqual ? 0 : isGrowing ? (nz(ud[1]) <= 0 ? 1 : nz(ud[1])+1) : (nz(ud[1]) >= 0 ? -1 : nz(ud[1])-1)
ud
crsi(float input = close, simple int length = 3, simple int ud_length = 2, simple int roc_length = 100) =>
updown = rsi(updown(input), length)
rsi = rsi(input, length)
updownrsi = rsi(updown, ud_length)
percentrank = ta.percentrank(ta.roc(input, 1), roc_length)
crsi = math.avg(rsi, updownrsi, percentrank)
crsi
ha_close(float Open = open, float High = high, float Low = low, float Close = close, bool enable = true) =>
ha_close = (Open + High + Low + Close) / 4
out = enable == true ? ha_close : Close
ha_open(float Open = open, float High = high, float Low = low, float Close = close, bool enable = true) =>
ha_open = float(na)
ha_close = ha_close(Open, High, Low, Close)
ha_open := na(ha_open[1]) ? (Open + Close) / 2 : (nz(ha_open[1]) + nz(ha_close[1])) / 2
out = enable == true ? ha_open : Open
ha_high(float Open = open, float High = high, float Low = low, float Close = close, bool enable = true) =>
ha_close = ha_close(Open, High, Low, Close)
ha_open = ha_open(Open, High, Low, Close)
ha_high = math.max(High, math.max(ha_open, ha_close))
out = enable == true ? ha_high : High
ha_low(float Open = open, float High = high, float Low = low, float Close = close, bool enable = true) =>
ha_close = ha_close(Open, High, Low, Close)
ha_open = ha_open(Open, High, Low, Close)
ha_low = math.min(Low, math.min(ha_open, ha_close))
out = enable == true ? ha_low : Low
rsi_open = crsi(open, c_length, ud_length, roc_length)
rsi_high = crsi(high, c_length, ud_length, roc_length)
rsi_low = crsi(low, c_length, ud_length, roc_length)
rsi_close = crsi(close, c_length, ud_length, roc_length)
Open = ha_open(rsi_open, rsi_high, rsi_low, rsi_close)
High = ha_high(rsi_open, rsi_high, rsi_low, rsi_close)
Low = ha_low(rsi_open, rsi_high, rsi_low, rsi_close)
Close = ha_close(rsi_open, rsi_high, rsi_low, rsi_close)
Openf = tma(Open, ma_length)
Highf = tma(High, ma_length)
Lowf = tma(Low, ma_length)
Closef = tma(Close, ma_length)
HL2 = math.avg(High, Low)
HLC3 = math.avg(High, Low, Close)
OHLC4 = math.avg(High, Low, Close, Open)
HLCC4 = math.avg(High, Low, Close, Close)
source =
input == "Open" ? Open :
input == "High" ? High :
input == "Low" ? Low :
input == "Close" ? Close :
input == "HL2" ? HL2 :
input == "HLC3" ? HLC3 :
input == "OHLC4" ? OHLC4 :
input == "HLCC4" ? HLCC4 : na
crsi = tma(source, ma_length)
alpha = candle == true ? 100 : 0
alpha_c = candle == true ? 0 : 100
red = color.new(#ef5350, alpha_c)
green = color.new(#26a69a, alpha_c)
purple = color.new(#7E57C2, alpha)
colour = Open < Close ? green : red
plot(crsi, "RSI-based MA", purple)
plotcandle(Openf, Highf, Lowf, Closef, "FCRSI Candles", colour, colour, true, bordercolor = colour)
rsiUpperBand = hline(70, "RSI Upper Band", color=#787B86)
hline(50, "RSI Middle Band", color.new(#787B86, 50))
rsiLowerBand = hline(30, "RSI Lower Band", #787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")
//strategy
if (ta.crossover(crsi, 30))
strategy.entry("Long", strategy.long, comment="Long")
if (ta.crossunder(crsi, 70))
strategy.entry("Short", strategy.short, comment="Short")