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

substract from a list

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

    substract from a list

    Hello,

    I am trying to substract from a list in a series of number.


    for (int i = 0; i <= barIndex; i++)
    {
    if(average < highPrice && average > lowPrice)
    {
    barminus -- barIndex;
    Print(barminus);
    }
    }

    barIndex stay underline in red, i cant use -= it cumulate the substraction. I want to substract barminus from each number singly in the list. Any idea how to do that?

    I tried this but its not working

    char[] myArray = {barIndex};
    foreach(char ch in myArray)
    barminus -- myArray;
    Print(barminus);

    thank you
    Last edited by frankduc; 06-03-2019, 07:57 AM.

    #2
    Hello frankduc, thanks for your note.

    This code will subtract the given number from each element of the list:

    Code:
    int decrementBy = 1;
    List<int> xlist = new List<int>() {2, 4, 7, 35};
    
    for(int i = 0; i < xlist.Count; i++)
    {
        xlist[i] = xlist[i] - decrementBy;
    }
    xlist: {1, 3, 6, 34}

    The same could be done with xlist[i] -= decrementBy

    Please let me know if this does not resolve your inquiry.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Chris,

      Now that i have my list, if i want to divide each number of that list by the total amount of a series of bars.

      int decrementBy = barminus;
      List<int> xlist = new List<int>() {barIndex};

      for(int y = 0; y < xlist.Count; y++)
      {
      xlist[y] = (xlist[y] - decrementBy);

      Do i have to list my new list again to divide it by the total amount?

      id like to do this
      xlist[y] /
      someOtherInt

      someOtherInt = total number of bars in a serie
      Last edited by frankduc; 06-03-2019, 12:10 PM.

      Comment


        #4
        Hi frankduc, thanks for your reply.

        I'm not quite following what you want to do. barIndex looks like it is just one number, in that case, you would not need to put it into a list. I am guessing NT crashes because at some point barIndex = 0.

        What are you wanting to solve here? If you could share what you need to get solved, I could possibly offer a solution.

        I look forward to your reply.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          id like to do this


          xlist[y] /
          someOtherInt
          someOtherInt = total number of bars in a serie

          now that i have my list of each number of bars i want to divide it by the number of bars of a previous series. Total number bars = 1400
          but my list contain (100, 200, 400 etc) each one should be divided by 1400 alone.

          thats why im asking if i have to create a new list of modified bars. im not sure i explain myself correctly

          I thought if id do this
          xlist[y] = (xlist[y] - decrementBy) /
          someOtherInt

          ; it would automatically divide my modified list by 1400 bars.
          but its not returning the right answer.

          it returns 2,0,1,2 etc instead of %'s

          its should be returning 100/1400 = 0.07% , 200/1400 = 0.14%, etc

          barIndex = (
          100, 200, 400, 700, 800, 850, 901, etc) actually thats the new barindex after been xlisted!
          Last edited by frankduc; 06-03-2019, 12:51 PM.

          Comment


            #6
            Hi frankduc, thanks for your reply.

            CurrentBar will give you the bar number of the bar that is processing, it is the count of bars since the very leftmost bar of the chart.

            Here is a code snippet that will take the list and modify each element by what you describe:

            Code:
            protected override void OnBarUpdate()
                    {
                        if(CurrentBar < 1)
                            return;
            
                        int decrementBy = 1;
                        List<double> xlist = new List<double>() {2, 4, 7, 35};
            
                        for(int i = 0; i < xlist.Count; i++)
                        {
                            xlist[i] = (double)(xlist[i] - decrementBy)/CurrentBar;
                        }
            
                        //... rest of OnBarUpdate
                    }
            Please let me know if I can assist any further.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Sorry i didn't explain myself correctly.

              At first i wanted to substract a series of number by a single one. You gave me the code and it works. Here's the code :


              int barminus = Math.Abs(bar - ChartBars.ToIndex);

              for(int barIndex = ChartBars.GetBarIdxByX(chartControl, cursorPointX); barIndex <= ChartBars.ToIndex; barIndex++)
              {
              double closePrice = Bars.GetClose(barIndex);
              double highPrice = Bars.GetHigh(barIndex);
              double lowPrice = Bars.GetLow(barIndex);
              double volumes = Bars.GetVolume(barIndex);
              double average = ((newsum2+newsum)/newbar1);

              for (int i = 0; i <= barIndex; i++)
              {
              if(average < highPrice && average > lowPrice)
              {
              int decrementBy = barminus;
              List<int> xlist = new List<int>() {barIndex};

              for(int y = 0; y < xlist.Count; y++)
              {
              xlist[y] = (xlist[y] - decrementBy);
              Print(xlist[y]);
              }


              Now i want to divide
              xlist[y] / someOtherInt. This will divide my new list by

              double new262 = (bar/2.62);
              int someint = (int)new262;
              int someOtherInt = Convert.ToInt32(new262);

              Unfortunately, it does not produce the result i was expecting. Like i said it gives back a bunch of 0,2,1 instead of the %.
              it should be returning 100/1400 = 0.07% , 200/1400 = 0.14%, etc someOtherInt = 1400
              Thats why i was asking do i have to repeat the same process again?
              int decrementBy = someOtherInt.;
              List<int> xlist = new List<int>() {barIndex}; what about the rest? I am not sure.
              for(int y = 0; y < xlist.Count; y++)
              {
              xlist[y] = (xlist[y] - decrementBy);

              Comment


                #8
                Hi frankduc, thanks for your reply.

                It sounds by the fact that you are only getting whole numbers whatever is printing out is an integer and not double or float data type. Try casting the operation to a doubl e.g.

                double numberToPrint = (double)integerx/integery;
                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  i dont understand what you are suggesting. Where do i plug
                  double numberToPrint = (double)integerx/integery;
                  in the code.
                  But i dont know how it could be the problem cause everything is in int.

                  Comment


                    #10
                    Hi frankduc,

                    That code piece was just a suggestion of what might be wrong here; all code samples I have given should be taken as a suggestion and not literally what to add to the code. Why is barIndex being placed into a list? I am still having trouble understanding what is meant to be accomplished here. If you could elaborate further it would be appreciated.
                    Chris L.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello frankduc, thanks for your reply.

                      If you assign a number with a decimal portion to an int, then the entire decimal portion will be truncated and only the whole number will remain. Make sure that your zlist is a list of doubles and not integers. I also highly recommend using the Print method to print variable data to the NinjaScript output window, I use prints just about every day for debugging.

                      Kind regards.
                      Chris L.NinjaTrader Customer Service

                      Comment


                        #12
                        Thanks you were right it was a double issue.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by cmtjoancolmenero, Yesterday, 03:58 PM
                        4 responses
                        23 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by Brevo, Today, 01:45 AM
                        1 response
                        14 views
                        0 likes
                        Last Post NinjaTrader_ChelseaB  
                        Started by rjbtrade1, 11-30-2023, 04:38 PM
                        2 responses
                        74 views
                        0 likes
                        Last Post DavidHP
                        by DavidHP
                         
                        Started by suroot, 04-10-2017, 02:18 AM
                        5 responses
                        3,021 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Started by Stanfillirenfro, Today, 07:23 AM
                        1 response
                        7 views
                        0 likes
                        Last Post NinjaTrader_Gaby  
                        Working...
                        X