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

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

    BarsRequest

    What is the secret sauce to using BarsReqeust in OnBarUpdate?

    Here is what I am trying to accomplish:
    Chart: 405 Minute ES (Daily Chart for the ES)
    Session: CME US Index Futures RTH

    For each bar on this Chart I need to do a BarsRequest for
    BarsPeriodType = BarsPeriodType.Tick, Value = 1

    Then with a for loop using bars.Bars.GetClose(i) build a SortedList of Key = Price and Value = Volume.

    Yes, I am trying to build a Volume Profile for each trading day. Depending on my code changes either nothing is returned or every bar is lumped together ( not individual day),
    if the fromDate and toDate is hard coded (DateTime fromDate = new DateTime(2017,7,5)) it works but using the bars time (DateTime fromeDate = Time.GetValueAt(CurrentBar)) does not. I am getting all kinds of results except what is needed.

    I have a simple indicator script if you are interested. Thanks.

    #2
    Hello TAJTrades,

    Thanks for opening the thread.

    I modified a sample BarsRequest indicator so it loops through the requested bars from a previous day to build a Volume at Price list, and I am not experiencing any issues.

    I would suggest to add prints to make sure the DateTime being passed to the BarsRequest is valid. In my example, the BarsRequest is being called outside of the context of OnBarUpdate(). You may want to update your fromDate and toDate variables within OnBarUpdate() at a certain time and then call the BarsRequest in OnBarUpdate() after you update the toDate and fromDate variables. That way you know those dates will be correct in relation to the CurrentBar.

    I have attached the sample indicator with modifications for reference. Keep in mind that we do not write or debug code for our clients. This is so we can maintain a high quality of service for our clients as well as our vendors. If you would like further assistance with modifying the code for your needs or with debugging your code, I can get you connected with our Business Development team who can then pass over a list of NinjaScript Consultants who would be happy to help you with your code. If that is the case, please write in to platformsupport[at]ninjatrader[dot]com with the text "Attention Jim" and the thread URL.

    Please let me know if I can be of further help.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #3
      Edit: I am working with an Indicator, NOT an Addon if the makes a difference.


      Thanks for the code sample. I have been using.
      Using your TestBarsRequestVolumeAtPrice I was trying to access the MyPairs dictionary outside of the DoBarsRequest. It is not available. How can I access it?
      PHP Code:
              private IDictionary<doubledoubleMyPairs;
              private 
      DateTime currentTime;
              private 
      NinjaTrader.Data.BarsRequest barsRequest;
              private 
      NinjaTrader.Data.BarsRequest DoBarsRequest(Instrument instrumentint lookBackPeriod)
              {
                  
      barsRequest = new NinjaTrader.Data.BarsRequest(instrumentDateTime.Now.AddDays(-lookBackPeriod), DateTime.Now);
                  
      barsRequest.BarsPeriod = new NinjaTrader.Data.BarsPeriod BarsPeriodType BarsPeriodType.TickValue };
                  
      barsRequest.TradingHours     NinjaTrader.Data.TradingHours.Get("Default 24 x 7");
                  
      barsRequest.Update     += MyOnBarUpdate;
                  
      barsRequest.Request(new Action<NinjaTrader.Data.BarsRequestErrorCodestring>((berrorCodeerrorMessage) =>
                  {
                      
      // Output the bars we requested. Note: The last returned bar may be a currently in-progress bar
                      
      for (int i 0b.Bars.Counti++)
                      {                    
                          if (
      MyPairs.ContainsKey(b.Bars.GetClose(i)))
                              
      MyPairs[b.Bars.GetClose(i)] += b.Bars.GetVolume(i);
                          else
                              
      MyPairs.Add(b.Bars.GetClose(i), b.Bars.GetVolume(i));
                      }

      //                foreach( KeyValuePair<double, double> kvp in MyPairs )
      //                {
      //                    NinjaTrader.Code.Output.Process(string.Format("Key = {0}, Value = {1}", 
      //                        kvp.Key, kvp.Value), PrintTo.OutputTab1);
      //                }
                      
                      
      Dispatcher.InvokeAsync(new Action(() =>
                      {
                        if (
      errorCode != ErrorCode.NoError)
                        {
                            
      BarsSeries bs = new BarsSeries(barsRequest.InstrumentbarsRequest.BarsPeriodFromToTradingHours);
                            return;
                        }
                      }));
                  }));
                  return 
      barsRequest;
              } 
      Trying to access the dictionary after the bars request is empty:
      PHP Code:
                  else if (State == State.Realtime)
                  {
                      
      DoBarsRequest(Instrument30);

                      
      NinjaTrader.Code.Output.Process(string.Format("Count = {0}"MyPairs.Count), PrintTo.OutputTab1);
                      
      //  Count is 0
          
                      
      foreach( KeyValuePair<doubledoublekvp in MyPairs )
                      {
                          
      NinjaTrader.Code.Output.Process(string.Format("Key = {0}, Value = {1}"
                              
      kvp.Keykvp.Value), PrintTo.OutputTab1);
                      }
                      
                  } 
      Thanks for the help.
      Last edited by TAJTrades; 12-02-2017, 08:24 AM.

      Comment


        #4
        Hello TAJTrades,

        I did not have a problem accessing the dictionary outside of the BarsRequest. MyPairs is a a Dictionary of doubles and should not be looped through as a a Dictionary of KeyValuePairs. Bare in mind, we do not provide support for expanding on or making modifications to provided sample code.

        Please also keep in mind, that performing a bars request on each bar iteration could add a lot of unneeded work to request historical data for each bar iteration. It would normally be advised to use AddDataSeries() for any needed data series and follow the Multi Series NinjaScripts documentation for usage of multi-series scripts.

        I'll provide a publicly available link for the thread's reference - https://ninjatrader.com/support/help...nstruments.htm
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by aa731, Today, 02:54 AM
        0 responses
        1 view
        0 likes
        Last Post aa731
        by aa731
         
        Started by thanajo, 05-04-2021, 02:11 AM
        3 responses
        469 views
        0 likes
        Last Post tradingnasdaqprueba  
        Started by Christopher_R, Today, 12:29 AM
        0 responses
        10 views
        0 likes
        Last Post Christopher_R  
        Started by sidlercom80, 10-28-2023, 08:49 AM
        166 responses
        2,237 views
        0 likes
        Last Post sidlercom80  
        Started by thread, Yesterday, 11:58 PM
        0 responses
        4 views
        0 likes
        Last Post thread
        by thread
         
        Working...
        X