Instead of cluttering up the forum with another useless post, I’ve decided I’ll turn this into something that others might find useful. Hopefully I can get some veteran MQL coders input on things.
I’m a programmer - I’ve learned bits and pieces of all sorts of different languages, but my main programming love affair has been with AutoIt, a windows automation language. I’ve picked up c++, python, java, c, Lua, Ruby, some c#, php, javascript and others along the way. So when I wanted to pick up MQL4, I wanted something that said “here’s a stupid program, and here’s how it works.”
Instead, I found “here’s how the language works” and “Here’s my AWESOME EA that will make you MILLIONS!” Needless to say, I was a bit irked. So, I started digging, and after 3 days of puttering around, I was able to figure things out. Codersguru does an excellent job of explaining MQL to a non-programmer audience, but he misses out on the implementation details. The MQL4 docs aren’t bad, but they’re dry and tend to assume prior knowledge in some places.
So I’ve figured out the basics, and would like to show a barebones progression for programmers like me - people who aren’t particularly interested in the complete newbie introduction to the language, and just want to get into how programs work.
Expert Advisor programs are structured like this:
int init() {
return(0);
}
int start(){
return(0);
}
int deinit(){
return(0);
}
init() is called once when the program starts. Variables declared here are global, and have persistence for the life of the program. It would be a good idea to place your function imports, database connects, service log-ins, and other such functionality here.
start() is called every tick. A tick is any change in price data. As such, a tick is not naive to market behavior - it only occurs when a change occurs, so there can be arbitrarily long delays between each tick. You want to place your trade functions inside start(), to act on market data as it changes. Variables declared inside start are locally scoped, and persist only for each run of the start() function (you could use an external program to implement persistence, or use global variables to iteratively build “temporary” persistent data structures on each run.)
deinit() is called when MetaTrader closes, when the Expert Advisor is turned off, or any other time that the EA exits its normal lifecycle. Obviously a crash will interrupt the normal cycle of a program and deinit() will not be called.
MQL4 is focused on trading. You can utilize its foreign function interface to call external code - the winapi DLLs, for example - and create any program you can think of. However, most of the built in functionality is explicitly designed to interact with the MetaTrader platform.
The built in functionality is covered in depth in the MQL reference. In order to access the reference, simply click on the “Help” tab at the bottom of the toolbox inside MetaEditor.
Once you’re able to navigate the docs and understand the program flow, things start to fall into place, and it’s easy as pie to move on to advanced discussions.
Some thoughts as I’ve churned through this stuff over the last few days:
MQL4 is not necessarily limited. While it’s intensely focused around the MT4 platform, it can be extended pretty easily, and there are a few methods to do so:
You could implement a Singleton pattern within the start() function that created a persistent, ongoing process. Doing this would require a single global variable that held the state of the ongoing process. It would be launched once, and then tracked. On each subsequent run of start(), the test would indicate whether the process was running or not, and then branch accordingly.
This would lend itself to a more continuous structure to the program, as opposed to the nonlinear, unpredictable tick by tick approach.
There are other options, too. Free download of the ‘“Native” MQL HTTP Client’ library by ‘gunzip’ for MetaTrader 4 in the MQL5 Code Base is a MQL library to interact over http. Any language that has a CGI implementation lends itself well to this setup. It’s also a good example of using the Windows API to extend a program. MSDN is a great resource for this type of thing.
Another method would be writing a windows DLL and calling it via the FFI. With this, you could feasibly implement your language of choice, using its normal embedding procedure.
After all is said and done, MQL4 is just your friendly neighborhood scripting language with heavy c roots. It’s got warts and peculiarities, but they’re constrained to the context of its environment, and are easily bypassed. Once you’ve got MQL4 doing simple things, it’s exceedingly easy to use existing code for your own projects.
Some tips: press F1 after highlighting a function name to open up its entry in the MQL reference. This will make learning a breeze. Here’s a simple, stupid example program. It’s not meant to do anything except introduce EA programming concepts. It’s not commented, but is simple enough to get the points across (I should get around to doing comments sometime in the near future.)
//+------------------------------------------------------------------+
//| StupidAdvisor.mq4 |
//| JRowe |
//| |
//+------------------------------------------------------------------+
#property copyright "JRowe"
#property link ""
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
ObjectCreate("label_object", OBJ_LABEL, 0, 0, 0);
ObjectSet("label_object", OBJPROP_XDISTANCE, 200);
ObjectSet("label_object", OBJPROP_YDISTANCE, 100);
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
int myNumber = MathMod(Seconds(),2);
if(myNumber==0) ObjectSetText("label_object", "EVEN", 14, "Times New Roman", Green);
if(myNumber!=0) ObjectSetText("label_object", "ODD", 14, "Times New Roman", Red);
//OrderSend(Symbol() , OP_BUY , 1 , Ask , 3 , Ask-25*Point, Ask+25*Point, "My order #2" , 16384 , 0 , Green);
//OrderSend(symbol , orderType , volume , price , slippage , stoploss , takeprofit , comment , identifier , expiration time, arrow color);
Sleep(1000);
//----
return(0);
}
//+------------------------------------------------------------------+
I’ve included a commented out OrderSend function lined up with the parameter types in order to get a headstart on the trading functions. In implementing a strategy, it’s probably best to use the docs and hike through the code yourself.
I hope this ends up being helpful to someone other than myself, but for the most part, this is or my own edification. I’ll add to this as time goes on and I learn more about the system. However, the learning curve for MQL4 is shallow - any intermediate or experienced programmer should have no difficulty picking it up, once they’ve cut through the initial piles of introductory crap (no offense intended to the tutorial writers, but sometimes “hello world” simplicity is worthwhile.)
If you see anything horribly wrong with anything I’ve written, let me know and I will correct it ASAP… I’m still trying to learn as much as I can, as fast as I can.