Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error in BarsRequest Example

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

    Error in BarsRequest Example

    There appears to be an error in the example below:

    Original
    Code:
    // Unsubscribe to any old bars requests
    if (barsRequest != null)
         barsRequest = new BarsRequest(Cbi.Instrument.GetInstrument("AAPL"),
         DateTime.Now.AddDays(-daysBack), DateTime.Now);
    I think it was meant to be:
    Code:
    // Unsubscribe to any old bars requests
    if (barsRequest != null)
         barsRequest.Update -= OnBarUpdate;
    
    barsRequest = new BarsRequest(Cbi.Instrument.GetInstrument("AAPL"),
         DateTime.Now.AddDays(-daysBack), DateTime.Now);
    http://ninjatrader.com/support/helpG...arsrequest.htm

    #2
    I believe I see your reasoning. Please correct me if I'm wrong?

    * You saw a snippet of code in which a barsRequest was already being checked to see if it persisted in memory
    * You saw a place later where it was assigned a handler
    * You wanted to make sure that, if barsRequest persisted in memory, that the old handler was invalidated

    If I'm correct about that, the problem is that

    Code:
    if (barsRequest != null)
         barsRequest.Update -= OnBarUpdate;
    
    barsRequest = new BarsRequest(Cbi.Instrument.GetInstrument("AAPL"),
         DateTime.Now.AddDays(-daysBack), DateTime.Now);
    does remove the handler from an existing barsRequest object - but then the object the handler was removed from is destroyed anyway, and replaced by a new barsRequest object.

    Given this, and the original code, I believe this plan will preserve the original author's intent while also addressing your concern :

    * If there is an existing bars request, do nothing (so that a new handler does not get added)
    * Otherwise, create and initialize a new barsRequest object

    I believe the following code matches this plan :

    Code:
    if (barsRequest == null)
    {
         barsRequest = new BarsRequest(Cbi.Instrument.GetInstrument("AAPL"), DateTime.Now.AddDays(-daysBack), DateTime.Now);
     
          // Parameterize your request. We determine the interval via the selection from our interval selector.
          barsRequest.BarsPeriod  = new BarsPeriod { BarsPeriodType = BarsPeriodType.Minute, Value = 1 };
          barsRequest.TradingHours = TradingHours.Get("Default 24 x 7");
     
          // Attach event handler for real-time events if you want to process real-time data
          barsRequest.Update      += OnBarUpdate;
    }
    In any event we will be reviewing this code. Thanks for bringing it to our attention.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Hi Jessica,
      The way it is written now the BarsRequest never get created because it is preceded by "if (barRequest !=null)". It is always null until it is instantiated.

      Comment


        #4
        I agree, thanks again for finding this and bringing it to our attention.
        Last edited by NinjaTrader_PatrickH; 12-24-2015, 09:49 AM.
        Jessica P.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Brevo, Today, 01:45 AM
        0 responses
        3 views
        0 likes
        Last Post Brevo
        by Brevo
         
        Started by aussugardefender, Today, 01:07 AM
        0 responses
        3 views
        0 likes
        Last Post aussugardefender  
        Started by pvincent, 06-23-2022, 12:53 PM
        14 responses
        239 views
        0 likes
        Last Post Nyman
        by Nyman
         
        Started by TraderG23, 12-08-2023, 07:56 AM
        9 responses
        384 views
        1 like
        Last Post Gavini
        by Gavini
         
        Started by oviejo, Today, 12:28 AM
        0 responses
        6 views
        0 likes
        Last Post oviejo
        by oviejo
         
        Working...
        X