Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 StopMar****rders/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 samish18, 04-17-2024, 08:57 AM
      16 responses
      55 views
      0 likes
      Last Post samish18  
      Started by arvidvanstaey, Today, 02:19 PM
      3 responses
      9 views
      0 likes
      Last Post NinjaTrader_Zachary  
      Started by jordanq2, Today, 03:10 PM
      2 responses
      8 views
      0 likes
      Last Post jordanq2  
      Started by traderqz, Today, 12:06 AM
      10 responses
      18 views
      0 likes
      Last Post traderqz  
      Started by algospoke, 04-17-2024, 06:40 PM
      5 responses
      47 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Working...
      X