Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Tracking Multiple ExitLongStopMarket Trades

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Tracking Multiple ExitLongStopMarket Trades

    Hi,

    I successfully enter and exit two trades (Long1 and Long2) in a strategy via StopMarketOrders/Limits.
    I try to set variables tracking whether StopLoss or TakeProfit has been hit for each trade via "OnExecutionUpdate".

    Problem: Only the last order seem to be tracked (i.e. SL2_OK or TP2_OK are set, not SL1_OK or TP1_OK), although both exit trade orders have been filled. The variable for the first order (Long1) seem to remain untouched.

    Question: Is it possible to track two order simultaneously via Order variables and "OnExecutionUpdate"?



    Code (copied and adjusted from SampleStrategy):

    //Definition of variable for first order

    private Order stopOrder = null; // This variable holds an object representing our stop loss order of the first trade
    private Order targetOrder = null; // This variable holds an object representing our profit target order of the first trade
    private Order entryOrder = null; // This variable holds an object representing our entry of the first trade

    private bool SL1_OK = false;
    private bool TP1_OK = false;



    // Definition of variables for second order

    private Order stopOrder2 = null; // This variable holds an object representing our stop loss order of the second trade
    private Order targetOrder2 = null; // This variable holds an object representing our profit target order of the second trade
    private Order entryOrder2 = null; // This variable holds an object representing our entry of the second trade

    private bool SL2_OK = false;
    private bool TP2_OK = false;
    [/I]



    if (State == State.Realtime)
    {
    if (entryOrder != null)
    entryOrder = GetRealtimeOrder(entryOrder);
    if (stopOrder != null)
    stopOrder = GetRealtimeOrder(stopOrder);
    if (targetOrder != null)
    targetOrder = GetRealtimeOrder(targetOrder);

    if (entryOrder2 != null)
    entryOrder2 = GetRealtimeOrder(entryOrder2);
    if (stopOrder2 != null)
    stopOrder2 = GetRealtimeOrder(stopOrder2);
    if (targetOrder2 != null)
    targetOrder2 = GetRealtimeOrder(targetOrder2);
    }

    // Verify Fill
    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 (order.Name == "Long1" || order.Name == "Short1")
    {
    entryOrder = order;
    if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
    {entryOrder = null;}
    }

    if(order.Name == "Long2" || order.Name == "Short2")
    {
    entryOrder2 = order;
    if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
    {entryOrder2 = null;}
    }
    }


    //SetTarget
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
    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))
    {
    stopOrder = ExitLongStopMarket(0, true, 10, SL_Level1, "Long1SL", "Long1");
    targetOrder = ExitLongLimit(0, true, 10, Buy_Level1"Long1TP", "Long1");

    }
    }

    if (entryOrder2 != null && entryOrder2 == execution.Order)
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled ||
    (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {
    stopOrder2 = ExitLongStopMarket(0, true, 10, SL_Level2, "Long2SL", "Long2");
    targetOrder2 = ExitLongLimit(0, true, 10, Buy_Level2, "Long2TP", "Long2");
    }
    }
    }

    if (execution.Order.OrderState == OrderState.Filled)
    {
    if(stopOrder == execution.Order)
    {
    SL1_OK = true;
    entryOrder = null;
    stopOrder = null;
    targetOrder = null;
    }
    if(targetOrder == execution.Order)
    {
    TP1_OK = true;
    entryOrder = null;
    stopOrder = null;
    targetOrder = null;
    }
    if(stopOrder2 == execution.Order)
    {
    SL2_OK = true;
    entryOrder2 = null;
    stopOrder2 = null;
    targetOrder2 = null;
    }
    if(targetOrder2 == execution.Order)
    {
    TP2_OK = true;
    entryOrder2 = null;
    stopOrder2 = null;
    targetOrder2 = null;
    }
    }
    }


    protected override void OnBarUpdate()
    {
    if(BuyCondition1)
    {EnterLong(10, "Long1");}

    if(BuyCondition2)
    {EnterLong(10, "Long2");}
    }

    #2
    Hello P-Sionic,

    Thanks for your inquiry.

    To answer your question directly, Yes, OnExecutionUpdate() and OnOrderUpdate() can be used to track any number of Order objects.

    To address your problem, debugging steps must be taken to step through the strategy logic and observe the logic in OnExecutionUpdate() and why it is not reaching the line where you set your bool.

    I would suggest to add a print for the whole execution at the top of OnExecutionUpdate(), a print for the variables used to control the logic that sets the bool, as well as prints directly beside where you set your bools. This will draw a complete picture of why the bools aren't setting.

    For example, a partial fill may occur but will not set your bool when an execution is made for that order.

    We have some informative threads on debugging NinjaScripts. I will link them below.

    Debugging - https://ninjatrader.com/support/foru...ead.php?t=3418

    TraceOrders - https://ninjatrader.com/support/foru...ead.php?t=3627

    Please let me know if I may be of further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thanks. Will try it. Indeed, it seems the code is working but the problem occurs after the code is executed during its evaluation.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by jonfan45, Today, 06:06 PM
      0 responses
      8 views
      0 likes
      Last Post jonfan45  
      Started by rustyspring, Today, 05:15 PM
      0 responses
      9 views
      0 likes
      Last Post rustyspring  
      Started by xulu13, Today, 05:01 PM
      0 responses
      12 views
      0 likes
      Last Post xulu13
      by xulu13
       
      Started by Pbarrionuevo, Today, 04:30 PM
      0 responses
      9 views
      0 likes
      Last Post Pbarrionuevo  
      Started by vantojo, Today, 01:39 PM
      0 responses
      20 views
      0 likes
      Last Post vantojo
      by vantojo
       
      Working...
      X