Question on application of 'ref' syntax

Questions directed to our Support Team. Responses can be expected within 24 hours.

Question on application of 'ref' syntax

Postby alastair9 » Wed Oct 10, 2012 8:30 am

Hi,

I have a simple strategy which I’d like to test. It involves a metric being at a sustained high or low level for two days before selling (or buying, as the case may be), as well as requiring another move in the stock intraday. The specific metric doesn’t really matter (I’m planning on testing a few) but in the interest of being specific, let’s use RSI.

So let’s say that on Friday post-market I wish to create a buy screen for stocks that have closed lower than 30 on their 15-day RSI for the last two days (i.e. Thursday and Wednesday). If they have also ended lower on Friday I will purchase them at Monday’s open.

The piece I’m struggling with is how to use the ‘ref’ code to best achieve this. Currently the best I can come up with is something like this and I’m fairly sure this isn’t the correct application of the syntax…

ENTRY

rsi(c,15)<30 and
ref(c,-1)<ref(rsi((c,15),-1)<30) and
ref(c,-2)<ref(rsi(c,15),-2)<30) and
c<ref(c,-1)

If this were to be changed so that instead of a fixed-value rsi(c,15)<30 we had variables like rsi(c,@days)<@rsilevel) throughout how would this change the calculation – would it test every permutation of the rsi calculation for each line? Obviously in this case I would want to keep the rsi parameters the same within each test (i.e. constant for each Wednesday/Thursday/Friday combination) and then just change those parameters for the next test.

Thoughts and advice very much appreciated.

Alastair
alastair9
 
Posts: 2
Joined: Wed Oct 10, 2012 6:25 am

Re: Question on application of 'ref' syntax

Postby Overload » Wed Oct 10, 2012 12:21 pm

Based on your description, I believe the formula should look like this:

ref(rsi2(c,15),-1) < 30 and // THURSDAY RSI<30
ref(rsi2(c,15),-2) < 30 and // WEDNESDAY RSI<30
c<ref(c,-1) // FRIDAY CLOSE LOWER THAN PRIOR DAY

(Note that I've used RSI2(), which places the value to be tested in the first parameter. RSI() instead uses only one parameter: the number of days.)

Although you didn't mention that Friday's RSI should also be below 30, your code seemed to make that reference. If so, you could add the current day's RSI test and make it look like this:

ref(rsi2(c,15),-1) < 30 and // THURSDAY RSI<30
ref(rsi2(c,15),-2) < 30 and // WEDNESDAY RSI<30
c<ref(c,-1) and // FRIDAY CLOSE LOWER THAN PRIOR DAY
rsi2(c,15) < 30 // FRIDAY RSI<30

If the above is what you wanted, you could also make use of the HadAlert() formula, which identifies how many times a certain condition has been met over the last X days. Converting the above (including Friday's RSI test), you could simplify your formula to this:

HadAlert(rsi2(c, 15) < 30, 3) = 3 and
c<ref(c,-1)

As for using variables, a variable such as @days or @rsilevel will be the same for each usage within the Entry and Exit Strings. So if you happened to use @days 5 times in your Entry String, and @days was 15 for the current back test, it would replace all 5 instances of @days with 15. The variable @days would only change to something else at the next back-test combination.

Hopefully this helps.

Pete
Overload
 
Posts: 2248
Joined: Wed Nov 30, 2005 12:14 pm

Re: Question on application of 'ref' syntax

Postby alastair9 » Thu Oct 11, 2012 4:37 am

Thanks Pete, that's very useful, especially the HadAlert formula with which I was unfamiliar. I'm afraid I don't quite follow your usage of RSI2 instead of RSI - would you mind clarifying why you put that in your formula? Would I be right in saying that the RSI formula defaults to assume I am using the close price and if this is the case the RSI and RSI2 are, to me, functionally identical since the RSI2 allows flexibility regarding choice of price variable (high, low, close etc etc)

Alastair
alastair9
 
Posts: 2
Joined: Wed Oct 10, 2012 6:25 am

Re: Question on application of 'ref' syntax

Postby Overload » Thu Oct 11, 2012 8:28 am

the RSI and RSI2 are, to me, functionally identical

That is correct. RSI(15) = RSI2(C, 15)

When we originally built the RSI, we hardcoded it to use the Close Price according to the strict definition provided by Welles Wilder (the creator of the RSI). We found, however, that many users wanted the flexibility to use the RSI against alternate values (moving averages, median price, etc). The RSI2 was therefore created to allow the RSI to be run against values other than the Close Price.

Pete
Overload
 
Posts: 2248
Joined: Wed Nov 30, 2005 12:14 pm

Re: Question on application of 'ref' syntax

Postby Kevin_in_GA » Sat Oct 13, 2012 6:27 pm

Hadalert() is a very useful function, since it allows you to require that a series of specific triggers all occur on a defined date or over a defined period. Very helpful in system development.
Kevin_in_GA
 
Posts: 171
Joined: Wed Aug 04, 2010 10:14 am


Return to Technical Support

cron