I have a very simple strategy, if (Close[0]>Open[1]) {EnterLong(Convert.ToInt32(DefaultQuantity), "MyEntry");}
After this with this code, I will set SL and TP.
protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
{
// Handle entry orders here. The entryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
// Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
// This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not gauranteed to be complete if it is referenced immediately after submitting
if (order.Name == "MyEntry")
{
entryOrder = order;
// Reset the entryOrder object to null if order was cancelled without any fill
if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
entryOrder = null;
}
}
protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{
// Print(execution.ToString());
/* We advise monitoring OnExecution to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate()
which ensures your strategy has received the execution which is used for internal signal tracking. */
if (entryOrder != null && entryOrder == execution.Order)
{
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 || entryOrder.OrderAction == OrderAction.Buy)
{
// Stop-Loss order
stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - (Stop_Lost )* TickSize, "MyStop", "MyEntry");
// Target order
targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + (Take_Profit ) * TickSize, "MyTarget", "MyEntry");
}
else if (Position.MarketPosition == MarketPosition.Short || entryOrder.OrderAction == OrderAction.Sell)
{
stopOrder = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + (Stop_Lost )* TickSize, "MyStop", "MyEntry");
targetOrder = ExitShortLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - (Take_Profit ) * TickSize, "MyTarget", "MyEntry");
}
// Resets the entryOrder object to null after the order has been filled
if (execution.Order.OrderState != OrderState.PartFilled)
entryOrder = null;
}
}
// Reset our stop order and target orders' Order objects after our position is closed.
if ((stopOrder != null && stopOrder == execution.Order) || (targetOrder != null && targetOrder == execution.Order))
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
{
stopOrder = null;
targetOrder = null;
}
}
Someone Can help me to solve this? Thanks.

Comment