Custom RSI study


Category:
0
0

Hello Hahn,

Real big fan of the site. A lot of the material here has helped me in trading immensely.

I’ve recently run into an issue coding a study using RSI, which I plan to eventually turn into a strategy.

Hoping that you might be able to extend me some help.

Here is what I am trying to do,

Plot a signal which becomes true once the RSI drops into Oversold (20) and REMAINS true until the RSI moves through the Middleline (50)

My issue is, how to code REMAINS?

 

Kind regards,

BambooRage

Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on July 15, 2017 12:49 am
363 views
0
Private answer

Yep, that requires recursion. Which is when a variable needs to look back at a previous value of itself in order to compute it’s current value. That is how we capture that first dip below 20, then hold onto that state until it moves back above 50. This code will NOT work in Study Alerts or Conditional Orders. Screenshot included to show the output.

declare lower;
input length = 14;
input over_Bought = 70;
input over_Sold = 30;
input price = close;
input averageType = AverageType.WILDERS;
def rsi = RSI(length, over_Bought, over_Sold, price, averageType, no).RSI;
def rsiBelowTwenty = rsi < 20;
rec rsiRemains = CompoundValue(1, if rsiBelowTwenty then 1 else if rsiRemains[1] and rsi < 50 then 1 else 0, 0);
plot remains = rsiRemains;

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on July 15, 2017 9:10 am
0
Private answer

Thanks Pete for explaining about recursion.

With that code, I was finally able to create my triggers in a STUDY using SlowRSI, MoneyFlowIndex and MACD Crossovers.

declare lower;

input emaLength = 6;
input rsiLength = 14;
input over_bought = 80;
input over_sold = 20;

input mfilength = 14;
input movingAvgLength = 1;

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input averageType = AverageType.EXPONENTIAL;
input crossingType = {default “Positive to Negative”, “Negative to Positive”};

def ema = ExpAverage(close, emaLength);
def netChgAvg = WildersAverage(close – ema, rsiLength);
def totChgAvg = WildersAverage(AbsValue(close – ema), rsiLength);
def chgRatio = if totChgAvg != 0 then netChgAvg / totChgAvg else 0;

def SlowRSI = 50 * (chgRatio + 1);
def OverBought = over_bought;
def MiddleLine = 50;
def OverSold = over_sold;

def MFI = Average(moneyflow(high, close, low, volume, mfilength), movingAvgLength);

def Diff = MACD(fastLength, slowLength, MACDLength, averageType).Diff;

#Conditions for Long Trigger
def SlowRSI_OverSold = SlowRSI <= OverSold;

rec SlowRSI_OverSold_Trigger = CompoundValue(1, if SlowRSI_OverSold then 1 else if SlowRSI_OverSold_Trigger[1] and SlowRSI < MiddleLine then 1 else 0,0);

def MFI_OverSold = MFI <= OverSold;

rec MFI_OverSold_Trigger = CompoundValue(1, if MFI_OverSold then 1 else if MFI_OverSold_Trigger[1] and MFI < MiddleLine then 1 else 0,0);

def MACD_Long_Trigger = crosses(Diff, 0, crossingType == crossingType.”Positive to Negative”);

plot Long_Trigger = SlowRSI_OverSold_Trigger and MFI_OverSold_Trigger and MACD_Long_Trigger;

#Conditions for Short Trigger
def SlowRSI_OverBought = SlowRSI >= OverBought;

rec SlowRSI_OverBought_Trigger = CompoundValue(1, if SlowRSI_OverBought then 1 else if SlowRSI_OverBought_Trigger[1] and SlowRSI > MiddleLine then 1 else 0,0);

def MFI_OverBought = MFI >= OverBought;

rec MFI_OverBought_Trigger = CompoundValue(1, if MFI_OverBought then 1 else if MFI_OverBought_Trigger[1] and MFI > MiddleLine then 1 else 0,0);

def MACD_Short_Trigger = crosses(Diff, 0, crossingType == crossingType.”Negative to Positive”);

plot Short_Trigger = SlowRSI_OverBought_Trigger and MFI_OverBought_Trigger and MACD_Short_Trigger;
Short_Trigger.SetDefaultColor(color.YELLOW);

 

However, now I’m having difficulty turning this into a useable automated STRATEGY.

You eluded to this in your answer, “This code will NOT work in Study Alerts or Conditional Orders.”

Is this why I’m unable to turn this Study into a Strategy?

Any help is much appreciated.

Attachments:
Marked as spam
Posted by (Questions: 1, Answers: 1)
Answered on July 26, 2017 5:38 am
0

You say you had trouble converting this to a Strategy. But you did not describe what sort of trouble you had. I was able to convert it to a Strategy by adding two lines of code, removing the declare lower statement and converting the plots to defs.

Here are the two lines of code I added:
AddOrder(OrderType.BUY_AUTO, open[-1], name = ”Long Entry $” + open[-1]);
AddOrder(OrderType.SELL_AUTO, open[-1], name = ”LongEntry $” + open[-1]);

( at July 26, 2017 7:54 am)