Giving Back: Snapshot your close for troubleshooting

 

I have been getting a lot of help in the past, so I would like to give some back. I am going to post a series of tools that may help you with your coding as you have helped me.

This one uses the snapshot feature to help you troubleshoot trade opening and closures. There are a few samples around here, but still needing some helping hands to put it together.

// Extern variables:
extern bool          EnableScreenShot = true;


// variables needed:
int                  ClosedOrderCnt = 0;
int                  Old_ClosedOrderCnt = 0;
int                  LastClosedOrder = 0;


// In the start, use this to get closed trades and take a snapshot of them 
// NOTE: not tested, just built, may need modification

   ClosedOrderCnt = CountOrdersClosed();
   if (Old_ClosedOrderCnt != ClosedOrderCnt && OrdersHistoryTotal() > 0)
   {
      LastClosedOrder = GetLastClosedOrder();
      TakeScreenShot(LastClosedOrder, "Closed");  // Use this command anywhere you open a trade or close a trade
      Old_ClosedOrderCnt = ClosedOrderCnt;        // to get a snapshot of the event. If open, then use "Open"
   }                                              // for control  value

// Functions to make it all come together:

// The Snapshot code, Control is the string where you state open or closed, it is made part of the file name
void TakeScreenShot(int Ticket, string Control)
{
   if (EnableScreenShot == true)
   {
      string filename = StringConcatenate(Username,"_",Ticket,"_",Symbol()); //Username is a variable we use
      int lasterror = 0;                                                     // To identify user. oprional
      if (IsTesting())
      {
         if(!WindowScreenShot("shots\\FT_tester"+"_"+Control+"_"+filename+".gif",640,480))
         {                 // Shots is the folder where it lives, "FT_" is the initials for program, change as
                           // desired.  Note: the tester version does not show all indicators, don't know why.
            lasterror=GetLastError();
         }

      }
      else
      {
         if(!WindowScreenShot("shots\\FT_" + filename + ".gif",640,480))
         {
            lasterror=GetLastError();
         }
      }
   }




}



// Get a count of closed orders, if count is different than last counted value, we have a trade or more that has
// closed.

int CountOrdersClosed()
{
   int CntClosed = 0;
   
   for(int i = 0; i <= OrdersHistoryTotal(); i++) 
   {
      OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
      if(OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()) 
      {
         CntClosed += 1;  
      }
   }
   return (CntClosed);
}
// Hopefully, we are only tracking a single closure, if so, this should catch it.
int GetLastClosedOrder()
{
      datetime last=0; 
      int retval;
      for(int i = 0; i <= OrdersHistoryTotal(); i++) 
   {
      OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
      if(OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol()) 
      {
         if(OrderCloseTime()>last)
         {
            last=OrderCloseTime(); 
            retval=OrderTicket();
         }
  
      }
   }
   return (retval);      
}
 

SRC is tripping up, I hope you can get it ok.

 

By changing the file path to look like the following, you can actually store the closures seperate from the opening files.

if(!WindowScreenShot("shots\\FT_" + Control + "\\_"+filename+".gif",640,480))
 

BTW: I played around with this idea a little bit more. After getting the ticket of the last closed trade, I also collected info like, was it a buy or sell, was it a profit or a loss, I then used those values in the dir structure to seperate the images according to their condition. I was able to identify how many losers were buys vs sells that were losers, or buys that were winners vs losers. By seperating them, I was also able to quickly identify why the losers lost. They say a pic is worth a 1000, well you get the pic, see if it makes your analysis easier too.

Word to MetaTrader, if something like this was embeded as an option flag in MT4 or 5, it would go a long way to help us improve our quality.

Note: Be sure to keep an eye on your file size usage. You can eat HD space quickly depending on how you use it.

 
In the tester, instead of taking a screen shot you can just pause:
// https://forum.mql4.com/35112
#include <WinUser32.mqh>
#import "user32.dll"
  int GetForegroundWindow();
#import
void PauseTest(){
    if ( IsTesting() && IsVisualMode() && IsDllsAllowed() ){
        int hmain = GetForegroundWindow();
        PostMessageA(hmain, WM_COMMAND, 0x57a, 0);  }   // 1402. Pause
}
 

However, in the tester, if you want to jump to current date and still use the visual, then this tool is ideal for you. Also, in tester, if you are not running the visual, you still get the snapshot. So in tester, it is worth it either way.

Reason: