Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Testing first tick of day bar

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

    Testing first tick of day bar

    To test for the value of the first tick of the day when charting 5 minute bars, I set COBC = false and use:
    ToTime(Time[0]) == 93500 && FirstTickOfBar

    What would I replace the ToTime(Time[0]) function with when using days bars as I don't think that I can use 161500 because the bars are marked with days instead of minutes. If it's some kind of date function that I need, I'd be appreciative if you could also give me the exact format that the date has to be in.

    Thanks,
    Stephen

    #2
    Hello Stephen,

    Thank you for your post.

    If you are working with daily bars, you only need to check FirstTickOfBar since there is only one daily bar in the session. No additional date checking should be needed.

    Give this a try and let me know if I can assist with anything else.
    DexterNinjaTrader Customer Service

    Comment


      #3
      Thanks.
      My strategy executes on day bars and depend on the opening price of the day (which you've shown me how to get). For backtesting, I'm going to need 1 minute bars (I don't have tick data going back far enough). I read the SampleIntrabarBacktest program. I realize that I have to execute buys and sells on the 1 minute bars, but do I have to explicitly reference the one minute bars when I need intraday data, e.g., for FirstTickOfBar when I'm doing my logical tests as they relate to day bars or will the backtesting program just have access to that 1 minute data and use it as appropriate. On the day bars, I just need to test the first tick of the day relative to the previous day's high and low and test prices during the day for their relationship to the previous day's high and low.

      Comment


        #4
        Hello,

        Thank you for your reply.

        Yes, when you need to reference data from the one minute bars, you need to specify such. This can be done in an if(BarsInProgress == 1) section. You could also use BarsArray[1], this is assuming your 1 minute series is the first additional timeframe added. OnBarUpdate is called for each timeframe added, with the primary being index 0.

        Since you are working with current day open high & low and previous day's high and low these two indicators may be of help:

        CurrentdayOHL - http://www.ninjatrader.com/support/h...nt_day_ohl.htm
        PriorDayOHLC - http://www.ninjatrader.com/support/h...r_day_ohlc.htm

        You can access their calculated values so this may save you some effort. You may be able to just use a single timeframe in your code, and use these indicators for the day data.

        Just let me know if I can assist with anything else
        DexterNinjaTrader Customer Service

        Comment


          #5
          Just to check that I understand this. Are these equivalent:

          EnterLong(1, 1, "Long: 1min");

          if (BarsInProgress == 1)
          EnterLong(1,"Long: 1min");

          Comment


            #6
            Hi Stephen,

            They are different, the first one would run on both timeframes added. For example, if you are running on a 5 minute chart, and have added a 1 minute period via Add(), OnBarUpdate will be called for each time frame, on each tick if COBC = false. Checking BarsInProgress will make the logic only apply to a certain timeframe.

            Please let me know if you have any other questions.
            DexterNinjaTrader Customer Service

            Comment


              #7
              I took your advice and went with all 1 minute bars, using PriorDayOHLC. The code uses SetStopLoss in Initialize for stops and ExitLong and ExitShort for target exits. The targets are set for the open of the first profitable day:
              if (ToTime(Time[0]) == 93100 && FirstTickOfBar)

              In the backtest I have ExitOnClose set to false. I don't understand why some profits are being taken during the day as opposed to 0931 and why some of my losses are substantially less than the $750 stop.

              I'm running the strategy on ES. If you could take a look at the attached code, I would be appreciative.
              Attached Files

              Comment


                #8
                Hello Stephen,

                I gave it a try on the ES. With Exit on Close set to false in the backtest options, it never closed on exit except for closing out of the strategy's final position. You may want to double check Exit On close is set correctly in the backtest UI, I do not see it set in the code anywhere.

                The stops were correctly being placed at the $750 loss price as well, so it's probably related to the exit on close setting as well.

                Double check that and let me know if you are still experiencing discrepancies.
                DexterNinjaTrader Customer Service

                Comment


                  #9
                  Dexter, thanks for going the extra mile and running the code.
                  I do have the ExitOnClose set to false in the strategy analyzer. I'm trading ES using the ETH index hours.
                  I've enclosed the Excel sheet of the trades. Several of the trades get closed out with a profit during the day and this should happen only at the first tick after 9:30 (I'm running 1 minute bars). Also, some of the losses get taken at 9:30 and for less than the $750 stop loss.
                  Attached Files

                  Comment


                    #10
                    Hi Stephen,

                    Thank you for that spreadsheet of trades.

                    To compare apples to apples, I'm trying to reproduce on my end using the code attached and am getting completely different results. Attached is a screenshot of the settings I'm using in strategy analyzer and the trades it is taking. 1 minute bars, 4/19/2010 to 4/19/2011, Exit on close false. They all look correct per your specification. Let me know if I'm missing a setting.

                    Also, If you have updated the strategy, please send me the latest copy and I will give that a try.
                    Attached Files
                    DexterNinjaTrader Customer Service

                    Comment


                      #11
                      I just re-ran the Strategy Analyzer again and compared all of the settings; everything is identical except for our hugely different results.
                      It seems to me that the only pssibility is the file. Here is a new zip of it.
                      Thanks.
                      Attached Files

                      Comment


                        #12
                        Thank you for your patience, and thank you for getting that for me. Re-imported and am now getting the same output.

                        First, some key things regarding backtesting. In backtests, no tick data is available, so FirstTickOfBar will not work correctly. Also, because of this, CalculateOnBarClose is always true. Entry and exit signals will be placed on the following bar. The workaround for this is back testing using market replay, it will work on tick conditions correctly.

                        If you enable TraceOrders in your Initialize(), it will output helpful info in the output window (Control Center, Tools > Output Window). Please see the helpguide article on TraceOrder here: http://www.ninjatrader.com/support/h...sub=TraceOrder

                        With this enabled, I am seeing entries like this:

                        4/23/2010 9:31:00 AM Ignored PlaceOrder() method: Action=BuyToCover OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal='' Reason='This was an exit order but no position exists to exit'

                        12/22/2010 9:31:00 AM Ignored PlaceOrder() method at 12/22/2010 9:31:00 AM: Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Short' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'

                        It looks like your logic is losing track of position entry/exits, so I would start there - When using ExitShort() and ExitLong(), you will need pass them the same signal you opened with, such as "Short" and "Long" in your case.

                        Please give this a shot and let me know if it does the trick.
                        DexterNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        668 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        377 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        110 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        575 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        580 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X