How to make an iteration of cumulative numbers?

FranK Duc 121 Reputation points
2021-02-16T14:09:38.113+00:00

Hello,

I want to cumulate numbers.

Something like:

double x = 195;
double cum = 195 + x;

The result should look like : 194,389,584,779,974,1169,1364...

I want to be able to stop the cumulation at any step i wish 1169 or 779 for example.

TY

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,306 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.5K Reputation points
    2021-02-16T14:36:09.227+00:00

    Maybe like this:

    int first = 194;
    int step = 195;
    int max = 779;
    
    for( int value = first; value <= max; value += step )
    {
       Console.WriteLine( value );
    }
    

    It is possible to show the results in a single line, separated by ','.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. FranK Duc 121 Reputation points
    2021-02-16T15:00:39.303+00:00

    Hi Viorel,

    Maybe you could tell me why my code not returning what i want.

     int firs = 194;
    
     int step = 195;
     int max = 1364;
    
     for( int value = firs; value <= max; value += step )
                 {
           Print("V"+ value );
    
    
              var listW = new List<int>() {value};
    
                foreach(var item in listW)
                        {
                    closePrice16 = Bars.GetClose(item);
    
                 Print("CP"+closePrice16);
                    }
    
    
        int firs0 = 195;
             int step0 = 195;
         int max0 = 1365;
    
             for( int valu = firs0; valu <= max0; valu += step0 )
                 {
                   Print("Val"+ valu );
    
    
    
                  var listQ = new List<int>() {valu};
    
                foreach(var item in listQ)
                        {
                    closePrice6 = Bars.GetClose(item);
    
                 Print("CQ"+closePrice6);
                    }
    
    
                var listMP = listW.Zip(listQ, (first, second) => first - second);    // listMP is returning a bunch of -1 ?
    
                                foreach(var item in listMP)
                        {
                            Print("PM"+item);
                        }
    
                foreach(var item in listW)
                        {
    
    
    
                    if(CurrentBar < item) return;       // returning a value seperatly for each item in listW
    
                    foreach(var iten in listMP)
                        {
    
    
                  candle = Close[0] + iten;                        // I want to apply the calculation over the same list of price.
                            Print("CDL"+candle);
                        }
                          } 
    
    
    
                 }
    
    
                 }
    

    For example, now it should be returning 10,11,16,
    9, 14, 17

    Suppose listMP would be returning the right answer like +2, -1, +3

    candle should return one list: 12, 10, 19, 11, 13, 20

    From 195 to 1364 it apply +2
    than from 389 to 1364 it apply -1 over the first one
    than from 584 to 1364 it apply +3 over the second one

    its always the same list it just it apply the first calculation from 195 than at 389 it apply the second one .etc

    TY


  2. FranK Duc 121 Reputation points
    2021-02-16T18:19:07.293+00:00

    You have the first part from line 1 to line 38. Every 195 bars it extract the closeprice and return for example 5, 8, 11 for the part between line 1 and 19 and from line 20 to 38 it would return 6, 7, 10. To be clear bar 194 return closeprice 5 when bar 195 will return closeprice 6. Bar 389 return closeprice 8 and bar 390 return closeprice 7.

    After that from line 40 to 45 it substract closeprice from the first part and second part. So 5- 6 = -1, 8- 7 = 1, 11- 10 = 1. That part should return in value -1 , 1, 1 but for a reason i dont get it always return -1.

    Line 52 if(CurrentBar < item) return; it lines up every closeprice starting bar 194, 389, 584 ...

    The output looks like this:

    194: 5,8,11
    389: 8,15,17
    584: 11, 22, 17

    line 58 add the result from line 40

    194: 5+ (-1) = 4 , 8 +(-1) = 7, 11 +(-1) = 10
    389: 8 + 1 = 9 , 15 (+1) = 16, ....
    584: well you get the idea

    Actually what i want is from 194 till we reach max 1364 sum (-1) and once it reach 389 it sum (1) till it reach max, than 584 it sum (1) over the first two till it reach max, etc.: producing the result of one serie.

    4, 7, 10, 9, 16, 12, 23, 18

    The original code looks like this:

    if(CurrentBar < 1) return;

                  double    closePri = Bars.GetClose(0);
                  double    closePri9 = Bars.GetOpen(0);
    
                   difCPP = closePri - closePri9;
    
                  candle = Close[0] + difCPP + difCP + difCP00 + difCP000 + difCP0 + difCP001 + difCP1 + difCP003 + difCP2 + difCP002 + difCP3 +difCP004 +difCP4 +difCP5 +difCP6 +difCP7 +difCP8 + difCP9 +difCP10 +difCP11 + difCP12 +difCP13;
    
                  listCan.Add(candle);
                  Print("Can"+candle);
    
                   if(CurrentBar < 77) return;
    
                  double    closePrice16 = Bars.GetClose(77);
                  double    closePrice9 = Bars.GetOpen(78);
    
                  difCP = closePrice16 - closePrice9;
             // and it goes on
    

    I try to find a way not to repeat the code again and again especially that part if(CurrentBar < 77) return;

    TY

    0 comments No comments