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.Update BarsSeries.GetAsk/GetBid

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

    #16
    Thanks, Chelsea.

    A related question. I monitor 56 Forex currency pairs (the 28 majors for Ask and Bid). I submit those 56 BarsRequests at the same time. What is the most efficient way to issue those 56 BarsRequests so they have minimal effect on the UI performance? At first, I noticed a dramatic impact on UI refresh, to the point of NT actually freezing for several seconds. I now stage their submission to 5 at a time every 2 seconds (for want of a protocol), and have added an async approach. I currently do the following for each BarsRequest (still using the default MarketDataType, but soon to use Ask and then Bid)
    Code:
    internal async Task DoBarsRequestAsync(Instrument pInstrument)
    {
        try
        {
            if (pInstrument == null)
                return;
    
            TheBarsRequest               = new BarsRequest(pInstrument,1);    // Only need the latest bar
            TheBarsRequest.BarsPeriod    = new BarsPeriod { BarsPeriodType = BarsPeriodType.Minute, Value = 1 };    // 1 Minute
            TheBarsRequest.TradingHours  = TradingHours.Get("Default 24 x 7");
            TheBarsRequest.Update       += OnBarUpdate;
    
            TheBarsRequest.Request(new Action<BarsRequest,ErrorCode,string>((bars, errorCode, errorMessage) =>
            {
                {
                    if (errorCode != ErrorCode.NoError)
                    {
                        Print("Error on requesting bars: " + errorCode + ", " + errorMessage);
                        return;
                    }
    
                    bool connected = false;
    
                    // Requesting real-time bars requires a current connection
                    lock (Connection.Connections)
                          connected = Connection.Connections.FirstOrDefault() != null;
    
                    if (!connected)
                    {
                        Print("No Connections!");
                        return;
                    }
                }
            }));
        }
        catch (Exception oops)
        {
            Print("EXCEPTION: " + oops.ToString());
            return;
        }
    }
    I have found that using the async Task approach has significantly reduced the impact on the UI. I also have modified the invocation of the Dispatcher in which those calls are made as follows:
    Code:
    Dispatcher.InvokeAsync(new Action(() =>
    {
        SubmitTheBarsRequests();
    }),DispatcherPriority.Loaded,CancelActionToken);    // Lessen impact on rendering
    Your further advice would be greatly appreciated.

    Thanks.
    Last edited by jeronymite; 09-17-2020, 02:20 PM. Reason: Minor addition
    Multi-Dimensional Managed Trading
    jeronymite
    NinjaTrader Ecosystem Vendor - Mizpah Software

    Comment


      #17
      Hello jeronymite,

      Using a task or another thread would be outside of what is supported by NinjaTrader Support, but may work for your needs.

      With Addon that doesn't have synchronized series, I don't see any issue with this.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #18
        Thanks, Chelsea. I appreciate the feedback.

        I would like to be able to subscribe to the data in the same (or a very similar) way to that in which the NinjaTrader FX Board does it. The FX Board essentially is getting the 56 data streams I speak of very quickly. Is there any insight you can give from that perspective? (I should add that although I have given the 28 major FX pairs as an example, any 28 instruments may be chosen.)

        Thanks.
        Multi-Dimensional Managed Trading
        jeronymite
        NinjaTrader Ecosystem Vendor - Mizpah Software

        Comment


          #19
          Hello jeronymite,

          The FX Board window does BarsRequests after the window is fully loaded.

          Loading a list of instruments in this window will also freeze the UI of that window.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #20
            Continuing to explore my "uncertainties" of the most efficient way to obtain the data I want in an AddOn. Here is my current understanding of the circumstances and solutions that are "best". Most grateful for comments/clarification/correction.

            Circumstance: Require only the very latest real-time Ask and Bid for any Instrument.
            Solution: Use Instrument.MarketData.Ask.Price and similarly .Bid.Price. No need for BarsRequests.

            Circumstance: Require historical Bars Highs and Lows for any Instrument.
            Solution: Use appropriate BarsRequests.

            Is the above correct? Are these the most efficient approaches to the described circumstances? If not, further advice requested.

            Thanks.
            Last edited by jeronymite; 02-27-2021, 05:17 PM.
            Multi-Dimensional Managed Trading
            jeronymite
            NinjaTrader Ecosystem Vendor - Mizpah Software

            Comment


              #21
              Hello jeronymite,

              This would be a good approach as long as you are calling Instrument.MarketData.Ask.Price when needed, and not multiple times a second. If you are wanting real-time updates of bid and ask, the BarsRequest is the way to go.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #22
                Thanks, Chelsea.

                I am calling Instrument.MarketData... for multiple Instruments each second. Is this reasonable? These calls are independent of any BarsRequest.

                I have found discrepancies between these independent Instrument.MarketData... calls and BarsRequests where the former is updated and the latter is not. Hence my question with the focus it has. It seems that even though OnBarUpdate is invoked, the values for Bars.GetAsk and Bars.GetBid are always the values at the end of the previous bar (or start of the current bar, I haven't narrowed it down, but the Bar index is e.MaxIndex).

                Similarly, e.BarsSeries.Instrument.MarketData... within a BarsRequest seems to update throughout the bar but the values are frequently not the same as the independent Instrument.MarketData... data.

                There seem to be some issues with how BarsRequest is updating. I'll investigate further.

                My assumption is that any values of the independent Instrument.MarketData... data should be reflected in at least one update of the BarsRequest, meaning that the BarsRequest always shows every update. Is that correct? ... Because that's not what I'm seeing. I see the independent data can be different from any BarsRequest update, and the BarsRequest updates sometimes may only reflect the independent data after one or more updates.

                I've added some trace information in an attached text file. The "..." entries are the BarsRequest updates. The "---" entries are the independent calls every second, or thereabouts. The data are Bid, Ask and, for the BarsRequest, the percentage of the Bar. The timespan is just over a full minute.

                Why would the independent data sometimes never be reflected in the BarsRequest updates? There are several examples of this in the output.

                Thanks.
                Attached Files
                Last edited by jeronymite; 02-28-2021, 07:46 PM. Reason: Added trace output
                Multi-Dimensional Managed Trading
                jeronymite
                NinjaTrader Ecosystem Vendor - Mizpah Software

                Comment


                  #23
                  Hello jeronymite,

                  Once a second is interesting. Why so many updates if you don't want real-time updates as they come in. There is already a way to trigger events for each incoming piece of data..

                  Yes, there will differences in when data is available. GetCurrentAsk() and GetCurrentBid() will skip the update line which is why they would cause higher resource use. Using updates from BarsRequests or OnMarketData (which would be the same type of subscriber) would be the way to get each update in order.

                  When you call stuff matters. If you are calling stuff once per second, you are going to have a real hard time lining that up with sequential events. I wouldn't event suggest trying to compare these.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #24
                    Thanks for the comments, Chelsea.

                    Once a second is to update a "real-time" display of the data, and to use the data when a "Trade Now" button is clicked. I chose once per second because the BarsRequest updates are more frequent than that, and I wanted to only update the UI at a rate that did not cause too many overheads, particularly when a "decision window" of one second is plenty of time.

                    I hope that makes sense. Do you have a more efficient recommended approach?

                    Thanks.
                    Multi-Dimensional Managed Trading
                    jeronymite
                    NinjaTrader Ecosystem Vendor - Mizpah Software

                    Comment


                      #25
                      Hello jeronymite,

                      If you are clicking a button and only needing information at that time, perhaps no 1 second timer is needed?

                      The data call could be in the button Click event..

                      However, if you've got working the way you like, I'd stick with that.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #26
                        Thanks again, Chelsea.

                        Just by way of insight into what I'm doing, it is the displaying of the prices and spread that allow a trading decision to be made (or not), so the display of the data is integral to the decision-making process. The data then are used for the trade, of course.

                        On the original observations above:
                        • In a BarsRequest, why am I seeing that the Bars.GetAsk and Bars.GetBid only ever seem to change at a new bar?
                        • Is using the e.BarsSeries.Instrument.MarketData... data the same as using the independent/standalone Instrument.MarketData...? And hence, of poorer efficiency in the same manner?
                        Thanks.
                        Multi-Dimensional Managed Trading
                        jeronymite
                        NinjaTrader Ecosystem Vendor - Mizpah Software

                        Comment


                          #27
                          Hello jeronymite,

                          This appears to be jumping directly to the instrument and getting data from the instrument and not really from the bars request. No, this would not be the same as OnBarUpdate or OnMarketData.

                          I'm also not understanding what is not efficient.

                          Choosing to get data snapshots at specific times would a way to get data, and skipping updates would mean less processing, but less data is processed. So it's a matter of what is necessary for the goal. If you need every update, then this wouldn't provide every update. If you want skip updates and get snapshots every so often, as long as it is not too frequent it will not use that much CPU, but when too frequent would be much worse than just subscripting to the real-time updates.
                          Last edited by NinjaTrader_ChelseaB; 03-01-2021, 03:46 PM.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #28
                            Thanks, Chelsea.

                            The efficiency aspect comes particularly from the code executed every one second. I do a lot of processing of the data and trading potential, so I want to avoid more than every second, and also avoid any overheads with data where possible.

                            One major overhead that causes grief, and which I have separately requested NT Development address, is the major performance hit when adding BarsRequests. NT often just freezes when adding them, and that can last for several seconds, sometimes several tens of seconds. That is why my original enquiry was asking if I needed to use BarsRequests or not and the efficiency of the various options.

                            If I use the BarsRequests, what is the recommended method for accessing the real-time Ask/Bid prices. As noted above, Bars.GetAsk and Bars.GetBid only ever seem to change at a new bar.

                            Thanks.
                            Multi-Dimensional Managed Trading
                            jeronymite
                            NinjaTrader Ecosystem Vendor - Mizpah Software

                            Comment


                              #29
                              Hello jeronymite,

                              Downloading / reading (from cache) historical data takes time. However, if you are requesting 1 bar, or 0 bars, I would not expect a lot of overhead. Indicators are getting data the same way, but then that is constructed into Series, that have updaters, which is more overhead than just doing a bars request.
                              Chelsea B.NinjaTrader Customer Service

                              Comment


                                #30
                                Understood, Chelsea. Thanks.

                                If I may ask again, with BarsRequests, what is the recommended method for accessing the real-time Ask/Bid prices. As noted above, Bars.GetAsk and Bars.GetBid only ever seem to change at a new bar.

                                Thanks.
                                Multi-Dimensional Managed Trading
                                jeronymite
                                NinjaTrader Ecosystem Vendor - Mizpah Software

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by timmbbo, Today, 08:59 AM
                                1 response
                                2 views
                                0 likes
                                Last Post NinjaTrader_ChelseaB  
                                Started by KennyK, 05-29-2017, 02:02 AM
                                2 responses
                                1,279 views
                                0 likes
                                Last Post marcus2300  
                                Started by fernandobr, Today, 09:11 AM
                                0 responses
                                0 views
                                0 likes
                                Last Post fernandobr  
                                Started by itrader46, Today, 09:04 AM
                                1 response
                                5 views
                                0 likes
                                Last Post NinjaTrader_Clayton  
                                Started by bmartz, 03-12-2024, 06:12 AM
                                5 responses
                                33 views
                                0 likes
                                Last Post NinjaTrader_Zachary  
                                Working...
                                X