MQL5 Tutorial

MQL5 Tutorial

By Raimund BauerBusinessTechnologyInvesting
Download on the App Store

All MQL5 Tutorial episodes

  • MQL5 TUTORIAL – SIMPLE POSITION INFO
    In this video we are going to talk about position properties. This chart is about the Australian against the Canadian Dollar. We have a position ticket number: 95885457, that’s the same ticket number we see here. Now we want to find out how we can create an Expert Advisor that is able to output the position properties directly on the chart. To do that please click the little button here or press F4 on your keyboard and now you should see the Metaeditor window and here you want to click on “File, New, Expert Advisor” from template (template), Continue (Next). I will call this one: SimplePositionInfo. Continue (Next) and Continue (Next) and Finish, and now everything above the “OnTick“ function here can be deleted and please also remove the two comment lines. The first thing we need to know is: “if positions for this currency pair exists”. To find it out we use “PositionSelect” and it will check the current symbol on the chart, and if that is true we are going to continue. If we would have no open position this value would be false. Let’s open the “if” statement and now we are going to “count down the number of positions until zero”. That is done by using a “for” loop, it uses the function “PositionsTotal” and will subtract one (-1) as long as the counter is above or equal zero. Now we want to calculate the ticket number. That is done by using the function “PositionGetTicket” for the currently selected position. We can also calculate the currency pair for the position by using “PositionGetString(POSITION_SYMBOL). The open price for the position is calculated by using: “PositionGetDouble(POSITION_PRICE_OPEN)” – that is the open price here in this column. To calculate the current position price we use: “PositionGetDouble(POSITION_PRICE_CURRENT)” If you want to know the position profit you can use: “PositionGetDouble(POSITION_PROFIT)” but don’t forget to calculate the position swap because position swap fees can have a big impact on your profit. You can get the information by using: “PositionGetDouble(POSITION_SWAP)”. If you don’t see any swap fees here they are simply hidden, just enlarge this part, right click and select: “Columns , Swap” and here it is! Currently we don’t pay any swap fees but if you intend to hold your position overnight that can be very costly! So, what else do we need? We want to calculate the current position net profit. That is done by simply adding position profit and position swap. Now if the position symbol equals the symbol on the chart by using the “comment” function it will output a text followed by the calculated value for each text and continue in a new line. This is the end of the “if” statement we also need to close the “for” loop from above and that should be it. Oh, this one belongs to the first “if” statement and the final one will close the “OnTick” function. Okay. If you don’t know if you have the right amount of braces, you can mark one and Metaeditor is going to mark the other one that belongs to this one. So, let’s mark this one, and yes it belongs to the for end brace. Now let’s compile the code. Everything is fine. So, now you can click on the little button here press F4 to go back to Metatrader. In Metatrader we are just going to open a chart, let’s close this position here and open a new one for testing purposes. Now you can simply drag the file: “SimplePositionInfo” on your chart, please allow automated trading and click “OK”. When the next price change comes in we should see this. Let’s try another currency pair! Drag it on the chart, allow automated trading, wait for the next tick… Oh I forgot I need to open a position! So, let’s do a sell position here! This is the new sell position and when the price changes we see the information that we have calculated for our open position. If you know how to do that, you can use the information to calculate a lot of automated things for your Expert Advisor. For example: you could close your position if the profit has reached the 10 Dollar m...
    7 min
  • MQL5 TUTORIAL – Why you need to remove yourself
    In this video I wanted to ask you something. Is this a good or a bad result? The system is trading, it’s trading automated, it’s the Forex Trading Framework from October 2017 and we made a profit, the balance is at almost 10% profit and the equity is also cashflow positive. When you have a look at the history graph this would probably be a system that you would like to trade. This blue line is the balance curve and you will see the balance curve moving upwards all the time, but the little green line here is the equity and right at this point the equity would have been only 96,904 Dollars, and that’s most likely the time when a human trader would stop to trade this system, because this is the point where it hurts. Well, the automated system, like the Forex Trading Framework doesn’t care, it continues to trade based on the rules that you have created. You might have other rules for your life, for example: I have a strict rule not to eat any chips and I’m able to obey to this rule when I never buy chips, but from time to time my wife might buy a bag of chips and it appears in our kitchen… Now it’s sitting there staring at me and it’s just a question of time when I’m going to open it. Once I have opened that full bag of chips it’s just a question of time when it’s an empty bag without any chips. That happens all the time and now the question is: if a simple bag of chips is able to make me break my own rules, what do you think would happen when I see my account go down for several thousand Dollars, like here? This is a drawdown that is bigger than 15,000 Dollars and it seems to get worse here, and now you start to shiver. Oh my God! I’m going to lose all my money! And now you quit. But your trading system won’t do that, it will go on to act as if nothing had happened, and it might see several other drawdowns here. This one is even worse! This would be over 40,000 Dollars drawdown. I couldn’t stand that but my system can! Well, all of this is absolutely no problem when you just see a few lines going up and down on a demo account, but once it is real money the meaning of these lines here will completely change your behavior, but it won’t change the code of your trading system. So, if you can get your trading system to trade exactly according to your rules on a demo account the only thing you need to do, is to get yourself out of the way on your real account. In the next video we are going to talk about an easy way how to do this. If I stop the strategy test right here, my results will be very good. I made 41,120 Dollars net profit, but most likely you would kill the system when the equity says you only have left 81,957 Dollars. This is why brokers make so much money and it’s also the reason why an automated system is a better trader. If there is one thing that is very predictable, it’s the fact that human emotions will cause the biggest losses and most likely you will create these losses yourself just because you kill your own system right here in the middle of the game and it will never see profits because you stopped it to get rid of your own emotional pain.
    5 min
  • MQL5 TUTORIAL – SIMPLE BWMFI EXPERT ADVISOR
    In this video we are having a look at the BWMFI Indicator, it’s the colorful indicator for the Market Facilitation Index by Bill Williams; BW is for Bill Williams and MFI is for the Market Facilitation Index. The Market Facilitation Index can create bars and several colors. The MFI indicator is used to find correlations between the indicator and the volume, so if you want to know how to use it, just go to the Wikipedia webpage for the Market Facilitation Index. They also say as an indicator on its own the MFI is of no significant value, but it’s part of MQL5 and Metatrader5, so now we want to find out how we can create an Expert Advisor that is able to calculate the BWMFI value on our chart. To do that, please click the little button here or press F4 on your keyboard and now you should see the Metaeditor window and here you want to select “File /New /Expert Advisor” from template (template), Continue (Next). I will call this one: “Simple BWMFI” (SimpleBWMFI). Now click on continue, continue and finish. Now everything above the “OnTick” function can be deleted and we also want to remove the two comment lines here. We start by creating an array for the Expert Advisor, we will call it: “BWMFIArray”, it’s a double array, so it can hold floating type values. Now we want to define the BWMFI Expert Advisor, and we used the MQL5 function: iBWMFI. That’s a built-in function, you can find it in the MQL5 reference, in the technical indicator section. It needs only three different parameters. The first one is for the symbol, the second one for the period, and the third parameter is for the applied volume. Actually, when you click on “Insert /Indicators /Bill Williams /Market Facilitation Index” you will see that the standard value is the tick volume so we are going to use “_Symbol” for the currently selected currency pair on the chart, ”_Period” for the time period you have selected on your chart, and the third parameter is Volume_Tick because we want to use the standard default volume that comes with the Market Facilitation Index Indicator. Okay. Let’s go on and sort the array from the current candle downwards, this is done by using “ArraySetAsSeries” for the BWMFIArray we have created here. For our defined EA we want to use the data for one buffer, that’s basically for the first indicator line. For example: for iBands – the Bollinger Bands – we would have three different buffers. The baseline would be buffer zero, the upper band would be buffer one and the lower band would be buffer two. We need to use the value from the current candle for at least 3 candles and store it in our array. We use “CopyBuffer” for the BWMFI definition for buffer zero because this histogram here just needs one buffer and we copy the values from candle zero – that’s the current candle, candle zero is always the candle that is built on the right side here, this would be candle one, candle two, candle three, and so on – and we want to store the data in the BWMFI array (BWMFIArray) we just created. And now we can actually calculate the value for the current candle. To do that we just have to have a look at the values for candle zero of the BWMFI array and we use “NormalizeDouble” and the five, because the indicator just needs to have five digits behind the dot. Finally, we create a chart output, this is done by using the “comment” function, it will output the word: “BWMFIValue” followed by the actual value that we have calculated here. Okay. That’s it. Let’s click on the compile button or press F7, and now you should see zero errors and zero warnings and in that case, you can click on the button here or press F4 to go back to Metatrader. In Metatrader you want to click on “View/Strategy Tester” or press CTRL and R, and here you want to select the SimpleBWMFI.ex5 file. Mark the visualization option here and click on start. And now you should see something like this on your chart, you can see colorful bars here, next here is the calculated value and as I said I’m...
    7 min
  • MQL5 TUTORIAL – SIMPLE FRACTAL ADAPTIVE MOVING AVERAGE
    In this video we want to create an Expert Advisor for the Fractal Adaptive Moving Average Indicator. Whenever the blue line is below the candles here that would be a buy signal and when the line is above the candles like here; that’s a sell signal. Now how can we create an Expert Advisor that is able to output our buy or sell signals directly on the Forex chart for Metatrader 5? To do that please click on the little button here or press F4 on your keyboard and now you should see the Metaeditor window. Here you want to click on file “File /New /Expert Advisor” from template (template), Continue (Next). I will call this one: “Simple Fractal Adaptive MA” (SimpleFractalAdaptiveMA). Continue, continue and finish. Now you can delete everything above the “OnTick” function and the two comment lines here. Let’s start by creating a string variable for the signal – we will also call it signal – and we don’t assign any values here. We also need to create a price array, that is done by using “MqlRates”. “MqlRates” is an MQL5 function and it stores information about the prices, volumes and the spread so whenever you need to get an open, high, low or close price just build a price array with “MqlRates”. Afterwards this array needs to be sorted by using “ArraySetAsSeries”, that will sort the price array from the current candle downwards and we use “CopyRates” to fill the price array with data for 3 candles. “CopyRates” takes a few parameters, the first one is the symbol parameter; that’s for the currency pair on the chart, the second one is for the currently selected period on the chart; like a 1-minute chart or an hourly chart. The zero here is the current candle zero – that’s always the candle on the right side of your chart – this would be candle 1, this would be candle 2, and so on. We just need the data for 3 candles and store the result in our price array (PriceArray). Now let’s create another array for the Expert Advisor, this one will be called: “FramaArray” for Fractal Adaptive Moving Average and we use the “iFrAMA” function that comes with MQL5 for the current symbol on the chart and the current period on the chart. It is calculated based on 14 candles – that’s also what you see when you right click on the indicator and select properties of the Fractal Adaptive Moving Average. Here are the 14 candles and the calculation is based on the close price. This parameter would be for the shift – that’s also 0 in the default settings – and in the last parameter we use “PRICE_CLOSE”. Now we have used all the default parameters here. This array also needs to be sorted, so we use “ArraySetAsSeries” once again and with “CopyBuffer” we fill our Frama array based on the definition we have created here for just one line; that’s buffer 0, from the current candle; that’s candle 0. We need the data for 3 candles – that’s the three here – and we store it in the Frama array. To get the signal we need to calculate the value for the last candle – that would be candle one in the Frama array and we would like to sell if the Frama value is above the last candle high. Like in this case. In MQL5 we do it by using an “if” statement, so if the Frama value is above the high of candle one in our price array we want to assign the word: “sell” to the signal. In the other case, if the Frama value is below the last candle low; so, if Frama value (FramaValue) is below the low price of candle one (PriceArray[1].low) in our price array we want to assign the word: “buy” to our signal. We also want to create a chart output directly on our Forex chart that is done by using the “comment” function. It will output the word: “FramaValue” for the Fractal Adaptive Moving Average value, followed by the calculated value. And in a new line we want to output the word: “Signal” followed by the calculated signal. When you are done you can click on the compile button here or press F7, and that should work without any errors or warnings and if that is the case please click on a little...
    7 min
  • MQL5 TUTORIAL – SIMPLE CHAIKIN EXPERT ADVISOR
    This time we want to create an Expert Advisor for the Chaikin Indicator, it’s an oscillator. When the blue line here crosses the dotted line from below, that is a buy signal and in the other case if the line crosses from above that’s a sell signal. Now how can we create an Expert Advisor that is going to calculate the value and to create an output for the signal here on the chart? To do that please click the little button here or press F4 on your keyboard, and now you should see the Metaeditor window and here you want to click on “File /New /Expert Advisor” from template (template), Continue. I will call this version “Simple Chaikin” (SimpleChaikin). Continue, continue and finish. Now let’s delete everything above the “OnTick” function and the two comment lines. Step one is to create a string variable for the signal – we will also call it signal and we do not assign any value because we are going to calculate that later on. The next step is to create an array for several prices, it will be called: “myPriceArray” and we use the type double, so it can hold floating-point values. Now we can define the properties of the Chaikin Expert Advisor by using the built-in function called: “iChaikin” for the current symbol on the chart and the period on the chart. If you wonder why we use a 3 and a 10 – that’s also what you see here – because in the properties of the Chaikin Oscillator you will see a value for the Fast Moving Average and another one for the Slow Moving Average. This one is the 3, here is the 10 and we are going to use the Exponential Moving Average so our next parameter will be MODE_EMA. In the last parameter we want to use the tick volume, so we use VOLUME_TICK. Now we are going to sort the price array from the current candle downwards, this can be done by using “ArraySetAsSeries” for the price array we have created here (myPriceArray) and with “CopyBuffer” we fill our defined EA for the Chaikin Indicator for one line; that’s buffer zero, from the current candle; that’s candle zero, for 3 candles – that’s the 3 here – and we store the result in our price array. To get the value of the current candle we assign the value from candle zero of our price array to the variable called: “ChaikinValue” and to get the value of the last candle – so this one is the current candle, that’s candle zero and this would be candle one – so, we get the price information for candle one from our price array and assign it to a variable called: ”PreviousChaikinValue”. Then when the blue signal line crossed the dotted line here from below – if ChaikinValue for candle zero is above zero now and if the previous ChaikinValue was below zero – that would be a cross over and in that case our signal would be buy, and we would like to sell if it crossed the zero line from above. So, if the blue line is crossing from above here in MQL5 this would be: if ChaikinValue0, that would be a sell signal, so we assign it to our variable called: signal. The last thing we need to do is to create a chart output by using the “Comment” function that will output the text: “ChaikinValue”, followed by the calculated value and in a new line we want to have the text: signal followed by the created signal. Please don’t forget the closing bracket and when you are done you can click on the compile button here or press F7 and that should work without any errors or warnings, and if that is the case for you, please click here or press F4 to go back to Metatrader. In Metatrader you want to click on “View/Strategy Tester” or press CTRL and R, and here you want to select the SimpleChaikin.ex5 file. Mark the visualization option here and start a test. And here we are! Let’s speed it up and you will see that it is going to create buy or sell signals directly on your Forex chart and you have coded it yourself with a few lines of MQL5 code.
    6 min
  • MQL5 Tutorial – Advanced RSI Entry Expert Advisor
    In this video, I wanted to show you how we can create an Expert Advisor Entry that is trading the RSI, like I’m doing here in the framework. As you can see this EA is trading like crazy! Most people would get out right here when the pain is very high and the loss is the biggest! That’s why 80% of all the traders lose their money! You see the Expert Advisor is picking up its course and it’s still rising but I bet you couldn’t stand this! Let’s slow it down a little bit, because this is the entry signal we are using – it’s the RSI indicator. Most of you might have heard of it or maybe you have traded it yourself. Now we want to find out how we can create an Expert Advisor that is able to trade the RSI indicator. We are trading the Golden Goose Framework from October 2017. This is an advanced Expert Advisor; the framework is able to handle all the open positions. If you want to learn how to code your own framework you can take the course and if you are interested in automated trading and just want to try it you can get the 1 Dollar trial on: forextradingframework.com. I can’t explain everything here in a 5 minutes video but I can explain how we calculate the entry. To do that please click on the little button here in your Metatrader or press F4 on your keyboard. Now you should see the Metaeditor and here you want to click on “File/ New/ Expert Advisor (template)” from template. Continue. I will call this one: CheckEntry_RSI.mq5 (Check Entry RSI) because that’s the name of the imported entry in the framework. Now you can click on continue, continue and finish. Now you can delete everything within the file because we are going to create a function that is called: CheckEntry_RSI. In the first step we create a string for the signal, that will be also called “signal” – right now it has no value, because we are going to calculate the value later. Right now, we need to create an array for the price data. That will be called: myRSIArray, and after we have done that, we define the properties for the RSI. That can be done with the built-in function “iRSI”. It takes some parameters. The first one is the current symbol on the chart. The second one is the period on the chart. The RSI calculation is based on the last 14 candles – that’s also what you see here, and it will be calculated based on the close price. Now we want to use “ArraySetAsSeries” to sort the price data from the current candle downwards and with “CopyBuffer” we fill the defined EA with (data) called: “myRSIDefinition” that we have defined here for the first buffer – that’s buffer zero (buffer 0) from the current candle – that’s also index 0, for three candles – that’s the three here – and save the result in our RSI array (myRSIArray) that we have created here. Now that we have done that, we can calculate the current RSI value by getting the value of candle zero in our RSI array and we use “NormalizeDouble” and this two, because the RSI indicator also only has two digits behind the dote and “NormalizeDouble” will round the floating-point number to the specified number of digits after the decimal point. If the RSI value is greater than 70 – that’s when the value is above the dotted line here – we consider that to be a sell signal. In the other case; if the RSI value is below 30 – that would be the case when it’s below the dotted line here – we consider it to be oversold and now we have a buy signal. That’s almost it! Finally, we need to return the signal to the main module. Now I need to save all the files here, reompile my framework – that worked without any errors. So, now I can click here or press F4 to go back to Metatrader. Oh! If you don’t know what a framework is; basically, it’s a collection of code here that handles all the other staff. The only thing you have to define in a framework is the entry. This is the German version, but you could also find the English version on forextradingframework.com. Okay. Now I am back in Metatrader and I need to click on View/ Strat...
    7 min
  • MQL5 TUTORIAL – HISTORY FILE QUALITY ISSUES IN BACKTESTING
    Today I have released a new version for the Forex Trading Framework. This is the kind of balance curve I am used to and to ensure that the Forex Trading Framework is working on your PC I’m always testing it on different computers. So, I pick a currency pair here and start a test. What I see are these irregularities here, this is looking like a saw, very different from the smooth line of the balance curve on my main PC. Let’s trade the Euro /US Dollar here! I’m trading the year 2016 from January to December with 100,000 Euros, the leverage is 1 to 500 and here we have the same problems. Let me change the language to English here, let’s select the British Pound against the US Dollar, start a new test, and this one is a disaster! It made a loss, and now you might think that the whole system is crap, but if you look at the history quality here you will see that it’s 0%! It says we have just traded 6207 bars. Let’s trade the same currency pair on the main PC, it’s also the year 2016, we use 100,000 US Dollars and a leverage of 1 to 500. Let’s see how this test is looking! The balance curve is going up, so this is what I have expected. It does take some time now. On the other PC the whole test took only about 4 seconds. This is the end result, and when you click on the results tab you will see that we have just traded 368,430 bars. The history quality is 95%, so if your trading system is giving you bad results please double check your history quality and the number of bars you traded, because that might be the only difference between a big profit and a loss.
    4 min
  • MQL5 Tutorial – How To unvover hidden Swap Fees with MQL5
    You might have noticed that sometimes your profit isn’t quite exact, and the reason might be that you don’t consider swap to be relevant. You might maybe not even see any swap here but this is going to change. If you do a right click here and select columns, you can enable the swap column. And now you see it. These are only a few cents but it can add up over time and now it would be a few dollars. Swap fees are charged when you hold a position overnight and it can quickly eat up all your profit. It can be positive or it can be negative and it is different for every currency pair. To find out how many swap fees you have to pay we will create a simple little Expert Advisor. To do that please click the little button over here or hit the F4 key in the MetaTrader and that will bring up the MetaEditor. Let’s select new Expert Advisor from template, continue. Give it a name, click on continue and finish. Let’s remove everything above the On Tick function and the two comment lines and now we need a double variable for swap long trades. And it will use the SymbolInfoDouble function for the current symbol to get the value of symbol_swap_long. And for the swap fees you have to pay for short trades, we will do the same and use SymbolInfoDouble for the current symbol and this time we will use symbol_swap_short. Let’s use the comment function to get the output right on the chart. Let’s compile the Expert Advisor and it says no errors and no warnings. Click this button or hit the F4 key to bring up Meta Trader, go to the Strategy Tester and select the simple account info.ex5 file. Make sure to enable visualisation here, and now start the test. And here is our output. You see that the swap fee, the negative value, is much higher than the positive value. Let’s check it for another current pair, this time we are going to use Euro US dollar and for this currency pair there isn’t any positive value. That means you will be charged a swap fee, no matter if you open a long or a short trade. And this is something almost nobody knows, but with this little Expert Advisor you are now able to find out if your broker is charging for swap, and in which currency pairs you cannot make a swap gain. And that might help you to avoid unnecessary swap costs.
    4 min
  • MQL5 TUTORIAL – ADVANCED RISK AND PROFIT
    In this video, I wanted to talk about risk. This is the Forex Trading Framework from October 2017, right now it’s trading the RSI indicator, this is the “graph” tab from the strategy tester and here you can see that we make a little bit profit, but most people would say that this is very boring for an account of this size it’s not making enough profit and it takes too long. Well, the other side of the equation is that this is also not very risky. We didn’t make much profit but we also didn’t have a lot of risk. The total net profit is 43 Dollars. I know every trader has a different risk tolerance and so I included an external trading risk user variable here, right now it’s very low. This input modifier here in the source code allows you to change the variable without recompiling the code. Ten would be very high and everything above would be insane! Now, let’s change this risk here and use a value of five! This time it seems to go a little bit faster, we already made some profit, but you have also increased the risk here. Now, you might say higher profits are worth a higher risk but believe me a lot of people would close the account right here! Now our test should be finished soon, this is it. This time we made a total net profit of 5213 Dollars. Now, let’s change the trading risk variable to ten – I would consider this one as very risky, but you might see that it trades a lot of positions. Right now, we have already made 14 thousand profit, but those drawdowns here will get bigger whenever you increase the risk. Believe me, seeing your real account going down like here will cost you sleepless nights, but the possible gain is also much higher now. As I said; I wouldn’t trade that on a real account but you might have a different risk tolerance. By the way, you never could trade so many positions without automation! And that’s the reason why so many people complain about missing entries and why automated trading becomes more and more popular these days. This test series should come to an end very soon and I would already say we will make a good profit, but trading is risky and everything can happen. When you see a big drawdown here that will eat up 50,000 Dollars you will be in real pain, no matter if you made 150 grand before! It’s no problem to watch something like this on a demo account but it’s a very different experience on a real account! Here is our result! We made a total net profit of 99,980 Dollars. Now, I wouldn’t recommend to go above ten, but just for demonstration purposes let’s change the value to twenty! I will maximize the window here and now you can see what is possible! This is the exact same Expert Advisor, trading the same currency pair. The only thing I have changed is the trading risk, and this is how it would perform if you had traded the Euro/ US Dollar in 2016.
    6 min
  • MQL5 TUTORIAL – SIMPLE DOUBLE EXPONENTIAL MOVING AVERAGE
    In this video, we are going to trade the Double Exponential Moving Average (DEMA) that’s basically a red line you see on your chart. If you hover the mouse over the line you will see that the Double Exponential Moving Average (DEMA) has a value. You can find the indicator if you click on: “Insert, Indicators, Trend, Double Exponential Moving Average”. The standard value is 14 candles, it has no shift and it applies to the close price. The Double Exponential Moving Average can be called by using the MQL5 function: iDEMA. The function only has one buffer, it takes the current symbol, the current period on the chart, the third parameter will hold a period. In our case it’s a 14. The shift will be zero because we don’t want to move it to the right or to the left. For the applied price, we will use the close price. You see that it is going to generate buy or sell signals on our chart and basically a buy signal will always occur when the Double Exponential Moving Average is below the close prices of our candles and when it’s above our close prices here we will generate a sell signal. Now how can we create an Expert Advisor that is able to calculate the indicator? To do that please click on the little button here or press F4 in your Metatrader and now you should see the Metaeditor window. Here you want to click on “File, New, Expert Advisor (template)” from template. Continue. I will call this one: SimpleDoubleExponentialMovingAverage. (Simple Double Exponential Moving Average) Click on continue, continue and finish. Now everything above the “OnTick” function can be deleted and please also delete the two comment lines here. Okay. Let’s create an array in the first step. We will call it: “PriceInformation” and we use MqlRates to create it. After that we are going to sort it from the current candle to the oldest candle. That is done by using the “ArraySetAsSeries” and now we use “CopyRates” to fill the array with price data for the current symbol and the period that is currently selected on the chart. We will start with the current candle, that’s candle zero. We only need prices for three candles and those candles go into our “PriceInformation” array. Let’s create a second array! This one is for the Moving Average and now we use the iDEMA function of MQL5 to define the properties for the Double Exponential Moving Average. The result will be stored in an integer variable called: “movingAverageDefinition” and we use the function iDEMA for the current symbol and the currently selected period on the chart for 14 candles. Let’s exchange this one to a zero, because we don’t want to use any shift value here and the Expert Advisor should be calculated based on the close prices. Let’s also sort this array by using “ArraySetAsSeries” to sort the price array from the current candle downwards and once again we use “CopyBuffer” for the “movingAverageDefinition” we have created here for buffer zero, because the Expert Advisor only has one signal line. We start from candle zero, that’s the current candle. We only need the values for three candles. You could use more but calculating three candles is much faster and the values will be stored in “myMovingAverageArray”. To calculate the Expert Advisor for the last candle we select candle one from “myMovingAverageArray” and store the result in “myMovingAverageValue”. Now that we have done that, we check if “myMovingAverageValue” is above the close price of candle one in our “PriceInformation[1]” array. In that case we have a sell signal, so we use the “Comment” function to create a chart output for the sell signal. In the other case if “myMovingAverageValue” is below the close price for candle 1 – that would be a buy signal. So, let’s create a chart output that’s about it. Please don’t forget the last bracket and when you’re done you can click on the Compile button here – oh I messed up this character here! Let’s try it again, and this time it worked without any errors and no warnings so now we can cli...
    7 min