# Get the sum of the Close on the same Days of the Year

Hello,

There are 365 days in one Year,
and I want to get the sum of the Close on the same Days of the Year.

for example, there are bars of 3 years, the Close of
the 5th Day of 1st Year is 10001,
the 5th Day of 2nd Year is 10002,
the 5th Day of 3rd Year is 10003,
then the result should be Sum_of_dayValue = 30006;

``````doy = DayOfYear();   // doy = {5, 6, 7, 8, 9, 12, 13, ..., 365, 4, 5, ... }
Sum_of_dayValue = 0; // Sum of the Close on the same Days of the year
for( i = 0; i < BarCount; i++ )
{
Sum_of_dayValue[doy[i]] += C[i];
// Error 10. Array subscript out of range. You must not access array elements outside 0.. (BarCount-1) range.
//           You attempted to access non-exsisting 289-th element of array.
}
``````

Subscript expect bar indexes (0 to BarCount-1) but not return of DayofYear. So what you do is incorrect.

Also to get output of upper example quote all you need is two lines of code and just array processing.
(To disable QuickAFL you may have to use SetBarsRequired().)

``````SetBarsRequired(1000);
doy = DayOfYear();   // doy = {5, 6, 7, 8, 9, 12, 13, ..., 365, 4, 5, ... }
Sum_of_dayValue = Cum(IIf(doy==5,C,0)); // Sum of the Close on the same Days of the year
``````

BTW read here for further help in future

3 Likes