The reason for this chapter is to get familiar with the usage of Metalang or Metaquotes language editor. We assume that you install your Metatrader terminal and you made an account, you logged in and now you are staring at the markets as they are ticking. Usually, you did it the script kiddie's way: you took an indicator from the web and you copied under the ...experts\indicators folder, you started Metatrader again and then, you did the drag & drop move to put the indicator on the chart. Sometimes it works and it shows you some signals but usually the market is evolving and it's changing its DNA and the signals are no longer good. Or no signal at all? Or just some mumbo-jumbo hard to comprehend? Now you want to solve this problem by understanding what the indicators are really doing. And not only the indicators! Any decent expert adviser is relying on the signals provided by indicators: some indicators are custom, some are predefined by the guys who built the Metatrader platform. So, you are in Metatrader client terminal. That's right, is a client for the Metatrader server installed on your broker side (between you and the market). What you see is a customized Metatrader client terminal to have some icon or name of the broker. Some brokers are offering in-house made indicators. Whatever... Either you press F4 or you go to Tools/MetaQuotes Language Editor the result will be the same: There you have it. Now you follow the steps of the File/New or you press the icon with the plus sign. The result is the following (just choose Custom Indicator check button before you click Next): Now it's your choice to sign the customer indicator details with the indicator name which is important, otherwise you will not pass the Next step. So, you have to give a name for the indicator: in this case "Hello World" seems perfectly reasonable. The other details like "Author" and "Link" will not influence at all how the customer indicator will perform: are just some informations to make you feel better about what your are doing: You see also some parameters table which is scary at this point. What parameters? So, just pretend you know what you are doing that you don't need any parameters and click again "Next". Then, you see something again scarier if you are a beginner in this: Just check the "Indicator in separate window". That means that your indicator window will appear in a separate window than the market is on Metatrader. Instead of another "Next" you got this time "Finish". Press it with confidence and look at your creation: You can see on the Navigator window (you are still in Metalang editor) if you expand the indicators your custom indicator. Now move your eyes to the code. It's difficult, right? You didn't right any of these lines and yet it's there and it's generated. You can recognize though, the name: "Hello World" but with a funny extension : mq4. That's the reason you are looking at it in Metalang editor, 'cause it's a source file and it needs to be compiled. If you move a little bit higher your eyes (can you take your eyes of your creation?) you see in the toolbar the magic word "Compile". You need to compile your source code because Metatrader terminal doesn't understand this readable mq4 form as you have in Metalang. And the result will be "Hello World.mq4" ---> "Hello World.ex4" Press Compile and cross your fingers : It should be perfect: no errors and no warnings. If you look on right, on the Navigator little window you don't see anything. Where is your Hello World.ex4 file? In the moment it was compiled successfully, your Metatrader client terminal is able to see it(and to use it). Just expand the Metatrader's Navigator Custom Indicator this time and not the Metalang's Navigator. You go back to the market staring again: Now you can do what you did until now with the indicators you picked up from the web: drag and drop the Hello World custom indicator on the market screen. It has its separate window as you ask it to have(remember?) and: Press the tab named "Experts" (from the status bar of the Metatrader) and you will see some info: your indicator loaded successfully and it's initialized. But is not doing anything! Because you programmed it like this? To do nothing. To have some message on the console we have to go back to the Metalang editor. You didn't close it, right? If you did, just press F4 like before and you have again the source code of your custom indicator named "Hello World". Just for you to sleep well you need this indicator to do something, to write something at the console at least... and that's exactly the purpose of all "Hello World"'s in the programming languages tutorials : to give you confidence to continue because it's a long road to Forbes magazine. If you take out all the comments from the code (the lines starting with //) you can see it better:
#property copyright "Copyright 2012, MetaQuotes Software Corp." #property link "http://www.metaquotes.net" #property indicator_separate_window int init() { return(0); } int deinit() { return(0); } int start() { int counted_bars=IndicatorCounted(); Print("Hello World"); return(0); }So, basically you have one line which somehow tells Metatrader that you want for this indicator to have its own window:
#property indicator_separate_windowAnd then you have 3 functions: init(), deinit(), start() returning happily 0 . In the body of the start(), meaning between the { }, just add this line of code (something intelligent ending with ;)
Print("Hello World");What you have to do now? Don't look yet at Metatrader terminal, you didn't finish the job: you have to compile your code. You modified it, right? Metatrader doesn't know about your genius line of code that can break the markets. Just press Compile: Again 0 errors and 0 warnings and if you look now on the Experts tab from Metatrader you customer indicator is printing the "Hello World" message continuously. This concludes the introduction.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.