• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to open a trade at both the open and close using daily OHLC historical data

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

    How to open a trade at both the open and close using daily OHLC historical data

    I am studying overnight gaps in the major indices. I can do this pretty easily in Excel by taken the close of the previous day minus the open of the next day. I want to do some more in depth research and get the trading metrics through NinjaTrader. My understanding was that I would need to make a second set of artificial OHLC data for the overnight hours that way I could enter the trade at the close of the day session, and then close the trade at the close of the overnight session (the artificial data).

    I actually did just that. I went ahead and made a second set of OHLC bars with proper timestamps and OHLC data and imported it. It seemed to import just fine but when I do an actual backtest it will use the overnight bars only for a couple months then flip to the day bars for a while, then flip back again, back and forth. It never shows both the daytime and overnight bars for each day. I am assuming that the issue is because the overnight OHLC data is more time than the daytime OHLC. Does that sounds true? I don't know if NT needs all OHLC records to all be of equal length.

    Am I approaching this the right way? Is there a better way? If there isn't a better way how can I massage my data so that I can trade both the open and close price of my daily OHLC data.
    Last edited by krugman25; 04-16-2019, 01:10 PM.

    #2
    Hello krugman25,

    Thanks for your post.

    So I understand correctly, you are trying to import your own data to have NinjaTrader use those values for order fill simulations. NinjaTrader 8's order fill simulation behavior is built in and closed source so there is little to control over how these order fills occur. You can read more about how NinjaTrader simulates order fills from OHLC data points below.

    Understanding historical processing - https://ninjatrader.com/support/help...ical_fill_.htm

    If you are trying to circumvent this behavior to simulate order fills at prices you specify, you could consider making a single tick data series from your own imported historical data that includes the price levels that you wish have the orders filled at. Having your own values specified in the single tick data series will use those exact price levels for order fills as opposed to using the virtual bar behavior seen to simulate order fills.

    NinjaTrader 8 was not designed to have an open source fill engine, and customizing this fill behavior would be limited to what you can specify in the single tick data series that you submit the orders against. As this is outside of the design of the platform, our input will be limited to pointing you in the right direction to accomplish this task.

    An example for having orders submitted to a single tick data series can be found below.

    Backtesting with intrabar granularity example - https://ninjatrader.com/support/help...ipt_strate.htm

    Please let me know if this information does not help solve your inquiry, and if it does not, please clarify on what you are trying to accomplish so we can offer clearer direction.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thank you for the quick response.

      In a nutshell, I have a set of historical daily OHCL data. The way Nintjatrader works is that I can only get a fill at the close of that daily bar. But since I have the candle open price I want to be able to get a fill at that open price and also get a fill at that close price. My workaround involved creating a second set of artificial OHLC data where the close of the previous day was the open of the overnight session. I then timestamped the overnight data to be market open and the daytime data timestamped for market close. That way I could get a fill at the open based on open price and a fill at the close on the close price. In theory that should work, but ninjatrader wasn't properly handling my historical OHCL data. I gave it an honest try at least.

      Now I think you may have answered my question. I was also considering creating a tick series with 1 tick being the open price at market open and one tick being the close price at market close. Although I wanted to see if anyone here would mention it before I spend more time on something that doesn't work. With your comment it does sound like that might get me what I need, which is the ability to fill orders at both open and close each day.

      I will give that a shot and if I have any problem let you know.
      Last edited by krugman25; 04-16-2019, 04:03 PM.

      Comment


        #4
        NinjaTrader_Jim

        Here is an update. I converted the historical file into tick series data, imported the data, and ran the backtest at 1 tick resolution, and it is only showing me the 1st tick for each day but not the second.

        Below is a sample of just a few of the rows in the tick data file for "SPY.csv"

        19930129 14:30:00.0000000;43.9687;0
        19930129 21:00:00.0000000;43.9375;1003200
        19930201 14:30:00.0000000;44.2187;0
        19930201 21:00:00.0000000;44.25;480500
        19930202 14:30:00.0000000;44.4062;0
        19930202 21:00:00.0000000;44.3437;201300
        19930203 14:30:00.0000000;44.9687;0
        19930203 21:00:00.0000000;44.8125;529400
        Next is the "Print()" of each bar timestamp going through "OnBarUpdate()" (as a side note, the data is in UTC and the output is in my local time)

        02/01/1993 08:30
        02/02/1993 08:30
        02/03/1993 08:30
        02/04/1993 08:30
        02/05/1993 08:30
        Below is the code for my OnBarUpdate()

        Code:
                protected override void OnBarUpdate()
                {
        
                    Print(Time[0].ToString("MM/dd/yyyy HH:mm"));
                 }
        Attached are screenshots for the Print() output, ticks in the history and back test settings.

        All of this shows that the historical file that I created has imported correctly. In my back test settings I am selecting 1 tick resolution. Yet I am only getting the 14:30 tick for each day and never the 21:00 tick.

        Click image for larger version  Name:	timestamps.png Views:	1 Size:	38.6 KB ID:	1054624Click image for larger version  Name:	ticks.png Views:	1 Size:	66.2 KB ID:	1054625Click image for larger version  Name:	settings.png Views:	1 Size:	281.4 KB ID:	1054626
        Last edited by krugman25; 04-16-2019, 05:43 PM.

        Comment


          #5
          Any thoughts on why NT isn't handing my tick data correctly? It looks correct in the historian.

          Comment


            #6
            Hello krugman25,

            Thanks for your posts.

            Time[0] will represent the timestamp of the bar. I do not have enough detail regarding any time filters in your strategy which may be affecting which ticks OnBarUpdate() processes. The Trading Hours template will also effect which data iterates through OnBarUpdate().

            I really do not want to get into specifics with massaging the data as this is definitely circumventing our order fill behaviors, but I may offer some further insight below.

            1. Create a duplicate instrument for SPY
            2. Download single tick data for SPY and export it
            3. Modify the data so when it is imported, it is assigned to the new SPY instrument and price values of the ticks are modified so any after hours tick represents the next open.
            4. In your strategy, add a single tick data series referencing the modified SPY single tick data series. Submit your orders to that data series. (Model SampleIntrabarBacktest)
            5. When enabling the strategy, test to see which tick is filling the orders, and adjust accordingly.

            As this is a big workaround to introducing your own fill behaviors outside of our design spec, this enters unsupported territory. I will leave this thread open for any community members that have attempted something similar and can offer further insight.

            I have also submitted a vote on your behalf for our feature request to open up the fill engine so it can be customized to your liking. The ticket ID is SFT-1137. As with other feature requests, we track interest in votes before deciding that a feature should be implemented. For that reason, we cannot offer an ETA or promise of fulfillment. Your impact is being tracked, however.

            If there are any other NinjaScript or NinjaTrader related items you would like assistance on, please do not hesitate to open a new thread.
            JimNinjaTrader Customer Service

            Comment


              #7
              It was exactly the Trading Hours Template. I knew the tick file that I create was fine. It had proper formatting, time stamping, price and volume, so there should have been nothing wrong there. It isn't any different than a "real" historical file, except that it has a lot less ticks than something you would download using TickStory or elsewhere.

              When you mentioned trading hours that really clicked. I have almost solely used NT backtesting for Forex and have never messed with trading hours. As a matter of fact I didn't really know it existed. I created a custom 24 hour trading hour template and applied it to SPY. I plan on doing this same type of testing on other indices so I will do these same steps to make it work. Thanks!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by marwanco, Today, 10:54 AM
              1 response
              3 views
              0 likes
              Last Post NinjaTrader_ChrisL  
              Started by daqu40, Today, 11:01 AM
              0 responses
              0 views
              0 likes
              Last Post daqu40
              by daqu40
               
              Started by n8dogg59, Today, 09:55 AM
              2 responses
              3 views
              0 likes
              Last Post n8dogg59  
              Started by ren37, Today, 09:42 AM
              2 responses
              4 views
              0 likes
              Last Post ren37
              by ren37
               
              Started by Austiner87, Today, 08:16 AM
              1 response
              9 views
              0 likes
              Last Post NinjaTrader_ChrisL  
              Working...
              X