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 alcorr247, Yesterday, 07:59 PM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_PaulH  
        Started by sdauteuil, Yesterday, 09:38 PM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_PaulH  
        Started by fscabrera03, Yesterday, 09:23 PM
        1 response
        13 views
        0 likes
        Last Post RJay
        by RJay
         
        Started by paulmadson, Today, 06:10 AM
        0 responses
        1 view
        0 likes
        Last Post paulmadson  
        Started by memonolog, Today, 06:03 AM
        0 responses
        1 view
        0 likes
        Last Post memonolog  
        Working...
        X