Author Message:
One of the ways I go when I develop strategies is by reducing the number of parameters and removing fixed parameters and levels.
In this strategy, I'm trying to create an RSI indicator with a dynamic length.
Length is computed based on the correlation between Price and its momentum.
You can set min and max values for the RSI , and if the correlation is close to 1, we'll be at a min RSI value. When it's -1, we'll be at the max level.
I got this idea from Sofien Kaabar's book.
The strategy is super simple, and there might be much room for improvement.
Performance on the deep backtesting is not excellent, so I think the strategy needs some filters for regimes, etc.
Thanks to @MUQWISHI for helping me code it.
Disclaimer
Please remember that past performance may not indicate future results.
Due to various factors, including changing market conditions, the strategy may no longer perform as well as in historical backtesting.
This post and the script don’t provide any financial advice.
CODE:
CSS:
#//@QuantNomad
#strategy("Dynamic RSI Momentum", "DRM Strategy",
# Converted and Mod by Sam4Cok@Samer800 - 01/2023
declare lower;
#// ++ INPUT ++
input ColorSignalBar = yes; # "Color Ob/Os Bar"
input MomentumLength = 10; # "Momentum Length"
input src = close; # "Source"
input min_rsi = 20; # "Min RSI"
input max_rsi = 50; # "Max RSI"
input OverBought = 70; # "OverBought"
input OverSold = 30; # "OverSold"
input InverseZigzag = no; # "Inverse Zigzag"
input ShowLines = yes; # "Show Lines"
input ShowLabels = yes; # "Show Labels"
def na = Double.NaN;
def last = isNaN(close);
script nz {
input data = close;
input repl = 0;
def ret_val = if IsNaN(data) then repl else data;
plot return = ret_val;
}
#// ++ CALCULATION ++
#rmaFun(src, len) =>
script rmaFun {
input src = close;
input len = 10;
def smaSum = fold i = 1 to len with p=src do
p + GetValue(src, i);
def sma = smaSum / len;
def alpha = 1 / len;
def sum;
sum = CompoundValue(1, alpha * src + (1 - alpha) * (sum[1]), sma);
plot return = sum;
}
#/ RSI Function
#rsiFun(src, len) =>
script rsiFun {
input src = close;
input len = 10;
def srcDif = src - src[1];
def rsiFun = 100 - 100 / (1 + rmaFun(if srcDif > 0 then srcDif else 0, len) /
rmaFun(if srcDif < 0 then AbsValue(srcDif) else 0, len));
plot return = rsiFun;
}
#// Momentum
def momVal = src - src[MomentumLength];
#// Calculation Price vs Momentum
def corr1 = Correlation(src, momVal, MomentumLength);
def corr = if corr1 > 1 or corr1 < - 1 then na else corr1;
def rsiLen = floor(min_rsi + nz(Round((1 - corr) * (max_rsi - min_rsi) / 2, 0), 0));
def rsiMom = rsiFun(src, rsiLen);
plot DRM = rsiMom;#, "Dynamic RSI Momentum",
DRM.SetLineWeight(2);
drm.AssignValueColor( if rsiMom < OverSold then color.green else
if rsiMom > OverBought then color.red else GetColor(118 % 10));
plot midLine = if last then na else 50;
midLine.SetDefaultColor(Color.DARK_GRAY);
plot obLevel = if last then na else OverBought;
obLevel.SetDefaultColor(Color.GRAY);
plot osLevel = if last then na else OverSold;
osLevel.SetDefaultColor(Color.GRAY);
#---- Background Color
AddCloud(obLevel, osLevel, CreateColor(76,10,127));
#/ ++ STRATEGY ++
def long = Crosses(rsiMom, OverSold, CrossingDirection.ABOVE) and !IsNaN(rsiMom);
def short = Crosses(rsiMom, OverBought, CrossingDirection.BELOW) and !IsNaN(rsiMom);
AssignPriceColor( if !ColorSignalBar then Color.CURRENT else
if long and highest(long[1], 5)==0 then Color.CYAN else
if short and highest(short[1], 5)==0 then Color.MAGENTA else Color.CURRENT);
#--- ZigZag Calc
def myRSI = rsiMom;
def crossUp = Crosses(myRSI, OverBought, CrossingDirection.ANY);
def crossDn = Crosses(myRSI, OverSold , CrossingDirection.ANY);
def buy = if InverseZigzag then (myRSI < myRSI[1]) and crossDn else
if !InverseZigzag then (myRSI > myRSI[1]) and crossUp else buy[1];
def sell = if InverseZigzag then (myRSI > myRSI[1]) and crossUp else
if !InverseZigzag then (myRSI < myRSI[1]) and crossDn else sell[1];
def myPosition = if buy then 0 else if sell or myPosition[1] then 1 else 0;
def trendColor = if buy then 1 else if sell then -1 else trendColor[1];
def rsiAlgo = if buy and myPosition[1] then myRSI - 0.004 else
if sell and !myPosition[1] then myRSI + 0.004 else na;
plot RSIAlgoLine = if ShowLines then rsiAlgo else na;
RSIAlgoLine.EnableApproximation();
RSIAlgoLine.AssignValueColor(if trendColor[1] > 0 then Color.UPTICK else
if trendColor[1] < 0 then Color.DOWNTICK else Color.GRAY);
#--- Bubbles
AddChartBubble(ShowLabels and buy and myPosition[1], myRSI - 0.005, "Sell", Color.RED, if InverseZigzag then no else yes);
AddChartBubble(ShowLabels and sell and !myPosition[1], myRSI + 0.005, "Buy", Color.GREEN, if InverseZigzag then yes else no);
#-- END Code
Last edited by a moderator: