How to count how many currency pair(s) running with active order(s)
- Start with an empty array of symbols.
- For each order, find the symbol in your array of symbols. If you don't find it, it's a new symbol, add it to the array.
- After the loop, the answer is the size of the array.
- Start with an empty array of symbols.
- For each order, find the symbol in your array of symbols. If you don't find it, it's a new symbol, add it to the array.
- After the loop, the answer is the size of the array.
Can you show me an example please?
No free help
urgent help.
Show us your attempt (using CODE button) and state the nature of your problem.
No free help
urgent help.
int running_symbol_total = 0; if(OrdersTotal() > 0) { for(int i = 0; i <= OrdersTotal()-1; i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == true) { if(OrderSymbol() != NULL && OrderMagicNumber() == magic_number) { running_symbol_total = ++running_symbol_total; } } } } Alert(running_symbol_total);
But this results me equal of OrdersTotal();
But this results me equal of OrdersTotal();
if you want to get the open Positions,then use PositionsTotal,Order and position are different.
There is no such positions total in MT4.
if(OrdersTotal() > 0){ for(int i = 0; i <= OrdersTotal()-1; i++)
The if is redundant. If total is zero the for loop does nothing.Simplify your condition: i <= n-1 is the same as i < n.
running_symbol_total = ++running_symbol_total;
V = V is redundant. Simplify your code: ++running_symbol_total;- Of course your code just equals OrdersTotal. I gave you the answer at #1. Where is your code that does that.
The easiest way is to use a set collection....
#include <Arrays\ArrayString.mqh> class StringSet : public CArrayString { public: bool Add(const string element) { if(SearchLinear(element) < 0) return CArrayString::Add(element); return false; } string ToString() { Sort(); string res = "["; for(int i=0;i<Total();i++) res+= this[i]+(i==Total()-1?"":", "); return res + "]"; } }; void OnStart() { StringSet sym_cnt; for(int i=OrdersTotal()-1;i>=0;i--) if(OrderSelect(i,SELECT_BY_POS) && sym_cnt.Add(OrderSymbol())) printf("Adding original symbol %s to set",OrderSymbol()); printf("There are %d unique symbols in the set %s", sym_cnt.Total(), sym_cnt.ToString()); }
The easiest way is to use a set collection....
Faster
int GetSymbols( string &Symbols[] ) { int Res = 0; for (int i = ArrayResize(Symbols, OrdersTotal()) - 1; i >= 0; i--) if (OrderSelect(i, SELECT_BY_POS)) Symbols[Res++] = OrderSymbol(); return(ArrayResize(Symbols, Res)); } // Возвращает количество уникальных элементов в массиве template <typename T> int GetUniqueElements( const T &Array[] ) { int Res = 0; const int Size = ArraySize(Array); if (Size) { Res = 1; T Max = Array[Size - 1]; T Min = Max; for (int i = Size - 2; i >= 0; i--) if (Array[i] > Max) { Max = Array[i]; Res++; } else if (Array[i] < Min) { Min = Array[i]; Res++; } } return(Res); } int GetAmountSymbols() { string Symbols[]; GetSymbols(Symbols); return(GetUniqueElements(Symbols)); } void OnStart() { Print(GetAmountSymbols()); }
Faster
negligible
Hello!
Can anyone help me to code, how to count total number of running currency pair(s) with active order(s).
In the picture, there are 4 active orders running with 3 different currency pairs.
Here,
I don't need to count total orders using OrdersTotal() function.
Also I don't need to identify currency pair name using OrderSymbol() function.
But I need to count total running currency pairs using a loop, just like --
How do I code that?
Thank you in advance.