ConnorsRSI - Variables Dynamically Changing Values at Runtime - Strange Things Happening

I am testiing ConnersRSI as an intraday strategy.

I was also monitoring the performance of it today using AmiQuote to update quotes so I could run "Explore" and "Backtest" to see the results.

So, a weird thing kept on happening. The value of ConnersRSI kept changing every time I used AmiQuote to update the quotes and then subsequently clicked the "Explore" and "Backtest"

I am really unclear how this is possible since I was using (I believe) data in the program from the previous day(s) so there is NO WAY that the value of ConnersRSI should change after the opening bell. But it did.

Below is the code:

As you can see in the ConnersRSI function, I was using Ref(C, -1), which refers to the previous day. How could the values of the ConnersRSI change throughout the day? As far as I understand it, that is not possible.

So, I must be missing something. I am puzzled. Any help/direction would be greatly appreciated.

PosSize = 1000;

maxpos =  30; // maximum number of open positions
SetOption("InitialEquity", 50000 ); // set initial equity = 50K
SetOption( "MaxOpenPositions", maxpos );
SetPositionSize( 5.0, spsPercentOfEquity );


paramLenRSI = Param("RSI Closes Length", 3, 2, 100, 1);
paramLenUD = Param("RSI UpClose Length", 2, 2, 100, 1);
paramLenRank = Param("PercentRank Length", 100, 10, 252, 1);
paramAverage = Param("Average", 5, 1, 100, 1);
paramConnersRSI = Optimize("paramConnersRSI", 20, 10, 20, 1);


function ConnorsRSI(lenRSI, lenUD, lenROC)
{
      upDays = BarsSince(Ref(C, -1) <= Ref(C, -2));
      downDays = BarsSince(Ref(C, -1) >= Ref(C, -2));
      updownDays = IIf(upDays > 0, upDays, IIf(downDays > 0, -downDays, 0));
      crsi = ( PercentRank(ROC(Ref(C, -1), 1), lenROC) + RSIa(updownDays, lenUD) + RSI(lenRSI)) / 3;
      return crsi;
}


SetTradeDelays(0, 0, 0, 0);
SetOption( "ActivateStopsImmediately", True );


ShortPrice = O;
CoverPrice = C;


Stocks = 
      
   AND Ref(C, -1) >= 10.0 
   AND ConnorsRSI( paramLenRSI, paramLenUD, paramLenRank ) < paramConnersRSI 
   AND Ref(C, -1) >= 2.0 
   AND MA(Ref(V, -1), 20) > 200000;
   

Short = Stocks;

Cover = 0;

Buy = 0;
Sell = 0;

ApplyStop( stopTypeLoss,  stopModePercent, StopLossPct, 1 );
ApplyStop( stopTypeNBar, stopModeBars, 0, ExitAtStop = 0 );
   

Filter = Stocks;

AddColumn( Ref(O, -1), "Aug 9 Open", 1.2 );
AddColumn( Ref(C, -1), "Aug 9 Close", 1.2 );
AddColumn( RSIa(Ref(C, -1), 2), "Aug 9 RSI", 1.2 );
AddColumn( Ref(O, -0), "Aug 10 Open", 1.2 );
AddColumn( Ref(C, -0), "Aug 10 Close", 1.2 );
AddColumn( Ref(H, -0), "Aug 10 High", 1.2 );
AddColumn( RSIa(Ref(C, -0), 2), "Aug 10 RSI", 1.2 );
AddColumn( Ref(ShortPrice, 0), "Aug 10 ShortPrice", 1.2 );
AddColumn( Ref(High, 0), "Aug 10 High", 1.2 );
AddColumn( Ref(CoverPrice, 0), "Aug 10 CoverPrice", 1.2 );
Profit = ( Ref(ShortPrice, 0) - Ref(CoverPrice, 0) ) * ( PosSize / O );
AddColumn( Max( Profit, -100.0), "Profit", 1.2 );
AddColumn( IIf( Profit > 0, 1, 0), "Win", 1.2 );
AddColumn( IIf( ShortPrice < H, 1, 0) , "Possible", 1.0 );
AddColumn( ConnorsRSI(paramLenRSI,paramLenUD,paramLenRank), "ConnersRSI", 1.2 );type or paste code here

@pmfiorini, as far as I understand, the formula you are using seems to have been modified to calculate the value based on yesterday values, but on the last code line, you still use standard RSI that internally will also use the TODAY Close.

I suggest to restore the original formula; here a nice implementation by @CesarA and figure out the proper way to use it!

2 Likes

Hi,

Thanks for your response...

I am trying to understand which part of the last line of code uses yesterday's values...

To be clear...

The
PercentRank(ROC(Ref(C, -1), 1), lenROC)

should be OK because that is using Ref(C, -1) - Right?

The quantity

            RSIa(updownDays, lenUD) 

Should be Ok because "updownDays" is computed using Ref(C, -1)

So the issue is with

                       RSI(lenRSI))

because, by default, that would use today's Close values?

Is this correct?

Many thanks...

Easy to visually prove. Plot any stock. Add the RSI indicator (to make it more reactive select a short period).
Go to the menu item Symbol->Quote Editor and edit manually the last (most recent) Close value (and if needed to be consistent the High). Save the modified values. RSI plot value will change too.

@pmfiorini, why would you go to all the trouble of modifying the ConnorsRSI function itself when you could just call it as is and then use Ref() on the result?

prevCRSI = Ref(ConnorsRSI(paramLenRSI, paramLenUD, paramLenRank), -1);
2 Likes

Nice answer! (I think)

(actually, I'm relatively new to AFL - just a couple of months - still learning..,)