I would like to understand how to program properly when using buy(sell) stop market/limit orders, i.e. “EnterLongStopMarket”, “EnterShortStopLimit”… Because they are not ‘filled’ immediately as your stop price trigger could wait for several bars till the order is executed or cancelled (depending on the logic of your code within OnBarUpdate) and only want to add TP and SL once the order is filled and, therefore, can know the ‘AverageFillPrice’. Some part of example code:
Private Order entryOrder = null;
………
EntriesPerDirection = 1;
……….
protected override void OnBarUpdate()
{
if (CurrentBar < BarsRequiredToTrade || BarsInProgress != 0) return;
// OPEN LONG
if (Position.MarketPosition == MarketPosition.Flat)
if (entryOrder == null)
{
if (CrossAbove(Close, EMA1, 1))
{ //Condition to prevent the order from being rejected
if (High[0] > GetCurrentAsk())
entryOrder = EnterLongStopMarket(0, true,DefaultQuantity, High[0] , "");
else
entryOrder = EnterLong("");
}
}
else if (entryOrder.OrderState != OrderState.Filled)
{
// Cancelling order if it was not filled after 5 bars or price below our initial stop loss.
if ((Close[0] < stopVar) || (CurrentBar > barNumberOfOrder + 5) )
{ Print("Cancelling order");
CancelOrder(entryOrder);
entryOrder =null;
}
}
…………….
Some few doubts:
- I suppose it is a mandatory best practice to include our SL and TP orders within “OnOrderUpdate”. is that correct?, or is it better to use OnExecutionUpdate? I really would not like to wait till having a long position for adding variable TP and/or SL.
…………
protected override void OnOrderUpdate (Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
{
if (entryOrder !=null && entryOrder == order)
if (order.OrderState == OrderState.Filled)
{
stopVar = Math.Min( Low[0] , Low[1] );
SetStopLoss("", CalculationMode.Price, stopVar, false);
SetProfitTarget("", CalculationMode.Price, 2*High[0]-stopVar);
entryOrder= null;
}
else if (order.OrderState == OrderState.Cancelled)
{
Print("Order was cancelled");
entryOrder = null;
}
}
- How to avoid using a variable ‘entryOrder’ and its conditions “entryOrder == null” , etc… to check if an order is or not in transit? I realized that although ‘MarketPosition’ can be ‘Flat’ however we still may have an order in any state different from null…(¿?) I really don’t like to force ‘entryOrder = null’ after everything was filled (including TP and SL), maybe I could need to access the order to check/change something later. I don’t understand why there is that time gap between a market position being flat and a -supposedly- finished order and not null.
- How to know that a stop loss has been already changed? For example this code needs a bool variable to control that the stop loss was already moved up till position price (in a Long), otherwise this code part could be executed several times if price continues to move up. Can I know it in other way? any parameter?
{
if (Close[0] > Position.AveragePrice + (Position.AveragePrice - stopVar))
{
SetStopLoss("",CalculationMode.Price, Position.AveragePrice, false);
stopChanged = true;
Print("Stop Loss moved up");
}
}
Many thanks for your help and clarification.
Kind Regards,
Garri
Comment