Heiken Ashi Filtered Connors RSI

#1
This CRSI uses Heikin Ashi definitions for ohlc and is filtered using a triple exponential moving average. The pine-script code is as follows -

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")
I need to code it in AmiBroker Formula Language. Please help me to do so. Thanks in advance. Regards.
 
#2
The following is the AFL code for "Filtered Connors RSI". But it's giving "ERROR 16: Too many arguments". It needs fixes.

Code:
_SECTION_BEGIN("Filtered Connors RSI");

ma_length  = 4;
c_length   = 3;
ud_length  = 2;
roc_length = 100;

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);

PlotGrid(70, colorGreen, pattern=10, width=2, label=True);
PlotGrid(50, colorBrown, pattern=9, width=2, label=True);
PlotGrid(30, colorRed, pattern=10, width=2, label=True);
Plot(fcrsi, "Filtered Connors RSI", colorBlue, styleLine|styleThick, width=2);
_SECTION_END();
Thanks in advance. Regards.
 
Last edited:

godfather

Well-Known Member
#3
_SECTION_BEGIN("Filtered Connors RSI");

EnableCandle = ParamToggle("Enable Candle", "No|Yes", 0);
MALength = Param("MA Length", 14, 1, 2000, 1);
CRSILength = Param("CRSI Length", 3, 1, 2000, 1);
UpDownLength = Param("CRSI Up Down Length", 2, 1, 2000, 1);
ROCLength = Param("CRSI ROC Length", 100, 1, 2000, 1);
Source = ParamList("Source", "Open|Close|High|Low|HL2|HLC3|OHLC4|HLCC4", 3);

EMA = EMA(Source, MALength);
TMA = 3 * (EMA - Ref(EMA, -1)) + Ref(EMA, -3);

RSI = RSI(Input, CRSILength);
UpDownRSI = RSI(UpDown(RSI), UpDownLength);
PercentRank = PercentRank(Roc(Input, 1), ROCLength);
CRSIValue = MA(RSI, UpDownRSI, PercentRank);

if (EnableCandle)
{
CandleOpen = (CRSI.Open + CRSI.High + CRSI.Low + CRSI.Close) / 4;
CandleClose = IIf(CandleOpen <= CRSI.Close, CRSI.Close, CandleOpen);
CandleColor = IIf(CandleOpen <= CRSI.Close, colorGreen, colorRed);

PlotOHLC(CandleOpen, CRSI.High, CRSI.Low, CandleClose, "FCRSI Candles", CandleColor, CandleColor);
}
else
{
Plot(CRSIValue, "RSI-based MA", colorViolet);
}

RSIUpperBand = HLine(70, "RSI Upper Band", colorBlue);
RSILowerBand = HLine(30, "RSI Lower Band", colorBlue);
PlotFill(RSIUpperBand, RSILowerBand, colorLightBlue, "RSI Background Fill");

BuySignal = Cross(CRSIValue, 30);
SellSignal = Cross(70, CRSIValue);

BuyPrice = SellPrice = Close;

for (i = 0; i < BarCount; i++)
{
if (BuySignal)
{
Buy = Ref(BuySignal, -1);
Sell = Ref(SellSignal, i);
BuyPrice = Close;
PlotShapes(shapeUpArrow * Buy, colorGreen, 0, L - 10);
}
else if (SellSignal)
{
Buy = Ref(BuySignal, i);
Sell = Ref(SellSignal, -1);
SellPrice = Close;
PlotShapes(shapeDownArrow * Sell, colorRed, 0, H + 10);
}
}


PlotShapes(IIf(BuySignal, shapeUpArrow, shapeNone), colorGreen, 0, L, Offset=-10);
PlotShapes(IIf(SellSignal, shapeDownArrow, shapeNone), colorRed, 0, H, Offset=10);


_SECTION_END();
Try this and please let me know.
 

godfather

Well-Known Member
#5
error got,syntex error 31,expecting "("
i checked but not able to rectify, i am using 6.351
Can you some more details as well if you get this error again, try this code

_SECTION_BEGIN("Filtered Connors RSI");

EnableCandle = ParamToggle("Enable Candle", "No|Yes", 0);
MALength = Param("MA Length", 14, 1, 2000, 1);
CRSILength = Param("CRSI Length", 3, 1, 2000, 1);
UpDownLength = Param("CRSI Up Down Length", 2, 1, 2000, 1);
ROCLength = Param("CRSI ROC Length", 100, 1, 2000, 1);
Source = ParamList("Source", "Open|Close|High|Low|HL2|HLC3|OHLC4|HLCC4", 3);

EMA = EMA(Source, MALength);
TMA = 3 * (EMA - Ref(EMA, -1)) + Ref(EMA, -3);

RSI = RSI(Input, CRSILength);
UpDownRSI = RSI(UpDown(RSI) == 1 ? RSI : (UpDown(RSI) == -1 ? -RSI : 0), UpDownLength);
PercentRank = PercentRank(Roc(Input, 1), ROCLength);
CRSIValue = (RSI + UpDownRSI + PercentRank) / 3;

if (EnableCandle)
{
CandleOpen = (CRSI.Open + CRSI.High + CRSI.Low + CRSI.Close) / 4;
CandleClose = IIf(CandleOpen <= CRSI.Close, CRSI.Close, CandleOpen);
CandleColor = IIf(CandleOpen <= CRSI.Close, colorGreen, colorRed);

PlotOHLC(CandleOpen, CRSI.High, CRSI.Low, CandleClose, "FCRSI Candles", CandleColor, CandleColor);
}
else
{
Plot(CRSIValue, "RSI-based MA", colorViolet);
}

RSIUpperBand = HLine(70, "RSI Upper Band", colorBlue);
RSILowerBand = HLine(30, "RSI Lower Band", colorBlue);
PlotFill(RSIUpperBand, RSILowerBand, colorLightBlue, "RSI Background Fill");

BuySignal = Cross(CRSIValue, 30);
SellSignal = Cross(70, CRSIValue);

BuyPrice = SellPrice = Close;

for (i = 0; i < BarCount; i++)
{
if (BuySignal)
{
Buy = Ref(BuySignal, -1);
Sell = Ref(SellSignal, i);
BuyPrice = Close;
PlotShapes(shapeUpArrow * Buy, colorGreen, 0, Low - 10);
}
else if (SellSignal)
{
Buy = Ref(BuySignal, i);
Sell = Ref(SellSignal, -1);
SellPrice = Close;
PlotShapes(shapeDownArrow * Sell, colorRed, 0, High + 10);
}
}

PlotShapes(IIf(BuySignal, shapeUpArrow, shapeNone), colorGreen, 0, Low, Offset=-10);
PlotShapes(IIf(SellSignal, shapeDownArrow, shapeNone), colorRed, 0, High, Offset=10);

_SECTION_END();


 

Similar threads