Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Solution found for first session bar misfire

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

    Solution found for first session bar misfire

    I have several strategies programmed to evaluate the first bar of the session, then decide whether to execute a trade. The strategies work as designed during backtesting, but in real-time trading, sometimes a trade fires at the opening of the session. What's the deal with that?? This was very frustrating as this event was inconsistent and would only happen about 30% of the time. And of course, it always seemed to happen at the worst time.

    I asked for help on more than one occasion and tried these various solutions that did not work:

    1. Don't trigger a trade if Historical is true. Based on the strategy and various parameters, historical information is loaded to populate indicators, etc. This can sometimes trigger the OnBarUpdate.

    2. Evaluate ToTime(Time[0]) >=093000 to begin trading. Meaning, make sure the timestamp of the bar is greater than you session begin time.

    3. (Bars.BarsSinceSession > X) Where X is the number of bars you want to skip.

    Many of these solutions seemed to work, but ultimately failed. I finally sat down and debugged several sessions where the problem occurred using the session replay, which is a really great tool. I determined that the cause of the issue was fairly simple. For whatever reason, the first bar of the session, was actually the closing bar of the previous session. Armed with this tid bit, you would think the Historical test would weed this bar out, but not so. In all my tests, Historical was false for the last bar of the previous session. Strange, eh? For obvious reasons, the ToTime evaluation didn't work either. The last bar was tagged with a time of 4 pm which is definitely greater than 9:30 am. Counting bars with the BarsSinceSession didn't work because it includes the 4PM bar in the current session so it is always off by one.

    The easy solution is to compare the date of the bar to today's date. Only execute a trade if they are the same.

    if (DateTime.Today.ToShortDateString() != Time[0].ToShortDateString())
    {
    return;
    }


    Remember this solution will work in real-time trading... which is where it's needed. Comment out this block during backtesting for obvious reasons.

    I hope this helps anyone experiencing this very frustrating issue.
    --
    Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

    #2
    Hello Sublime,

    Thanks for sharing your experience and approaches to this problem. Some additional points:

    The close of a bar doesn't take place until the open of the next bar. They are essentially the same event.

    You don't mention trying the function "FirstBarOfSession" which should return true if the bar is the first bar of session. The times used are defined in your chart properties. http://www.ninjatrader-support.com/H...OfSession.html

    The FirstBarOfSession is not the closing bar of the previous session. It's the first bar that occurs after your Session Begin times. It's reset each new calendar day.

    You can use the snippet below to check if the bar is today's bar:

    Code:
     
    if( ToDay(Time[0]) == ToDay(DateTime.Today))
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Yeah, I think I tried FirstBarOfSession as well, but I'll double check. What killed me was the inconsistancy. I couldn't predict when it would happen, so I couldn't program around it. It might have something to do with my data provider? I don't know, but with this logic I can ignore the OnBarUpdate event of a bar from a different session. And I now feel confident my live strategy will perform as intended.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by GwFutures1988, Today, 02:48 PM
      1 response
      5 views
      0 likes
      Last Post NinjaTrader_Clayton  
      Started by ScottWalsh, 04-16-2024, 04:29 PM
      6 responses
      32 views
      0 likes
      Last Post ScottWalsh  
      Started by frankthearm, Today, 09:08 AM
      10 responses
      36 views
      0 likes
      Last Post frankthearm  
      Started by mmenigma, Today, 02:22 PM
      1 response
      4 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by NRITV, Today, 01:15 PM
      2 responses
      10 views
      0 likes
      Last Post NRITV
      by NRITV
       
      Working...
      X