DRM Strategy for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
3u7XxgH.png

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:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
568 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top