Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop not working in Real Time

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

    Stop not working in Real Time

    Stop not working in Real Time
    I have a Strategy that submits an order followed by an unmanaged stop. The strategy works as designed in backtest but when I try running it in realtime with IB Simulated envirment the stop does not get submitted. Here is the effected statements statements.
    protected override void Initialize()
    {
    CalculateOnBarClose = true;
    BarsRequired = 25;
    TraceOrders = false;
    ExitOnClose = true;
    Unmanaged = true;

    // for speed set to true
    ExcludeTradeHistoryInBacktest = false;

    Add(PeriodType.Minute,SecBarLen);


    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {

    if(BarsInProgress == 0)
    {
    MinBars();
    }
    else
    {
    HourBars();
    }


    } // End On Bar Update

    private void HourBars()
    {
    // Market is Long -- Test for exit
    if (Position.MarketPosition == MarketPosition.Long)
    {
    entryOrder = SubmitOrder(0, OrderAction.Sell, OrderType.Market, Quanity, 0, 0, "L", "ExitLong");
    }

    // Market is Short -- Test for exit
    if (Position.MarketPosition == MarketPosition.Short)
    {
    entryOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Market, Quanity, 0, 0, "S", "ExitShort");
    }


    } // End HourBars
    private void MinBars()
    {

    if (Position.MarketPosition == MarketPosition.Long)
    {

    entryOrder = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, Quanity, 0, NewStop, "L", "BreakLong");



    } // End Long
    if (Position.MarketPosition == MarketPosition.Short)
    {

    entryOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Stop, Quanity, 0, NewStop, "S", "BreakShort");
    //

    } // End Short


    } // End of MinBars

    #2
    Hi GuyFB, any errors in your log tab as this occurs? Please also activate the TraceOrders option and then open the output window to debug the order behavior you're seeing.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Bertrand,
      Still testing why a stop is not executed in real time vs backtest. What I have now found is that a stop WILL NOT be executed if it is executed in the "OnOrderUpdate" function both in back test and in real time. This to me is a logical place to initiate a stop. You have just found that the fill has taken place. If you wait until the next bar you could take a huge loss!!

      I am using unmanged stops.

      Comment


        #4
        GuyFB,

        The code you have posted has no OnOrderUpdate() at all. Please post the latest version of your code along with what output/printouts you see from TraceOrders and any additional tracking you may be doing through Print() statements in OnOrderUpdate(). Thank you.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Could you tell me why NJ canceled the stop order in this example?

          Below is the submission and the trace. There were no errors in the Log file

          protected override void OnOrderUpdate(IOrder order)
          {
          if(entryOrder != null && entryOrder.Token == order.Token)
          { if(order.OrderState == OrderState.Filled)
          {
          if (Position.MarketPosition == MarketPosition.Long)
          {

          NewStop = entryOrder.AvgFillPrice - (entryOrder.AvgFillPrice * stopPct);
          entryOrder = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, Quanity, 0, NewStop, "L", "BreakLong");
          Print("Break Long " + Time[0].ToString()+ " " + entryOrder.Instrument.FullName + " Stop " + NewStop);
          }
          else
          {
          NewStop = entryOrder.AvgFillPrice + (entryOrder.AvgFillPrice * stopPct);
          entryOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Stop, Quanity, 0, NewStop, "S", "BreakShort");
          Print("Break Short " + Time[0].ToString()+ " " + entryOrder.Instrument.FullName + " Stop " + NewStop);
          }
          }

          } // Order Filled
          } // Entery order check

          } // end OnOrderUpdate

          Trace Ouptut
          5/25/2010 8:50:00 AM Entered internal SubmitOrder() method at 5/25/2010 8:50:00 AM: Action=Sell OrderType=Market Quantity=200 LimitPrice=0 StopPrice=0 OcoId='S' Name='EnterShort'

          5/25/2010 8:50:00 AMOrder Opened: MDY Price: 130.61

          5/25/2010 8:50:00 AM Entered internal SubmitOrder() method at 5/25/2010 8:50:00 AM: Action=Buy OrderType=Stop Quantity=200 LimitPrice=0 StopPrice=132.31 OcoId='S' Name='BreakShort'

          Break Short 5/25/2010 8:50:00 AM MDY Stop 132.30793

          5/25/2010 8:50:00 AM Cancelled OCO paired order: BarsInProgress=0: Order='NT-00247/Sim101' Name='BreakShort' State=Working Instrument='MDY' Action=Buy Limit price=0 Stop price=132.30793 Quantity=200 Strategy='TestTrendControlProdV1' Type=Stop Tif=Gtc Oco='S' Filled=0 Fill price=0 Token='95d372e986474d4fb0d7321e09c40699' Gtd='12/1/2099 12:00:00 AM'

          Short 5/25/2010 8:55:00 AM MDY Close: 130.66
          Short 5/25/2010 9:00:00 AM MDY Close: 130.6
          Short 5/25/2010 9:05:00 AM MDY Close: 131.66
          Short 5/25/2010 9:10:00 AM MDY Close: 131.12
          Short 5/25/2010 9:15:00 AM MDY Close: 131.32
          Short 5/25/2010 9:20:00 AM MDY Close: 131.77

          Comment


            #6
            GuyFB,

            Orders were submitted with the same OCO id. If any order is filled with this id, it will cancel all others with the same id. I'm not sure where your "EnterShort" rule is in the example.

            5/25/2010 8:50:00 AM Entered internal SubmitOrder() method at 5/25/2010 8:50:00 AM: Action=Sell OrderType=Market Quantity=200 LimitPrice=0 StopPrice=0 OcoId='S' Name='EnterShort'

            5/25/2010 8:50:00 AMOrder Opened: MDY Price: 130.61

            5/25/2010 8:50:00 AM Entered internal SubmitOrder() method at 5/25/2010 8:50:00 AM: Action=Buy OrderType=Stop Quantity=200 LimitPrice=0 StopPrice=132.31 OcoId='S' Name='BreakShort'

            Break Short 5/25/2010 8:50:00 AM MDY Stop 132.30793

            5/25/2010 8:50:00 AM Cancelled OCO paired order: BarsInProgress=0: Order='NT-00247/Sim101' Name='BreakShort' State=Working Instrument='MDY' Action=Buy Limit price=0 Stop price=132.30793 Quantity=200 Strategy='TestTrendControlProdV1' Type=Stop Tif=Gtc Oco='S' Filled=0 Fill price=0 Token='95d372e986474d4fb0d7321e09c40699' Gtd='12/1/2099 12:00:00 AM'
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              At the time the stop was canceled the short order was still in effect and the system was still short after the cancel. The display "Short 5/25/2010 8:55:00 AM MDY Close: 130.66" comes after the test. "if (Position.MarketPosition == MarketPosition.Short)"

              Is there a way to get what the next order number will be so I can use it for the stop OCO name?

              Comment


                #8
                GuyFB,

                One thing that should be noted is that you should not be checking .Token. Token will change as the strategy rolls from historical to live and as such is not a good check. Instead you can compare the IOrder objects directly for equality. http://www.ninjatrader.com/support/h...nt7/iorder.htm

                if (entryOrder != null && entryOrder == order)

                Furthermore it looks like you are using only one IOrder object. This is not recommended. You should hold onto the original entry IOrder reference till that trade is completely finished. This means having separate IOrder objects for your exit orders instead of recycling your entryOrder object already. The way you have it coded right now you lose reference to all prior orders and it becomes impossible to monitor anything besides the last submitted order which would make debugging your setup difficult.

                Unfortunately there is no way to get the next order's ID. Should you want to do this I suggest you running your own counter variable to increment.
                Josh P.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by DJ888, Yesterday, 10:57 PM
                0 responses
                6 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by MacDad, 02-25-2024, 11:48 PM
                7 responses
                158 views
                0 likes
                Last Post loganjarosz123  
                Started by Belfortbucks, Yesterday, 09:29 PM
                0 responses
                7 views
                0 likes
                Last Post Belfortbucks  
                Started by zstheorist, Yesterday, 07:52 PM
                0 responses
                7 views
                0 likes
                Last Post zstheorist  
                Started by pmachiraju, 11-01-2023, 04:46 AM
                8 responses
                151 views
                0 likes
                Last Post rehmans
                by rehmans
                 
                Working...
                X