private double currentStop = 0;
private double firstStop = 0;
~~~~~~
firstStop = Close[0] - 80;
if (Position.MarketPosition == MarketPosition.Flat)
{
currentStop = 0;
}
~~~~~~
protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{
if (execution.Order.OrderState == OrderState.Filled)
{
if (currentStop > 0)
{
ExitLongStopMarket(0, true, Position.Quantity, currentStop, "", "");
}
if (currentStop == 0)
{
ExitLongStopMarket(0, true, Position.Quantity, firstStop, "", "");
}
}
}
protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
{
if (order.OrderType == OrderType.StopMarket && order.OrderState == OrderState.Accepted)
currentStop = order.StopPrice;
}
The ~~~~ lines are just to show that these things are in different places in the code. Theory being: When flat, current stop is set to 0. firstStop is updated on every bar to the be the correct value for an initial stop position. Once a position is entered, it enters ExitStopMarket orders and checks to see if currentStop is anything but 0, otherwise uses the value of FirstStop. When I manually move the stop using ChartTrader, once the stop is accepted it sends the order accepted signal from the exchange and currentStop is updated to this value (the place I just moved it to). Thus, the value is greater than 0 so when it does the check on the next bar, the stop is set again to same value I set it at. Anyways, it seems to be working perfectly on some, but on some others today I noticed it was still resetting the stop to the initial position. Would it be better if I changed the order of things in the OnExecutionUpdate or used ElseIf instead if? I am just trying to figure out why it is working well sometimes and others not.
Thanks

Comment