Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

BarsArray[0].GetHigh(0) and High[0] values different?

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

    BarsArray[0].GetHigh(0) and High[0] values different?

    I am coding a pivot with a source of a series.

    When I assign the series values using:

    Code:
    tradeH[0] = BarsArray[0].GetHigh(0);
    I have very different results by assigning the the values using:

    Code:
     tradeH[0] = High[0];
    I would expect that "BarsArray[0].GetHigh(0)" would return the same value as "High[0]"

    What am I missing?

    #2
    Hello waltS,

    Yes that would be expected, those methods are not alike. One takes a BarsAgo and one takes a Index, BarsAgo and Index are not the same concept. 0 index is the first bar on the chart, 0 BarsAgo relates to where you are in processing right now.





    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by waltS View Post
      What am I missing?
      GetHigh() is using an array index, where '0' is the first
      bar as seen on the far left of the chart.

      High[] is using a series index, where '0' is adjusted to
      always mean the most recently closed bar, on the far
      right of the chart. A series index has a special name,
      it's called a BarsAgo index.

      When a new bar is closed, it's high value is accessible
      via GetHigh(CurrentBar) or High[0].

      The high value of the very first bar on the far left is always
      accessible via GetHigh(0) or High[CurrentBar].

      -=o=-

      Careful ...
      High[CurrentBar] reads like a misnomer -- so you must
      first understand what the BarsAgo index means, and
      then you'll understand why the series syntax is most often
      used to 'look back' at historical bars.

      (Hint: The 'why' is probably due to ease of programming.)

      Good reading here, here, and here.
      Last edited by bltdavid; 06-02-2022, 09:37 AM.

      Comment


        #4
        Thank you Bitdavid, I was beginning to suspect that was the case that the series is reverse and the spots have to be "populated in reverse";

        I did look over the links, thank you;

        Ok so to populate a "double series" "ask" with the asks values;

        Code:
        ask[ask.count] = GetAsk(currentBar)
        and then can the "ask" series be used in an indicator??
        Last edited by waltS; 06-02-2022, 04:10 PM.

        Comment


          #5
          Have to say, I've been programming for many years, (many languages & platforms), but to correctly populate a series with "ask" and "bid" in Ninja, is NOT straightforward... Still trying to wrap my head around it...
          Last edited by waltS; 06-02-2022, 07:30 PM.

          Comment


            #6
            Originally posted by waltS View Post
            Thank you Bitdavid, I was beginning to suspect that was the case that the series is reverse and the spots have to be "populated in reverse";

            I did look over the links, thank you;

            Ok so to populate a "double series" "ask" with the asks values;

            Code:
            ask[ask.count] = GetAsk(currentBar)
            and then can the "ask" series be used in an indicator??
            Did you try?
            ask[0] = Bars.GetAsk(CurrentBar);

            -=o=-

            But why do you need to use a series?

            I mean, if Bars.GetAsk(CurrentBar) is the current ask price of the most
            recently closed bar, then Bars.GetAsk(CurrentBar-1) is the ask price of
            the previous recently closed bar.

            What I mean is, if you use,

            Bars.GetAsk(CurrentBar-n)

            (where 'n' is basically the BarsAgo index) doesn't this do what you want?

            Comment


              #7
              Originally posted by waltS View Post
              Have to say, I've been programming for many years, (many languages & platforms), but to correctly populate a series with "ask" and "bid" in Ninja, is NOT straightforward... Still trying to wrap my head around it...
              Frankly, I've never seen a usage case for GetAsk() and GetBid().

              I mean, the bid/ask prices can be take on many different values through out
              the life of the bar -- I've always preferred to use the real-time bid/ask prices
              gathered via OnMarketData.
              Last edited by bltdavid; 06-02-2022, 08:34 PM.

              Comment


                #8


                Thanks for the info bitdavid;

                I am currently translating an FX strategy from Multicharts where I get Low pivots using the bid and and High pivots using the ask. (in MC these are already available)
                >>In FX these two data are sometimes very different from the "normal" close... and give a better indication of direction changes.

                <<ask[0] = Bars.GetAsk(CurrentBar);>>

                I will try this (which should be fine to use in the "Bar change" event) but I think I may have to load the initial history in the "State.DataLoaded"... so I can use as source for "zigZag"
                That was the reason for the initial question...

                You have been a great help and I will post when I figure out what works.

                Walt

                Comment


                  #9
                  ok; Here is my solution:

                  Code:
                  private Series<double> bid;
                  private Series<double> ask;
                  
                  protected override void OnStateChange()
                  {
                  
                       if (State == State.Configure)
                         {          
                          AddDataSeries("AUDUSD", BarsPeriodType.Minute, 5, MarketDataType.Ask);
                         AddDataSeries("AUDUSD", BarsPeriodType.Minute, 5, MarketDataType.Bid);
                       }
                  else if (State == State.DataLoaded)
                  {
                  bid = new Series<double>(this);
                  ask = new Series<double>(this);
                  }
                  
                  //.....
                  
                  protected override void OnBarUpdate()
                  {
                  
                  if (BarsInProgress == 0)
                  {
                   //...
                  }
                  else if (BarsInProgress == 1)
                  {
                  [B]ask[0][/B] = BarsArray[1].GetClose(CurrentBar);
                  }
                  else if (BarsInProgress == 2)
                  {
                  [B]bid[0][/B] = BarsArray[2].GetClose(CurrentBar);
                  }
                  
                  if (BarsInProgress < 2) return;
                  
                  //...... calculations ...
                  
                  }
                  The last piece of the puzzle is to find where I can grab the instrument name in the "OnStateChange" as the "instrument" object is null...

                  I now can use the bid and ask series in my calculations...

                  Comment


                    #10
                    Ah, I see, looking good.

                    Some final thoughts...

                    If you clicked through and read all the links, you'll note that I mentioned
                    that the series are kept as 'parallel' to each other. That is, for any value
                    of 'n',

                    Open[n]
                    High[n]
                    Low[n]
                    Close[n]
                    Volume[n]
                    Time[n]
                    ask[n] <--- user-defined series
                    bid[n] <--- user-defined series


                    the values in each series at each 'n' are all from the same(*) bar.

                    The point is:
                    This axim is true for all series, even user-defined series.

                    That means when you defined your ask and bid series, you are
                    adding to the parallel data NinjaScript maintains on a per bar basis.

                    And it doesn't matter what type, series<double> and series<int>
                    and series<string> ... etc, these will all become parallel data.

                    You can also think of the BarsAgo index as a slot number to
                    retrieve data from historical bars. Looking backwards, the
                    first historical bar is at Close[1], the 2nd historical bar is at
                    Close[2], and so on. The key design of a series is that you
                    are looking backwards (not forward like an array).

                    Let's discuss your 'slap forehead' moment,

                    ask[0] = BarsArray[1].GetClose(CurrentBar);

                    Your 'ask' series is now part of the parallel data structures that are
                    automatically maintained by NinjaScript. Thus, Close[n] and ask[n]
                    etc etc, will always retrieve data from the same bar, for all values of 'n'.

                    Your series are first-class citizens. They are not any different than
                    the predefined series -- thus if you understand what the BarsAgo
                    index of '2' is doing in Close[2], then ask[2] is no different.

                    Just my 2˘.



                    (*) Assumes all series are synced to the same bars array, which is
                    done by the 'this' in the code 'new Series<double>(this);'
                    Last edited by bltdavid; 06-03-2022, 09:34 AM.

                    Comment


                      #11
                      Thank for that confirmation that I'm on the right track..
                      Also, it seems as I can now get the High (or Low) ask;
                      BarsArray[1].GetHigh(CurrentBar)..

                      Running into some weird issues with the Optimizer not working with the full range of data specified...
                      I'm hoping it's just "user-ignorance" vs software "un-documented features"

                      Comment


                        #12
                        Originally posted by waltS View Post
                        weird issues with the Optimizer not working with the full range of data specified...
                        Do you mean Optimization testing in Strategy Analyzer?

                        From AddDataSeries,
                        "Should your script be the host for other scripts that are creating indicators and series dependent resources in State.DataLoaded, please make sure that the host is doing the same AddDataSeries() calls as those hosted scripts would."

                        Is your strategy doing that?

                        Comment


                          #13
                          Nope; Single script..
                          When I spec a start & end of 3/30 to 6/3; Calculates as expected... Then I changed the start to 5/20 and starts calculating but then just stops with no results.
                          >> a similar issue with another symbol where it only worked with a five day period...
                          Debugged with VS and nothing was thrown...
                          Also, Opened a chart with the data and all looks OK...and re-loaded the history (just in case); no better...
                          >> I suspect there is a history cache somewhere that is confused... But it's a cross between and elephant & a rhino: Elll - if - I- no.


                          Comment


                            #14
                            Originally posted by waltS View Post
                            Nope; Single script..
                            When I spec a start & end of 3/30 to 6/3; Calculates as expected... Then I changed the start to 5/20 and starts calculating but then just stops with no results.
                            >> a similar issue with another symbol where it only worked with a five day period...
                            This could happen ...
                            if you don't have enough historical data, and you're
                            not connected to your data provider.

                            For me, I always disconnect before running SA.
                            But, just before I disconnect, I manually download
                            all the historical data I might need via,
                            CC -> Tools -> Historical Data -> Load tab

                            Since you're trying to use bid and ask data, you might
                            need to deliberately download bid/ask historical data.

                            See also CC -> Tools -> Options -> Market Data
                            Not sure if 'Global merge policy' impacts SA's use
                            of historical data, but mine is always set to
                            'Merge back adjusted'.

                            Last edited by bltdavid; 06-06-2022, 08:23 AM.

                            Comment


                              #15
                              I browsed the forum and noticed similar issues were solved by deleting the "DB" folder...
                              This seems to have worked... but I am a bit concerned as when I go live, my system runs from 5pm Sunday to 5pm Friday...
                              >> I have already had issues with the IB gateway disconnecting at least once and have created an "auto-it" applet to monitor and reconnect... hope I don't have to build another to fix the data...

                              I'll look at those settings you mentioned and hope that it helps prevent issues down-the-line... ...
                              Thanks for the "heads-up"!

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by pibrew, Today, 06:37 AM
                              0 responses
                              0 views
                              0 likes
                              Last Post pibrew
                              by pibrew
                               
                              Started by rbeckmann05, Yesterday, 06:48 PM
                              1 response
                              12 views
                              0 likes
                              Last Post bltdavid  
                              Started by llanqui, Today, 03:53 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post llanqui
                              by llanqui
                               
                              Started by burtoninlondon, Today, 12:38 AM
                              0 responses
                              10 views
                              0 likes
                              Last Post burtoninlondon  
                              Started by AaronKoRn, Yesterday, 09:49 PM
                              0 responses
                              15 views
                              0 likes
                              Last Post AaronKoRn  
                              Working...
                              X