PDA

View Full Version : Reversing positions


jonesr227
09-12-2007, 01:34 AM
The following code backtests OK but in live simulated trading the initial 1 short contract gets reversed to 2 long contracts, whereas I expect that there to be only 1 long contract. The live simulation result is shown in attached graphic.

The relevant reversing code segment is given below. I don't see what is wrong with my code (especially since it backtests OK).


elseif (Position.MarketPosition == MarketPosition.Short)
{
sellsToday = true;
traded = true;



if (!buysToday)
{
bp = MIN(Low, BarsSinceEntry())[0] + diff;
ExitShortStop(bp);
if ( (ToTime(Time[0]) <= Time_RevEnd) && ShortRev )
{
EnterLongStop(bp, "SRev");
LongRev = false;
sellsToday = false;
}
}
}

NinjaTrader_Josh
09-12-2007, 10:40 AM
Hi jonesr227,

Are you using CalculateOnBarClose = false? I suspect the reason you are experiencing extra reversals is because your code is running through the logic more than once while your first reversal order is still in a pending/working state. This can happen because your check is only a Position check and even though you submitted an order to close this position you will still have the position until the order is completed.

Please put in some Print() to see what exactly is happening in your code. A print right after the ExitShortStop(bp) will work. I suspect it will print twice. You can prevent this with a variable that goes true right after the first exit and only allows for the logic to run again if that variable is false.