Learning about Order processing - Indicator displays total profit/loss for past 24 hours.

 

Hi All, I wanted to learn more about order selection and processing so I wrote this indicator. It displays the profit or loss of the past 24 hours (or whatever time period you like). Two areas for improvement are: it should run each time an order closes rather than when each bar closes. It should programmatically select "All History" in the Terminal. Note: OrderSelect will only look for the range of orders filtered in the Terminal window.

Here is the code -- comments please... 

 

#property indicator_chart_window

datetime CurrentTime;


// ------------------------------------------------------------------
int deinit()
{
   ObjectDelete("prof");
}
  
  
// ------------------------------------------------------------------
// be sure to select "All History" in your terminal window! There may be a way to do this programmatically
int start()
{
   int ix; // index counter
   double sumProf; // running profit total
   
   if (CurrentTime != Time[0]) // do this once per bar, should be once per closed order
   {
      CurrentTime = Time[0];

      for (ix=0; ix < OrdersHistoryTotal(); ix++) // look through all history
      {
         OrderSelect(ix,SELECT_BY_POS,MODE_HISTORY); // select next order
         if (OrderCloseTime() > TimeCurrent()-86400) // within last 24 hours, 86400 = 24 hours, 604800 = 1 week
         {
            sumProf = sumProf+OrderProfit(); // sum the profits of the orders
         }
      } // for
      
      ObjectCreate("prof", OBJ_LABEL, 0, 0, 0); // display profit or loss on screen
      ObjectSetText("prof", "24hr Profit: "+(DoubleToStr(sumProf,2))+"$", 10, "Arial", Cornsilk);
      ObjectSet("prof", OBJPROP_XDISTANCE, 20);
      ObjectSet("prof", OBJPROP_YDISTANCE, 20);
      
   } // if (CurrentTime...
   return;
} // start()      

 
MisterDog: Here is the code -- comments please... 
"All History" in your terminal window! There may be a way to do this programmatically
#include <WinUser32.mqh>
#import "user32.dll"
  int    GetAncestor(int, int);
#import
void     EnableAllHistory(){                          #define GA_ROOT 2
   // https://forum.mql4.com/ru/14463/page5#401551
   #define MT4_WMCMD_ALL_HISTORY 33058
   int      main = GetAncestor(WindowHandle(Symbol(), Period()), GA_ROOT);
   PostMessageA(main, WM_COMMAND, MT4_WMCMD_ALL_HISTORY, 0);
}
 
WHRoeder:



Thanks, I never would have come up with this on my own. 
Reason: