openedPosition.TakeProfit and openedPosition.Stoploss are null in the OnOpenedPosition event?
openedPosition.TakeProfit and openedPosition.Stoploss are null in the OnOpenedPosition event?
27 Nov 2012, 06:03
I called a Trade.CreateBuyStopOrder, the log shows that the pending order was placed and then filled with the stoploss and takeprofit defined. However when It calls the OnPositionOpened event, Why is it that the openedPosition.TakeProfit and openedPosition.Stoploss are null?
My intention is to modify one and leave the other as defined when I created the pending order.
Replies
admin
04 Jan 2013, 17:24
The reason this is, is that the Stop Loss and Take Profit are set right after the OnPosistionOpened event. The workaround for this would be to modify the SL/TP in another function called if a Position field set in the OnPositionOpened is not null.
i.e.
Position _pos; protected override void OnTick() { if (Trade.IsExecuting) return; double targetPrice = Symbol.Ask + Symbol.PipSize; if (Account.PendingOrders.Count == 0 && Account.Positions.Count == 0) Trade.CreateBuyLimitOrder(Symbol, 1000, targetPrice, targetPrice - Symbol.PipSize, targetPrice + Symbol.PipSize, Server.Time.AddMinutes(10)); if (_pos != null) { // call method to modify SL/TP Print("Stop Loss {0}", _pos.StopLoss); Print("Take Profit {0}", _pos.TakeProfit); } } protected override void OnPendingOrderCreated(PendingOrder newOrder) { Print("Pending order created"); Print("Stop Loss {0}", newOrder.StopLoss); Print("Take Profit {0}", newOrder.TakeProfit); } protected override void OnPositionOpened(Position openedPosition) { _pos = openedPosition; }
@admin
admin
29 Nov 2012, 12:36
I'm not sure why that is maybe you can share your code. In the meantime please test this and tell us if it solves your problem:
@admin