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

sum bars

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

    sum bars

    How do we SUM bars.

    Try to sum bars in a barIndex but is there a sumOfBars?
    There's a sumOfVolumes += Bars.GetVolume(barIndex);

    Is there a sumOfBars += ?

    Want to sum bars inside that if ;

    if(closePrice > cma)
    {
    // Formulas


    sumOfVolumesx += Bars.GetVolume(barIndex);
    sum += barIndex;
    {
    Print(sum);
    }
    }
    Last edited by frankduc; 06-06-2019, 12:18 PM.

    #2
    Hello frankduc,

    It looks like this question relates to the post you started previously: https://ninjatrader.com/support/foru...a-series/page4

    The variable names you are using here such as sumOfVolumes are unique to the script you are making, this is not a NinjaScript property. If you wanted a sumOfBars you will need to make that and then write the logic to populate the value.

    In the other thread, we had covered doing an accumulation over a set of bars which seems to be similar to what you are asking here, if you wanted a SUM you would follow the same process and use math to make a SUM of whatever value you need. Any variables you need to do that calculation, you would need to make while you create your calculation. Because you are not using OnBarUpdate for the processing, you cannot use any existing system indicators like SUM for this purpose, you will need to do the calculations yourself just like we covered in the other post.

    Please let me know if I may be of further assistance.
    Last edited by NinjaTrader_Jesse; 06-06-2019, 12:42 PM.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Sorry i was under the impression you wanted me to start a new thread because the last one was too long.
      Explain me why you can do sumOfVolumesx += Bars.GetVolume(barIndex); but there is no sumOfBars += Bars.GetBars(barIndex);

      and
      Bars.GetBars dont relate to bars but time so it cant be use. NT has strange ways to do things sometimes. Why not create Bars.GetTime instead and a Bars.GetBars, it would be more logical. So if i want my sum of bars inside the If i will have to enter another For logic?



      If sumofbars is not NT property where does it come from? Josh came with that script i thought it could be use i n any case.

      double sum =0;
      int barIndex1 = 0;
      double sumOfVolumesx = 0;


      for(int barIndex = ChartBars.GetBarIdxByX(chartControl, cursorPointX) - someOtherInt; barIndex <= ChartBars.GetBarIdxByX(chartControl, cursorPointX); barIndex++)
      {
      double closePrice = Bars.GetClose(barIndex);
      double volumes = Bars.GetVolume(barIndex);

      sum += closePrice;

      barIndex1++;

      double cma = sum / barIndex1;


      if(closePrice > cma)
      {



      sumOfVolumesx += Bars.GetVolume(barIndex);

      }
      Last edited by frankduc; 06-06-2019, 01:52 PM.

      Comment


        #4
        Hello frankduc,

        The post about creating a new thread was in reference to your Fibonacci question which was not related to what we were talking about in that thread. This question is related to the other thread so it would be fine to continue talking about this question in that thread. For any new questions which are not related to the thread you are in, please a start a new thread for that new question/topic. This just helps keep the information together so the thread makes some sense to others who read it.

        Explain me why you can do sumOfVolumesx += Bars.GetVolume(barIndex); but there is no sumOfBars += Bars.GetBars(barIndex);
        GetBars is not a documented method, are you trying to get a price? What value are you trying to retrieve with this method?

        If you are unsure about what something does, please use the help guide to look it up. Searching for "GetBars" does not return a result for NinjaScript so this should not be used in your script. There are internal methods which may show up in intelleprompt however if it is not documented in the help guide it would not be suggested to be used.

        Code:
        So if i want my sum of bars inside the If i will have to enter another For logic?
        Depending on what you want to do maybe, you are doing a custom loop over certain bars. All calculations will need to be your own at this point. If you wanted to do a sum of some value in the loop, its very likely going to be the same process as the accumulation where you loop and calculate. Very likely the same loop can be used for more than one purpose.

        If sumofbars is not NT property where does it come from? Josh came with that script i thought it could be use i n any case.
        sumofbars does not exist in what you provided, did you mean sumOfVolumesx? sumOfVolumesx is defined in what you provided as a double variable:

        Code:
        double sumOfVolumesx = 0;
        If this part of the script is currently confusing to you, I would suggest reviewing some general C# variable tutorials as that would help understanding in this area of your script. A variable is defined in the script and can later have a value set to it, or have a value retrieved from it. This is not specifically something our support can provide C# education for however you can find many C# tutorials going over everything from variable use to for loops online. Here is one public link which lightly explains variable and scope: https://csharp.net-tutorials.com/basics/variables/


        Please let me know if I may be of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          I am sorry, again i didn't explain myself correctly. I mistake number of bars from bars position. If i sum bars above the CMA it will sum the value of each bars in terms of position like bar 3200, 3201 etc.

          What i meant is to count the number of bars above the CMA. In that matter i think i have seen in the tutorial a bar.count method, am i right?
          I got to figure out how to count the number of bars above:

          I tried with count and chartcount but it gives me the entire number of bars in the chart. Is there anyway we can count the number of bars above the CMA using count?
          if(closePrice > cma)

          I discover that if i print(barIndex1) it returns all the bars above the cma but it does not give me the count.

          Thank you
          Last edited by frankduc; 06-07-2019, 07:54 AM.

          Comment


            #6
            Hello frankduc,

            Thanks for your reply.

            If all you want to do is count bars when a condition is true then you can use an int variable as a counter.

            private int myCounter;

            in OnBarUpdate():

            if (closePrice > cma)
            {
            myCounter++; // increment counter (Note this would only work with Calculate.OnBarClose or placed under IsFirstTickOfBar condition if not using Calculate.OnBarClose.
            }

            Print ("Number of bars above: "+myCounter); // print to ninjascript output window

            Paul H.NinjaTrader Customer Service

            Comment


              #7
              I dont think its going to work. I have tried and i get error messages. I use OnRender.

              I tried with
              IEnumerable<int> longs = newList<long> {barIndex1};
              intresult = longs.Count();

              Print(result)

              But it only returns 1's.
              Last edited by frankduc; 06-07-2019, 10:08 AM.

              Comment


                #8
                Hello frankduc,

                Thanks for your reply.

                I'm not sure how that relates to what I suggested.

                If you are doing this in OnRender() then I assume you are only wanting to work with the visible bars in the chart.

                You would need to index through all the bars in the chart and test on each bar for the condition (closePrice > cma), then increment the simple integer counter when true. You can use the example shown in the help guide here to loop through the bars: https://ninjatrader.com/support/help..._fromindex.htm






                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  If i do what you say i only get the list of bars traded above the CMA in the output window.
                  I know there's 34 bars above the CMA. I want the output window to give me the total number of the 34 bars in the output window.
                  The OW should show 34.
                  Attached Files

                  Comment


                    #10
                    Hello frankduc,

                    Thanks for your reply.

                    Are you resetting the integer counter before counting the bars that meet the condition?

                    Please keep in mind that if you would like your indicator created for you, we can provide references to 3rd party programmers.
                    Paul H.NinjaTrader Customer Service

                    Comment


                      #11
                      That's the code:

                      double sum =0;
                      int barIndex1 = 0;
                      double sumOfVolumesx = 0;
                      double count = 0;

                      for(int barIndex = ChartBars.GetBarIdxByX(chartControl, cursorPointX) - someOtherInt; barIndex <= ChartBars.GetBarIdxByX(chartControl, cursorPointX); barIndex++)
                      {
                      double closePrice = Bars.GetClose(barIndex);
                      double volumes = Bars.GetVolume(barIndex);

                      sum += closePrice;

                      barIndex1++;

                      double cma = sum / barIndex1;


                      if(closePrice > cma)
                      {
                      sumOfVolumesx += Bars.GetVolume(barIndex);

                      count = barIndex1;


                      {
                      Print(count);

                      Most third party's in the ecosystem list cant deliver what i want. They cant even make the difference between a VWAP and a CMA. I will learn c# and do the job myself. I dont know why i am stuck on that now that i have half the job done.

                      Comment


                        #12
                        Hello frankduc,

                        Thanks for your reply.

                        You are indexing barIndex on every bar in the chart and then assigning the number to count and then printing count.

                        You only want to increment the bar counter when closePrice > cma) in order to report the actual number of chart bars that meet that criteria. and then you would only Print when the loop has finished.

                        Pseudo code::

                        reset counter to 0
                        loop through chart bars
                        if ( closePrice > cma) increment the counter
                        end loop
                        Print counter
                        Paul H.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks for the help, i think it worked. I am not sure what you meant by end loop. Did you mean break?




                          double sum =0;
                          int barIndex1 = 0;
                          double sumOfVolumesx = 0;
                          int counter = 0;

                          for(int barIndex = ChartBars.GetBarIdxByX(chartControl, cursorPointX) - someOtherInt; barIndex <= ChartBars.GetBarIdxByX(chartControl, cursorPointX); barIndex++)
                          {
                          double closePrice = Bars.GetClose(barIndex);
                          double volumes = Bars.GetVolume(barIndex);

                          sum += closePrice;

                          barIndex1++;

                          double cma = sum / barIndex1;



                          if(closePrice > cma)
                          {


                          counter++;


                          sumOfVolumesx += Bars.GetVolume(barIndex);




                          Print(counter);


                          }
                          Last edited by frankduc; 06-08-2019, 08:25 PM.

                          Comment


                            #14
                            Hello frankduc,

                            I believe in the last sample the "end loop" is simply used to demonstrate where the end of the loops body is, or its closing brace }.

                            The print would be outside your loop in what Paul provided:
                            Code:
                            for()
                            {
                            
                            }
                            Print(...)
                            If you want to print the counter value each iteration of the loop, it would go inside the loop otherwise if you want to print the accumulated value that needs to be after your loop completes.

                            Please let me know if I may be of further assistance.
                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              good info
                              ty

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by wzgy0920, 04-20-2024, 06:09 PM
                              2 responses
                              26 views
                              0 likes
                              Last Post wzgy0920  
                              Started by wzgy0920, 02-22-2024, 01:11 AM
                              5 responses
                              32 views
                              0 likes
                              Last Post wzgy0920  
                              Started by wzgy0920, Yesterday, 09:53 PM
                              2 responses
                              49 views
                              0 likes
                              Last Post wzgy0920  
                              Started by Kensonprib, 04-28-2021, 10:11 AM
                              5 responses
                              192 views
                              0 likes
                              Last Post Hasadafa  
                              Started by GussJ, 03-04-2020, 03:11 PM
                              11 responses
                              3,234 views
                              0 likes
                              Last Post xiinteractive  
                              Working...
                              X