Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Order error when the market gaps

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

    Order error when the market gaps

    Hello,

    I am using the following code to trigger a long order

    void Buy()
    {
    double threshold = MAX(this.High, 2)[0];
    barsLong = 0;
    base.EnterLongStopMarket(1, threshold, "VLong");

    }

    I get the following error when the market opens gap up above my threshold value

    "Buy stop or buy stop limit orders can't be placed below the market.." and then my strategy is diabled.

    The funny thing is that once I re-enable the strategy the order is submitted just fine.

    What am I doing wrong? Please advise on what to do.

    kind regards

    Ioannis



    #2
    Hello im197,

    Thanks for your post.

    Submitting the order to the maximum of the last two High values will not guarantee that when the order is submitted it will be above the current market price. You could give the order greater distance from the current market price by submitting your order to a higher price level, I. E. using something like High[0] + 10 * TickSize will submit your stop market order at a higher distance to prevent a market gap from causing a rejection of your order.

    As an alternative, you can set RealtimeErrorHandling to IgnoreAllErrors and you can trap the rejection in OnOrderUpdate. The strategy will stay running in the event of a rejection and you can have the strategy take a different action if you desire.

    RealtimeErrorHandling - https://ninjatrader.com/support/help...orhandling.htm

    OnOrderUpdate - https://ninjatrader.com/support/help...rderupdate.htm

    Please let us know if we can be of further assistance.

    Comment


      #3
      Hello Jim,


      I think the problem is created by the fact that I am using the ES future and regular trading hours for the strategy.

      If the previous two bars are the last bars of the day, the MAX of the previous two highs is only known when the market re-opens. If the market gaps up I am still bound to get an error message. So I have to look up the error handling part.

      I still don't understang why I don't get the same error when I re-enable the strategy. In this case the order is executed without a problem. Why does this happen?

      Kind regards

      Ioannis

      Comment


        #4
        Hello again,

        still trying to figure out how to work around the problem

        I have changed RealtimeErrorHandling and I am trying to trap the rejection, but I cant get it to work. What am I doing wrong?

        here is my code

        ----------------------------------------------

        if (State == State.SetDefaults)
        {
        RealtimeErrorHandling = RealtimeErrorHandling.StopCancelCloseIgnoreRejects ;
        }

        void Buy()
        {
        barsLong = 0;
        longEntryOrder = base.EnterLongStopMarket(1,MAX(this.High, 2)[0], "VLong");
        }

        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 == "VLong" && orderState == OrderState.Rejected)
        EnterLong(1, "New VLong");
        }

        Comment


          #5
          Hello im197,

          I still don't understang why I don't get the same error when I re-enable the strategy. In this case the order is executed without a problem. Why does this happen?
          After re-enabling the strategy and the strategy calculates historical data, it will calculate that trade using historical rules instead of handling the order like it is a live order or realtime simulated order. In the historical context, the rejection will not be seen. For more information on the differences in behavior that you will see between historical and realtime data, please see the documentation below.

          Discrepancies between realtime and backtest - https://ninjatrader.com/support/help...ime_vs_bac.htm

          I have changed RealtimeErrorHandling and I am trying to trap the rejection, but I cant get it to work. What am I doing wrong?
          I have attached an example that can be used to demonstrate rejection handling. You can observe how the behavior is handled by applying the strategy to a 5 second data series.

          For a more comprehensive look at using OnOrderUpdate and Order objects, please refer to the SampleOnOrderUpdate strategy.

          SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

          If you have any questions, please don't hesitate to write back.
          Attached Files

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, 03-13-2026, 05:17 AM
          0 responses
          86 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          151 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          79 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          52 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          59 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X