Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Backtest Trading at the Closing Price on the Current Bar

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

    Backtest Trading at the Closing Price on the Current Bar

    Attached is a very simple custom fill type that allows market orders to get filled at the current bar's closing price rather than the next bar's opening price. Drop the file into your "Types" source directory (something like C:\Users\UserName\Documents\NinjaTrader 6.5\bin\Custom\Type) and open for editing and recompile any Strategy/Indicator inside NT to install it. Choose the new CurrentBar fill type in the backtesting parameters dialog to activate it. All order types other than market orders proceed using the default fill method. Multi-instrument strategies where you submit orders to bars other than the primary/first bar set are not supported.

    Note: I'm aware of the dangers and the objections from backtesting purists with regards to "peeking in the future" in this way, so I don't want to argue about that specific issue. For anyone who doesn't have a deep understanding of how market movements around bar breaks affect your strategy, I don't recommend you use this fill type. In fact, I only recommend this fill type for backtesting trading on daily or larger bars, where your realtime trades happen immediately before the day's trading session closes, but you don't want the time/storage overhead of backtesting using minute bars. For that type of trading, after hours market fluctuations may make this fill type more accurate than the default fill type. If you trade intraday, using this fill type is probably a very bad idea.
    Attached Files

    #2
    Thank you, Anagoge

    Comment


      #3
      Originally posted by Anagoge View Post
      Attached is a very simple custom fill type that allows market orders to get filled at the current bar's closing price rather than the next bar's opening price. Drop the file into your "Types" source directory (something like C:\Users\UserName\Documents\NinjaTrader 6.5\bin\Custom\Type) and open for editing and recompile any Strategy/Indicator inside NT to install it. Choose the new CurrentBar fill type in the backtesting parameters dialog to activate it. All order types other than market orders proceed using the default fill method. Multi-instrument strategies where you submit orders to bars other than the primary/first bar set are not supported.

      Note: I'm aware of the dangers and the objections from backtesting purists with regards to "peeking in the future" in this way, so I don't want to argue about that specific issue. For anyone who doesn't have a deep understanding of how market movements around bar breaks affect your strategy, I don't recommend you use this fill type. In fact, I only recommend this fill type for backtesting trading on daily or larger bars, where your realtime trades happen immediately before the day's trading session closes, but you don't want the time/storage overhead of backtesting using minute bars. For that type of trading, after hours market fluctuations may make this fill type more accurate than the default fill type. If you trade intraday, using this fill type is probably a very bad idea.
      Thank you!

      Do you have any ideas to do something like this for MaxDrawDown? As it stands, the results are based on the trade open to trade close, subtracted from starting equity. This is completely unacceptable.

      Rather an appropriate use of DrawDown would show the trader the actual risk that was involved in the trade. Could you offer any ideas for how to do this?

      ACTUAL DRAW DOWN = The adverse difference between "Fill Price of trade" and the max drawdown while the trade was open. Or the adverse excursion while the trade was live.

      Clearly, going from when the trade was opened to when the trade was filled does not even begin to give a true picture of the actual risk or and hence the profit factor.

      If you could offer any assistance in this regard, I would appreciate it.

      Comment


        #4
        If you are looking for the maximum adverse extension of each individual trade, NT has a graph for that built-in.

        If you want the maximum unrealized drawdown across all trades, there is some related information here:

        In general, the drawdown calculations are a bit confusing and buggy, but that thread at least attempts to clear things up, and NT 6.5 or 7 might have what you want.

        I've asked here for a feature that graphs the unrealized equity curve and drawdowns, which is a very useful for strategies with trades that are held for several days/months or that trade very volatile instruments. For example, if you are trading leveraged futures, NT could report a profit on a trade where your account in reality was actually wiped out to 0 (or you had a margin call), if you went down to 0 and then went way back up, on an unrealized basis.
        Last edited by Anagoge; 02-26-2009, 12:51 PM.

        Comment


          #5
          Anagoge,

          Thanks. I read that thread first before posting here.

          The graph you refer to in your last trade, that is not for backtesting, correct?

          Josh gave me an idea here:


          Which is a good work around for now. Essentially a method that will trade per trade and just write to a file. I can then at least get the info I need. I will also get unrealized per trade in the process.

          As may as well just write my own backtesting log with my own class, lol.

          Another way I was getting around it was with a Max Stop setting, but that was a lot of trial and error. But it did give me an idea how bad my intraday drawdown was.

          For example. Set Close on Exit to false. Set Max Stop to something crazy like 1000 points. Then work it back to 500 points, then 250, until your values change
          then keep splitting the difference until your stop is no longer changing the results and of course that would give you an "idea" as to how bad your worst draw down was. Then do it with Close on Exit as well for intraday.

          not perfect, but seemed to help. Ideally, getting said per trade would be nice (or an accurate MAE based on real cost).

          Anyway, you echoed my sentiment exactly. The drawdown in its current state is simply not acceptable as a true measurement of a real drawdown that one may have incurred in real trading with a real broker, who will really shut off your account and take your real money, lol.

          Thanks for the post.

          Comment


            #6
            The maximum adverse extension graph is for backtesting and is under Graphs and then MAE in the dropdown. There is some MAE info in the help, if you search for it there. Using stops to get the max is a decent, if time consuming, workaround for the moment. If you get anything working to log the unrealized equity curve and drawdowns, I'd love to see it.

            Comment


              #7
              thx a lot @Anagoge

              Is it possible to change the Fill Time????? Just Date-1 ... . Because the trade ist filled by ClosePrice but the wrong Date.

              Comment


                #8
                Someone asked for more detailed steps to use this, so here they are:
                • Copy the attached "CurrentBarFillType.cs" file in my original post into your existing "Types" source directory (something like C:\Users\<YourUserName>\Documents\NinjaTrader 6.5\bin\Custom\Type in Vista/Windows 7, for example)
                • Open any indicator/strategy and compile it to force a compile of the new file. For example, Tools, Edit NinjaScript, Strategy, SampleMACrossOver, and in the resulting window click the Compile toolbar button. Close the Strategy code window.
                • File, New, Strategy Analyzer, select any instrument/ticker in the left-side tree, hit the "B" (backtest) toolbar button
                • In the resulting Backtest properties dialog, under "Historical Fill Processing", set Fill Type to CurrentBar (see screenshot), fill in the rest of the parameters, and hit OK to begin backtesting.


                Note that you don't need to make any changes to your strategies, indicators, etc. to use this. Just make sure you choose the FillType when desired.
                Attached Files

                Comment


                  #9
                  Thanks

                  Thanks, this works perfectly and fits right in line with the TSP's trading rules.

                  Comment


                    #10
                    Hi Anagoge

                    A great "enhancement" you have provided here, exactly what I was looking for.
                    Unfortunately I found now out, that with this enhancement StopLoss-Commands no more work. Maybe also other limit/stop commans, I have to check it.
                    Did you not encounter this problem too? Or do you use only simple Enter and Exit commands?

                    Thanks for any feedback,
                    Andre

                    Comment


                      #11
                      You are correct - this custom fill type forces orders to be filled at the current bar's closing price. It probably isn't appropriate for any other types of orders that must fill at other prices, such as a stop loss, or intrabar exits using Level 2 data and multiple time frames, etc. You might be able to hack the code to force fills at other prices based on the order type (stop loss, trailing stop, etc.), but I don't know exactly what you would need to change, since I'm only using this for backtesting on daily data and I'm happy to only fill at the closing price.
                      Last edited by Anagoge; 01-06-2010, 02:40 AM.

                      Comment


                        #12
                        Thanks alot Anagoge for this custom filltype; exactly what I was looking for. I've corrected four NinjaTrader 7.0 errors in your original file, which makes it compatbile with NT 7.0. Please see the attachment for this.

                        Originally posted by Hugo123 View Post
                        Is it possible to change the Fill Time? Just Date-1 ... . Because the trade ist filled by ClosePrice but the wrong Date.
                        I have the same "problem", the prices are correct, but the drawing of the errors and lines is consistently off by 1 unit. Does anyone know how to fix this confusing behavior?

                        Edit: I've just noticed that this same problem persists in the "Trades" tab of the Strategy Analyzer, where the time is + 1 unit (in comparison with the chart). So, the chart uses the closing price of 18-8-2010, places the arrows and lines on 19-8-2010, and the "Trades" tab places the entry also at 19-8-2010. Is it possible to override this (apparently) default behavior of NinjaTrader?

                        Any thoughts would be helpful,

                        Regards,
                        Attached Files
                        Last edited by J_o_s; 08-24-2010, 05:48 AM.

                        Comment


                          #13
                          J_o_s, when I looked at NT 6.5, I did not find a way to change the trade date, and made a note of that in my comments in the source file. Unfortunately, since I'm mainly using another trading tool, I don't have the urge to investigate methods for modifying the dates in NT 7, but it might be possible. Someone else here might be able to assist you.

                          Comment


                            #14
                            Originally posted by Anagoge View Post
                            J_o_s, when I looked at NT 6.5, I did not find a way to change the trade date, and made a note of that in my comments in the source file. Unfortunately, since I'm mainly using another trading tool, I don't have the urge to investigate methods for modifying the dates in NT 7, but it might be possible. Someone else here might be able to assist you.
                            Thanks for responding, even though you use another trading tool.

                            I noticed that the class "NinjaTrader.Cbi.Order" includes an "Time" component, which "gets or sets the time of last change". That's the only class I've come across which looks like it might work.

                            However, I can't access the value of the previous bar in the CurrentBarFillType.cs, neither with Time[1] nor with a DateTimeSeries class.

                            Anyone got a hint or tip for me?

                            Regards,

                            Comment


                              #15
                              Any Update

                              Originally posted by J_o_s View Post
                              Thanks for responding, even though you use another trading tool.

                              I noticed that the class "NinjaTrader.Cbi.Order" includes an "Time" component, which "gets or sets the time of last change". That's the only class I've come across which looks like it might work.

                              However, I can't access the value of the previous bar in the CurrentBarFillType.cs, neither with Time[1] nor with a DateTimeSeries class.

                              Anyone got a hint or tip for me?

                              Regards,
                              Sitting here with the same problem and it is soon 2014. Was wondering if anyone found a fix for it without getting intraday data? Would be really great. I.e. how to get the NT to plot and report trades on the bar close instead of the day after.

                              Kruus

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by sightcareclickhere, Today, 01:55 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post sightcareclickhere  
                              Started by Mindset, 05-06-2023, 09:03 PM
                              9 responses
                              258 views
                              0 likes
                              Last Post ender_wiggum  
                              Started by Mizzouman1, Today, 07:35 AM
                              4 responses
                              18 views
                              0 likes
                              Last Post Mizzouman1  
                              Started by philmg, Today, 01:17 PM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_ChristopherJ  
                              Started by cre8able, Today, 01:01 PM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X