Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Volume[] and Vol()[]

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

    Volume[] and Vol()[]

    Hello,

    If i do this:
    Code:
     double clovol = Close[barsAgo] * VOL()[barsAgo];
    It complile.

    But if i try this one:
    Code:
     double clovol = Close[barsAgo] * Volume[barsAgo];
    Error:


    NinjaScript File Error Code Line Column NEWSDBAobu.cs Cannot apply indexing using [] to expression of type 'int' CS0021 132 51

    Anyone?

    Frank
    TY

    #2
    Hello frankduc,

    Thank you for your inquiry.

    I'm not seeing this throw an error on my end with the below example:

    Code:
    public class FrankducVolumeTest : Indicator
    {
    private int barsAgo;
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "FrankducVolumeTest";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = true;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
    //See Help Guide for additional information.
    IsSuspendedWhileInactive = true;
    barsAgo = 5;
    
    }
    else if (State == State.Configure)
    {
    }
    }
    
    protected override void OnBarUpdate()
    {
    if(CurrentBar < barsAgo)
    return;
    
    double clovol = Close[barsAgo] * Volume[barsAgo];
    
    Print(clovol);
    }
    }
    I'm not seeing anything about that line that I would expect to have an error thrown with. If you try the above in an indicator do you see an error?

    Thanks in advance; I look forward to assisting you further.

    Comment


      #3
      Kate,

      Than it must be related to my indicator:

      Code:
      double closePrice = 0;
      double volumes = 0;
      double sum1 = 0;
      double sum2 = 0;
      
      
      for (int barsAgo = foundIndex; barsAgo <= tot; barsAgo++)
      {
      closePrice = Close[barIndex];
      volumes = VOL()[barIndex];
      [B]volumes = [/B][B]Volume[barsAgo]; // not working either return error about int[/B]
      
      
      //  [B]double clovol = Close[barsAgo] * Volume[barsAgo]; [/B]  or  double clovol = Close[barsAgo] * VOL()[barsAgo]; The first one  in bold is not wroking.
      
      
      sum1 += clovol;
      sum2 += volumes;
      
      cma = sum1 / sum2;
      
      
      
      }
      Is there a difference between VOL()[] and Volume[]? Cause its not returning the same outpout.

      Frank
      TY

      Comment


        #4
        Hello frankduc,

        Thank you for your reply.

        There's not enough of the code shown for me to exactly understand where the error might be but I suspect it's from your use of barIndex vs barsAgo - where is barsAgo being set in your script and what is the value being set?

        Thanks in advance; I look forward to assisting you further.

        Comment


          #5
          Hello,

          If i am trying to loop barsAgo from a specific index, lets say 100.
          I am going backward to the left side of the chart.
          So i am going from 100 to 200 (if 200 is the last barsAgo).

          Should it be for (int barIndex = index; barIndex >= 0; barIndex--) or for (int barsAgo = index; barsAgo <= CurrentBar; barsAgo++) ?

          TY

          Comment


            #6
            Hello frankduc,

            Thank you for your reply.

            Keep in mind that BarsAgo is different from the CurrentBar bar index. CurrentBar or the bar index counts up from 0 starting at the left most bar on the chart so the CurrentBar would be the highest index, whereas BarsAgo would start from the most current bar being 0 and counting backwards so the left most bar would be the highest BarsAgo.

            If you are doing that in OBU, you could check if CurrentBar == some index, and then start your loop from there where you start at 0 and loop to 100 bars ago.

            Code:
            if (CurrentBar == myIndex)
            {
            for( int count = 0, count < 100, count++)
            {
            // do something
            }
            }
            Please let us know if we may be of further assistance to you.

            Comment


              #7
              Kate,

              Can i simply do :

              Code:
              for (int barsAgo = index; barsAgo >= CurrentBar; barsAgo++)
              {
              DateTime timeValue = Bars.GetTime(barsAgo);
              sumOfVolumes4 += VOL()[barsAgo];
              if (sumOfVolumes4 >= sumvolfibo)
              {
              foundIndex = barsAgo;
              
              
              
              
              
              break;
              }
              
              Draw.VerticalLine(this, "tag3", RfoundI, Brushes.Green);
              Draw.TextFixed(this, "Time", "Time: " + timeValue.ToString("yyyy-MM-dd H:mm:ss"), TextPosition.TopLeft, Brushes.Lime, new SimpleFont("Courier New", 16), Brushes.Transparent, Brushes.Transparent, 10);
              
              Print("BI"+barsAgo);
              }
              Anyway its not working. OnBarUpdate is so complicated. You dont have that kind of problem with OnRender.

              My index (1095) is going backward to the left side of the chart. But with barsAgo it cant be CurrentBar? It cant be zero either. The total number of bars is 1870. It should be counting backward from 987 to 1870. The count is increasing so it should be barsAgo ++. But does it make sense if in fact you are going backward. Shouldn't be barsAgo--?

              Code sometimes work but not sending back right answer. Its going from 1095 to 1870 foward. Should be the other side and stop once it >= to sumvolfibo.

              TY

              Comment


                #8
                Hello frankduc,

                Thank you for your reply.

                What is index in for (int barsAgo = index; barsAgo >= CurrentBar; barsAgo++)?

                This would only iterate throught the loop if whatever you're assigning to barsAgo is greater than the CurrentBar index, so if you've got index = 100, and the current bar on the chart has a CurrentBar index of 4000, nothing's going to occur.

                If you want to look back from the current bar and loop through the previous 100 bars, you'd want to do it as I suggested above.

                Please let us know if we may be of further assistance to you.

                Comment


                  #9
                  Kate,

                  Sorry for the wrong code.
                  What i meant is :

                  for (int barIndex = index; barIndex < tot; barIndex++) //actually barIndex or barsAgo is irrelevant and leads to the same result

                  index = bar (1099) to tot (CurrentBar = 1876)

                  That is fine and return the write answer. But its extremely slow. I have 3 other loops.

                  One is doing this:

                  for (int barIndex = index; barIndex > 0; barIndex--) //from index 1099 to zero right side of the chart.

                  Somwhere it is conflicting. Error: ndicator 'NEWSDBAobu': Error on calling 'OnBarUpdate' method on bar 0: 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.

                  Frank

                  TY

                  Comment


                    #10
                    Hello frankduc,

                    Thank you for your reply.

                    Do you have a check for the number of bars on the chart you'll be looking back through in your loop that you've actually processed enough bars to look back that far before trying to do so? Such as:

                    if (CurrentBar < index)
                    return;

                    Thanks in advance; I look forward to assisting you further.

                    Comment


                      #11
                      Kate,

                      I tried that and it does not solve the problem. if (CurrentBar < index)return;

                      Can you tell me the difference between Close[]; and Bars.GetClose();? The first is for OnBarUpdate and the second for OnRender?



                      if i try with this for (int barIndex = foundIndex; barIndex <= tot; barIndex++) It returns the right answer sometimes but it is very slow.

                      If i try with for (int barsAgo = index; barsAgo > 0; barsAgo--) //from index 1099 to zero right side of the chart.

                      I will get an error saying: squence has no element . Now you will say it is related to LINQ but i am not sure about that. Even by eleminating all linq query it still return an error.

                      I guess its out of the scope of your assistance anyway.

                      Frank
                      TY

                      Comment


                        #12
                        Hello frankduc,

                        Thank you for your reply.

                        Close[0] is a series that is synced to each bar. You wouldn't want to use this in OnRender because OnRender is not synced with OnBarUpdate and you might not be getting the accurate current value. In that case you'd want to use Bars.GetClose() since that uses an an absolute bar index value to retrieve the correct close value.

                        As for the error, I would really need to see more of the code to be able to give direction on what may be occurring.

                        Please let us know if we may be of further assistance to you.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        636 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        366 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        107 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        568 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        571 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X