Connors RSI implementation

Posted By: Dalla

Connors RSI implementation - 08/23/17 11:32

I've implemented Connors RSI, sharing here in case some one is interesed

Code:
vars CRSI(vars close, int rsiPeriod, int streakRsiPeriod, int percentRankPeriod) {
	static int streak = 0;

  if (close[0] > close[1]) {
    if (streak >= 0) {
      streak += 1;
    } else {
      streak = 1;
    }
  } else if (close[0] < close[1]) {
    if (streak <= 0) {
      streak -= 1;
    } else {
      streak = -1;
    }
  } else if (close[0] == close[1]) {
    streak = 0;
  }

  vars streakSeries = series(streak);
  vars rsi = series(RSI(close,rsiPeriod));
  vars streakRsi = series(RSI(streakSeries,streakRsiPeriod));
  
  vars returns = series((close[0]-close[1])/close[1]);

  vars rank = series(PercentRank(returns,percentRankPeriod,returns[0]));
  return series((rsi[0] + streakRsi[0] + rank[0])/3);
}



Call by doing something like
Code:
vars close = series(priceClose());
vars crsiSeries = CRSI(close,3,2,100);



Please note that this requires the 1.61 Beta since it uses the new PercentRank function
Posted By: MatPed

Re: Connors RSI implementation - 08/23/17 15:42

Thank you for sharing!
Posted By: kmerlo

Re: Connors RSI implementation - 08/23/17 18:54

me too I want to say thanks to Dalla.
Posted By: johnnyp

Re: Connors RSI implementation - 08/26/17 07:26

This code sets streak to 1 if the price hasn't moved. Setting streak to 0 would seem more logical in that case.

For those not yet using the beta version. I think this should do the trick.
Code:
var PercentRank(var* Data,int Period,var Value)
{
	int i,n;
	for(i=0,n=0; i<Period; i++)
		if(Data[i] <= Value) n++;
	return ((var)n)/Period;
}

Posted By: Dalla

Re: Connors RSI implementation - 08/26/17 07:38

Thanks, clearly a bug. I updated the post to fix the issue
Posted By: johnnyp

Re: Connors RSI implementation - 08/27/17 13:56

This indicator returns a series unlike most other indicators. I also notice that the code uses lots of series where they aren't strictly necessary. So in the interest of learning here is my version that returns a single value, uses fewer series, and limits the length of the series used (except for the streak series).

I didn't limit the length of the streak series because that changes the indicator output, probably because the RSI calculation uses EMAs.

Code:
var CRSI(vars close, int rsiPeriod, int streakRsiPeriod, int percentRankPeriod) 
{
	vars streak = series(0);
	vars returns = series((close[0]-close[1])/close[1], percentRankPeriod);

	if (close[0] > close[1])
		streak[0] = max(1, streak[1] + 1);
	else if (close[0] < close[1])
		streak[0] = min(-1, streak[1] - 1);
	else // (close[0] == close[1])
		streak[0] = 0;
	
	var rsi = RSI(close,rsiPeriod);
	var streakRsi = RSI(streak,streakRsiPeriod);
	var rank = PercentRank(returns,percentRankPeriod,returns[0]);
	
	return (rsi + streakRsi + rank)/3;
}

Posted By: Materz

Re: Connors RSI implementation - 03/17/18 18:51

Hi ,

I have added your ConnorsRSI to indicator.c , i am unable to use or call the indicator in my strategy .

Kindly assist.
Posted By: Brax

Re: Connors RSI implementation - 03/18/18 19:11

You must add the function in another file, indicator.c is for educational purpose only.
Posted By: Materz

Re: Connors RSI implementation - 06/22/18 18:53

Hi all,

I need assistance on the below Connors RSI Script, it opens Position wrongly.
//**** ConnorsRSI Short Term Trading Strategy ** //
function run()
{
set(PARAMETERS); // generate and use optimized parameters
BarPeriod = 60; // 1 hour bars
LookBack = 500;
StartDate = 2015;

vars Price = series(priceClose());
vars Close = series(priceClose());
vars SMAFast = series(SMA(Close,5)); // Fast SMA for Position trigger
vars SMASlow = series(SMA(Close,200)); // Slow SMA for Trend Direction
vars cRSI = series(ConnorsRSI(Close,2,3,100)); // ConnorsRSI
int Threshold1=65;
int Threshold2= 25;
Capital= 5000;
Stop=2*ATR(10);



// ** set up max trades ** //

MaxLong = MaxShort = 1;

// ** Type ** //
while(asset(loop("EUR/USD","USD/JPY")))



Margin = 0.5 * OptimalF * Capital/2 ;

// *** Condition for Buy *** //

if(crossUnder(cRSI,Threshold2) && (Price > SMASlow && Price < SMAFast))

enterLong();


// ** Exit Long Position ** //

if( NumOpenLong >0 && Price > SMAFast)

exitLong();

// *** Condition for Sell *** //

if(crossOver(cRSI,Threshold1) && (Price < SMASlow && Price > SMAFast))

enterShort();

// ** Exit Short Position ** //

if(NumOpenShort >0 && Price < SMAFast)

exitShort();

}
Posted By: MatPed

Re: Connors RSI implementation - 06/22/18 22:37

do not use
set(PARAMETERS);

you are not optimizing any parameters.

CiaO
Posted By: Materz

Re: Connors RSI implementation - 07/02/18 17:41

Dear All,

Still having problem with my script, unable to open trades ;

//**** ConnorsRSI Short Term Trading Strategy ** //
function run()
{

BarPeriod = 60; // 1 hour bars
LookBack = 500;
StartDate = 2012;
EndDate = 2013;

vars Price = series(priceClose());
vars Close = series(priceClose());
vars SMAFast = series(SMA(Close,5)); // Fast SMA for Position trigger
vars TrendSMA = series(SMA(Close,200)); // Slow SMA for Trend Direction
vars cRSI = series(ConnorsRSI(Close,2,3,100)); // ConnorsRSI
int Threshold1=90;
int Threshold2= 10;
Capital= 5000;




// ** set up max trades ** //

MaxLong = MaxShort = 1;

// ** Type ** //
//while(asset(loop("EUR/USD","USD/JPY")))

while(asset(loop("EUR/USD")))



Margin = 0.5 * OptimalF * Capital;

// *** Condition for Buy *** //

if(Price > TrendSMA && Price < SMAFast && crossUnder(cRSI,Threshold2))

//
enterLong();


// ** Exit Long Position ** //

if( NumOpenLong >0 && Price > SMAFast)

exitLong();

// *** Condition for Sell *** //

if(Price < TrendSMA && Price > SMAFast && crossOver(cRSI,Threshold1))

enterShort();

// ** Exit Short Position ** //

if(NumOpenShort >0 && Price < SMAFast)

exitShort();

}
© 2024 lite-C Forums