I've recently introduced a fixed profit target into my system and every now and then it's causing a crash somewhere, I have checked and rechecked my code and can't see an issue, furthermore I have replayed several instances of the crash on Market Replay and it doesn't happen the way it did on the live SIM. It has occurred to me that I may be referring to objects incorrectly in my OnExecution Method. Perhaps you could confirm. I have pasted the code below, for simplicity I've removed as much of the stuff that is not relevant.
Basically this order is a stop and reverse order that has just executed, on execution I set a profit target for the order based on a parameter that decides whether to add a fixed pip value specified in another parameter or whether to use an R-Multiple ratio which is not relevant as in this instance I'm using a fixed target. So FLTT parameter is 1.
Here is the code:
protectedoverridevoid OnExecution(IExecution execution)
{
if (ShortOrderSAR != null && ShortOrderSAR.Token == execution.Order.Token)
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
LongEntry=ShortOrderSAR.AvgFillPrice;
if(FLTT==1)
LongTargetPrice = LongEntry + FixedLimTarget * TickSize;
else
LongTargetPrice = LongEntry + ((LongPriceDifference*FixedLimTarget)*TickSize);
ShortOrderTargetSAR = ExitLongLimit(1, true, execution.Order.Filled,LongTargetPrice, "SSAR P", "SSAR1");
}
if (execution.Order.OrderState != OrderState.PartFilled)
{
ShortOrderSAR = null;
}
}
}
What happens is intermittendly my program crashes with a could not create order below the bid etc. and says I tried to use a price of 0.0150 which is effectively my fixed target but somehow it has lost the original price that should be set as the fill price in this object. Should I be using
LongEntry=execution.Order.AvgFillPrice;
instead of
LongEntry=ShortOrderSAR.AvgFillPrice;
I can't see how this makes a difference but somewhere along the lines I seem to be getting a zero in LongEntry when I believe it should always be populated with the fill price of the order executing.
Thanks for the help.
Cheers
Ross
Comment