retrieve all the magic numbers in Ordershistory
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 int add_to_int_array(int &array[],int add_what){ //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 array[new_size-1]=add_what; //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 */ bool add_if_not_in(int &array[],int add_what){ bool added=false; //step one : look for it int find=find_in_int_array(array,add_what); //if it does not exist if(find==-1){ find=add_to_int_array(array,add_what); added=true; } return(added); } /* 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, char _1_for_buys_2_for_sells_zero_all=0){ //loop to open orders int typeF=_1_for_buys_2_for_sells_zero_all;//shortening it int t=OrdersTotal(); int new_found_magics=0; for(int i=0;i<t;i++){ //select the order if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){ //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())){ //buys or sells or all if(typeF==0||(typeF==1&&is_order_buy((ENUM_ORDER_TYPE)OrderType(),pendings_allowed))||(typeF==2&&is_order_sell((ENUM_ORDER_TYPE)OrderType(),pendings_allowed))){ //we know we are interested in this trade so get its magic number //and add it if it does not exist if(add_if_not_in(mn_array,OrderMagicNumber())){ //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, char _1_for_buys_2_for_sells_zero_all=0){ //loop to open orders int typeF=_1_for_buys_2_for_sells_zero_all;//shortening it 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())){ //buys or sells or all if(typeF==0||(typeF==1&&is_order_buy((ENUM_ORDER_TYPE)OrderType(),pendings_allowed))||(typeF==2&&is_order_sell((ENUM_ORDER_TYPE)OrderType(),pendings_allowed))){ //we know we are interested in this trade so get its magic number //and add it if it does not exist if(add_if_not_in(mn_array,OrderMagicNumber())){ //if its added increase our count of newly found magic numbers on this round new_found_magics++; } } } } } } return(new_found_magics); } bool is_order_buy(ENUM_ORDER_TYPE _type,bool pendings_allowed){ if(_type==OP_BUY||(pendings_allowed&&(_type==OP_BUYLIMIT||_type==OP_BUYSTOP))){ 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){ if(_type==OP_BUYLIMIT||_type==OP_SELLLIMIT||_type==OP_BUYSTOP||_type==OP_SELLSTOP){return(true);} 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.

You are missing trading opportunities:
- Free trading apps
- Free Forex VPS for 24 hours
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register