Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Order Entry Not Being Reset to Null

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

    Order Entry Not Being Reset to Null

    Hi,

    I have created a relatively simple strategy that follows a trail stop that comes from an indicator.

    OnBarUpdate() section:

    Code:
     
    protected override void OnBarUpdate()
    {
    switch(Position.MarketPosition)
    {
    case MarketPosition.Flat:
    .... // ENTRY CODE AND CANCEL ORDER CODE
    break;
    case MarketPosition.Short: // === OPEN SHORT POSITION
     
    trailValue = TrailDown.TrailStop[0];
     
    if (High[0] < trailValue)
    {
    if (stopOrder != null && stopOrder.StopPrice > trailValue)
    {
    // ... modify the stop loss to the new value of the trailing stop
    stopOrder = ExitShortStop(0, true, stopOrder.Quantity, trailValue, "ShortStop", "ShortEntry");
    }
    }
    break;
    default:
    break;
    }
    Then OnOrderUpdate() section:

    Code:
     
    protectedoverridevoid OnOrderUpdate(IOrder order)
    {
    if (entryOrder != null && entryOrder.Token == order.Token)
    {
    if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
    entryOrder = null;
    }
    }

    Then OnExecution() section:

    Code:
     
    protectedoverridevoid OnExecution(IExecution execution)
    {
    if (entryOrder != null && entryOrder.Token == execution.Order.Token)
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {
    // INITIAL Stop-Loss
    stopOrder = ExitShortStop(0, true, execution.Order.Filled, trailValue, "ShortStop", "ShortEntry");
     
    // INITIAL Target - 50 ticks away from the entry price
    targetOrder = ExitShortLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 50 * TickSize, "ShortTarget", "ShortEntry");
     
    // Resets the entryOrder object to null after the order has been filled or partially filled
    if (execution.Order.OrderState != OrderState.PartFilled)
    {
    entryOrder = null;
    }
    }
    }
     
    // ... Reset the stop order and target orders' IOrder objects after the position is closed.
    if ((stopOrder != null && stopOrder.Token == execution.Order.Token) || (targetOrder != null && targetOrder.Token == execution.Order.Token))
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
    {
    stopOrder = null;
    targetOrder = null;
    }
    }
    }
    The issue is that the entryOrder object is not being reset to null. Therefore orders are being missed. Also, some positions fail to exit at the trail stop value. I know that my knowledge of how strategies execute is incomplete. Any suggestions/advice would be greatly appreciated. Thank you.

    #2
    Hi Zeos6,

    Thank you for your note.

    If you add a print to where the entryOrder is set to null does the print show?

    If you check that entryOrder is null right after it is set to null does that show as true?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      I have placed more Print statements and I think, I may have mischaracterized the issue. It seems the code is setting the entryOrder null before the end of the bar. I am really not sure what is going on here. I am sending you a pic from the strategy and the printout of the output for the area marked. Thank you for your help.

      Time[0] = 7/28/2014 7:00:00 AM
      fast avg = 1141.175
      TMA = 1141.2
      Here 2
      7/28/2014 7:00:00 AM Entered internal PlaceOrder() method at 7/28/2014 7:00:00 AM: BarsInProgress=0 Action=BuyToCover OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1141.5 SignalName='ShortStop' FromEntrySignal='ShortEntry'
      7/28/2014 7:00:00 AM Amended open order: Action=BuyToCover OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1141.5 SignalName=ShortStop' FromEntrySignal='ShortEntry'
      OnOrderUpdate(): entryOrder 1 =
      OnOrderUpdate(): entryOrder 2 =
      OnOrderUpdate(): entryOrder 1 =
      OnOrderUpdate(): entryOrder 2 =
      OnOrderUpdate(): entryOrder 1 =
      OnOrderUpdate(): entryOrder 2 =
      7/28/2014 7:00:00 AM Cancelled pending exit order, since associated position is closed: Order='NT-00035/Backtest' Name='ShortTarget' State=Working Instrument='TF 09-14' Action=BuyToCover Limit price=1136 Stop price=0 Quantity=1 Strategy='PB_TrailStopStrategy' Type=Limit Tif=Day Oco='' Filled=0 Fill price=0 Token='e115a19738324e6187b5a359b4bcd2ad' Gtd='12/1/2099 12:00:00 AM'
      OnOrderUpdate(): entryOrder 1 =
      OnOrderUpdate(): entryOrder 2 =
      OnOrderUpdate(): entryOrder 1 =
      OnOrderUpdate(): entryOrder 2 =
      OnOrderUpdate(): entryOrder 1 =
      OnOrderUpdate(): entryOrder 2 =
      OnOrderUpdate(): entryOrder 3 =
      =========================

      Strategy Time[0] = 7/28/2014 7:05:00 AM
      ==========
      Time[0] = 7/28/2014 7:05:00 AM
      fast avg = 1141.425
      TMA = 1141.5
      shortTrailExists = True
      longTrailExists = False
      entryOrder =
      Here 1
      7/28/2014 7:05:00 AM Entered internal PlaceOrder() method at 7/28/2014 7:05:00 AM: BarsInProgress=0 Action=SellShort OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1140.9 SignalName='ShortEntry' FromEntrySignal=''
      OnOrderUpdate(): entryOrder 1 = Order='NT-00036/Backtest' Name='ShortEntry' State=PendingSubmit Instrument='TF 09-14' Action=SellShort Limit price=0 Stop price=1140.9 Quantity=1 Strategy='PB_TrailStopStrategy' Type=Stop Tif=Day Oco='' Filled=0 Fill price=0 Token='ca261eef781348ff8b350fc9aaab9f00' Gtd='12/1/2099 12:00:00 AM'
      OnOrderUpdate(): entryOrder 2 = Order='NT-00036/Backtest' Name='ShortEntry' State=PendingSubmit Instrument='TF 09-14' Action=SellShort Limit price=0 Stop price=1140.9 Quantity=1 Strategy='PB_TrailStopStrategy' Type=Stop Tif=Day Oco='' Filled=0 Fill price=0 Token='ca261eef781348ff8b350fc9aaab9f00' Gtd='12/1/2099 12:00:00 AM'
      OnOrderUpdate(): entryOrder 1 = Order='NT-00036/Backtest' Name='ShortEntry' State=Accepted Instrument='TF 09-14' Action=SellShort Limit price=0 Stop price=1140.9 Quantity=1 Strategy='PB_TrailStopStrategy' Type=Stop Tif=Day Oco='' Filled=0 Fill price=0 Token='ca261eef781348ff8b350fc9aaab9f00' Gtd='12/1/2099 12:00:00 AM'
      OnOrderUpdate(): entryOrder 2 = Order='NT-00036/Backtest' Name='ShortEntry' State=Accepted Instrument='TF 09-14' Action=SellShort Limit price=0 Stop price=1140.9 Quantity=1 Strategy='PB_TrailStopStrategy' Type=Stop Tif=Day Oco='' Filled=0 Fill price=0 Token='ca261eef781348ff8b350fc9aaab9f00' Gtd='12/1/2099 12:00:00 AM'
      OnOrderUpdate(): entryOrder 1 = Order='NT-00036/Backtest' Name='ShortEntry' State=Working Instrument='TF 09-14' Action=SellShort Limit price=0 Stop price=1140.9 Quantity=1 Strategy='PB_TrailStopStrategy' Type=Stop Tif=Day Oco='' Filled=0 Fill price=0 Token='ca261eef781348ff8b350fc9aaab9f00' Gtd='12/1/2099 12:00:00 AM'
      OnOrderUpdate(): entryOrder 2 = Order='NT-00036/Backtest' Name='ShortEntry' State=Working Instrument='TF 09-14' Action=SellShort Limit price=0 Stop price=1140.9 Quantity=1 Strategy='PB_TrailStopStrategy' Type=Stop Tif=Day Oco='' Filled=0 Fill price=0 Token='ca261eef781348ff8b350fc9aaab9f00' Gtd='12/1/2099 12:00:00 AM'

      =========================
      Strategy Time[0] = 7/28/2014 7:10:00 AM
      ==========
      Time[0] = 7/28/2014 7:10:00 AM
      fast avg = 1141.85
      TMA = 1141.85
      shortTrailExists = False
      longTrailExists = False
      entryOrder = Order='NT-00036/Backtest' Name='ShortEntry' State=Working Instrument='TF 09-14' Action=SellShort Limit price=0 Stop price=1140.9 Quantity=1 Strategy='PB_TrailStopStrategy' Type=Stop Tif=Day Oco='' Filled=0 Fill price=0 Token='ca261eef781348ff8b350fc9aaab9f00' Gtd='12/1/2099 12:00:00 AM'
      OnOrderUpdate(): entryOrder 1 = Order='NT-00036/Backtest' Name='ShortEntry' State=Filled Instrument='TF 09-14' Action=SellShort Limit price=0 Stop price=1140.9 Quantity=1 Strategy='PB_TrailStopStrategy' Type=Stop Tif=Day Oco='' Filled=1 Fill price=1140.7 Token='ca261eef781348ff8b350fc9aaab9f00' Gtd='12/1/2099 12:00:00 AM'
      OnOrderUpdate(): entryOrder 2 = Order='NT-00036/Backtest' Name='ShortEntry' State=Filled Instrument='TF 09-14' Action=SellShort Limit price=0 Stop price=1140.9 Quantity=1 Strategy='PB_TrailStopStrategy' Type=Stop Tif=Day Oco='' Filled=1 Fill price=1140.7 Token='ca261eef781348ff8b350fc9aaab9f00' Gtd='12/1/2099 12:00:00 AM'
      OnOrderUpdate(): entryOrder 3 = Order='NT-00036/Backtest' Name='ShortEntry' State=Filled Instrument='TF 09-14' Action=SellShort Limit price=0 Stop price=1140.9 Quantity=1 Strategy='PB_TrailStopStrategy' Type=Stop Tif=Day Oco='' Filled=1 Fill price=1140.7 Token='ca261eef781348ff8b350fc9aaab9f00' Gtd='12/1/2099 12:00:00 AM'
      OnOrderUpdate(): entryOrder 4 = Order='NT-00036/Backtest' Name='ShortEntry' State=Filled Instrument='TF 09-14' Action=SellShort Limit price=0 Stop price=1140.9 Quantity=1 Strategy='PB_TrailStopStrategy' Type=Stop Tif=Day Oco='' Filled=1 Fill price=1140.7 Token='ca261eef781348ff8b350fc9aaab9f00' Gtd='12/1/2099 12:00:00 AM'
      7/28/2014 7:10:00 AM Entered internal PlaceOrder() method at 7/28/2014 7:10:00 AM: BarsInProgress=0 Action=BuyToCover OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1141.5 SignalName='ShortStop' FromEntrySignal='ShortEntry'
      **NT** A BuyToCover stop order placed at '7/28/2014 7:10:00 AM' has been ignored since the stop price is less than or equal to the close price of the current bar. This is an invalid order and subsequent orders may also be ignored. Please fix your strategy.
      7/28/2014 7:10:00 AM Ignored PlaceOrder() method at 7/28/2014 7:10:00 AM: Action=BuyToCover OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1141.5 SignalName='ShortStop' FromEntrySignal='ShortEntry' Reason='Invalid order price, please see log tab'
      7/28/2014 7:10:00 AM Entered internal PlaceOrder() method at 7/28/2014 7:10:00 AM: BarsInProgress=0 Action=BuyToCover OrderType=Limit Quantity=1 LimitPrice=1135.7 StopPrice=0 SignalName='ShortTarget' FromEntrySignal='ShortEntry'
      OnOrderUpdate(): entryOrder 5 =
      OnOrderUpdate(): entryOrder 1 =
      OnOrderUpdate(): entryOrder 2 =
      OnOrderUpdate(): entryOrder 1 =
      OnOrderUpdate(): entryOrder 2 =
      OnOrderUpdate(): entryOrder 1 =
      OnOrderUpdate(): entryOrder 2 =
      ==========
      Time[0] = 7/28/2014 7:15:00 AM
      fast avg = 1141.65
      TMA = 1141.3
      ==========

      7:05 is the end of the trail. The order exited at 7:00 before breaking the trail. This should not happen with a ExitShortStop() order.
      Attached Files
      Last edited by Zeos6; 08-12-2014, 03:09 PM. Reason: Added a pic

      Comment


        #4
        Hi Chelsea,

        I think I found the issue but I am not sure why it is happening. The print outs show that my position closed at 7:00 even though the chart shows it closed at 7:05 but the code says I am flat at 7:05.

        ==========
        Time[0] = 7/28/2014 7:00:00 AM
        fast avg = 1141.175
        TMA = 1141.2
        Here 2
        7/28/2014 7:00:00 AM Entered internal PlaceOrder() method at 7/28/2014 7:00:00 AM: BarsInProgress=0 Action=BuyToCover OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1141.5 SignalName='ShortStop' FromEntrySignal='ShortEntry'
        7/28/2014 7:00:00 AM Amended open order: Action=BuyToCover OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1141.5 SignalName=ShortStop' FromEntrySignal='ShortEntry'
        OnOrderUpdate(): entryOrder 1 =
        OnOrderUpdate(): entryOrder 2 =
        OnOrderUpdate(): entryOrder 1 =
        OnOrderUpdate(): entryOrder 2 =
        OnOrderUpdate(): entryOrder 1 =
        OnOrderUpdate(): entryOrder 2 =
        7/28/2014 7:00:00 AM Cancelled pending exit order, since associated position is closed: Order='NT-00035/Backtest' Name='ShortTarget' State=Working Instrument='TF 09-14' Action=BuyToCover Limit price=1136 Stop price=0 Quantity=1 Strategy='PB_TrailStopStrategy' Type=Limit Tif=Day Oco='' Filled=0 Fill price=0 Token='faa4b15a820d438488572e5f26b41103' Gtd='12/1/2099 12:00:00 AM'
        OnOrderUpdate(): entryOrder 1 =
        OnOrderUpdate(): entryOrder 2 =
        OnOrderUpdate(): entryOrder 1 =
        OnOrderUpdate(): entryOrder 2 =
        OnOrderUpdate(): entryOrder 1 =
        OnOrderUpdate(): entryOrder 2 =
        OnOrderUpdate(): entryOrder 3 =
        =========================
        Strategy Time[0] = 7/28/2014 7:05:00 AM
        Position.MarketPosition = Flat
        ==========

        Can you please explain the sequence that occurs on order entry and exit? In others words, what gets executed when and in which order. I may have a sequencing issue here. Thank you.

        Comment


          #5
          Hello Zeos6,

          OnOrderUpdate does not happen at the close of a bar. This is asynchronous and runs when an order changes state.

          It is expected that the condition would be triggered before the bar closes as this happens asynchronously.

          7/28/2014 7:10:00 AM Entered internal PlaceOrder() method at 7/28/2014 7:10:00 AM: BarsInProgress=0 Action=BuyToCover OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1141.5 SignalName='ShortStop' FromEntrySignal='ShortEntry'

          This shows the short stop was placed at 7:10. It can fill at any time after being placed. It will not fill exactly when a bar closes.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Hi Chelsea,

            That is an incorrect buy that should not have occurred because there is no trail present. This only happened because I the actual buy was somehow triggered at 7:05 and as I give it 3 bars to enter before I cancel the order, the order was triggered at 7:10. The question is, how can I have triggered that order when I was still in position at 7:05? It seems that I exited an order and placed an order on the same bar at 7:05. In fact, I place a plum covered dot on the chart every time I place an order. If you look at the 7:05 bar of my pic you will see the order exit as well as the order entry plum colored dot on the same bar.

            In addition, the results show the position was closed at 7:00 - ahead of time.

            =========================
            Strategy Time[0] = 7/28/2014 7:00:00 AM
            Position.MarketPosition = Short
            ==========
            Time[0] = 7/28/2014 7:00:00 AM
            fast avg = 1141.175
            TMA = 1141.2
            Here 2
            7/28/2014 7:00:00 AM Entered internal PlaceOrder() method at 7/28/2014 7:00:00 AM: BarsInProgress=0 Action=BuyToCover OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1141.5 SignalName='ShortStop' FromEntrySignal='ShortEntry'
            7/28/2014 7:00:00 AM Amended open order: Action=BuyToCover OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=1141.5 SignalName=ShortStop' FromEntrySignal='ShortEntry'
            OnOrderUpdate(): entryOrder 1 =
            OnOrderUpdate(): entryOrder 2 =
            OnOrderUpdate(): entryOrder 1 =
            OnOrderUpdate(): entryOrder 2 =
            OnOrderUpdate(): entryOrder 1 =
            OnOrderUpdate(): entryOrder 2 =
            7/28/2014 7:00:00 AM Cancelled pending exit order, since associated position is closed: Order='NT-00035/Backtest' Name='ShortTarget' State=Working Instrument='TF 09-14' Action=BuyToCover Limit price=1136 Stop price=0 Quantity=1 Strategy='PB_TrailStopStrategy' Type=Limit Tif=Day Oco='' Filled=0 Fill price=0 Token='faa4b15a820d438488572e5f26b41103' Gtd='12/1/2099 12:00:00 AM'
            OnOrderUpdate(): entryOrder 1 =
            OnOrderUpdate(): entryOrder 2 =
            OnOrderUpdate(): entryOrder 1 =
            OnOrderUpdate(): entryOrder 2 =
            OnOrderUpdate(): entryOrder 1 =
            OnOrderUpdate(): entryOrder 2 =
            OnOrderUpdate(): entryOrder 3 =
            =========================
            Last edited by Zeos6; 08-12-2014, 04:16 PM. Reason: Extra Info Added

            Comment


              #7
              Hi Zeos6,

              Please clarify how this strategy works.

              What actions should have happened, what code should cause that to happen.

              What actions actually happened, how are you tracking this?

              In the output of your last post I do not see any entry order.

              In OnOrderUpdate what code is that print that has "OnOrderUpdate(): entryOrder 1 = ".
              Isn't this showing that there is no order?
              Can you add a time stamp to this?
              If this is to show that entryOrder is null, can you add a condition that shows us this is null.
              Something like:
              if (entryOrder == null)
              Print(Time[0]+" - entryOrder == null");
              else
              Print(Time[0]+" - entryOrder: "+entryOrder.ToString());

              At this point I am not clear what you are trying to do, what should be happening, or what is going wrong.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Hi ChelseaB,

                Thank you for your reply. I am simply trying to learn the proper way to set up a strategy. I have gone through the various examples and none quite cover what I wish to do. I apologize for my lack of understanding but it would help a lot if I understood what gets executed when, and in what sequence, especially the OnMarketUpdate, OnExecuttion, OnOrderUpdate and OnPositionUpdate.

                As this is a test strategy, it is extremely simple. When my trailing stop triggers, place a sell on stop 5 ticks below the low of the bar on which the trail stop triggered. If the short position is not entered in 3 bars, cancel the order. Then, trail the position using the trail stop values calculated on each subsequent bar. When the High of the bar hits or exceeds the trailing stop, exit the position with a stop or limit order. Ideally I would also like to set an initial stop loss and a target for the position as well. Seems simple yet I am getting weird results.

                Comment


                  #9
                  Hi Zeos6,

                  Can we have an export of your script?
                  Please attach this to your next post.
                  (Let me know if you want to keep this private and I will have you email it in)

                  To export your script do the following:
                  1. Click File -> Utilities -> Export NinjaScript
                  2. Enter a unique name for the file in the value for 'File name:'
                  3. Select the strategy from the objects list on the left -> click the right facing arrow ">" to add the strategy to the export
                  4. Click the 'Export' button -> click 'yes' to add any referenced indicators to the export -> click OK to clear the export location message


                  By default your exported file will be in the following location:
                  • (My) Documents/NinjaTrader 7/bin/Custom/ExportNinjaScript/<export_file_name.zip>


                  Below is a link to the help guide on Exporting NinjaScripts.
                  http://www.ninjatrader.com/support/h...nt7/export.htm
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi Chelsea,

                    I would prefer to keep it private. Please let me know how to email it in. Thank you.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    648 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    369 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    109 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    573 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    575 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X