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.
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.
I use mt4,can you show me the code,please.thank you.
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.
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); }
Not compiled, not tested, just typed.
Is it mql5 code?I only use Mql4. I got errors .
- iEnd is not passed by reference, so it is irrelevant whether the function modifies it or not to the caller. The const is unnecessary.
- 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.
- 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.
- 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.
- 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.
- Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
- MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
- Cloud Protector Bug? - MQL4 programming forum 2020.07.25
- iEnd is not passed by reference, so it is irrelevant whether the function modifies it or not to the caller. The const is unnecessary.
- 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.
- 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.
- isocpp.org
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.