Removing "else" from trading loop
Author: ReneW
Creation Date: 11/15/2012 8:39 AM
profile picture

ReneW

#1
I have changed (errorneously?) the base loop in a strategy from:

CODE:
Please log in to see this code.


To:


CODE:
Please log in to see this code.



In my opinion the (bool) flag IsLastPositionActive can only take 2 values
and the statements should be semantically equivalent.
My strategy shows a difference of 13% annually over 16 years backtest
with if (!IsLastPositionActive).

Please comment.

Regards, Rene
profile picture

Eugene

#2
When you remove the "else", here's what happens:

1. IsLastPositionActive was true.
2. You exited, making IsLastPositionActive = false.
3. The code falls down and since !IsLastPositionActive == true, executes your entries.

Simple as that. That's why the standard single-position template is there.

P.S. See also (on a somewhat related matter): the WealthScript Programming Guide > Programming Trading Strategies > Peeking > Trading based on Position Active Status.
profile picture

ReneW

#3

Thank you for the explanation.

My strategy exits at bar with a limit/stopLoss or holding period expired
and calculates new entries for the open at bar+1.

Therefore, in my opinion, I can use if(!IsLastPositionActive)
and Buy at bar+1 and it is not a mistake.


As I understand, the basic templates with "else" makes
a one day "break" for each symbol and
the if(!IsLastPositionActive) allows a fast entry.

That would explain the annual difference (and would not be peeking).

Is this correct ?


Rene




profile picture

Eugene

#4
Well, that's up to you to not get the exit alerts because they use "bar" vs. "bar+1" as they should be, not to mention that it doubles your chance of peeking into the future.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).