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

confusion about input and Volume

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

    confusion about input and Volume

    Hello,

    I want to calculate a certain number of periods without for loop.

    So i tried:

    Code:
    double volus = Volume[0];
    
    double price = Input[0];
    It returns all the volume and close price for all the bars in the chart.

    If i want only the volume and price from barsago 448, how can i achieve that?

    I tried
    Volume(468)[0], Volume[468][0], but no luck.

    Frank
    Ty

    #2
    Hello frankduc,

    Thank you for your post.

    Both Volume[] and Input[] are iSeries variables:



    This code example you provided will not return the volume and close for all bars at once - your wording there was unclear. Rather, this provides the current value of Volume[] for the current bar and Input[] for the current bar.
    Code:
     
     double volus = Volume[0];  // this would assign the current volume for the current bar to volus  double price = Input[0];   // this would assign the current close price for the current bar to price
    If you want to retrieve a value from a set number of bars ago, you can access that like this:

    Code:
    double volus = Volume[448];  // assigns the value of Volume from 448 bars ago to volus
    double price = Input[448]; // assigns the value of the price from 448 bars ago to price
    I have noticed you seem to have quite a bit of confusion where it comes to bar indexing and accessing the correct values. This link to our help guide may assist you in better understanding how to properly access values: https://ninjatrader.com/support/help...orrect_bar.htm

    Also, please refer to this section of our help guide which provides guidance on using iSeries variables in a multi-instrument script for additional information: https://ninjatrader.com/support/help...arsninjascript

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Kate,

      Thanks for the reply but i tried
      double volus = Volume[448];
      and it return this error message:
      Indicator 'NEWSDBA': Error on calling 'OnBarUpdate' method on bar 0: Object reference not set to an instance of an object.

      Now my goal was to recreate my moving average without for loop:

      Code:
      { 
      
           double volus = Volume[788];
      
               double price = Input[788];
      
      
         double privol = volus * price;
      
          sum01 += privol++;
          sum02 += volus++;
      
          cma0 = sum01/sum02;
      
          Print("sum" + sum01);
          Print("sumP" + cma0);
      
           Value[0] = cma0;
      
      
          }
      This way i was hoping to use indexer more easly to create more than one cma. Have a cma for [0], [468] or [788]. But you are right i am struggling to understand how indexers work. There are a few exemples on the net but they all look alike.
      The cma returns the right answer at [0] but return an error at [788].

      Comment


        #4
        Hello frankduc,

        Thank you for your reply.

        The error you have received is an indexing error. In this case, what it's telling you is that it cannot look 448 bars back from the first historical bar on the chart, because nothing exists for it to look at. You will want to ensure that enough bars exist on the chart prior to executing the lookback. You can do this by adding something like this to the beginning of OnBarUpdate:

        if (CurrentBar < 448)
        return;

        This would exit OnBarUpdate without processing any further code unless there are at least 488 bars on the chart for it to look back to. If you wanted to look back 788 bars, you'd need to put at least 788 there.

        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          I did add

          if (CurrentBar < 448)
          return;

          but the error still there and there is plenty of bars. Actually there is 1200.
          If you look at the chart with
          Volume[0];
          you can see the cma passing thought the chart but at 448 the cma line should draw around the middle.

          what is really strange is if i keep
          if (CurrentBar < 448)
          return;

          and keep
          Volume[0]; and
          Input[0]; its working, but cma not returning the right answer!

          Is it suppose to do that?
          Attached Files

          Comment


            #6
            Hello frankduc,

            Thank you for your reply.

            If you added it to this:
            Code:
             
             {        double volus = Volume[788];           double price = Input[788];      double privol = volus * price;      sum01 += privol++;     sum02 += volus++;      cma0 = sum01/sum02;      Print("sum" + sum01);     Print("sumP" + cma0);       Value[0] = cma0;       }
            Yes, you would get an error., because in this code you need to be looking 788 bars back, as I pointed out in my previous post. You need to change it to this if you need to look back 788 bars:

            Code:
            if (CurrentBar < 788)
            return;
            Volume[0] and Input[0] do not give an error because they are looking at the currently forming bar, not trying to look back to a previous bar. I would highly suggest perhaps reviewing our help guide sections on bar indexing I posted previously as these explain this concept in depth .

            You will need to have a good grasp on this concept for you to be successful in creating your scripts. We cannot provide programming instruction, but if you need further assistance in understanding these basic concepts we would be happy to provide you information on NinjaScript Consultants who could provide more indepth instruction.

            Please let us know if we may be of further assistance to you.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              Hi,

              Is it possible to use :

              Code:
              [LEFT][COLOR=#252C2F][FONT=Courier]if (CurrentBar < 788)
              return;[/FONT][/COLOR][/LEFT]
              to go backward?

              Like i have my variable index2 = 690 and i want to count bars from 690 to 0?

              Obviously that wont work:

              Code:
              if (CurrentBar > index2)
                    return; 
              
              or
              
              sum02 += volus--;
              or must i use a for loop in OBU?

              Frank
              ty

              Comment


                #8
                Hello francduc,

                Thank you for your reply.

                That is not at all the use of these lines.

                Code:
                if (CurrentBar < 788)
                return;
                All this does is exit OnBarUpdate if the value of CurrentBar is still less than 788. CurrentBar will always be the bar number of the current bar on the chart.

                If you want to loop through bars in OBU, yes, you would need a loop in OBU.

                Please let us know if we may be of further assistance to you.
                Kate W.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by samish18, 04-17-2024, 08:57 AM
                16 responses
                55 views
                0 likes
                Last Post samish18  
                Started by arvidvanstaey, Today, 02:19 PM
                3 responses
                9 views
                0 likes
                Last Post NinjaTrader_Zachary  
                Started by jordanq2, Today, 03:10 PM
                2 responses
                8 views
                0 likes
                Last Post jordanq2  
                Started by traderqz, Today, 12:06 AM
                10 responses
                18 views
                0 likes
                Last Post traderqz  
                Started by algospoke, 04-17-2024, 06:40 PM
                5 responses
                47 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Working...
                X