Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to make backtested orders fill at the next traded price on gaps?

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

    How to make backtested orders fill at the next traded price on gaps?

    Hello,

    I am backtesting an overnight strategy that needs to be able to enter the market in the opposite of the market direction when the market moves better than a certain price level, say going short when the market moves 1% up from its Close price overnight. I am trying to get my strategy to enter the position on the first 1 Minute Bar the next day, somewhere within the price range of that first 1 Minute Bar.

    I am happy to use Limit Orders, Stop Orders or Market Orders to achieve this while backtesting, but all three seem to have their problems in Ninja Trader for the situation described above, for instance:

    1. Short Stop Orders can't be placed above the market in NT. They are ignored:
    When I try to place a Short Stop Order 1% above the market, the order is ignored by Ninja Trader. With TraceOrders=true, the Output Window says that Ninja Trader ignored the Short Stop order because you can't place a Short Stop Order above the market, only below the market, i.e. Ninja Trader can only use Stop Orders to enter in the direction of the market.

    2. Limit Orders produce fills that are in the middle of gap opens, where no trading occurred:
    When I use a Limit Order to go short 1% above the market and there is a gap open up 3%, Ninja Trader is giving me the Short position at where I placed my Limit Order, whereas what I really wanted to do was go short on the first 1 Minute Bar that breached my price level, which is the bar that opened up 3% the next morning.

    3. Market Orders placed on the opening 8:30AM-8:31AM bar always get executed at the Close Price of the SECOND bar, the 8:31AM-8:32AM bar.

    How can I get my backtested strategy to enter a short position in a rising market within the price range of the opening 1 minute bar, EVEN on a gap open, when 1) Stops are ignored, 2) Limits give fills in the middle of gaps, and 3) markets entered on the 8:30-8:31 bar get filled at the closing price of the SECOND 1 minute bar?

    Thank you VERY much for any help in solving this problem.

    I feel like I am stuck in a corner and have no options for something that seems like it should be simple.

    ChiTrader2000

    #2
    1. If you submit EnterShortStop() you need to have a price above the market. If you do EnterLongStop() you need to have a price below the market. You need to use TraceOrders = true to evaluate the true reason your order is rejected.

    2. Then you need to use a stop order, not a limit order. Limit orders try to guarantee you price.

    3. Of course, especially if you are backtesting. When you have CalculateOnBarClose = true, you get your signals on a bar (8:30-31). To get this signal you already have the close price of that bar therefore you cannot possibly trade on that bar. Any trade send occurs at the open of the next bar.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Looking for a workaround to stated problem. Could I use a Custom FillType?

      Hello,

      NinjaTrader_Josh, thanks for the response that confirmed all of my current understanding about the fill behavior of Market, Stop, and Limit Orders.

      My problem is that I still need a way to do what I posted about, and all three types of orders can't do what I was asking about, for the reasons that I stated and that you confirmed.

      So, how can I get my backtested strategy to enter a short position in a rising market within the price range of the opening 1 minute bar, EVEN on a gap open, when 1) Stops are ignored, 2) Limits give fills in the middle of gaps, and 3) markets entered on the 8:30-8:31 bar get filled at the closing price of the SECOND 1 minute bar?

      Should I write a Custom Fill Type do achieve this?

      If so, I would happy to do this by copying the @LiberalFillType and making my own fill type, but I can't seem to get any intellisense on the FillType class, so I don't know what properties are available to me.

      Any ideas? I will be happy to code a workaround. I just need to know if it is possible to solve my problem with NinjaTrader.

      Thank you VERY much for any help on this. I am trying to show my boss how good Ninja Trader is for our needs, and this problem is currently a deal breaker for him.

      ChiTrader2000

      Comment


        #4
        ChiTrader2000,

        I don't understand why you want this stop order. It is an illegal order at the exchange and would never work in real-life. Just submit a market order on the bar before the open and then it'll go through to grab you the open price of that bar. By the way, this is how ALL backtesting orders works. You have a signal bar, but the order is not available till the next bar.

        You can try your own fill type, but we cannot offer you any support in that regards. I suggest you just tweak the limit fill algorithm and go from there.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Custom FillType is working! Where is base class doc, since no intellisense.

          NinjaTrader_Josh,

          Thanks a lot for the Custom Fill Type tweaking suggestion. I did this and I am already almost able to get the fill behavior that my initial thread was inquiring about.

          Where can I find all the protected & public properties of the "FillType" class so that I can see all my options for making a perfectly tweaked "MyCustomFillType" that inherits from FillType? (I can't get intellisense in VS .NET 2005 to work, and I can't edit my "MyCustomFillType" class in NT, which probably would have intellisense if I could edit it there.)

          Thanks! I am almost there!

          ChiTrader2000

          Comment


            #6
            Unfortunately I cannot guide you any further on this.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              NT Future Enhancement Suggestion: Provide base class documention for developers.

              NinjaTrader_Josh,

              No problem. I understand that the NT staff doesn't have tons of time to give every C# developer doing advanced projects lots of special attention, so could you guys at least provide base class documentation for classes such as FillType?

              Thanks for considering this idea for a future enhancement to an already great product.

              ChiTrader2000

              Comment


                #8
                ChiTrader2000,

                Thank you for the suggestion. I will add it to our list.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by ChiTrader2000 View Post
                  Where can I find all the protected & public properties of the "FillType" class so that I can see all my options for making a perfectly tweaked "MyCustomFillType" that inherits from FillType? (I can't get intellisense in VS .NET 2005 to work, and I can't edit my "MyCustomFillType" class in NT, which probably would have intellisense if I could edit it there.)
                  You can open custom .cs files from the Types directory in NinjaTrader, in a round about way. Edit the file in any text editor and introduce a syntax error. Then try to compile any indicator or strategy in NT. It will show the syntax error in the error list, and then you can double-click the error and NT opens the .cs file. In general, the intellisense then works. The base fill type class looks like this:

                  Code:
                  public abstract class FillType
                  {
                      // Fields
                      private StrategyBase strategy;
                  
                      // Methods
                      protected FillType();
                      public abstract void Fill(Order order);
                  
                      // Properties
                      public double FillPrice { get; set; }
                      protected double NextClose { get; }
                      protected double NextHigh { get; }
                      protected double NextLow { get; }
                      protected double NextOpen { get; }
                      public double SlippagePoints { get; }
                      public StrategyBase Strategy { get; set; }
                  }

                  Comment


                    #10
                    Thanks a lot! Causing a syntax error then opening in NT Script Editor works great.

                    Anagoge,

                    Thanks a lot! It works great. I can now see what properties are available to me using the Intellisense built into the NT Script Editor.

                    By the way, using your intelliesense instructions and a couple of lines of code, I was in fact able to make my own "MyCustomFillType" to do exactly what I was asking about in my initial post. Basically, my entries use my custom "Limit Orders" that receive fills more like Stop Orders, in that they never get filled in the middle of a gap. Also, they do not get 1 bar delayed fills like market orders. As I was saying in my initial post, NT (and probably all backtesting software) cancels stops that are on the "better priced" side of the market, which is why I had to set limit orders in NT, but make my Limit Orders behave like stop orders.

                    Thanks again,

                    ChiTrader2000

                    Comment


                      #11
                      Great! It might be helpful to others if you post the code for your custom fill class, in case other users are looking for examples of this more advanced technique.

                      Comment


                        #12
                        Code example for overriding FillType to make your own interesting custom Fill Type

                        Anagoge,

                        Good suggestion. Here is how I did it...

                        First, I checked out page 530 in the NT Version 6 PDF manual. It is entitled "Custom Fill and Optimizer Types", subtitled "Fill Types" "Custom Fill Algorithms".

                        Here is what it says...

                        Fill Types

                        Custom Fill Algorithms

                        In addition to the two system fill types (Default and Liberal) you can also script a custom fill algorithms using any code/text editor. Copy the file "My Documents\<NinjaTrader Folder>\bin\Custom\Type\@LiberalFillType.cs"
                        and provide a name new exluding the "@" sign such as "My Documents\<NinjaTraderFolder>\bin\Custom\Type\MyFi llType.cs".


                        Then within your code editor:
                        1. Provide a new display name for example rename "Liberal" to "MyFill"
                        2. Provide a new class name for example rename "LiberalFillType" to "MyFillType"
                        3. Code your fill algorithm within the Fill() method
                        4. Once complete, open any indicator or strategy within the NinjaTrader's NinjaScript Editor and compile. This will compile the new fill algorithm and make it available within any backtest dialog window.

                        I followed these instructions (I copied and pasted the @LiberalFillType.cs file as the basis of my custom fill type), then I changed the overridden Fill() method's handling of Limit Orders.

                        I made my code look like this, changing only the "Liberal" Limit Order behavior of entries, not exits...


                        elseif (order.OrderType == OrderType.Limit)
                        {

                        double nextLow = NextLow;
                        double nextHigh = NextHigh;
                        double nextOpen = NextOpen;


                        double nextClose = NextClose;
                        if ((order.Action == Action.Buy && order.LimitPrice >= nextLow - Strategy.TickSize / 2)
                        || (order.Action == Action.SellShort && order.LimitPrice <= nextHigh + Strategy.TickSize /
                        2))
                        {
                        FillPrice = nextOpen;

                        }
                        elseif ((order.Action == Action.BuyToCover && order.LimitPrice >= nextLow - Strategy.TickSize / 2)
                        || (order.Action == Action.Sell && order.LimitPrice <= nextHigh + Strategy.TickSize /
                        2))
                        {
                        FillPrice = (order.Action == Action.BuyToCover) ? Math.Min(order.LimitPrice, nextHigh) : Math.Max(order.LimitPrice, nextLow);

                        }
                        }

                        Then I built my Ninja Trader Code in the NT Code Editor, went to go backtest a strategy, and the backtest properties Dialog Box gave me a choice of not only "Default" and "Liberal", but also "MyCustomFillType".

                        Everything worked great. I will still probably build on my code above, but this is how its done in general. Notice how on my Limit Order Entries, this Fill Algorithm won't fill me in the middle of a gap, where no trading occurred, but I left the fill algorithm untouched for Limit Order exits, which I need to function more realistically.

                        Hope this helps!

                        ChiTrader2000

                        Comment


                          #13
                          Hi ChiTrader2000, thanks for the detailed post, great use of the fill type customization...

                          Comment


                            #14
                            Last

                            I know it’s late in this thread! Wouldn’t a GTC Good-til-Canceled have the determent factor here? This is the SEC order book that has to be maintained, they just don't knock you out of it, excpecially on the GTC. only the market can.
                            It's not so much that you're in money or out. So, you’re going against the markets current trend, “ forecasting ” laying in-wait, waiting for it to come to you, so you have enough bit, depth, run in your trade. Counter trend treading is still a legitimate tactic?

                            You’re not dictating a market or sticking the Ask or Bid creating a market away from the market maker. Which your broker most likely, would receive a call on.

                            That’s my 2 cents, all I wanted to know, weiging in was on the GTC? Does it in fact have an override effect on the orders in NT?

                            Thanks

                            Comment


                              #15
                              OK, that's me after trying to find any reference to Last[0], you know...like Close[0] just not the close of the bar, specificaly current trade (price) between Open[0] and Close[0]. I find this amuzing that Ninja does not have Last[0] built in (TradeStation has just that).

                              Is it possible to simulate Last[0] for use with Fill method?
                              Or perhaps Input[0] does the same?
                              I am not even sure how Close[0] works. Can I access intrabar price via Input[0] or Close[0]?

                              I want to trigger Fill order intrabar on the first 30 minute bar(prime) with exucution on 5 min Bar(secondary)

                              So to refrase as soon as(intrabar)entry condition is true on primary bar I need execution on secondary bar. I have read all prior posts .
                              Any new sugestions?

                              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
                              108 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              572 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              573 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X