3

I am learning MQL4. On their reference website, creating a custom indicator is done like as follows:

#property indicator_chart_window

int init(){
    return(0);
}

int deinit(){
    ObjectsDeleteAll();
    return(0);
}

int start(){

    return(0); 
}

But when I create a new Indicator from inside the MetaEditor, I get another syntax, like this:

int OnInit()
 { 
 //--- indicator buffers mapping

 //---
 return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
            const int prev_calculated,
            const datetime &time[],
            const double &open[],
            const double &high[],
            const double &low[],
            const double &close[],
            const long &tick_volume[],
            const long &volume[],
            const int &spread[])
 { 
//---

//--- return value of prev_calculated for next call
return(rates_total);
}

Why is it different?

Is there a web link, or book someone can refer me to? From what I read the best place is the MQL4 website, but it looks different and I am not sure where to turn to now.

Any help will be appreciated. Thanks in advance.

user3666197
  • 1
  • 6
  • 50
  • 92
CyberShed
  • 41
  • 1
  • 7

2 Answers2

2

Where the observed difference comes from?

An MQL4 language has moved ( somewhere about a MetaTrader4 Terminal LiveUpdate release Build 572 .. 6xx ) a "half-step" towards another generation of the MetaQuotes programming language ( named an MQL5 ).

While MQL4 language has spanned many years and is a mature and stable, both in concept and execution, the "new"-MQL4 introduces things, the "traditional" MQL4 language ( and the code-execution environment ) were not ready to use.

So, the observation you have made, is right due to the "old"-syntax code snippets residing on many web pages and a "new"-syntax, pasted inside a blank/new Indicator template by a "new"-MQL4-IDE.

What to do with that in practice?

Your "new"-syntax will work on any post-Build-572+ MetaTrader4 Terminals. You need not worry about an "incompatible" case, where "new" code would be colliding with an "old"-terminal, as MetaTrader4 Server has an internal setting, which dictates the "oldest"-allowed Terminal the Server is willing to allow to connect and serve. As a matter of fact, that means you will newer be able to connect / run a pre-Build-572 MetaTrader4 Terminal connected to a broker side, where the "new"-code could collide with "old"-syntax rules.

Anyway, for a sake of an MQL4 syntax disambiguation, there is a compiler directive available:

#property strict             // read MQL4-IDE Help file for more details
user3666197
  • 1
  • 6
  • 50
  • 92
  • Thanks for the reply. So what I am getting from what you are saying and please correct me if I am wrong: To buy a book MQL5 and study that instead of MQL4? – CyberShed Mar 29 '15 at 01:02
  • Salutes to Scotland,Jamerfjr! First, let me excuse not to dare recommend anything without a proper knowledge about all your needs and the target application development context. I can't say anything without taking care of these important pieces of information. **Nevertheless**, the dilemma is not in place. MQL5 has come after many years of silence and the "server-side" of the business -- the FX Brokers -- do not move into the MQL5 direction any fast ... and not only God knows why ... So, in case your Project goes seriously into Production-grade system, do not hesitate to ask more, or email me – user3666197 Mar 29 '15 at 18:29
2

There are some changes to the MQL4 after build 600. The OnInit() is the Init-event handler. You can still use Init() as it is backward compatible. OnInit() is the newer practice.

Some of the online documentations are outdated and are not yet updated. This link provide info on the changes: http://docs.mql4.com/mql4changes

jlee88my
  • 2,935
  • 21
  • 28