How to check if an order is selected - page 12

 
grell:

So that's my point. My functions have no versatility, each is strictly dedicated to its own business. Even buy and sell are separate. So, let the order numbers change, let the stops creep up, but the ticket and the magician will remain.

Well, you got carried away with the slosoll:)
 
tara:

Well, you got carried away with the clobbering:)

There have been no misses yet. I'm a tyrant in that respect:)
 
tara:
No, not like cheese in butter, but I did my fair share of service as a lieutenant colonel. Sorry if I offended you :(
No, don't be offended, don't be obliged if I said anything wrong, but sincerely. :)
 
borilunad:
No, no reason to be offended, you don't have to commit if I said something wrong, but sincerely. :)
Never mind.
 
Ant_TL:

You've got me all wrong. So do half of the people for some reason. I don't need in function B() to process that order, which is selected in function A(). Function B() works with other orders, no matter which ones, it has nothing to do with function A(); function B() has its own logic. It can count the number of orders, their total profit, look through their comments, TP SL, etc. The task is to return from function B() back to function A() such that no matter what function B() has done with orders, the logic of function A() operation at the point of call of function B() from it will not be violated due to the fact that the order selected by function A() before function B() was called is not selected anymore and the selected order is another random order processed by function B() that most probably also searches orders in the loop.


You keep forgetting that only one order can be selected at any one time. Exit = store the list of orders in a savings pool (your array). one global variable lastorder is not enough. a better option would be lastords[ticket][function].
 
//+------------------------------------------------------------------+
//| Description included Functions                                   |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                       ORDERS.mq4 |
//|           Copyright © 2012. XrustSolution. mail:xrustx@gmail.com |
//|          https://www.youtube.com/user/opmlv http://forexrust.info |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012. XrustSolution. mail:xrustx@gmail.com"
#property link      "https://www.youtube.com/user/opmlv http://forexrust.info"
//+------------------------------------------------------------------+
//| Defines and Exports and Includes                                 |
//+------------------------------------------------------------------+
#define  ORDS_TOTAL 100
#define  HIST_TOTAL 100000
//+------------------------------------------------------------------+
//| Orders Property Indexes                                          |
//+------------------------------------------------------------------+
#define  ORD_TICK 0
#define  ORD_LOTS 1
#define  ORD_OPTM 2
#define  ORD_OPPR 3
#define  ORD_STOP 4
#define  ORD_TAKE 5
#define  ORD_CLPR 6
#define  ORD_CLTM 7
#define  ORD_PROF 8
#define  ORD_SWAP 9
#define  ORD_COMM 10
#define  ORD_EXPR 11
#define  ORD_SYMB 12
#define  ORD_COMN 13
//+------------------------------------------------------------------+
//| Extern and Global variables                                      |
//+---externs--------------------------------------------------------+

