Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Entry Order Filled Without Calling OnOrderUpdate/OnExecution for StopLoss

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

    Entry Order Filled Without Calling OnOrderUpdate/OnExecution for StopLoss

    Recently I'm frequently having really scary experiences with my strategies running on real time live trading data. My broker is IB.

    After my strategy submitted entry order, the order is confirmed filled with IB. However, the OnOrderUpdate and OnExecution didn't get called yet strategy position reflects newly entered position. I'm scared as I need OnOrderUpdate and OnExecution to submit corresponding stop loss order and profit target order, as well as notifying me through email.

    I never had such problem in backtesting, however, it happened again in today's Fed rate meeting hours.

    Please help with this issue.

    #2
    Please see attached code:


    Code:
            #region OnOrderUpdate
            protected override void OnOrderUpdate(IOrder order)
            {
                #region Set Initial Stop Loss
                if (entryorder == order)
                {
                    switch (order.OrderAction)
                    {
                        case OrderAction.Buy:
                            if (order.OrderState == OrderState.Filled)
                            {
                                if (stoplossticks != 0)
                                {
                                    if (channelbarsfilter > 0)
                                    {
                                        SetStopLoss(order.Name, CalculationMode.Price, Math.Max(order.AvgFillPrice - stoplossticks * TickSize, 10* TickSize), false);
                                        SendMail("[email protected]", "[email protected]", "Long " + order.Instrument
                                            + " Position Stop Loss Set @ " + Math.Max(order.AvgFillPrice - stoplossticks * TickSize, 10 * TickSize) + " Qty=" + order.Quantity, "Sell to Close Stop Loss Set");
                                    }
                                    else
                                    {
                                        SetStopLoss(order.Name, CalculationMode.Price, order.AvgFillPrice - stoplossticks * TickSize, false);
                                        SendMail("[email protected]", "[email protected]", "Long " + order.Instrument
                                            + " Position Stop Loss Set @ " + (order.AvgFillPrice - stoplossticks * TickSize) + " Qty=" + order.Quantity, "Sell to Close Stop Loss Set");
                                    }
                                }
                                else
                                {
                                    SetStopLoss(order.Name, CalculationMode.Price, order.AvgFillPrice - stoploss, false);
                                    SendMail("[email protected]", "[email protected]", "Long " + order.Instrument
                                        + " Position Stop Loss Set @ " + Math.Round(order.AvgFillPrice - stoploss, 4) + " Qty=" + order.Quantity, "Sell to Close Stop Loss Set");
                                }
    
                                if (profit_ticks != 0)
                                {
                                    SetProfitTarget(order.Name, CalculationMode.Ticks, profit_ticks);    //profit taking
                                    SendMail("[email protected]", "[email protected]", "Long " + order.Instrument
                                        + " Profit Target Set @ " + (order.AvgFillPrice + profit_ticks * TickSize) + " Qty=" + order.Quantity, "Sell to Close Profit Target Set");
                                }
    
    
                                post_entry_value = order.AvgFillPrice;
                                Last_EntryOrder_AvgFillPrice = order.AvgFillPrice;
                                done_with_adjustment = false;
                                next_entry = false;
                                //
                                //Print(Time[0] + "    " + "Initial Post Entry Value Set at " + order.AvgFillPrice + "      @@@");
                            }
                            break;
                    }
                }
                #endregion
                //Post stop adjustment ---enabling next entry
    
    
    
    
            }
            #endregion
    
            #region OnExecution
            protected override void OnExecution(IExecution execution)
            {
                if (execution.Order != entryorder)
                {
                    if (execution.Order.FromEntrySignal == Last_Entry_Signal_Name)
                    {
                        if (entryorder.OrderAction == OrderAction.Buy)
                        {
                            if (execution.Order.AvgFillPrice < Last_EntryOrder_AvgFillPrice)
                            {
                                next_entry = true;
    
                            }
                            SendMail("[email protected]", "[email protected]", "Long " + execution.Instrument
                                + " Position Stopped @ " + execution.Price + " Qty=" + execution.Quantity, "Order Filled");
    
                        }
    
                        if (entryorder.OrderAction == OrderAction.SellShort)
                        {
                            if (execution.Order.AvgFillPrice > Last_EntryOrder_AvgFillPrice)
                            {
                                next_entry = true;
    
                            }
                            SendMail("[email protected]", "[email protected]", "Short " + execution.Instrument
                                + " Position Stopped @ " + execution.Price + " Qty=" + execution.Quantity, "Order Filled");
    
                        }
    
                        next_entry = true;
                    }
    
                    stopwatch_next_entry = true;   //switch on
                    PrintWithTimeStamp("##################Stopwatch     On        ###");
    
                    stopwatch_next_entry_switch = false;  //restrict next entry
                    PrintWithTimeStamp("##################Stopwatch    ###NEXT TRADE RESTRICTED###");
    
                    previous_order_bar = CurrentBars[1]; //record bar number
                    PrintWithTimeStamp("$$$$Previous Order Bar Number= " + CurrentBars[1].ToString());
    
    
                    //Print(Time[0] + "Execution Order From EntrySignal:  " + execution.Order.FromEntrySignal + "\t" + "entryOrder From EntrySignal: " + Last_Entry_Signal_Name);
                }
    
                if (execution.Order == entryorder)
                {
                    if (execution.MarketPosition == MarketPosition.Short)
                    {
                        SendMail("[email protected]", "[email protected]", "Short " + execution.Instrument
                            + " Entered @ " + execution.Price + " Qty=" + execution.Quantity, "Order Filled" + " ***XXX***");
                    }
    
                    if (execution.MarketPosition == MarketPosition.Long)
                    {
                        SendMail("[email protected]", "[email protected]", "Long " + execution.Instrument
                            + " Entered @ " + execution.Price + " Qty=" + execution.Quantity, "Order Filled" + " ***XXX***");
                    }
    
                    previous_entryorder_bar = CurrentBars[1];
    
                    if (time_stop_minutes != 0)
                    {
                        stopwatch_timestop = true;
                    }
                }
    
    
    
    
            }
            #endregion

    Comment


      #3
      Here pls see attached screenshot
      Click image for larger version

Name:	NT Log 10.30.13.jpg
Views:	1
Size:	560.2 KB
ID:	869402

      I simultaneously run the same strategy on both real and sim acct (for chart viewing purpose).
      I suspect that there's some programming error on your end on handling multiple orders of multiple accts and strategies as you can see in attached screenshot. Stop loss order and profit target orders associated with real IB acct#U15** never got submitted.

      I hope you could investigate this issue.

      Thanks!

      Comment


        #4
        Hi Kelvin,

        Thank you for your post.

        From your code, I can see that you have a switch for the OrderUpdate. However you only have one case in the method.
        Are you planning on adding more cases later on?

        I recommend to try running TracerOrders = true; from the Initialize section of the code
        This will print out the order being submitted and if they get ignored or rejected and a reason why. This will print out to the Output Window which can be found by Tools -> Output Window.

        I would also try using Print() Statements in the OnOrderUpdate to see what is getting called for the live account and whats not.
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          hi cal,

          i edited the code before i posted it.
          it has nothing to do with that case switch.
          i have no problem running strategy in backtesting nor real time trading.
          the below descried event happens occasionally.

          i'd appreciate if you could help. do u have a ticket# so i can email you the trace file?




          Originally posted by NinjaTrader_Cal View Post
          Hi Kelvin,

          Thank you for your post.

          From your code, I can see that you have a switch for the OrderUpdate. However you only have one case in the method.
          Are you planning on adding more cases later on?

          I recommend to try running TracerOrders = true; from the Initialize section of the code
          This will print out the order being submitted and if they get ignored or rejected and a reason why. This will print out to the Output Window which can be found by Tools -> Output Window.

          I would also try using Print() Statements in the OnOrderUpdate to see what is getting called for the live account and whats not.

          Comment


            #6
            Kelvin2088,

            You can send me an email at support[at]ninjatrader[dot]com and reference this thread in the body and put ATTN Cal in the subject
            Cal H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by CortexZenUSA, Today, 12:53 AM
            0 responses
            1 view
            0 likes
            Last Post CortexZenUSA  
            Started by CortexZenUSA, Today, 12:46 AM
            0 responses
            1 view
            0 likes
            Last Post CortexZenUSA  
            Started by usazencortex, Today, 12:43 AM
            0 responses
            5 views
            0 likes
            Last Post usazencortex  
            Started by sidlercom80, 10-28-2023, 08:49 AM
            168 responses
            2,265 views
            0 likes
            Last Post sidlercom80  
            Started by Barry Milan, Yesterday, 10:35 PM
            3 responses
            12 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Working...
            X