|
|
Standart library for MT4 expertsIntroductionThis article requires some knowlege of MQL scripting language. As tutorials are available at MT4 home page, I am not going to explain basics of the language here. Instead, we are going to create an expert for MT4, making sure all little pitfalls are avoided. Also, as part of the code we are about to create is the same for all experts, we are going to move it to a separate library file, this way, if we need to edit this code, we will only have to do it in one place, instead of making the (error-prone) changes to every expert we have. This article is about the abovementioned library file, for an example of an expert using this library, see the next article. How it is supposed to work?As the library we are working on is designed to be part of an expert (MTS, capable of trading without human interference), let's discuss problems, that improperly written experts may have, as well as ways to resolve them. An expert contains 3 functions, init(), start() and deinit(), first is called after the expert is used for the first time (duting the current session), the last one is called before the expert is removed (MT4 is closed, chart containing the expert is closed, or an expert is removed from the chart. As for the start(), it is called every tick. This is the first, and the most important of possible pitfalls, as we want our expert to work the same way during the testing and in the run time. But there are no ticks during the testing! Of course, MT4 TRIES to emulate the most probable prices, using available OHLC (Open-High-Low-Close) data, but still... Also, if an expert is a complex one, or if we have many charts, with experts attached to them, the computer slows down. The most common solution is to trade "at the end of the bar". It means, that we use a piece of code, located at the beginning of the start() function, to figure out, if this tick is the first tick of a new bar, and only if it is so, we run the rest of the "start", otherwise we simply stop, until the next tick. The second thing to keep in mind is that simple fact, that at the beginning of a new bar we DO NOT know it's High, Low and Close - we only have Open. This pitfall we have already discussed in the previous article, but as this is the "favorite" beginner's error, let me repeat: the code below is incorrect: double x = Close[0]; If you want your expert to run every tick, it is OK. In this case, the Close[0] means not "the bar's close", but simply "the last value". Same, as the High[0] simply means "the highest value since the bar's beginning, so far". Note, it is "so far", not "till theend of the bar". As soon as you try to do testing, using historical data, the Close[0] will create a problem. Because MT4 "knows" what Close[0] is, for all the historical data. The testing will go fine, but the result will not be applicable to real time trading, as in real time trading we never know what High, Low, Close[0] are! In our expert, we are going to use High[1], Low[1], Close[1], to make sure our trading system does not "look into the future". Open[0] is fine. The thirt important thing is related to the fact, that, regardless the multitasking nature of MT4, you can not perform two trades simultaneously. As the mater of fact, you need to make sure one trade is complete, and only then to proceed with the second one. This is not a big deal, if you only have one expert - a "normal" expert will not try to do few trades in a time anyway. Usually, it means, that your testing will go fine, and then, when you try to run your expert on your demo account, it will "compete" with other experts (provided you have more than one expert running) for the right to open/close/modify positions. As the result, one of the two trades will not be executed. There is a "cure" for this "deadlock", of course. All you need, is to make sure your experts trade one after another, and never - simultaneously. To do it, we are going to use "magic numbers". Magic number is an integer, associated with the order. We are going to extend it a little bit. For our experts, the magic number will uniquely identify a combination of an expert (expert name), a currency (say, EURUSD) and a time frame (say, H1). For example:
The first thing our expert will do, when the start() is called, is checking the global variable ("global" variables are "visible" to all experts, so experts can use them to exchange the information). If this variable is 0, an expert assigns its unique magic number to it (this approach is called "semaphores"). From this point on, all the experts "know", that they can NOT trade - until our expert releases the semaphore. To release it, at the very end of its start() function, it should assign 0 to it. Technically speaking, this approach should work, but we are going to add an extra level of "protection against competition between experts". We are going to assign them TIME when they are allowed to trade. Once again, it is just an extra precaution. Let's say, our experts are fast and robust. To run, their start() function need only 1-2 seconds. Then, if we start the first expert (one with magic number equal one) at the beginning of the bar, the second at the beginning of the bar PLUS 10 seconds, the third - at the beginning of the bar PLUS 20 seconds and so on, they (most of the time) will not even try to compete for order execution. The libraryThis is just a code to be included in all experts. We do not want to retype it every time, so we keep it separately. According to the coding standarts, it should be located in MT4's experts/include directory, but I prefer to have access to it from the list of experts, so I placed it to experts directory. Note, that in addition to "trading" functions, this code also contains some "reporting" functions. They are usefull when you test your expert, trying to figure out, why is it not working. Note also, that we use some variables here, that should be assigned values in the expert, for example, dStopLoss, dTakeProfit... It is convenient, of course, but in the same time, you need to remember, to use these names. For example, you can not use "take_profit" instead of "dTakeProfit" in your expert.
What is FOREX?
Free Stock trading Course The course consists of five E.mails and covers all essentials of Technical Analysis approach to Stock Trading. Compare to $$$ that you will have to pay elsewhere. All you need to do is to enter your name (nickname will do) and E.mail address in the form below. |
|
|
Keywords: review software stock trading
day trading e book
atr breakout
investing quote stock
archive quote stock
current quote stock yahoo
commodity market system trading
stock trend chart
stock day trading tip
sp 500 trading system
The language resembles the Basic, and gives you the access to many functions, such as convergence divergence test, chart pattern recognition, point figure charting and much more. historical market quote stock
history market quote stock
free system trading
day profit trading
day living trading
convergence divergence test
historical intraday stock data
definition ppo
formula macd
screen stochastic
real time day trading signal
downloader stock
chart pattern recognition software
wmt stock quote
computer system trading
historical stock quote free
day trading simulation
download historical quote
day trading pattern
stock index trading system
yahoo finance stock quote
simulator trading
moving average trading system
trading indicator
free intraday charts
day trading simulator
intraday quotes
technical analysis stock screen
yahoo historical stock quote
macd convergence divergence
technical indicator macd
point figure charting
technical analysis of stock trend
day trading techniques
technical analysis formula
day trading tutorial
day trading how to
macd formula
trading system
historical intraday data
technical investment analysis
online day trading education
day trading faq
stock market quote historical
trading course
historical daily stock quote
free real time streaming stock quote
day profit trading
previous stock quote
ppo software
current quote stock yahoo
archive quote stock
historical market quote stock
review software stock trading
stock trend chart
quick quote stock
day living trading
history market quote stock
sp 500 trading system
formula macd
free system trading
stock day trading tip
commodity market system trading
definition ppo
best system trading
investing quote stock
free software stock trading
day trading tool
free live stock quote
free streaming stock quote
stochastic definition
simulator trading
wmt stock quote
day trading simulation
day trading pattern
stock quote download
historical stock quote free
download historical quote
stock index trading system
current own quote stock yahoo
yahoo finance stock quote
past stock quote
day trading simulator
free stock trading system
best stock trading system
stock trading simulator
yahoo stock quote
historical stock quote
trading indicator
|