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

get the bid ask volume inside the bar from the example

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

    #31
    Hello PaulMohn,

    There is a lot of information here that is difficult to absorb.

    The best way to understand issues you are facing while developing is to implement just that part in a separate script to focus only on the part you are having trouble with.

    If the following line of code reproduces the error, set up the code in a separate script to focus just on how that part of code is set up and used.

    Code:
    DateTime myStartTime = BarsArray[0].GetTime(CurrentBars[0]).Subtract(TimeSpan.FromMilliseconds(7000));
    With a separate isolated script that just includes the code needed to reproduce the same issue, then check:

    What exactly on this line is being indexed/referenced when I get the error?

    On which data series?

    aka - Which bar on which data series is being referenced?

    Does that bar exist? (Bar indexes start at 0. -1 implies the first bar of that data series has not yet been formed. Referencing a bar that has not yet been formed would give an error because the bar does not exist.)
    Last edited by NinjaTrader_Jim; 05-20-2022, 01:55 PM.
    JimNinjaTrader Customer Service

    Comment


      #32
      Thanks for the segmentation on a separate script suggestion, I'll test that on Monday.

      How I understand the references of the line:

      BarsArray[0] = The Primary Series array/list (i.e. the 1min CL bars, 30 seconds bars etc.) (vs thee BarsArray[1] secondary Series = the 1 tick Bars)

      CurrentBars[0] = Also the Primary Series, but refers to the bars Indexes/ inner-positions in the Array (i.e. it refers to single bars in the array/Series)

      Difference between BarsArray[0] and CurrentBars[0] =
      both refer to the same bars Series i.e. the Primary Series on the Chart, but the former refers to the collection/whole array unit of the Primary Series, while the latter goes one step further and refers to each individual Bars in the Primary Series.

      .GetTime(CurrentBars[0]) = refers to each individual Bar in the Primary Series List/Array Closing Bars Time (i.e. the time between the close of the bar and the open of the next bar)

      .Subtract(TimeSpan.FromMilliseconds(7000)); = refers to 7 seconds before the close of each individual bar of the primary Series.


      That said, as demonstrated in my previous post, BarsArray[0].Count and CurrentBars[0] do have a difference of 1 Bar such that CurrentBars[0] has 1 bar less than BarsArray[0].Count.
      What's confusing it seems is that they both refer to the Primary Series but somehow they don't have the same number of units/elements in their collections.


      BarsArray[0].GetTime(CurrentBars[0]) seems to mean

      take the Primary Series Array/List (BarsArray[0]), of that Primary Series Array/list Get the Closing Time stamp of each individual bar (.GetTime(CurrentBars[0])).
      Now, considering the printing results of my previous post and your comment both BarsArray[0].Count and CurrentBars[0] start at 0,
      when BarsArray[0] is at index/position 0 in the array (in its array/list the 1st position/index), .GetTime(CurrentBars[0]) 'looks' for the closing time stamp of Bar -1 in the CurrentBars[0] array/list.

      -1 0 1 2 3 4 5 = CurrentBars[0]
      0 1 2 3 4 5 6 = BarsArray[0]

      So at BarsArray[0] 1st index, the compiler finds no TimeStamp for the corresponding CurrentBars[0] bar since there's no -1 Bar.

      So the solution seems to be to Start from 1 bar later in the BarsArray[0]. How would I formulate the delayed start from index 0 to index 1 in the BarsArray[0] ?

      Can we do it with a CurrentBars[0] Check? it doesn't seem possible.

      Else with a loop?

      PHP Code:
      for (int BarsArrayIndex 1BarsArrayIndex <= CurrentBars[0]; BarsArrayIndex++)
      {
           
      DateTime myStartTime BarsArray[0].GetTime(CurrentBars[0]).Subtract(TimeSpan.FromMilliseconds(7000));

           
      DateTime myEndTime BarsArray[0].GetTime(CurrentBars[0]).Subtract(TimeSpan.FromMilliseconds(2000));

           if (
      Time[0] >= myStartTime && Time[0] <= myEndTime)
           {
                
      Draw.Line(this"tag1"+CurrentBartrue0max.Key0max.Key + (TickSize), Brushes.WhiteDashStyleHelper.Dash5);
           }

      But is a loop a valid way to solve an indexing error? (if so do you usually recommend/use it? Because I've only found CurrentBar Checks recommendations so far in the forum for indexing errors).

      If so we could also reduce the loop range to a lesser one as we wouldn't need data from the start of the BarsArray[0] array/list since we only need it for realtime processing mode, for example with only 21 bars prior to the currently processing bar on the chart

      PHP Code:
      for (int BarsArrayIndex = (CurrentBars[0] - 21) ; BarsArrayIndex <= CurrentBars[0]; BarsArrayIndex++)
      {
           
      DateTime myStartTime BarsArray[0].GetTime(CurrentBars[0]).Subtract(TimeSpan.FromMilliseconds(7000));

           
      DateTime myEndTime BarsArray[0].GetTime(CurrentBars[0]).Subtract(TimeSpan.FromMilliseconds(2000));

           if (
      Time[0] >= myStartTime && Time[0] <= myEndTime)
           {
                
      Draw.Line(this"tag1"+CurrentBartrue0max.Key0max.Key + (TickSize), Brushes.WhiteDashStyleHelper.Dash5);
           }


      I'll test it on Monday. Have a good weekend. Thanks!
      Last edited by PaulMohn; 05-20-2022, 05:36 PM.

      Comment


        #33
        Hello Jim,

        drawing from the GetTime() example, I found this satisfactory solution

        PHP Code:
                for(int barIndex = ((BarsBack ChartBars.FromIndex) ? (ChartBars.ToIndex-BarsBack) : ChartBars.FromIndex);
                    
        barIndex <= ChartBars.ToIndex;
                    
        barIndex++)
                {
                
        DateTime myStartTime BarsArray[0].GetTime(barIndex).Subtract(TimeSpan.FromMilliseconds(StartDisplay));

                
        DateTime myEndTime BarsArray[0].GetTime(barIndex).Subtract(TimeSpan.FromMilliseconds(EndDisplay));

                  if (
        Time[0] >= myStartTime && Time[0] <= myEndTime)
                  {
                        
        Draw.Line(this"tag1"+CurrentBartrue0max.Key0max.Key + (TickSize), Brushes.WhiteDashStyleHelper.DashDashWidth);
                  }
                } 

        I think the BarsArray isn't suited for the purpose going with the CurrentBars[0]. If you know of a way to accomplish the same as my above loop with the BarsArray I'd be very interested and appreciative getting a conclusive direction about it. My take on it is that we might not achieve the needed result with the BarsArray. But I could be wrong. The BarsArray doc isn't helpful/documented enough to get positive on this. For instance, I noticed unexpected result with BarsArray[0][10] which returned a price value (the doc only states 1 parameter
        (i.e. Syntax
        BarsArray[int index]
        ) when we would normally expect a Bars index value. Why does it returns a price value?

        I also would be interested in the solution with BarsArray[0] in order to test calling other series POC.
        I tested CurrentBars[0].GetTime(CurrentBars[0]) instead to see if I could get the same result, but the .GetTime() method throws a compile error.

        Would you please explain why CurrentBars[0] has 1 less bar than BarsArray[0] ?

        Is it because CurrentBars[0] doesn't count the currently processing bar (0 to n-1) while BarsArray[0] does (0 to n) ?


        It would be salutary to have that information available in the doc. Since they both are arrays/lists referring tho the same series it's counter-logical-expectation for them to have different Counts/Lengths. When such logical disparities arise adding an explicative/comparative note in the doc would be helpful.
        If you do have the explanation/example somewhere (in another doc preferably) I'll be grateful for the reference. Thanks!

        I've reviewed the Multi-Time Frame & Instruments doc but I don't find it.

        Also the CurrentBars doc does provide this note
        Note: In multi series processing, the CurrentBars starting value will be -1 until all series have processed the first bar.

        But it's not clear what it means. Which CurrentBars? CurrentBars[0]? CurrentBars[1]? How do we know. Which Series's first bar? How do we know.
        Also why not just add a complementary note on how to handle it (so we don't have to guess/memorize it, instead we'd just have to refer to it in the doc) ? For example, why not share what the problems and solutions to this noted spec are? Thanks!


        Also, why isn't there in the documentation any one explicit fix to all possible indexing errors similar to this statement
        PHP Code:
        for(int barIndex = ((BarsBack ChartBars.FromIndex) ? (ChartBars.ToIndex-BarsBack) : ChartBars.FromIndex); 

        Where BarsBack is the User Input/programmer choice of how many bars back the indicator will perform its calculations on.

        But for CurrentBar/CurrentBars[n] ?
        Something like
        PHP Code:
        for(int barIndex = ((BarsBack CurrentBars[0].First()) ? (CurrentBars[0].Last()-BarsBack) : "The minimum CurrentBars[0] index to prevent any indexing error is automatically set"); 
        or
        PHP Code:
        for(int barIndex = ((BarsBack > (CurrentBars[0].First() + n)) ? (CurrentBars[0].Last()-BarsBack) : "The maximum BarsBack value before any indexing error is thrown is automatically set"); 

        Where n would be any number preventing any possible indexing error.

        Or some simpler formulation and one still solving all possible indexing errors.

        So that either way the User Input number of bar inputted the indexing error never shows up/if the User does put a wrong number of bars it doesn't throws the indexing error (a warning could possibly be signaled that the minimum number of bars needed was used instead of the User Input). That way the user doesn't need to wonder what the error stems from and only needs to focus on the number of bars wanted the indicator to calculate on.

        What would be that one universal fix formulation? So we can just refer to it an never have to ask NT advisors. Why not add it to the doc so we don't need to guess/memorize that issue and have one fix once and for all, preferably automated. Thanks!
        Last edited by PaulMohn; 05-22-2022, 02:09 AM.

        Comment


          #34
          Hello PaulMohn,

          Count or BarsArray[0].Count is the total number of bars in the collection, and the count starts at 1.

          CurrentBar is the bar that is currently processing in OnBarUpdate, and the first bar is bar 0.

          The same is true for arrays and lists. The count is the total number of objects, the index starts at 0.

          In historical data, lets say 2000 bars are going to load.

          Each time OnBarUpdate() updates, CurrentBar increments. It starts by updating bar 0, then updates bar 1, then bar 2, etc. CurrentBar counts each bar as it is the bar updating OnBarUpdate().
          During this time BarsArray[0].Count does not increase. When CurrentBar is 0, BarsArray[0].Count still has a count of 2000. On CurrenBar 2, 3, 500, all the way up to 1999 the count of the collection doesn't change.

          This is how we can know the last historical bar. Since CurrentBar starts at 0 (CurrentBar 0 is the first bar), CurrentBar 1999 is the 2000th bar. That bar won't close historically until a new bar opens it in real-time. So the previous bar will be the last fully closed historical bar until real-time starts. Meaning once CurrentBar reaches the size of the collection minus 2, its the last updating historical bar.

          To find the last historical bar:
          Code:
          if (State == State.Historical && CurrentBar == Count - 2)

          In debugging some code I found that that the Current Bar variable and the Bars.Count numbers are off by 2. Place the below code in any indicator or strategy

          As I continue to do my testing specially around dates in which there is a partial holiday or a full holiday, I am using the Custom Range in the Data Series Time Frames section to set the date range for which I wan to run the strategy. Whenever I set a range an by the time it runs through the historical time range, the

          I have an indicator which calculates an EMA on a secondary series initialized in the DataLoaded state: else if (State == State.DataLoaded) { EMA1 = EMA(BarsArray[1], 100); .... Is there any way to access the EMA value from the last historical bar prior to the first call of OnBarUpdate? The reason I ask is I am trying to set


          When it comes to series updating OnBarUpdate, this will depend on the size of the bar and when the data starts. If the primary series BarsInProgress 0 (BarsArray[0] and CurrentBars[0] the chart bars) are a 60 minute series, and the first added series BarsInProgress 1 is a 1 minute series, then BarsInProgress 1 is going to update OnBarUpdate 59 times before BarsInProgress 0 updates it once. But on the 60th update, BarsInProgress 0 would update before BarsInProgress 1 would update, as since they are both updating OnBarUpdate at the end of a minute close, the order would be in the BarsInProgress progression.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #35
            Hello Chelsea and thanks for the Historical/Realtime | BarsArray[0].Count/CurrentBars[0] update comparison/explanation and reply.

            From the realtime perspective/use:

            BarsArray[0]

            Historically : Pre-Counted / no incrementation
            Realtime Start : Starts incrementing in sync with CurrentBars[0], + 1 if OnEachTick, + 2 in OnBarClose calculation mode.

            0 - 10 -> BarsArray[0] Array Collection total units = 11
            1 - 10 -> BarsArray[0] Count Collection total Units = 10 (Count start at index 1, so the 2nd position in the array)

            CurrentBars[0]

            Historically : Pre-Counted / no incrementation
            Realtime Start : Starts incrementing in sync with BarsArray[0], - 1 if OnEachTick calculation mode., - 2 in OnBarClose calculation mode.

            0 - 9 -> CurrentBars[0] Array Collection total units = 10
            0 - 9 -> CurrentBars[0] Count Collection total Units = 10 (Count start at index 0, so the 1st position in the array, to sync with the BarsArray[0].Count Array )

            How can I use this info to prevent all possible indexing errors in one simple formulation to refer to every time ? I'm not sure I see the how of the use of the historical vs realtime comparison for this purpose since it only seems to point out that CurrentBars[0] starts -1 (or newly known -2 Bars) from BarsArray[0].Count and why.
            How do we use that info to prevent indexing errors, all possible indexing errors, automatedly? Thanks!

            Comment


              #36
              Any idea why the POC /max.key value does get calculated for most but for some of the bars?

              Here's a demo

              Please see at 6:06
              the bar at 9:45:53

              and at 8:23
              the bar at 9:48:53

              don't print the max.key values no they display the POC.

              How to make sue it prints/display the POC for all of the bars? Thanks!

              Comment


                #37
                Hello PaulMohn,

                (and thanks NinjaTrader_ChelseaB for the explanation on historical processing and bar indexing.)

                The questions left here really go further than the level we can help. The examples are meant to show the way for handling the single tick data for analysis. This can get complicated very quickly, but this framework would be what we have found that works for analyzing the single tick data without getting errors.

                How you derive your own calculations, checking to make sure the correct calculations are made, and checking to make sure the correct values are being assigned to plots when you expect will all be tasks that involve careful review and debugging.


                JimNinjaTrader Customer Service

                Comment


                  #38
                  Hello Jim and thanks for the reply.

                  The questions left here really go further than the level we can help. The examples are meant to show the way for handling the single tick data for analysis. This can get complicated very quickly, but this framework would be what we have found that works for analyzing the single tick data without getting errors.
                  O, I'll try to figure it out for multiple POC use and get back to you if needed. Thanks!

                  How you derive your own calculations, checking to make sure the correct calculations are made, and checking to make sure the correct values are being assigned to plots when you expect will all be tasks that involve careful review and debugging.
                  I think my 2nd post calculations and values are correct since I didn't change them from the previous version which didn't omit max.key and the POC on some bars. What else would you suggest debugging than the max.key value? Thanks!

                  Comment


                    #39
                    Hello Jim! I try your BuySellVolumeOneTick.zip and find GetClose(whatbar) always equals to GetBid(whatbar).
                    Thus impossible to separate buy and sell volumes. I use NinjaTrader FXCM (demo). Pls check.

                    Comment


                      #40
                      Hello aleeninja,

                      Forex does not have a centralized exchange and instead trading is done directly between institutions. This means the volume will only be for the single institution and will not have any meaning for the broader market.

                      Because of this, typically brokerages will not provide volume with forex instruments, or will provide a false volume.

                      Forums posts that discuss this found through a google search of 'forex volume':
                      Hello, With FX there is no volume but in the time and sale there is data in millions of dollar. How can we use that information. You cant get the value in dollar with Bars.GetVolume(barIndex) Thank you


                      Hello everybody, When I opening my Forex chart (eg. 1hr chart) and let it run for several hours, the volume indicator is showing only low volume from the time I opened the chart. When I reload the historical data after a while, it shows the correct volume of the bars. Calculation is on each tick and my data provider is Oanda

                      Hi I did a migration to NT8 from NT7 and everything was fine and since I have a lifetime licence with NT, I was able to load your included indicators like volume profile and vwap on my forex chart ( for ex USDCAD chart) But after doing a historical data reload from Interactive Brokers TWS for this week data, all of a sudden


                      Another symptom of this is that historical data obtained from a data provider or broker for Last data is actually Bid data only and does not contain Ask data. There isn't any true Last data for forex instruments.

                      From the help guide:
                      "Forex price quotes do not use the concept of "Last Price" the same as other markets; only Bid and Ask quotes are available. Thus, when building bars using the default "Last" price type, the Bid price will be used instead."


                      Supporting forum threads discussing.
                      Forex; last = bid therefore, if I needed to actually differentiate between last and bid. How can I do that? B/c in reality the trade is actually executed at either



                      However, that said, I can confirm that the example provided by Jim does separate bid and ask volume when used in real-time only, or with an instrument that reports volume such as equities and futures in historical data.

                      A script could use AddDataSeries() with MarketDataType.Ask to add the ask information instead as workaround, if the data provider or broker has this available. BarsInProgress 0 (Closes[0], BarsArray[0], etc) would be the bid data, and BarsInProgress 1 would be the ask data.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #41
                        Hello ChelseaB! Thank you for answer. I open Time&Sales window and find the Bid and Ask are equal ones getting from GetAsk and GetBid from Jim script. The only issue is that
                        GetClose does not return last price. But in Time&Sales it is possible to view by color marker what the deal was (buy or sell). Thus there is opportunity to separate volumes. Pls provide
                        info how to do that. Thank you.

                        Comment


                          #42
                          In addition. We are speaking about real-time state only and Calculate.OnEachTick mode. Thank you.

                          Comment


                            #43
                            Hello aleeninja,

                            Forex does not have a true last price. The last price is actually the bid price.

                            GetClose() will return the bid price because the last is the bid with forex instruments.

                            In real-time the bid and ask will stream in OnMarketData(), which is used for the Time & Sales, or you can add an ask series with AddDataSeries() and get the Ask with BarsInProgress 1.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #44
                              Hello ChelseaB! Can you explain how it is possible to mark by color each deal in Time & Sales depending on what price ( Bid or Ask) deal was?

                              Comment


                                #45
                                Hello aleeninja,

                                Below is a link to an example TnSPrintsExample_NT8. The TnSVolumeExample_NT8 will not apply here as forex does not work with volume.
                                Chelsea B.NinjaTrader Customer Service

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by DanielSanMartin, Yesterday, 02:37 PM
                                2 responses
                                12 views
                                0 likes
                                Last Post DanielSanMartin  
                                Started by DJ888, 04-16-2024, 06:09 PM
                                4 responses
                                12 views
                                0 likes
                                Last Post DJ888
                                by DJ888
                                 
                                Started by terofs, Today, 04:18 PM
                                0 responses
                                11 views
                                0 likes
                                Last Post terofs
                                by terofs
                                 
                                Started by nandhumca, Today, 03:41 PM
                                0 responses
                                8 views
                                0 likes
                                Last Post nandhumca  
                                Started by The_Sec, Today, 03:37 PM
                                0 responses
                                4 views
                                0 likes
                                Last Post The_Sec
                                by The_Sec
                                 
                                Working...
                                X