Understand RSI Agita strategy
Author: Musashi1970
Creation Date: 4/4/2009 1:59 AM

#### Musashi1970

#1

Hi

just having subscribed to WLD5, I analyzed the code of the RSI Agita strategy to get used to the C# environment (which I am not to familiar until now). During this task, two questions came up which could not resolve, so I looking for help here.

1.
[...]
//Entries are allowed only if price is below last entry price
double lastEntryPrice = IsLastPositionActive ? LastPosition.EntryPrice : Double.MaxValue;

To my understanding the ?: expression in C# is a kind of "if then else". What I do not understand here is the else-part: what is Double.Maxvalue ? (I understand that double alone describes which type a variable is like in the beginning of the last example line (double lastentryprice = ....)

2.
//Check penetration of RSI below levels 35 down to 5
level = oversold.ValueInt;
while (level > 0)
{
if (CrossUnder(bar, rsi20, level))
if (Bars.Close[bar] < lastEntryPrice)
level -= 5;
}

I am totally not able to understand what this while-loop is for. To my understanding the strategy buys if the RSI-indicator of the current bar is below a certain threshold (level-variable) and if the close of the current bar is below the last entry price. I would think that the while-loop should result in multiple purchases at the same bar if the RSI is some multiples of 5 below 35. But this does not happen...
Even more astonishing for me was that the strategy generated less orders when I turned off the, in my opinion, unnecessary lines:

level = oversold.ValueInt;
// while (level > 0)
//{
if (CrossUnder(bar, rsi20, level))
if (Bars.Close[bar] < lastEntryPrice)
// level -= 5;
//}

So, somehow they are obviously not unnecessary....

It would be highly appreciated if somebody could help me out, please

Kind Regards

#### Eugene

#2
QUOTE:
To my understanding the ?: expression in C# is a kind of "if then else".

That right, it's sometimes called "ternary operator" and can be stacked. Double.Maxvalue is the largest possible value a double can take. This way, when there's no active position, entries are allowed. But when the system creates a new position, its entry price becomes the new lastEntryPrice.
QUOTE:
I am totally not able to understand what this while-loop is for.

Your understanding is correct. Multiple purchases on the same bar will happen if the value of the RSI's decline on that bar will be greater than 1 multiple of 5 e.g. RSI was at 32, crossed below the default level 30, and has become 21: here we have 2 buys.

#### Musashi1970

#3

Q1 is clear now

Regarding Q2:

I use ticker ^GSPC (S&P 500) of the Yahoo Indices. When I run RSI Agita, it buys seven times in the period between 23.04.1970 and 26.05.1970 and closes all positions at 20.07.1970

If I change the code to:

CODE:
I just added the commentarty signs in the four arrow-marked lines. This change should suppress the multiple purchases at the same bar. But when I run the script, it gives only one buy signal at the 23.04.1970 but not the other six till 26.05.1970. How can that be, it should suppress only multiple purchases at the same bar but not at a different bar if all other conditions are met.

Is that a bug or am I overlooking something ?

Kind Regards

#### Eugene

#4
This is not a bug, because you have commented out the essential line that progressively decreases the threshold level:
CODE:

That of course will make the entries disappear -- until there is a crossover of 35 again. As you see, the strategy works absolutely as expected.

#### Musashi1970

#5

Sorry, but I am not sure if I understand. After a purchase (e.g. at the 23.04.1970), we´ll go one step further in the for-loop which also means it set back the level-variable to 30 (level = oversold.ValueInt;). Therefore, at least to my understanding, the system can do another purchase based on the condition test...

#### Eugene

#6
Actually, the variable isn't set "back" to 30 -- in fact, commenting fixed the threshold at 30 for the whole length of the test. On the contrast, the original system lowers the threshold level at the increment of 5, allowing further trades. The six trades (now inhibited) you're pointing at were taken with the following RSI levels (approx.): 24.54, 19.14, 23.9, 22.8, 23.05 and 19.09 - you can check that in the Trades tab by modifying the entry line as shown below. Since the level became fixed, there were no CrossUnders at 30.

CODE:

#### Musashi1970

#7
Thx for the hint with the trade tab.
Maybe we come closer, but I still feel lost.
Why were no crossunders below 30 ? Especially if the value is fixed at thirty for the whole test, the RSI levels the trade tab is showing should lead to buy signals because they are all under 30.

I also changed the increment to 1 in the original system and it seems to me (looking at the signals in the same time period) that the system somehow remembers the level during the next bars which I again do not understand because the next bar means the next loop-run-through of the for-loop. And if this is the case, the variable level will be set back to 30 (level = oversold.ValueInt;) before the while-loop and the check of the other conditions starts.

#### Eugene

#8
QUOTE:
Why were no crossunders below 30 ?

Please re-read the definition of CrossUnder in the QuickRef tool, and review the chart for the time span of 23.04.1970 till 26.05.1970. No crossunder happened until 1971, and no crossunder means no trade.

QUOTE:
it seems to me

The next iteration of the for-loop means a new bar, and the threshold level is reset to the initial value (i.e. 30). The while-loop here is a nested loop and will be executed several times on the same bar. Please point to the concrete trade that confuses you.

#### Musashi1970

#9
Here we go. The definition of crossunder was my problem.
Just to make sure, the other six trades of the time period in the original RSI Agita script were executed because there were crossunders of the 25- or 20-level, respectively ?

Thanks again for the quick support.

Kind Regards

#### Eugene

#10
You're welcome.

QUOTE:
Just to make sure, the other six trades of the time period in the original RSI Agita script were executed because there were crossunders of the 25- or 20-level, respectively ?

That's right, but don't take my word on it - you could check it out easily if you plot the RSI value. Replace this line:
CODE: