metatrader debugging EA using DEBUGview - great for beginners to learn

 

You can download Debug view from Microsoft to externalise anything in your code to help debug it . I have learned a great deal about MT4 from using this tool simply to output the results of some code so I understand what it is doing . For example you may see some code that you do not understand but can output parts of it and keep a track of what each calculation does ..essentially slowing it down to understand.

I adapted this from somebody else who wrote log( " " ) so thank you to that person. I have since created some new functions to make it easier to control what I see .

Process-

## IMPORTANT NOTE ##

Please test this in a DEMOtrade account until you understand what is happening .


STEP 1) download the debug view program above

STEP 2) Insert this MT4 code below in your MT4 meta editor and save it in your "INCLUDE" folder.

//
// send information to OutputDebugString() to be viewed and logged by
// SysInternal's DebugView (free download from Microsoft) This is ideal for
// debugging as an alternative to Print(). The function will take up to 10
// strings (or numeric) arguments to be concatenated into one debug message.
// http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx
//
//         #property indicator_separate_window

#import "kernel32.dll"                 // for debug commands
void OutputDebugStringW(string msg);   // for debug commands
#import 


//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// FUNCTION log

void log(   string s1,    string s2="",    string s3="",    string s4="",    string s5="", 
   string s6="",    string s7="",    string s8="")

{
   string out = StringTrimRight(StringConcatenate(" |||| ",
      WindowExpertName(), " ||||", " ", Symbol()," |||| ", 
      " ", s1, 
      " ", s2, 
      " ", s3, 
      " ", s4, 
      " ", s5, 
      " ", s6, 
      " ", s7, 
      " ", s8
   ));
   //string out1 = StringTrimRight(StringConcatenate( "++++++++++++++++"   ));
    OutputDebugStringW(out);
    //OutputDebugStringW(out1);
    
}
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// FUNCTION log symbol

void logs(   string s1,    string s2="",    string s3="",    string s4="",    string s5="", 
   string s6="",    string s7="",    string s8="")

{
   string out = StringTrimRight(StringConcatenate(" |||| ", Symbol()," |||| ", 
      " ", s1, 
      " ", s2, 
      " ", s3, 
      " ", s4, 
      " ", s5, 
      " ", s6, 
      " ", s7, 
      " ", s8
   ));
   //string out1 = StringTrimRight(StringConcatenate( "++++++++++++++++"   ));
    OutputDebugStringW(out);
    //OutputDebugStringW(out1);
    
}
    
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//   FUNCTION logB- blank details 

void logb(   string s1,    string s2="",    string s3="",    string s4="",    string s5="", 
   string s6="",    string s7="",    string s8="")

{
 
   string out1 = StringTrimRight(StringConcatenate("~" 
      " ", s1, 
      " ", s2, 
      " ", s3, 
      " ", s4, 
      " ", s5, 
      " ", s6, 
      " ", s7, 
      " ", s8
   ));
   //string out1 = StringTrimRight(StringConcatenate( "++++++++++++++++"   ));
    OutputDebugStringW(out1);
    //OutputDebugStringW(out1);
    
}

//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// FUNCTION logN -file name 

void logn(   string s1,    string s2="",    string s3="",    string s4="",    string s5="", 
   string s6="",    string s7="",    string s8="")

{
   string out = StringTrimRight(StringConcatenate(" |||| ",
      WindowExpertName()," ||||", 
      " ", s1, 
      " ", s2, 
      " ", s3, 
      " ", s4, 
      " ", s5, 
      " ", s6, 
      " ", s7, 
      " ", s8
   ));
   //string out1 = StringTrimRight(StringConcatenate( "++++++++++++++++"   ));
    OutputDebugStringW(out);
    //OutputDebugStringW(out1);
    
}

//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// FUNCTION logT1 - uses taged names for DEBUG COLOUR quick easy to identify

void logt1(   string s1,    string s2="",    string s3="",    string s4="",    string s5="", 
   string s6="",    string s7="",    string s8="")

{
   string out = StringTrimRight(StringConcatenate(" |||| tag1 ||||", 
      " ", s1, 
      " ", s2, 
      " ", s3, 
      " ", s4, 
      " ", s5, 
      " ", s6, 
      " ", s7, 
      " ", s8
   ));
   //string out1 = StringTrimRight(StringConcatenate( "++++++++++++++++"   ));
    OutputDebugStringW(out);
    //OutputDebugStringW(out1);
    
}
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// FUNCTION logT2 - uses taged names for DEBUG COLOUR quick easy to identify

