// Alternately save reports on the trading history of accounts 
// located in the Favorites tab of the Navigator
 
#property  show_inputs
#include <WinUser32.mqh>
 
extern  int  Amount = 5 ; // The number of accounts in the Favorites tab of the Navigator window 
extern  int  Pause = 10 ; // Pause between switching trading accounts
 
#import  " user32.dll "
  int  GetParent (  int  hWnd  ) ;
  int  GetDlgItem (  int  hDlg , int  nIDDlgItem  ) ;
  int  GetLastActivePopup (  int  hWnd  ) ;
#import
 
#define  VK_HOME  0x24
#define  VK_DOWN  0x28
#define  VK_ENTER  0x0D
 
#define  PAUSE  1000
 
// Connects to the account located in the line Num number in the Favorites tab of the window Navigator 
void  Connect (  int  Num  ) 
{ 
   int  hwnd = WindowHandle ( Symbol () , Period ()) ;
   int  hwnd_parent = 0 ;
 
   while  ( ! IsStopped ()) 
   { 
      hwnd = GetParent ( hwnd ) ;
      
      if  ( hwnd == 0 ) 
        break ;
        
      hwnd_parent = hwnd ;
   }
   
   if  ( hwnd_parent != 0 )   // found the main window 
   { 
     hwnd = GetDlgItem ( hwnd_parent , 0xE81C ) ; // found Favorite windows Navigator 
     hwnd = GetDlgItem ( hwnd , 0x52 ) ;
     hwnd = GetDlgItem ( hwnd , 0x8A70 ) ;
     
     PostMessageA ( hwnd , WM_KEYDOWN , VK_HOME , 0 ) ; // top line of the bookmark of the Favorites window of the Navigator
     
     while  ( Num > 1 )   
     { 
       PostMessageA ( hwnd , WM_KEYDOWN , VK_DOWN , 0 ) ; // shifted to the desired line 
       Num - ;
     }
 
     PostMessageA ( hwnd , WM_KEYDOWN , VK_ENTER , 0 ) ;  // login 
     Sleep ( PAUSE ) ;                                  // wait
     
     hwnd = GetLastActivePopup ( hwnd_parent ) ;  // found the login form 
     PostMessageA ( hwnd , WM_KEYDOWN , VK_ENTER , 0 ) ; // logged in 
   }
 
  return statement ;
}
 
// Saves a detailed / short (Detailed = TRUE / FALSE) account trading history report 
void  SaveStatement (  bool  Detailed , string  FileName  ) 
{ 
   int  hwnd = WindowHandle ( Symbol () , Period ()) ;
   int  hwnd_parent = 0 ;
 
   while  ( ! IsStopped ()) 
   { 
      hwnd = GetParent ( hwnd ) ;
      
      if  ( hwnd == 0 ) 
        break ;
        
      hwnd_parent = hwnd ;
   }
   
   if  ( hwnd_parent != 0 )   // found the main window 
   { 
     if  ( Detailed ) 
       PostMessageA ( hwnd_parent , WM_COMMAND , 35502 , 0 ) ;  // Detailed report 
     else 
       PostMessageA ( hwnd_parent , WM_COMMAND , 33064 , 0 ) ;  // Short report
 
     Sleep ( PAUSE ) ;
 
     hwnd = GetLastActivePopup ( hwnd_parent ) ;  // found the file save form 
     SetWindowTextA ( GetDlgItem ( hwnd , 0x480 ) , FileName ) ;  // enter the file name
 
     PostMessageA ( hwnd , WM_KEYDOWN , VK_ENTER , 0 ) ;  // Confirm Saved 
     Sleep ( PAUSE ) ;
 
     hwnd = GetLastActivePopup ( hwnd_parent ) ;  // Found a warning about overwriting the 
     PostMessageA file ( hwnd , WM_KEYDOWN , VK_DOWN , 0 ) ;  // Confirm overwrite 
     PostMessageA ( hwnd , WM_KEYDOWN , VK_ENTER , 0 ) ;
   }
 
  return statement ;  
}
 
void  start () 
{ 
  int  i ;
  
  Pause *= 1000 ;
 
  for  ( i = 1 ; i <= Amount ; i ++ ) 
  { 
    Connect ( i ) ;
    
    Sleep ( Pause ) ;
    
    SaveStatement ( TRUE , AccountNumber ()) ;
  } 
 
  return statement ;
}