Situation 1: a stop and reverse is appropriate
Situation 1: an exit occurs on one bar and an occurs a few bars later.
I want to manage ALL entries and exits separately so I have some flexibility.
My strategy works fine on Situation 2 where an exit occurs on one bar and an entry occurs a few bars later.
Unfortunately, on situation 1 where a stop a reverse occurs, I don't get an exit of the previous position and instead end up with 2 contracts going in the reverse direction. As an example: Let's say I am long 1 contract when a stop and reverse situation occurs. I end up short 2 contracts instead of an exit of 1 from the long position and an entry of 1 contract for the short position.
This is the code... Notice that my exit conditions are exactly opposite to the entry conditions. This code is a simplified version of my "real" strategy but coded this to make sure of what I was doing. It behaves the exact same way my real strategy does. CalculateOnBarClose is set to true.
Based on my research of the documentation I expected the following to occur.
1. Open of bar #2 skips the ExitShort and ExitLong because there is no open position.
2. Evaluate open of bar #2 for an entry and enter either long or short. Let's say it entered a long with 1 contract.
3. Bar #3 is evaluated for an exit and let's say an exit condition is met so ExitLong removes 1 contract causing the long position to exit.
4. Continuing at open of Bar #3 to evaluate conditions for an entry. Conditions are met and so a short position is entered with 1 contract.
Following is what actually happens or so it seems.
1. First bar skips the ExitShort and ExitLong because there is no open position.
2. Evaluate at the open of bar #2 for an entry and enter either long or short. Let's say it entered a long with 1 contract.
3. This is where the difference occurs...Bar #3 is evaluated for an exit and let's say an exit condition is met so the exit block is fired but no exit occurs or at least it isn't apparent that an exit occurs.
4. Continuing on Bar #3 to evaluate conditions for an entry. Conditions are met and so a short position is entered with 2 contracts.
Seems that I must be misunderstanding what ExitLong and ExitShort are doing.
protected override void OnBarUpdate()
{
//***** Evaluate for an exit ******* //
if (CrossAbove(SMA(Fast), SMA(Slow), 1))
ExitShort();
else if (CrossBelow(SMA(Fast), SMA(Slow), 1))
ExitLong();
//***** Evaluate for an entry ******* //
if (CrossAbove(SMA(Fast), SMA(Slow), 1))
EnterLong();
else if (CrossBelow(SMA(Fast), SMA(Slow), 1))
EnterShort();
}
Please beat me over the head and shoulders if need be to make me understand.
I'm not providing screen shots until you tell me you need them because I'm thinking you will be able to tell me what I'm doing wrong just by looking at this.
Thanks,
Mike Winfrey

Comment