retrieve all the magic numbers in Ordershistory

int totalOrders = OrdersTotal();
int magicNumbers[];
ArrayResize(magicNumbers, totalOrders);
int magicNumberCount = 0;

for (int i = 0; i < totalOrders; i++)
{
int currentMagicNumber = OrderMagicNumber();
bool isUnique = true;

for (int j = 0; j < magicNumberCount; j++)
{
if (magicNumbers[j] == currentMagicNumber)
{
isUnique = false;
break;
}
}

if (isUnique)
{
magicNumbers[magicNumberCount] = currentMagicNumber;
magicNumberCount++;
}
}

for ( i = 0; i < magicNumberCount; i++)
{
Print("Magic number: ", magicNumbers[i]);
}

i am trying to retrieve all the magicnumbers (EAs) used in my account automatically so i can calculate some metrics , i tried but it didn't work . i want to retrieve the magicnumbers in a list so i can use them later in the code

nidalzd:

You need to use OrdersHistoryTotal() for the loop size , and in the OrderSelect , MODE_HISTORY .

Also here is a code example in case you are facing other issues as well.

//function to find a number in an array
int find_in_int_array(int &array[],int find_what){
for(int i=0;i<ArraySize(array);i++){
//if this is what you are looking for return the position
if(find_what==array[i]){return(i);}
}
return(-1);//-1 if found nothing
}
//function to add a number to an array
//get the size of the array and add 1
int new_size=ArraySize(array)+1;
//resize the array
ArrayResize(array,new_size,0);
//store the new item in location new_size-1
//return the new index
return(new_size-1);
}
//function to remove an item from an array
int remove_from_int_array(int &array[],int which_ix){//which index not by item value
//new size
int new_size=ArraySize(array)-1;
//the new size is also the pointer to the last item
//move the last item to the removed ix
array[which_ix]=array[new_size];
//shrink the array
if(new_size>0){ArrayResize(array,new_size,0);}
else{ArrayFree(array);}
//return the new size
return(new_size);
}
/*
Now , having the primitive functions you can combine them
so let's create a function that adds a number only if
it does not exist
*/
//step one : look for it
//if it does not exist
if(find==-1){
}
}
/*
And how about removing a specific value ?
*/
int remove_this_value(int &array[],int target){
//while this value exists we keep removing it
int ix=find_in_int_array(array,target);
int new_size=ArraySize(array);
while(ix!=-1){
//a:remove that index get new size in case we exit
new_size=remove_from_int_array(array,ix);
//b:search for the target value again
ix=find_in_int_array(array,target);
//if values of "target" still exist then we keep looping
//if not we exit
}
return(new_size);
}
//so let's test this bad boy
//magic numbers array
int magics[];
//function to catch them all
int catch_all_live_magic_numbers(int &mn_array[],
bool pendings_allowed=false,
string _symbol_filter=NULL,
//loop to open orders
int t=OrdersTotal();
int new_found_magics=0;
for(int i=0;i<t;i++){
//select the order
//of the symbol filter or symbol filter is null
if(_symbol_filter==NULL||_symbol_filter==OrderSymbol()){
//pendings allowed check
if(pendings_allowed||!is_pending((ENUM_ORDER_TYPE)OrderType())){
//we know we are interested in this trade so get its magic number
//and add it if it does not exist
//if its added increase our count of newly found magic numbers on this round
new_found_magics++;
}
}
}
}
}
}
return(new_found_magics);
}
//function to catch history
int catch_all_history_magic_numbers(int &mn_array[],
bool pendings_allowed=false,
string _symbol_filter=NULL,
//loop to open orders
int t=OrdersHistoryTotal();
int new_found_magics=0;
for(int i=0;i<t;i++){
//select the order
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
//of the symbol filter or symbol filter is null
if(_symbol_filter==NULL||_symbol_filter==OrderSymbol()){
//pendings allowed check
if(pendings_allowed||!is_pending((ENUM_ORDER_TYPE)OrderType())){
//we know we are interested in this trade so get its magic number
//and add it if it does not exist
//if its added increase our count of newly found magic numbers on this round
new_found_magics++;
}
}
}
}
}
}
return(new_found_magics);
}
return(true);
}
return(false);
}
bool is_order_sell(ENUM_ORDER_TYPE _type,bool pendings_allowed){
if(_type==OP_SELL||(pendings_allowed&&(_type==OP_SELLLIMIT||_type==OP_SELLSTOP))){
return(true);
}
return(false);
}
bool is_pending(ENUM_ORDER_TYPE _type){
return(false);
}

nidalzd:
int totalOrders = OrdersTotal();

Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
Next time, post in the correct place. The moderators will likely move this thread there soon.

#property strict

// #include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

int GetMagics( long &Magics[] )
{
int Amount = 0;

for (int i = ArrayResize(Magics, OrdersHistoryTotal()) - 1; i >= 0; i--)
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) && (OrderType() <= OP_SELLSTOP))
Magics[Amount++] = OrderMagicNumber();

int Count = (bool)ArrayResize(Magics, Amount);
ArraySort(Magics);

for (int i = 1; i < Amount; i++)
if (Magics[i] != Magics[i - 1])
Magics[Count++] = Magics[i];

return(ArrayResize(Magics, Count));
}

void OnStart()
{
long Magics[];

for (int i = GetMagics(Magics) - 1; i >= 0; i--)
Print(Magics[i]);
}

Not tested, not compiled, just typed.
int GetUniqueMagics(int &Magics[]){
int count = 0;
for (int i = ArrayResize(Magics, OrdersHistoryTotal()) - 1; i >= 0; i--) if(
OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)
&& OrderType() <= OP_SELLSTOP
)  Magics[count++] = OrderMagicNumber();
ArraySort(Magics);
return unique_copy(Magics, 0, count, Magics);
}
template<typename Ti, typename To>
INDEX    unique_copy(const Ti& inp[], INDEX iBeg, INDEX iEnd,
To& out[], INDEX oBeg){
if(iBeg == iEnd)  return oBeg;
Ti    prev  = out[oBeg++]  = inp[iBeg];
while(++iBeg != iEnd){
if(!(prev == inp[iBeg]) )  prev  = out[oBeg++]  = inp[iBeg];
}
return oBeg;
}
Not tested, not compiled, just typed.

fxsaber #:

Very nice  🤯