//+---globals--------------------------------------------------------+
int    gOrdsTotal[7];                     // number open orders
int    gOrdsTicks[ORDS_TOTAL][6];         // array of open ords tickets
double gOrdsProps[ORDS_TOTAL][6][12];     // array of open ords properties
double gPreOrdsProps[ORDS_TOTAL][6][12];
double gOrdsPrf[6];                       // open ords summary profit for order types
double gOrdsLts[6];                       // open ords summary lots for order types
//+------------------------------------------------------------------+
int    gHistTotal[7];                     // number closed orders
int    gHistTicks[HIST_TOTAL][6];         // array of closed ords tickets
double gHistProps[HIST_TOTAL][6][12];     // array of closed ords properties
double gHistPrf[6];                       // closed ords summary profit for order types
double gHistLts[6];                       // closed ords summary lots for order types
//+------------------------------------------------------------------+
//|   Function  :  double iOrdProps(OrderType,PropIndex,Count)       |
//+------------------------------------------------------------------+
double iOrdProps(int type,int indx,int co){int i;double res=0;
   i = gOrdsTicks[co][type];
   if(OrderSelect(i,SELECT_BY_TICKET)){
      if(OrderCloseTime()==0){
         switch(indx){
            case ORD_TICK : res = OrderTicket(); break;
            case ORD_LOTS : res = OrderLots(); break;
            case ORD_OPTM : res = OrderOpenTime(); break;
            case ORD_OPPR : res = OrderOpenPrice(); break;
            case ORD_STOP : res = OrderStopLoss(); break;
            case ORD_TAKE : res = OrderTakeProfit(); break;
            case ORD_CLPR : res = OrderClosePrice(); break;
            case ORD_CLTM : res = OrderCloseTime(); break;
            case ORD_PROF : res = OrderProfit(); break;
            case ORD_SWAP : res = OrderSwap(); break;
            case ORD_COMM : res = OrderCommission(); break;
            case ORD_EXPR : res = OrderExpiration(); break;
            default: res = 0; break;
         }
      }
   }else{
      return(EMPTY_VALUE);
   }
   return(res);
}
//+------------------------------------------------------------------+
//|   Function  :  double fOrdProps(OrderType,PropIndex,Count)       |
//+------------------------------------------------------------------+
double fOrdProps(int type,int indx,int co){return(gOrdsProps[co][type][indx]);}
//+------------------------------------------------------------------+
//|   Function  :  int fOrdsTicket(OrderType,Count)                  |
//+------------------------------------------------------------------+
int fOrdsTicket(int type, int indx = 0){return(gOrdsTicks[indx][type]);}
//+------------------------------------------------------------------+
//|   Function  :  int fOrdsTotal(OrderType)                         |
//+------------------------------------------------------------------+
int fOrdsTotal(int type = 6){return(gOrdsTotal[type]);}
//+------------------------------------------------------------------+
//|   Function  :  double fHistProps(OrderType,PropIndex,Count)      |
//+------------------------------------------------------------------+
double fHistProps(int type,int indx,int co){return(gOrdsProps[co][type][indx]);}
//+------------------------------------------------------------------+
//|   Function  :  int fHistTicket(OrderType,Count)                  |
//+------------------------------------------------------------------+
int fHistTicket(int type, int indx = 0){return(gHistTicks[indx][type]);}
//+------------------------------------------------------------------+
//|   Function  :  int fHistTotal(OrderType)                         |
//+------------------------------------------------------------------+
int fHistTotal(int type = 6){return(gOrdsTotal[type]);}
//+------------------------------------------------------------------+
//|          Function  : int HistRefresh(Magik,Comment,Symbol)       |
//|          Copyright © 2012, XrustSolution.  mail:xrustx@gmail.com |
//|          https://www.youtube.com/user/opmlv http://forexrust.info |
//+------------------------------------------------------------------+
//|          Description:                                            |
//+------------------------------------------------------------------+
int HistRefresh(int mn=-1,string comm="",string sy=""){int i,ii=0,type;bool iMn=true,iComm=true;
   if(mn<0){iMn=false;}
   ArrayResize(gHistTotal,7);
   ArrayInitialize(gHistTotal,0);
   ArrayResize(gHistProps,ORDS_TOTAL);
   ArrayInitialize(gHistProps,0);
   ArrayResize(gHistPrf,ORDS_TOTAL);
   ArrayInitialize(gHistPrf,0);
   ArrayResize(gHistLts,ORDS_TOTAL);
   ArrayInitialize(gHistLts,0);      
   if(StringLen(comm)<1){iComm=false;}
   for(i = OrdersHistoryTotal()-1; i>=0; i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
         if(OrderType()>5){continue;}
         if(OrderCloseTime()==0){continue;}
         if(sy!=""){if(OrderSymbol()!=sy){continue;}}
         if(iMn){if(OrderMagicNumber()!=mn){continue;}}
         if(iComm){if(StringFind(OrderComment(),comm)<0){continue;}}
         type = OrderType();
         gHistProps[gHistTotal[type]][type][0] = OrderTicket();
         gHistProps[gHistTotal[type]][type][1] = OrderLots();
         gHistProps[gHistTotal[type]][type][2] = OrderOpenTime();
         gHistProps[gHistTotal[type]][type][3] = OrderOpenPrice();
         gHistProps[gHistTotal[type]][type][4] = OrderStopLoss();
         gHistProps[gHistTotal[type]][type][5] = OrderTakeProfit();
         gHistProps[gHistTotal[type]][type][6] = OrderClosePrice();
         gHistProps[gHistTotal[type]][type][7] = OrderCloseTime();
         gHistProps[gHistTotal[type]][type][8] = OrderProfit();
         gHistProps[gHistTotal[type]][type][9] = OrderSwap();
         gHistProps[gHistTotal[type]][type][10] = OrderCommission();
         gHistProps[gHistTotal[type]][type][11] = OrderExpiration();
         gHistPrf[type] += OrderProfit()+OrderSwap()+OrderCommission();
         gHistLts[type] += OrderLots();
         gHistTotal[type]++;// count for ordertypes
         gHistTotal[6]++;// all orders count
         ii++;
      }
   }   
   return(ii);
}
//+------------------------------------------------------------------+
//|          Function  : int OrdsRefresh(Magik,Comment,Symbol)       |
//|          Copyright © 2012, XrustSolution.  mail:xrustx@gmail.com |
//|          https://www.youtube.com/user/opmlv http://forexrust.info |
//+------------------------------------------------------------------+
//|          Description:                                            |
//+------------------------------------------------------------------+
int OrdsRefresh(int mn=-1,string comm="",string sy=""){int i,ii=0,type;bool iMn=true,iComm=true;
   if(mn<0){iMn=false;}
   if(StringLen(comm)<1){iComm=false;}
   ArrayResize(gOrdsTotal,7);
   ArrayInitialize(gOrdsTotal,0);
   ArrayResize(gOrdsTicks,ORDS_TOTAL);
   ArrayInitialize(gOrdsTicks,0);
   ArrayResize(gOrdsProps,ORDS_TOTAL);
   ArrayInitialize(gOrdsProps,0);
   ArrayResize(gOrdsPrf,ORDS_TOTAL);
   ArrayInitialize(gOrdsPrf,0);
   ArrayResize(gOrdsLts,ORDS_TOTAL);
   ArrayInitialize(gOrdsLts,0);
   for(i = OrdersTotal()-1; i>=0; i--){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderType()>5){continue;}
         if(OrderCloseTime()!=0){continue;}
         if(sy!=""){if(OrderSymbol()!=sy){continue;}}
         if(iMn){if(OrderMagicNumber()!=mn){continue;}}
         if(iComm){if(StringFind(OrderComment(),comm)<0){continue;}}
         type = OrderType();
         gOrdsTicks[gOrdsTotal[type]][type] = OrderTicket();        
         gOrdsProps[gOrdsTotal[type]][type][0] = OrderTicket();
         gOrdsProps[gOrdsTotal[type]][type][1] = OrderLots();
         gOrdsProps[gOrdsTotal[type]][type][2] = OrderOpenTime();
         gOrdsProps[gOrdsTotal[type]][type][3] = OrderOpenPrice();
         gOrdsProps[gOrdsTotal[type]][type][4] = OrderStopLoss();
         gOrdsProps[gOrdsTotal[type]][type][5] = OrderTakeProfit();
         gOrdsProps[gOrdsTotal[type]][type][6] = OrderClosePrice();
         gOrdsProps[gOrdsTotal[type]][type][7] = OrderCloseTime();
         gOrdsProps[gOrdsTotal[type]][type][8] = OrderProfit();
         gOrdsProps[gOrdsTotal[type]][type][9] = OrderSwap();
         gOrdsProps[gOrdsTotal[type]][type][10] = OrderCommission();
         gOrdsProps[gOrdsTotal[type]][type][11] = OrderExpiration();
         gOrdsPrf[type] += OrderProfit()+OrderSwap()+OrderCommission();
         gOrdsLts[type] += OrderLots();
         gOrdsTotal[type]++;// count for ordertypes
         gOrdsTotal[6]++;// all orders count
         ii++;
      }
   }   
   return(ii);
}
//+------------------------------------------------------------------+
 
