Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Proper Order Entry at Bar Close

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

    Proper Order Entry at Bar Close

    Hi,

    I'm sure this question has been addressed but I couldn't seem to find anything helpful searching the forum.

    I am using the following order entry line of code

    entryOrder = EnterLongLimit(NumStocks, Close[0] + 1 * TickSize, entrySignal);

    I have found that the order fills on the next bar regardless of whether or not the price range of the next bar encompasses the limit price. For example if the current bar closes at $10.00 the limit price would be set at $10.01. If the following bar (where the order would be placed) gaps down to $10.00 at open and the bar range never goes higher than $10.00, then the buy order still fills at $10.01 and then closes out at the appropriate profit target. The problem is that during back testing this situation causes an error which results in no trades being entered after the above situation occurs.

    Any advice?

    Thanks in advance

    #2
    Hello AresBowman,

    The scenario you describe is what limit orders will do in a live environment and this is then simulated in a backtest. When you're placing a limit order, you're saying "I'd like to be filled at the specified price or better". If you place a buy limit order above where the market is currently trading, then your limit order is marketable and you will get a fill "better" than the specified price.

    The backtesting engine will use the same principles to determine if there should be a fill. The fill price is then determined by the fill type you have selected. There are two fill types you can use or you can program your own. Reference to these two fill types is included at the bottom of this post.

    The problem is that during back testing this situation causes an error which results in no trades being entered after the above situation occurs.
    I'm not sure why this scenario would cause a problem with subsequent trades. What is the error you're seeing here?


    =======

    Default




    An algorithm that takes a conservative and more realistic approach to filling limit and stop limit orders.
    • Limit orders only fill if the limit price was penetrated
    • Limit orders are always filled at the limit price specified never better (for example, if a limit order is submitted on bar n, NT will check if the order is filled on bar n+1, if this bar gaps down and the limit order was a buy, the order would be filled at the limit price and NOT the high of bar n+1)
    Liberal




    An algorithm that takes a liberal approach to filling limit and stop limit orders.
    • Limit orders fill if the limit price was touched
    • On gap down bars, buy limit orders will fill at the high of the gap down bar
    • On gap up bars, sell limit orders will fill at the low of the gap up bar
    http://www.ninjatrader-support.com/H...AStrategy.html
    Last edited by NinjaTrader_RyanM1; 06-23-2010, 03:39 PM.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Hi, thanks for the quick reply.

      After your comments I was able to determine the true cause of the error while back testing. Here is what is happening

      1) EnterLongLimit is called but the next bar does not penetrate the limit.

      2) Follow on bars, e.g. 10 bars later, which would pierce the limit do not trigger the order.

      3) Follow on bars which would trigger a buy condiiton based on the strategy do not trigger a buy order (Entries per Direction = 1)

      My strategy only enters the "if" statement to EnterLongLimit if entryOrder == null. I also call a OnPositionUpdate() to set entryOrder = null, if MarketPosition.Flat. I'm guessing my problem has something to do with the null condition, but I'm not sure what I'm doing wrong.

      Thanks

      Comment


        #4
        Was able to fix the problem. The solution was to use an order.OrderState == Order.Cancelled condition rather than a position.MarketPosition == Market.Position.Flat to set entryOrder = null.

        Comment


          #5
          Great to hear. Thanks for the update, AresBowman.
          Ryan M.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          633 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          364 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          105 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          567 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          568 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X