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

Why I get data for old date when calling Close[0]?

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

    Why I get data for old date when calling Close[0]?

    I tried to analyze some bars serie, but i get wrong date quotes and finally error

    This is the code:


    protected override void OnBarUpdate()
    {
    int i;
    if (BarsInProgress != 0)
    return;
    if (CurrentBars[0] < 1)
    return;
    if (ifHistoryScanned == false)
    {
    Print(Close[0]);
    for(i=0; i<=10; i++)
    {
    if (High[i] > highestHigh)
    highestHigh = High[i];
    Print(High[i]);
    Print(Time[i]);
    Print(i);
    // If current low is lower than current lowest low, set lowest low to current low
    if (Low[i] < lowestLow)
    lowestLow = Low[i];
    Print(Low[i]);
    Print(Time[i]);
    Print(i);
    }
    ifHistoryScanned = true;
    }


    This is an output:

    7740
    7740.75
    10/7/2019 1:00:00 AM
    0
    7729.25
    10/7/2019 1:00:00 AM
    0
    7734.75
    10/7/2019 12:30:00 AM
    1
    7698.75
    10/7/2019 12:30:00 AM
    1
    Strategy 'MAcrossingBreakEven': Error on calling 'OnBarUpdate' method on bar 1: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.



    I dont understand why CLose[0] returnes a quote for 7th october

    #2
    Welcome to the forums timur!

    In your script, you are using a loop to access different BarsAgo indexes. You are also only checking that only 1 bar has passed when your loop tries to reference 10 previous bars.

    Close[0] reflects the bar that is being processed by the script, and Close[1] would represent the last bar processed.

    NinjaTrader also processes bars chronologically, and begins processing the historical data of the data series that the script is applied against. I.E. If we open chart with 5 days to load, that is where the script will begin.

    I have included some documentation on working with Price Series as well as some information on making sure that enough bars have been processed before you make a BarsAgo reference for that bar.

    Working with Price Series - https://ninjatrader.com/support/help...ice_series.htm

    Making sure you have enough bars in the data series you are accessing - https://ninjatrader.com/support/help...nough_bars.htm

    Let us know if you have any additional questions.
    JimNinjaTrader Customer Service

    Comment


      #3
      The strategy is linked to 15-min MNQ12-19 data serie. So am I correct, considering that Close[0] is a current bar close value, close[1] is a previous 15 min close value ... Close[10] is 10 time per 15 min ago close value?

      The mystery for me is that is i just write Print(Close[0]) - I always get current quote. But if I try to get Close[i] when i=0 I get a coute from 7th October at 12-00-00

      Comment


        #4
        Hello timur,

        It may be helpful to first observe how OnBarUpdate unrolls the data with a print like:

        Print(String.Format("Time[0]: {0} Close[0]: {1} CurrentBar: {2}", Time[0], Close[0], CurrentBar));

        After observing the prints in the output window, you can enable the Data Box on your chart and then right click in the Data Box to enable Bars Ago and CurrentBar indexes. Using the crosshair together with this will help to illustrate how OnBarUpdate is processed and what each bar is for a given index.

        This should then make it clearer for what your loop is referencing.
        JimNinjaTrader Customer Service

        Comment


          #5
          I got that, just putting it in OnBarUpdate sub:

          Time[0]: 10/7/2019 1:00:00 AM Close[0]: 7740 CurrentBar: 1
          Time[0]: 10/7/2019 1:30:00 AM Close[0]: 7743.5 CurrentBar: 2
          Time[0]: 10/7/2019 2:00:00 AM Close[0]: 7738.25 CurrentBar: 3
          Time[0]: 10/7/2019 2:30:00 AM Close[0]: 7727.5 CurrentBar: 4
          Time[0]: 10/7/2019 3:00:00 AM Close[0]: 7726.75 CurrentBar: 5
          Time[0]: 10/7/2019 3:30:00 AM Close[0]: 7727 CurrentBar: 6
          Time[0]: 10/7/2019 4:00:00 AM Close[0]: 7731.5 CurrentBar: 7
          Time[0]: 10/7/2019 4:30:00 AM Close[0]: 7725.25 CurrentBar: 8
          Time[0]: 10/7/2019 5:00:00 AM Close[0]: 7727 CurrentBar: 9
          Time[0]: 10/7/2019 5:30:00 AM Close[0]: 7729.75 CurrentBar: 10
          Time[0]: 10/7/2019 6:00:00 AM Close[0]: 7728.5 CurrentBar: 11
          Time[0]: 10/7/2019 6:30:00 AM Close[0]: 7731.5 CurrentBar: 12
          Time[0]: 10/7/2019 7:00:00 AM Close[0]: 7738 CurrentBar: 13
          Time[0]: 10/7/2019 7:30:00 AM Close[0]: 7735.5 CurrentBar: 14
          Time[0]: 10/7/2019 8:00:00 AM Close[0]: 7736 CurrentBar: 15
          Time[0]: 10/7/2019 8:30:00 AM Close[0]: 7738.75 CurrentBar: 16
          Time[0]: 10/7/2019 9:00:00 AM Close[0]: 7732 CurrentBar: 17


          But why do i get each time the next bar after each on each tick update?
          Please how to directly get n-th bar from the latest one in selected chart?
          In other word, how can I run through the number of bats to the left from the latest bar?

          Comment


            #6
            I put this in OnBarUpdate, but I always got the last bar, but in your case it magically moves from bar to bar...

            for(i=0; i<=10; i++)

            {

            if (High[0] > highestHigh)

            highestHigh = High[0];

            Print(High[0]);

            Print(Time[0]);

            Print(i);

            // If current low is lower than current lowest low, set lowest low to current low

            if (Low[0] < lowestLow)

            lowestLow = Low[0];

            Print(Low[0]);

            Print(Time[0]);

            Print(i);



            }

            Comment


              #7
              Hello timur,

              When you are using Calculate.OnEachTick or OnPriceChange, OnBarUpdate processes whenever there is an update to the developing bar. Close[0] and Time[0] will represent the bar that is developing. When using Calculate.OnBarClose, OnBarUpdate processes whenever a bart closes. Close[0] and Time[0] represent the bar that has just closed.

              if you want to loop through all the bars on the chart, you could create a loop as follows. Note that I added a ClearOutputWindow so we only see the resulting prints from the loop.

              ClearOutputWindow();
              for (int i = CurrentBar; i >= 0; i--) Print(Time[i]);

              If you use static values other than CurrentBar, I.E.

              for (int i = 10; i >= 0; i--) Print(Time[i]);

              Then you will need to make sure that the strategy has first processed that many bars before you reference that BarsAgo. I.E.

              if (CurrentBar < 10) return;
              for (int i = 10; i >= 0; i--) Print(Time[i]);

              See "Making sure you have enough bars in the data series you are accessing" in post 2 for more details.
              JimNinjaTrader Customer Service

              Comment


                #8
                Thank you


                But trying the following doesnt work also((

                for ( i = CurrentBar; i>=0; i++)

                {

                if (High[i] > highestHigh)

                highestHigh = High[i];

                Print(High[i]);

                Print(Time[i]);

                Print(i);




                if (Low[i] < lowestLow)

                lowestLow = Low[i];

                Print(Low[i]);

                Print(Time[i]);

                Print(i);



                }

                Comment


                  #9
                  Hello timur,

                  Please check the indexes that are being used in your loop and what the index "i" is that is being used in the BarsAgo reference before making your BarsAgo reference.

                  You are starting at the CurrentBar index and incrementing the index. There will never be CurrentBar+N bars on the chart as CurrentBar is the index for current bar being processed in OnBarUpdate. Using a BarsAgo reference greater than CurrentBar will always result in an invalid reference as you would be referencing before the first bar.
                  JimNinjaTrader Customer Service

                  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,281 views
                  0 likes
                  Last Post marcus2300  
                  Started by fernandobr, Today, 09:11 AM
                  0 responses
                  3 views
                  0 likes
                  Last Post fernandobr  
                  Started by itrader46, Today, 09:04 AM
                  1 response
                  6 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