Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

BarsRequest - a few questions

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

    BarsRequest - a few questions

    Hey there,

    I'm using the BarsRequest class to get bar info. In particular I want to know when the BarUpdate callback is resulting from a Bar closing. I see that the callback is firing with every tick - and but I can't see anything obvious to say when a bar has closed. What would be the best method to derive that a bar has just closed?

    Also what is the situation with this class and Historical Data? Will BarUpdates come in as a result of historical data being played back? I note it takes "FromDate" and "ToDate" params - so I'm not sure how this class will work with Historical data.

    I am presently using both BarRequest and MarketData updates in my addon - as I need bar updates and realtime price updates - but I'm wondering if there is any merit to using both, or if BarsRequest will suffice. I'm only using Bid and Ask from the MarketData Updates.

    Final question, what does this property represent in the BarSeries class: EstimatedFutureBarTimeDiff

    thank you!

    Kevin.

    #2
    Hello reach4thelasers,

    Thanks for writing in.

    You may set the BarsPeriod of the BarsRequest to represent a the time period you wish to have the created MyOnBarUpdate() use. Let's look at the sample code in the sample indicator provided.

    Code:
    private NinjaTrader.Data.BarsRequest DoBarsRequest(Instrument instrument, int lookBackPeriod)
    		{
    		  barsRequest = new NinjaTrader.Data.BarsRequest(instrument, DateTime.Now.AddDays(-lookBackPeriod), DateTime.Now);
    		  barsRequest.BarsPeriod = new NinjaTrader.Data.BarsPeriod { BarsPeriodType = BarsPeriodType.Second, Value = 10 };
    		  barsRequest.TradingHours     = NinjaTrader.Data.TradingHours.Get("Default 24 x 7");
    		  barsRequest.Update     += MyOnBarUpdate;
    		  barsRequest.Request(new Action<NinjaTrader.Data.BarsRequest, ErrorCode, string>((b, errorCode, errorMessage) =>
    		  {
    		      Dispatcher.InvokeAsync(new Action(() =>
    		      {
    		          if (errorCode != ErrorCode.NoError)
    		          {
    					  BarsSeries bs = new BarsSeries(barsRequest.Instrument, barsRequest.BarsPeriod, From, To, TradingHours);
    					  return;
    		          }
    		      }));
    		  }));
    		  return barsRequest;
    		}
    The code above sets the BarsPeriod to a 10 second interval. The code is called in the created MyOnBarUpdate() as:

    Code:
    private void MyOnBarUpdate(object sender, NinjaTrader.Data.BarsUpdateEventArgs e)
    		{
    		  	if (e.BarsSeries.GetTime(e.MaxIndex - 1) > currentTime)
    			{
    				Print(e.BarsSeries.GetTime(e.MaxIndex -1) + " MyOnBarUpdate " + e.BarsSeries.GetClose(e.MaxIndex -1));
    				currentTime = e.BarsSeries.GetTime(e.MaxIndex -1);
    			}
    		}
    The close price for that interval will be printed to the Output window. It should be synchronized with the primary data series you assign it to. If you apply the indicator to another 10 second chart, you will see consistent results. This logic is not documented in the help guide but you should be able to simulate the close of a bar using e.MaxIndex -1.

    To answer your second question, a BarsRequest will download any bars that are not already available locally. This includes historical data in the BarsRequest.

    For your third question, the BarsRequest will receive OHLC (Open High Low Close) data. If you wish to use Bid and Ask, you may subscribe to OnMarketData().

    To answer your final question, EstimatedFutureBarTimeDiff is an undocumented internal property. I was not able to come up with any information on it, but you can place Print() calls to observe this Property's behavior.

    Please let me know if you have any further questions.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #3
      Thanks for the response Jim. So are you saying that to determine if the bar has just closed I should keep a record of the Time of BarIndex - 1 and when I get an update with a new time then it means it's a new bar? Makes sense.

      My question about historical data was about playback connections. Can this be used on a playback connection the same way as live?

      Comment


        #4
        Hello reach4thelasers,

        Thanks for writing back.

        Yes you understand correctly, keeping track of the time of BarIndex - 1 should coincide with the last bar close.

        As there is no established connection, a BarsRequest will not be able to download new data when it is connected to Market Replay. BarsRequest will only give you results when connected to a live connection or when Market Replay is playing and the historical data exists.

        If you have any other questions, please don't hesitate to ask.
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Aviram Y, Today, 05:29 AM
        0 responses
        2 views
        0 likes
        Last Post Aviram Y  
        Started by quantismo, 04-17-2024, 05:13 PM
        3 responses
        25 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by ScottWalsh, 04-16-2024, 04:29 PM
        7 responses
        34 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by cls71, Today, 04:45 AM
        0 responses
        6 views
        0 likes
        Last Post cls71
        by cls71
         
        Started by mjairg, 07-20-2023, 11:57 PM
        3 responses
        218 views
        1 like
        Last Post PaulMohn  
        Working...
        X