Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

How to make orders fill on close of candle on a backtest

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

    How to make orders fill on close of candle on a backtest

    Hello,

    So I know that when using EnterLong() in a backtest, the entry will be made on the open of the next candle. My question is, how can I make it to where the entry is made on the close of the current candle.

    In this strategy, I buy when the Renko bar closes above the brown moving average, but since EnterLong() fills on the open of the next candle, it fills me lower. This is giving me overstating my profit in the back test and overstating my profit. How can I make the backtest fill me on the close of the green candle?


    Attached Files

    #2
    Hello franco12364,

    Thank you for your post.

    The real problem with entering on the close is that you have no real way of knowing whether a certain tick will be the tick that closes the bar since Renko bars are price movement based and not time based. Your best option here would be to run the strategy On Each Tick or On Price Change, which would allow intrabar entry. However, going this route would mean that the strategy would enter the order as soon as it crosses above the moving average, and the bar could easily move in a different direction - however, this would be your best option for this behavior in real time.

    I notice you specifically mentioned backtesting here. Backtesting uses OHLC values from the bar to simulate order fills and actual market movement is not used, so no matter what, in a back test, your entries would always be on the open of the next bar. Also, Renko Bars also change the open of the bar, which cannot be simulated with historical data. Therefore, it is generally not advised to consider Renko backtest results as being reliable. You could use Market Replay data in the Playback Connection and run the strategy there so it is simulated as if it it were running with realtime data, which will show more accurate results and can be used with the calculation methods mentioned above.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      So there is really no way around it?

      I have another strategy that buys as the Renko closes below the moving average, and then sells as it closes above. I understand that in a real market environment it will be different, but for back testing purposes, I want to be filled at the close. As you can see in the picture below, I want to buy at the close of the red bar. By getting filled on the open of the green bar, I am losing 10 ticks on almost every single trade. This is getting me really bad back testing results. Is there a way to change the default fill on the open of the next bar by unlocking the code?
      Attached Files

      Comment


        #4
        Hello franco12364,

        Thank you for your reply.

        Backtesting uses historical data, which does not include intrabar movements. You could unlock your code modify your script to include a single tick data series that you could submit orders to in order to be able to simulate intrabar price movements, but you still don't know which incoming tick will close the bar - there would not be a way to know that a tick was the last tick of the bar until the next tick comes in and unlocking the code will not change that.

        For more accurate testing of Renko strategies you would need to use Market Replay data with the Playback Connection. You can apply the strategy to a chart and let it run just as if real time data was coming in. You can then right click on the chart and select Strategy Perfomance to view the perfomance of the strategy.

        I have provided a publicly available link below to our YouTube video that provides a brief overview of downloading Market Replay data, connecting to the Playback Connection, and replaying data:
        To use the Playback Connection you will first need to have data for Market Replay.

        There are 2 ways of getting market data for Market Replay:
        1. Record live market data To do this you will need to enable the Market Replay Recorder by going to Tools> Options> Market Data > check 'Enable market recording for playback' > OK. Now data will be recorded for any instrument in any NinjaTrader window that is receiving live market data. Level II (market depth) data is only recorded if a Level II, SuperDOM, or FX Pro window is open and receiving data for the instrument. The Market Analyzer window is the recommended recording window as multiple instruments can be added to one Market Analyzer window and all recorded at the same time. Please see the following publicly available help guide page for more information: http://ninjatrader.com/support/helpG...ReplayRecorder
        2. Download replay data from the NinjaTrader server.

          NinjaTrader offers a limited amount of replay data free to download. Only the most common instruments are currently available.

        To download replay data:
        • Select the Tools menu in the Control Center, select the menu item Historical Data and select "Load" tab. Here the section "Get Market Replay data" can be expanded.
        • Select the instrument and date of the desired replay data and press the OK button to begin the download.

        Please see the following publicly available help guide page for more information:



        Notes:
        • Record for market replay must be disabled from the Data tab of the Options menu before downloading replay data.
        • NOT available when connected to the Playback connection.

        To start playback, first disconnect from any other connections. Connect to the Playback Connection by going to Connections > Playback Connection.

        Select the time frame for the Market Replay Data you would like to view. NinjaTrader windows will only display data for instruments and times you have data available.

        You can check what data you have available by right clicking on the Playback Connect controls and selecting 'Available data'. Data will only be displayed within the selected time frame.

        I have provided a publicly available link below to our Help Guide that goes over Market Replay.
        Please let us know if we may be of further assistance.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Franco is right, I want to do the same, on NT7 I've created a Custom FillType, trying to do the same on NT8 with OrderFillResolution but when compile I get an error that I can't modify the sealed NinjaTrader.NinjaScript.OrderFillResolution as I did with NT7 FillType

          is there any way to unlock the "sealed" OrderFillResolution to compile the code in order to fill the order at the current close ?

          Comment


            #6
            Hello fercho,

            Thank you for your reply.

            There would not be a supported way (or unsupported that I'm aware of) to create your own OrderFillResolution type in NinjaTrader 8. You could certainly add a 1 tick series to the strategy and submit your orders to that series as mentioned in my previous post for better accuracy of fill prices. Here's an example from our help guide of adding intrabar granularity to a script:



            Please let us know if we may be of further assistance to you.

            Kate W.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by DJ888, 04-16-2024, 06:09 PM
            5 responses
            14 views
            0 likes
            Last Post NinjaTrader_Erick  
            Started by samish18, Yesterday, 08:31 AM
            4 responses
            14 views
            0 likes
            Last Post elirion
            by elirion
             
            Started by funk10101, Yesterday, 09:43 PM
            1 response
            14 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by TheWhiteDragon, 01-21-2019, 12:44 PM
            5 responses
            551 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by rtwave, 04-12-2024, 09:30 AM
            5 responses
            37 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Working...
            X