Rustam, thank you and happy Holidays.
 
Yeah, Happy Holidays to you too! A hundred grams for everyone :)
 
TarasBY:

In my code, this error will not occur, because this function is called AFTER OrderSelect(). And the check in the code is left from the times when codes were written without a common structure of all executable EA functions.

In addition, most of my functions contain error handling functions and I am very careful to avoid them. And there are also a number of functions that generate an error, like the answer you were looking for: "Order selected earlier, or not?"

P.S. By the way, I remembered one peculiarity (may be useful for someone) of working with OrderSelect() functions from compiled libraries: we have selected an order (how - not principally), OrderTicket() - returns number of selected order. But if we want to get the properties of this selected order from the function which is located in the compiled library, we will get nothing. We have to select that order again (again).

In fact, I encountered a situation where the error was caused by one of the general-purpose functions that could be used both within the order processing loops and outside those loops, i.e. before the order was selected, which led to the error. If we want to make universal service functions working with orders that can be used both after an order has been selected and before that, both when there are open orders and when there are none, we should use a mechanism like the one I cited to make sure we don't get this error.

I already wrote earlier in this thread about the fact that order selection is not passed to library modules and back.

 
Ant_TL:

In fact, I encountered a situation where the error was caused by one of the general-purpose functions that could be used both inside and outside the order processing loops, i.e. before an order was selected, which caused the error. If we want to make universal service functions that work with orders and can be used both after an order has been selected and before that, both when there are open orders and when there are no orders at all, we should use such mechanism as I mentioned on the previous page to avoid this error.

I already wrote earlier in this thread about the fact that order selection is not passed to library modules and back.


You should make yourself accustomed to the fact that each trade operation has its own select order and you won't have any problems. And, yes, you have been correctly written, only one order can be selected. Everything else is solved by arrays.
Reason: