New article LifeHack for traders: Blending ForEach with defines (#define) has been published:
Author: webmasterfa
(i,true)
#define ForEachSymbol(s,i) string s=SymbolName(0,true); int os_total=SymbolsTotal(true); for(int i=1;i<os_total;i++,s=SymbolName(i,true))
There is a bug, first symbol process is with position_index 0, next one is with position_index 2. You are missing position_index 1 and your loop
is executed only os_total-1 times.
#define ForEachOrder(ticket,i) HistorySelect(0,TimeCurrent()); ulong ticket=OrderGetTicket(0); int or_total=OrdersTotal(); for(int i=1;i<or_total;i++,ticket=OrderGetTicket(i)) //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- ForEachOrder(orderticket,index) { Print(index,": #",orderticket," ",OrderGetString(ORDER_SYMBOL)," ", EnumToString((ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE))); } /* Sample output 1: 13965457 CADJPY ORDER_TYPE_SELL_LIMIT 2: 14246567 AUDNZD ORDER_TYPE_SELL_LIMIT */ }
In this one there is the same bug as previously.
And additionally you are mixing functions to work on open orders and history selection. If your intend was to work with open orders, there is no
need to use HistorySelect().
By the way these bugs demonstrate well the problem with without macro. It's hard or impossible to debug.
The most strongest criticism of using #define is the fact that macro substitutions do not allow for code debugging. I agree with this, although, as fxsaber says, "A reliably fixed patient requires no anesthesia debugged macro requires no debugging".
I stopped reading after that, sorry.
New article LifeHack for traders: Blending ForEach with defines (#define) has been published:
Author: Vladimir Karputov
To Vladimir Karputov,
Thanks for your delicated illustaration of macros!
Learned a lot from this article!

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
New article LifeHack for traders: Blending ForEach with defines (#define) has been published:
The article is an intermediate step for those who still writes in MQL4 and has no desire to switch to MQL5. We continue to search for opportunities to write code in MQL4 style. This time, we will look into the macro substitution of the #define preprocessor.
Creating Expert Advisors almost always entails a lot of work with loops. Loops surround us everywhere: searching orders, trades in history, chart objects, Market Watch symbols, bars in an indicator buffer. To make a programmer's life a bit easier, MetaEditor features snippets meaning that when you enter the first characters, they automatically turn into a small piece of code after pressing Tab. This is how the 'for' loop snippet works:
Author: Vladimir Karputov