1) If it's filled for 1 contract Long, the Stop called in the OnExecution section only has 1 contract to sell, instead of 2 so the system can close out the long and initiate a short position (but the code asks for 2)? The OnBarUpdate section works fine for long positions on subsequent bars.
2) On short positions, the OnExecution section doesn't place a protective stop as requested, but only when the bar closes will it update to 2 contracts, one to cover and one to go long in the OnBarUpdate section.
So two different issues depending on direction but exactly the same code? Am I missing something simple in the following code:
protectedoverridevoid OnBarUpdate()
{
{//Start of Entry Logic
if (Low[0] >= Low[1] && Position.MarketPosition == MarketPosition.Flat)
{
entryOrder = EnterShortStop(1,Low[0]-.25, "MyEntry");
}
{
entryOrder = EnterLongStop(1,High[0]+.25, "MyEntry");
}
}//End of Entry Logic
{
if (Position.MarketPosition == MarketPosition.Long)//Adjust stop for Long positions on every new bar.
{
stopOrder = ExitLongStop(0, true,2, Low[0]-.25, "MyStop", "MyEntry");
}
if (Position.MarketPosition == MarketPosition.Short)//Adjust stop for Short positions on every new bar.
{
stopOrder = ExitShortStop(0, true,2, High[0]+.25, "MyStop", "MyEntry");
}
}
}
protectedoverridevoid OnExecution(IExecution execution)
{
if (entryOrder != null && entryOrder.Token == execution.Order.Token)
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
if (Position.MarketPosition == MarketPosition.Long)
{
stopOrder = ExitLongStop(0, true, 2,Low[0]-.25, "MyStop", "MyEntry");
}
if (Position.MarketPosition == MarketPosition.Short)
{
stopOrder = ExitShortStop(0, true, 2, High[0]+.25, "MyStop", "MyEntry");
}
// Resets the entryOrder object to null after the order has been filled or partially filled
if (execution.Order.OrderState != OrderState.PartFilled)
entryOrder = null;
}
}
// Reset our stop order and target orders' IOrder objects after our position is closed.
if ((stopOrder != null && stopOrder.Token == execution.Order.Token) || (targetOrder != null && targetOrder.Token == execution.Order.Token))
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
{
stopOrder = null;
// targetOrder = null;
}
}
}

Comment