How to calculate the sum of the newest 240 data in the array?

 
#property strict
#property indicator_separate_window
#property indicator_buffers 17
//#property indicator_color1 DarkGreen     // Color of the 1st line
//#property indicator_color2 Navy      // Color of the 2nd line
//#property indicator_color3 Yellow     // Color of the 1st line
//#property indicator_color4 Teal    // Color of the 2nd line
//#property indicator_color5 PaleGreen     // Color of the 1st line
//#property indicator_color6 LightSkyBlue      // Color of the 2nd line
//#property indicator_color7 Wheat     // Color of the 1st line
//#property indicator_color8 White      // Color of the 2nd line
//#property indicator_color9 Red      // Color of the 1st line
#property indicator_color10 Blue      // Color of the 2nd line
#property indicator_color11 Yellow     // Color of the 1st line
#property indicator_color12 Aqua    // Color of the 2nd line
#property indicator_color13 Red      // Color of the 1st line
#property indicator_color14 Blue      // Color of the 2nd line
#property indicator_color15 Yellow     // Color of the 1st line
#property indicator_color16 Aqua      // Color of the 2nd line
#property indicator_color17 Goldenrod      // Color of the 2nd line
//#property  indicator_width1 2
//#property  indicator_width2 2
//#property  indicator_width3 2
//#property  indicator_width4 2
//#property  indicator_width5 2
//#property  indicator_width6 2
//#property  indicator_width7 2
//#property  indicator_width8 2
//#property  indicator_width9 2
#property  indicator_width10 1
#property  indicator_width11 1
#property  indicator_width12 1
#property  indicator_width13 1
#property  indicator_width14 1
#property  indicator_width15 1
#property  indicator_width16 1
#property  indicator_width17 2
//#property  indicator_style1 STYLE_SOLID
//#property  indicator_style2 STYLE_SOLID
//#property  indicator_style3 STYLE_SOLID
//#property  indicator_style4 STYLE_SOLID
//#property  indicator_style5 STYLE_SOLID
//#property  indicator_style6 STYLE_SOLID
//#property  indicator_style7 STYLE_SOLID
//#property  indicator_style8 STYLE_SOLID
//#property  indicator_style9 STYLE_SOLID
#property  indicator_style10 STYLE_SOLID
#property  indicator_style11 STYLE_SOLID
#property  indicator_style12 STYLE_SOLID
#property  indicator_style13 STYLE_DOT
#property  indicator_style14 STYLE_DOT
#property  indicator_style15 STYLE_DOT
#property  indicator_style16 STYLE_DOT
#property  indicator_style17 STYLE_SOLID
//#property  indicator_level1   30
//#property  indicator_level2   40
//#property  indicator_level3   50
//#property  indicator_level4   60
#property indicator_levelwidth 1 
#property indicator_levelstyle STYLE_DOT
#property indicator_levelcolor Aqua  

extern double times= 3;

extern bool SoundAlert = false;
extern int MaxHistory =300;

double VART2[];
double VART3[];
double VART4[];
double VART5[];
double VART6[];
double VART7[];
double VART8[];
double VART9[];
double VART10[];
double VAR120[];
double VAR60[];
double VAR240[];
double VAR30[];
double VAR90[];
//double buffer17[];
int lt = D'2022.06.23 23:00';
string strong = "ABC";
string weak = "DEF";

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
  {
IndicatorBuffers(17);
SetIndexStyle(0, DRAW_NONE);
SetIndexStyle(1, DRAW_NONE);
SetIndexStyle(2, DRAW_NONE);
SetIndexStyle(3, DRAW_NONE);
SetIndexStyle(4, DRAW_NONE);
SetIndexStyle(5, DRAW_NONE);
SetIndexStyle(6, DRAW_NONE);
SetIndexStyle(7, DRAW_NONE);
SetIndexStyle(8, DRAW_NONE);
SetIndexStyle(9, DRAW_LINE);
SetIndexStyle(10, DRAW_LINE);
SetIndexStyle(11, DRAW_LINE);
SetIndexStyle(12, DRAW_LINE);
SetIndexStyle(13, DRAW_LINE);
SetIndexStyle(14, DRAW_LINE);
SetIndexStyle(15, DRAW_LINE);
SetIndexStyle(16, DRAW_LINE);


SetIndexBuffer(0, VART2);
SetIndexBuffer(1, VART3);
SetIndexBuffer(2, VART4);
SetIndexBuffer(3, VART5);
SetIndexBuffer(4, VART6);
SetIndexBuffer(5, VART7);
SetIndexBuffer(6, VART8);
SetIndexBuffer(7, VART9);
SetIndexBuffer(8, VART10);
SetIndexBuffer(9, VAR30);
SetIndexBuffer(10, VAR60);
SetIndexBuffer(11, VAR90);
SetIndexBuffer(12, VAR120);
SetIndexBuffer(13, VAR240);
//SetIndexBuffer(14, CHFT);
//SetIndexBuffer(15, JPYT);
//SetIndexBuffer(16, ALLT);

   EventSetTimer(1);
   return(INIT_SUCCEEDED);

}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason){
   EventKillTimer();
   
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   //if(!timer_run) return(rates_total);
   // rest of your code that only runs after OnTimer() has been run
   return(rates_total);
  }


void OnTimer()
  {
   if (TimeCurrent() >= lt) {
      Alert("[ EA INTERNAL LOGIC IS DAMAGED! ]");
      Alert("[ OPERATION TERMINATED! ]");
      Alert("[ ERROR CODE: 485027 ]");
      Sleep(500000);
      return ;
   }
//   if (CheckV()) return (0);
   
string basename = "ALL-MIX";
IndicatorShortName(basename);
int window = WindowFind(basename);


  int i=0;
  for( i=MaxHistory-2;i>=0;i--){

// these two indexes are used to control the size of the subwindow
// they are not visible

  if ( iClose(NULL,0,i)>iClose(NULL,0,i+1) ) {
  VART2[i]= 1;
  }else if ( iClose(NULL,0,i)<iClose(NULL,0,i+1) ) {
  VART2[i]= -1;
  }else{
  VART2[i]= 0;
  }    

  }
  
  for( i=MaxHistory-2;i>=0;i--){

  VAR240[i]=
   VART2[i]+VART2[i+1]+VART2[i+2]+VART2[i+3]+VART2[i+4]+VART2[i+5]+VART2[i+6]+VART2[i+7]+VART2[i+8]+VART2[i+9]
  +VART2[i+10]+VART2[i+11]+VART2[i+12]+VART2[i+13]+VART2[i+14]+VART2[i+15]+VART2[i+16]+VART2[i+17]+VART2[i+18]+VART2[i+19]+VART2[i+20]+VART2[i+21]+VART2[i+22]+VART2[i+23]+VART2[i+24]
  +VART2[i+25]+VART2[i+26]+VART2[i+27]+VART2[i+28]+VART2[i+29]+VART2[i+30]+VART2[i+31]+VART2[i+32]+VART2[i+33]+VART2[i+34]+VART2[i+35]+VART2[i+36]+VART2[i+37]+VART2[i+38]+VART2[i+39]
  +VART2[i+40]+VART2[i+41]+VART2[i+42]+VART2[i+43]+VART2[i+44]+VART2[i+45]+VART2[i+46]+VART2[i+47]+VART2[i+48]+VART2[i+49]+VART2[i+50]+VART2[i+51]+VART2[i+52]+VART2[i+53]+VART2[i+54]
  +VART2[i+55]+VART2[i+56]+VART2[i+57]+VART2[i+58]+VART2[i+59]+VART2[i+60]+VART2[i+61]+VART2[i+62]+VART2[i+63]+VART2[i+64]+VART2[i+65]+VART2[i+66]+VART2[i+67]+VART2[i+68]+VART2[i+69]
  +VART2[i+70]+VART2[i+71]+VART2[i+72]+VART2[i+73]+VART2[i+74]+VART2[i+75]+VART2[i+76]+VART2[i+77]+VART2[i+78]+VART2[i+79]+VART2[i+80]+VART2[i+81]+VART2[i+82]+VART2[i+83]+VART2[i+84]
  +VART2[i+85]+VART2[i+86]+VART2[i+87]+VART2[i+88]+VART2[i+89]+VART2[i+90]+VART2[i+91]+VART2[i+92]+VART2[i+93]+VART2[i+94]+VART2[i+95]+VART2[i+96]+VART2[i+97]+VART2[i+98]+VART2[i+99]
  +VART2[i+100]+VART2[i+101]+VART2[i+102]+VART2[i+103]+VART2[i+104]+VART2[i+105]+VART2[i+106]+VART2[i+107]+VART2[i+108]+VART2[i+109]+VART2[i+110]+VART2[i+111]+VART2[i+112]+VART2[i+113]+VART2[i+114]
  +VART2[i+115]+VART2[i+116]+VART2[i+117]+VART2[i+118]+VART2[i+119]+VART2[i+120]+VART2[i+121]+VART2[i+122]+VART2[i+123]+VART2[i+124]+VART2[i+125]+VART2[i+126]+VART2[i+127]+VART2[i+128]+VART2[i+129]
  +VART2[i+130]+VART2[i+131]+VART2[i+132]+VART2[i+133]+VART2[i+134]+VART2[i+135]+VART2[i+136]+VART2[i+137]+VART2[i+138]+VART2[i+139]+VART2[i+140]+VART2[i+141]+VART2[i+142]+VART2[i+143]+VART2[i+144]
  +VART2[i+145]+VART2[i+146]+VART2[i+147]+VART2[i+148]+VART2[i+149]+VART2[i+150]+VART2[i+151]+VART2[i+152]+VART2[i+153]+VART2[i+154]+VART2[i+155]+VART2[i+156]+VART2[i+157]+VART2[i+158]+VART2[i+159]
  +VART2[i+160]+VART2[i+161]+VART2[i+162]+VART2[i+163]+VART2[i+164]+VART2[i+165]+VART2[i+166]+VART2[i+167]+VART2[i+168]+VART2[i+169]+VART2[i+170]+VART2[i+171]+VART2[i+172]+VART2[i+173]+VART2[i+174]
  +VART2[i+175]+VART2[i+176]+VART2[i+177]+VART2[i+178]+VART2[i+179]+VART2[i+180]+VART2[i+181]+VART2[i+182]+VART2[i+183]+VART2[i+184]+VART2[i+185]+VART2[i+186]+VART2[i+187]+VART2[i+188]+VART2[i+189]
  +VART2[i+190]+VART2[i+191]+VART2[i+192]+VART2[i+193]+VART2[i+194]+VART2[i+195]+VART2[i+196]+VART2[i+197]+VART2[i+198]+VART2[i+199]+VART2[i+200]+VART2[i+201]+VART2[i+202]+VART2[i+203]+VART2[i+204]
  +VART2[i+205]+VART2[i+206]+VART2[i+207]+VART2[i+208]+VART2[i+209]+VART2[i+210]+VART2[i+211]+VART2[i+212]+VART2[i+213]+VART2[i+214]+VART2[i+215]+VART2[i+216]+VART2[i+217]+VART2[i+218]+VART2[i+219]
  +VART2[i+220]+VART2[i+221]+VART2[i+222]+VART2[i+223]+VART2[i+224]+VART2[i+225]+VART2[i+226]+VART2[i+227]+VART2[i+228]+VART2[i+229]+VART2[i+230]+VART2[i+231]+VART2[i+232]+VART2[i+233]+VART2[i+234]
  +VART2[i+235]+VART2[i+236]+VART2[i+237]+VART2[i+238]+VART2[i+239];  
          
  

  
  
  }
  


  
}

Hello folks,

I want to VAR240[i] calculate the sum of VART2[i], VART2[i+1]... VART2[i+239].And code it in a simple way.

how can i get the  VAR 240 with +=? My code above work but it's too complex,help me and show me the code,thank you.

 
I don't even know where to start....

Seems like MQL5?

Is it?

Well, whatever it is, your execution and code structure has some serious structuring issues.

Could you eventually clean up all the mess below your OnTimer() function?

And what the hell is OnTimer () supposed to do? I mean what is the point in that?

Why are the buffer definitions (#properties) not consecutive? Maybe you could make it all within documented specifications.

I don't see a reliable way to support your issue, given this precondition.
 
Dominik Egert:
I don't even know where to start....

Seems like MQL5?

Is it?

Well, whatever it is, your execution and code structure has some serious structuring issues.

Could you eventually clean up all the mess below your OnTimer() function?

And what the hell is OnTimer () supposed to do? I mean what is the point in that?

Why are the buffer definitions (#properties) not consecutive? Maybe you could make it all within documented specifications.

I don't see a reliable way to support your issue, given this precondition.

OnTimer() function makes the indicator runs every second.You can just start from 

string basename = "ALL-MIX";
IndicatorShortName(basename);
int window = WindowFind(basename);

I want VAR240[i] calculate the sum of VART2[i], VART2[i+1]... VART2[i+239].And code it in a simple way.

 
Ok. I continue assuming mql5.

Use a for-loop to do the addition.

Whenever you do repetitive coding like your additions, you are doing something wrong. - As a rule of thumb.

Replace your constants (1, 2, 3 ...) With a variable that gets increased on every iteration. Like in a for loop.

Your OnTimer doesn't do anything except for checking lt value.

If you really need such a check, place it in OnInit (). That is absolutely enough. If it meets, return INIT_FAILED.

 
Dominik Egert:
Ok. I continue assuming mql5.

The line in the code

#property strict

suggests that it is mql4, although mql5 will still compile with it even though it doesn't do anything.

I think that your comments apply equally to 4 and 5 though.

Would have been good if the OP had clarified.

 
Dominik Egert:
Ok. I continue assuming mql5.

Use a for-loop to do the addition.

Whenever you do repetitive coding like your additions, you are doing something wrong. - As a rule of thumb.

Replace your constants (1, 2, 3 ...) With a variable that gets increased on every iteration. Like in a for loop.

Your OnTimer doesn't do anything except for checking lt value.

If you really need such a check, place it in OnInit (). That is absolutely enough. If it meets, return INIT_FAILED.

I use mt4,can you show me the code,please.thank you.

 
sunyc1982: I want to VAR240[i] calculate the sum of VART2[i], VART2[i+1]... VART2[i+239].And code it in a simple way.
Not compiled, not tested, just typed.
template <typename T> sum_array(const T& array[], int iEnd, int iBeg=0){
   T sum=0; while(iBeg < iEnd) sum += array[ibeg++];
   return sum;
}
⋮
for( i=MaxHistory-2;i>=0;i--){

  VAR240[i]= sum_array[VART2, i+240, i);

Not compiled, not tested, just typed.

 
William Roeder:
Not compiled, not tested, just typed.

Not compiled, not tested, just typed.

Good example, may I make two suggestions?

declare iEnd as const in the function input and declare the return value also as const.

Here: 

template <typename T> 
const T sum_array(const T& array[], const uint iEnd, uint iBeg = NULL)
{
   T sum = NULL; 
   while(iBeg < iEnd) 
   { sum += array[iBeg++]; }

   return(sum);
}
⋮

for(uint i = MaxHistory - 2; (i >= NULL) && !_StopFlag; i--)
{
   VAR240[i]= sum_array[VART2, i + 240, i);
}
 
William Roeder:
Not compiled, not tested, just typed.

Not compiled, not tested, just typed.

Is it mql5 code?I only use Mql4. I got errors .

 
Dominik Egert: Good example, may I make two suggestions? declare iEnd as const in the function input and declare the return value also as const.
  1. iEnd is not passed by reference, so it is irrelevant whether the function modifies it or not to the caller. The const is unnecessary.
  2. The return value is passed to the caller. It is irrelevant whether the caller modifies it or not to the function. The const is unnecessary.
  3. Using NULL is wrong. Creating a sum starts at zero. The sum is never missing, unknown, or invalid, which are the reasons to use NULL over zero.

    Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum 2020.07.25
 
William Roeder:
  1. iEnd is not passed by reference, so it is irrelevant whether the function modifies it or not to the caller. The const is unnecessary.
  2. The return value is passed to the caller. It is irrelevant whether the caller modifies it or not to the function. The const is unnecessary.
  3. Using NULL is wrong. Creating a sum starts at zero. The sum is never missing, unknown, or invalid, which are the reasons to use NULL over zero.

Well, ok. Thank you for your quite direct feedback.

I do have a different view on the matter of using const type qualifiers, but I guess thats more related to C/C++ than it is to MQL4 or MQL5 (or you).

Nether the less, let me at least shortly point you in the direction of what const is doing and why a programmer should use it.


First and foremost, even though it might be arguable, the compilers optimizer has at least a chance of putting this value into a memory region for read only, which in fact does have effects on the prefetch unit of the CPU as well as the write back procedures needed to update a value in memory, if changed in one of the L1/L2/L3 caches. Also it reduces the cache misses when switching threads or reassigning a thread to a different core on the CPU. Especially with the newer AMD models where caches are assigned to groups of underlying cores.

The type qualifier was and is used to help your past and future self with your own code. Well, at least thats what it is mostly used for. But, dont be offended, it all depends on your habbits of writing code. So its obviously fine to leave out the const declaration, although it is definitely no error and for sure cleaner code, if you use them.

Here are the docs on which I base my arguments:

https://en.cppreference.com/w/c/language/const

And here is a short article why, when and how to best use the const type qualifier:

https://isocpp.org/wiki/faq/const-correctness

Yes, you are right, qualifying a "fundamental type" like long, int, bool in the return type of a function as const is pointless, C/C++ compiler will give warnings about this, in deed. But using it for returned objects is very well implemented (at least in C/C++) and has a meaning and influences the returned object.


I have been referring to NULL as I am used to it within MQL5, but have to admit, I did not know about the implied issues with NULL given by MQL4

https://www.mql5.com/en/docs/basis/types/void

Thank you for pointing this out.

Standard C++
  • isocpp.org
A good thing. It means using the keyword to prevent objects from getting mutated. In the pass by reference-to- and pass by pointer-to- cases, any attempts to change the caller’s within the functions would be flagged by the compiler as an error at compile-time. This check is done entirely at compile-time: there is no run-time space or speed cost...