void logt2(   string s1,    string s2="",    string s3="",    string s4="",    string s5="", 
   string s6="",    string s7="",    string s8="")

{
   string out = StringTrimRight(StringConcatenate(" |||| tag2 ||||", 
      " ", s1, 
      " ", s2, 
      " ", s3, 
      " ", s4, 
      " ", s5, 
      " ", s6, 
      " ", s7, 
      " ", s8
   ));
   //string out1 = StringTrimRight(StringConcatenate( "++++++++++++++++"   ));
    OutputDebugStringW(out);
    //OutputDebugStringW(out1);
    
}



   

STEP 3) cut and past this demo EA to meta editor,save it to Expert adviser folder and run compile, this helps understand how to use log functions

//+------------------------------------------------------------------+
//|                                               zzz_Debug_test.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "dasser  - in MQL4.com forum "
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#include <debug_inc.mqh>


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
                  logb(" blank log "); // no information added 
                  log(" standard log "); // adds symbol and file information
                  logs(" symbol log  "); // adds symbol in line 
                  logn(" file name log "); //adds filename in line 
                  logt1(" tag1 log  "); // adds tag1 as text to filter colour
                  logt2(" tag2 log "); // adds tag1 as text to filter colour
                  logb("  blank log  ") ;// no information added

                  logb("Symbol() = ", Symbol()) ; // demo showing how to add paramters 
                  logb(" OrdersTotal() = ", OrdersTotal()) ;// demo showing how to add paramters
                  logt1("Symbol() = ", Symbol()) ;
                  logt2(" OrdersTotal() = ", OrdersTotal()) ;
                  logn("Symbol() = ", Symbol()) ;
                  logn(" OrdersTotal() = ", OrdersTotal()) ;
   
  }
//+------------------------------------------------------------------+


STEP 4 - Find The EA you just saved and drag it onto a blank worksheet chart

STEP 5 - Open the debug view program you downloaded in earlier steps


STEP 6 - you should now see demonstration of the log showing text and EA functions like Symbol() and OrdersTotal() .

STEP 7 - Open a position ( if you don't ) and see the OrdersTotal() change showing how many orders you have open.

STEP 8 - with any new EA you build at the top place this code at the start of the EA

#include <debug_inc.mqh>

This adds the debug functions to you ea code and now you can use the functions in such as loga( " ");, logt1("..") etc ;

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Below is an example showing debug view in action ..

Here I calculate my open positions maximum profit and maximum loss amounts, and the ratio,

Debug view example


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Notice in the EA posted .. OrdersTotal() function even counts pending orders ( something I was made aware of by using this log function )

Hope this helps people ... let me know of any better ideas .

 
Looks interesting ... will it work when using the Strategy Tester?
 
dasser: from somebody else who wrote log( " " ) so thank you to that person.
Freefox: Looks interesting ... will it work when using the Strategy Tester?
  1. You're welcome.
  2. Yes it does and you don't get the drop outs like Print does in the tab.
 
impossible de compiler votre <debug_inc.mqh>
 


Hi,

 

I am using this successfully on Windows 7 but on Windows 10 x64 I get an error:

 Cannot call 'kernel32.dll::OutputDebugStringW', 'kernel32.dll' is not loaded

Does anyone know how to fix this?

 

thanks! 

 

 


 

Why using an external tool, when the IDE now has a built in debugger?

 
eddie:

Why using an external tool, when the IDE now has a built in debugger?



I find it to be a more convenient way to view the logs.
 

OK - than i think you never used breakpoints.

 
eddie:

OK - than i think you never used breakpoints.


In the MT4 version I used it is not possibe to have breakpoints in the tester and I mainly debug and test while using the strat tester. So I need to use log lines for debugging.

Yes, if I could use breakpoints, I would have no need for this..
 
Quintos:


Hi,

 

I am using this successfully on Windows 7 but on Windows 10 x64 I get an error:

 Cannot call 'kernel32.dll::OutputDebugStringW', 'kernel32.dll' is not loaded

Does anyone know how to fix this?

 

thanks! 

 

 



This may be necroing this post, but in case someone else comes across this: I put the full path in - "#import "C:\Windows\System32\kernel32.dll".


HTH!


 


 
Quintos: In the MT4 version I used it is not possibe to have breakpoints in the tester 

DebugView was necessary before the built-in debugger in the editor (not the tester).

Reason: