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 Christopher_R, Today, 12:29 AM
        0 responses
        9 views
        0 likes
        Last Post Christopher_R  
        Started by sidlercom80, 10-28-2023, 08:49 AM
        166 responses
        2,235 views
        0 likes
        Last Post sidlercom80  
        Started by thread, Yesterday, 11:58 PM
        0 responses
        3 views
        0 likes
        Last Post thread
        by thread
         
        Started by jclose, Yesterday, 09:37 PM
        0 responses
        8 views
        0 likes
        Last Post jclose
        by jclose
         
        Started by WeyldFalcon, 08-07-2020, 06:13 AM
        10 responses
        1,415 views
        0 likes
        Last Post Traderontheroad  
        Working...
        X