Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need an alternative to OnBarUpdate() when no transaction occurs

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

    Need an alternative to OnBarUpdate() when no transaction occurs

    Hi there,

    I am working on a strategy which has time-limits on its entry orders (I am managing those time limits directly in the strategy, cancelling those orders when the current 1min bar closes at the time limit). When the market doesn't trade for an extended period of time (see NG overnight for example), it is possible that this time limit gets "skipped over", and before I have a chance to cancel it it order is executed past its time limit.

    Is there an alternative to OnBarUpdate(), which can be called on every single 1min interval ? I need this for both backtesting & live trading.

    Thanks in advance
    Dominique

    #2
    dom993,

    You can make a multi-series strategy that would call OnBarUpdate() every minute. Then you just filter by BarsInProgress.

    In Initialize() :

    Add(PeriodType.Minute,1);


    In OnBarUpdate() :

    if ( BarsInProgress == 0 )
    {
    //do stuff with primary series
    }
    else if ( BarsInProgress == 1)
    {
    //do stuff with 1 minute added series
    }
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Hi Adam,

      The strategy is already using the 1min bars as its primary series, and besides when NG doesn't trade overnight for 15min consecutive minutes, there is no 1min bar printed during that time and no call to OnBarUpdate().

      I could use a distinct instrument with more volume (ES comes to mind) as additional timeframe, but even ES sometimes doesn't trade overnight for a couple (or more) minutes in a row.

      I could create a fake instrument and stuff its historical database with 1min bars from 1980 to 2020, that would work for backtesting but I will need a different approach for live trading (which I don't like to start with, as it makes for untested or weakly tested software paths).

      Ideally, I would add a 1min "timer" to the strategy, in the same way I would add an instrument ... is this possible?

      Thanks
      Dominique

      Comment


        #4
        Dom,

        This here is a reference sample on creating custom events.



        You could have some looping method that takes into account time before its termination using DateTime objects (such as DateTime.Now). You can also call this.Update() to force a OnBarUpdate() call.

        OnBarUpdate() is only called by NinjaTrader when a bar closes, or a tick comes in depending on your CalculateOnBarClose setting. Likely you would need to create a custom event that loops and terminates when some time is reached. That can get a little hairy or computationally intensive, and isn't technically supported. It's just possible you could make a work around here.

        I have a situation where I don't want to be executing a particular function too quickly. I currently have this code: DoSomething(); Thread.Sleep(TimeSpan.FromMilliseconds(200)); How can I change ...


        Manipulating datetime : http://www.ninjatrader.com/support/f...ad.php?t=19292

        Another work around would be to possibly add a secondary series to your code that doesn't have this 15 minute gap and then sort of piggy back on it's onbarupdate calls.
        Last edited by NinjaTrader_AdamP; 10-23-2012, 09:13 AM.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by dom993 View Post
          Hi Adam,

          The strategy is already using the 1min bars as its primary series, and besides when NG doesn't trade overnight for 15min consecutive minutes, there is no 1min bar printed during that time and no call to OnBarUpdate().

          I could use a distinct instrument with more volume (ES comes to mind) as additional timeframe, but even ES sometimes doesn't trade overnight for a couple (or more) minutes in a row.

          I could create a fake instrument and stuff its historical database with 1min bars from 1980 to 2020, that would work for backtesting but I will need a different approach for live trading (which I don't like to start with, as it makes for untested or weakly tested software paths).

          Ideally, I would add a 1min "timer" to the strategy, in the same way I would add an instrument ... is this possible?

          Thanks
          Dominique
          Create your own timer, and use it when necessary.

          ref: http://msdn.microsoft.com/en-us/libr...(v=vs.71).aspx

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          646 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          367 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          107 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          569 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          573 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X