MQL5 Tutorial

MQL5 Tutorial

By Raimund BauerBusinessTechnologyInvesting
Download on the App Store

All MQL5 Tutorial episodes

  • MQL5 TUTORIAL – SIMPLE IDEMA STANDALONE EXPERT ADVISOR
    In this video we are going to create a standalone Expert Advisor for the Double Exponential Moving Average Indicator; it’s the red line here and whenever it crosses our price it will produce either a buy or a sell signal and now we are going to find out how to create an EA that is able to actually trade the signals automated. To do that please click on the little button here or press F4 in Metatrader. Now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will give it the name: “SimpleIDEMAStandaloneEA”, click on “Continue”, “Continue” and “Finish”. Now we can delete everything above the “OnTick” function. We start by including the file: “Trade.mqh” that is part of MQL5, now we create an instance of “CTrade” that will be called: “trade”, let’s remove the two comment lines here. Inside of the “OnTick” function we calculate the Ask price and the Bid price by using “SymbolInfoDouble” for the current symbol on the chart, “SYMBOL_ASK” will give us the Ask price while “SYMBOL_BID” will give us the Bid price and with “NormalizeDouble” and “_Digits” we calculate the number of digits behind the dot because some currency pairs have 3 and others have 5 digits behind the dot. Now let’s continue by using “MqlRates”, we create an array for the price data that will be called: “PriceInfo”, we sort the price array (PriceInfo) from the current candle downwards that is done by using “ArraySetAsSeries” for the price info array (PriceInfo) we just created and by using “CopyRates” we fill the array (PriceInfo) with price data, that is done for the current symbol and currently select a period on the chart, we start with candle 0 (zero) – that’s the current candle – and we copy the prices for 3 candles and store it in the price info array (PriceInfo). We also need to create a signal that is a string type variable, this is an empty value because we need to calculate the value later on. Let’s create an array for the price data of the Expert Advisor that will be called: “MyEaArray” and we define the properties for the Double Exponential Moving Average by using the built in “iDEMA” function that comes with MQL5 for the current symbol and the currently selected period on the chart. We will calculate the result based on 14 candles without any shift to the right or to the left and we use “PRICE_CLOSE” to calculate the results based on the close price. Now when you click on: “Insert/ Indicators/ Trend/ Double Exponential Moving Average” you will see that we here also see the period of 14 candles, the shift value of 0 (zero) and it is applied to the close price so that’s what we use here. Let’s sort that array also by using “ArraySetAsSeries” and use “CopyBuffer” to fill it according to the my moving average definition (movingAverageDefinition) that we have created here for buffer 0 (zero) – that’s the red line here – from the current candle 0 (zero) for 3 candles and store the values in our price array (MyEaArray). Now we can calculate the Expert Advisor for the last finished candle – that’s candle 1 in our EA array – (MyEaArray) and we assign the result to the variable “myMovingAverageValue” and if that moving average value (myMovingAverageValue) is bigger than the close price for candle 1 in our price info array (PriceInfo) that would be a sell signal and in that case we assign the word: “sell” to our signal. Otherwise if the moving average value (myMovingAverageValue) is below the close price for candle 1 in our price info array (PriceInfo) – that’s when we want to buy – so now we assign the word: “buy” to our signal. So if our signal equals sell and “PositionsTotal” is below 1 – in other words we don’t have any open positions – that’s when we use “trade.Sell” to sell 10 micro lot. Otherwise if the signal equals buy and we have no open positions we use “trade.Buy” and buy 10 micro lot. In the last step we are going to use the “Comment” function to output the words: “Th...
    7 min
  • MQL5 TUTORIAL – SIMPLE VOLUMES FILTER
    In this video we are going to create an Expert Advisor that is going to use the Bollinger Bands as an entry signal and it will also use the Volumes Indicator as a filter. Only if the filter is positive and we have a buy or sell signal we want to open a new position. This is going to be a standalone version and it contains three separate modules. In this video we will talk about the filter function. Now how can we create a filter that is using the Volumes Indicator? To do that please click on the little button here or press F4 in your Metatrader, now you should see the Metaeditor window, this is the main module, it will include the Bollinger Bands Entry (CheckEntry_Bollinger) and the Check Filter module (CheckFilter_Volumes) for the Volumes Indicator. If you don’t know what the rest of the code does please have a look at the other videos in this channel. The “OnTick” function is called whenever the price changes and here we will use the “CheckEntry” function to get the signal, this is the Check Entry Module (CheckEntry_Bollinger) that will give us the signal for the Bollinger Bands but in this video we are going to talk about how to get the filter value. To do that we need a new file, this one, it is called: “CheckFilter_Volumes” and we are including it here using the “include” statement. Inside of the function we first need to create a variable for the filter that will be called: “filter” and we don’t assign a value here. Afterwards we are going to create an array for the prices that will be called: “myPriceArray” and now we define the filter by using the “iVolumes” function that is included in MQL5, we use “_Symbol” for the first parameter to get the currency pair, that’s for the selected time period on the chart and parameter 3 is “VOLUME_TICK” all in capital letters. We use “ArraySetAsSeries” to sort the prices from the current candle downwards for our price array (myPriceArray) and now we use “CopyBuffer” to fill the array with data. We are going to use the volumes definition (VolumesDefinition) that we have created here to fill the array (myPriceArray) for buffer 0 (zero) from the current candle 0 (zero) for 3 candles and store the values in our price array (myPriceArray). Let’s calculate the value for the current candle that is done by looking into candle 0 (zero) of our price array (myPriceArray) and we are going to store the value in the variable: “CurrentVolumesValue“ and now we do the same for the candle before – that is candle 1– and we will store that value in the variable: “LastVolumesValue“ and in the case that the current volumes value (CurrentVolumesValue) is bigger than the last volumes value (LastVolumesValue), with other words; we have a rising volume like here, that’s when we want to assign the word: “positive” to our filter. In the other case if the current volumes value (CurrentVolumesValue) is below the last volumes value (LastVolumesValue) that would be the case here, the current candle has a volume value of 14 and the volumes value of the candle before was 37 and in that case we want to set the filter value to negative so we assign the word: “negative” to our filter and last but not least we are going to use the return statement to return the filter value to our main module. So that’s done all by calling this function here and when the trading signal equals buy and the filter is positive and “PositionsTotal” equals 0 (zero) – that means we don’t have any open position – that’s when we use “trade.Buy” to buy 10 micro lot. In the other case if the trading signal equal sell and the filter is positive (remember we only want to use increasing volumes because in theory an increasing volume means that the trend is going to continue) and we only want to open a new position when we don’t already have an open position and in that case we use “trade.Sell” to sell 10 micro lot. Here is the final chart output that will output the words: “Trading signal:” followed by the calculated value for the entry signal and ...
    7 min
  • MQL5 TUTORIAL – SIMPLE BULLS POWER STANDALONE EXPERT ADVISOR
    In this video we want to code a standalone version for the trading of the Bulls Power Indicator this Expert Advisor is going to output buy and sell signals directly on the chart and it’s also possible to open positions with it, everything is done automatically and now let’s find out how to do that with MQL5! To do that please click on the little button here or press F4 in your Metatrader, now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this one: “SimpleBullsPowerStandaloneEA”, click on “Continue”, “Continue” and “Finish”, now we can delete everything above the ”OnTick” function and let’s also remove the two comment lines here. We start by including the file: “Trade.mqh” that one comes with MQL5 and now we are able to create an instance of the class: “CTrade”, that instance will be called: “trade” and we are going to use it to open new positions later on. First we need to calculate a few things, we start by calculating the Ask price and the Bid price that is done by using “SymbolInfoDouble” for the current symbol and we either use “SYMBOL_ASK” or “SYMBOL_BID” to calculate the prices, we also use “NormalizeDouble” and “_Digits” to automatically calculate the right number of digits behind the dot because some currency pairs only have 3 digits and other ones have 5 digits behind the dot. Now let’s create a string for the signal that will be called: “signal” and we don’t assign a value here because that is going to be calculated later on. First we use “MqlRates” to create an array for the prices that will be called: “PriceInformation”. “MqlRates” is a structure that stores the information about prices, volumes and spread. With “ArraySetAsSeries” we sort the array (PriceInformation) from the current candle downwards and by using “CopyRates” we copy the price data into the array (PriceInformation), we do it for the current symbol and the currently selected period on the chart, we will start with the current candle 0 (zero), copy the price data for 3 candles and store it in the price information array (PriceInformation). Now let’s create a second array (myPriceArray), this one will be the array for the Expert Advisor data, it is also going to be sorted downwards by using “ArraySetAsSeries”. Now we use the integrated “iBullsPower” function of MQL5 to define the bulls power EA (BullsPowerDefinition) for the current symbol and the currently selected period. The third parameter is 13 and if you click on: “Insert/ Indicators/ Oscillators/ Bulls Power” you will see that it is calculated based on the last 13 candles so that’s what we use here. Now we use “CopyBuffer” to fill the array (myPriceArray) with data according to the Bulls power definition (BullsPowerDefinition) that we have created here we do that for the first buffer – that’s buffer 0 (zero) – from the current candle 0 (zero) for 3 candles and we store the information in our price array (myPriceArray) Now let’s calculate the current value that is done by looking at candle 0 (zero) in our price array (myPriceArray) and we assign the value to the floating type variable Bulls power value (BullsPowerValue) and if the “BullsPowerValue” is above 0 (zero) we consider that to be a buy signal so we assign the word: “buy” to our signal. In the other case if the “BullsPowerValue” is below 0 (zero) we consider that to be a sell signal so we assign the word: “sell” to our signal and if the signal equals sell and “PositionsTotal” is below 1 – that will mean that we have no open positions – we use “trade.Sell” to sell 10 micro lot. In the other case if the signal equals buy and we also have no open position we use “trade.Buy” to buy 10 micro lot. Finally we want to create a chart output by using the “Comment” function, this one will output the words: “The current signal is:” followed by the signal. That’s it so far, please click on the “Compile” button or press F7 and if that works without an...
    6 min
  • MQL5 TUTORIAL – THE SECRETS OF SUCCESSFUL TRADING
    My daily updated real account results: https://www.myfxbook.com/statements/2559459/statement.html In this video I wanted to talk about a presentation that I have created for the last I think about two weeks, you can find it on: robottradingsystem.com and it contains so much useful information that I think that you might want to watch it. Actually it’s over two hours long, most of it is pure content, I talked about my numbers and my systems, this slide is showing the results for the Robot Trading System on I think September the 28th because that’s what it says here. Let’s visit the website, click on the real time account statement, today we have October the 11th 2018, let’s click on the Robot Trading System to show the current results and currently we have made 7.67%, the profit is 3,819 Dollars and 86 cents, that’s a little bit more than you see here and if you are interested we are going to talk about the 5 steps to develop a trading system that works. It doesn’t matter if you are ever going to buy something from me, I think that the content is something that you might find very interesting and it should help you even if you never consider being an automated trader. Here is a little spoiler; the one big breakthrough that actually became the foundation for the Robot Trading System can be found in the book: “Trade your way to financial freedom” by Van Tharp, Van Tharp once talked to a guy named Tom Basso and this guy thought it was possible to make profits with a random based entry if the other components are good enough. Let’s do that live, we will test it for the year 2018 up to the current day, let’s click on start, this shouldn’t take very long. Okay, in this case we made 50,249 Dollars end-result, that’s a total net profit of: 255 Dollars and 19 cents. Let’s repeat the test, this time it says we made: 125 Dollars and 59 cents and because of the random entry we have different results for the same time span. So if you are interested to find out the details and watch the results for the Robot Trading System for the last two weeks just follow the link and watch a little over 2 hours of video content to find the solution that works for you.
    4 min
  • MQL5 TUTORIAL – SIMPLE ENVELOPES STANDALONE EXPERT ADVISOR
    In this video we are going to find out how to create an Expert Advisor that is able to trade buy and sell trades for this Indicator; it’s called the Envelopes Indicator, we have just opened a new position everything is traded automatically and it will also output buy and sell signals directly on the chart. So how are we going to code it in MQL5? To do that please click the little button here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, I will call this one: “SimpleEnvelopeStandaloneExpertAdvisor”, click on “Continue”, “Continue” and “Finish”, let’s delete everything above the “OnTick” function and remove the two comment lines here. We start by including the file: “Trade.mqh” that comes with MQL5 and afterwards we create an instance from “CTrade” that is called: “trade” and we are going to use it to open new positions later on. Inside of the “OnTick” function we need to calculate the Ask price and the Bid price that can be done by using: “SymbolInfoDouble” for the current symbol on the chart, with SYMBOL_ASK” we will get the Ask price and with “SYMBOL_BID” we will get the Bid price. If you click on a currency pair you will see a red and a blue line, the red is the Ask price and the blue is the Bid price. There are some currency pairs with 3 digits behind the dot and others have 5 digits, so we use “NormalizeDouble” and “_Digits” to automatically calculate the right number of digits for the current currency pair. Now we create a string called: “signal”, we will assign the signal later on, right now it’s empty, let’s create an array for the prices (PriceInformation), we use “MqlRates”, if you mark “MqlRates” and press F1 you will learn that it stores the information about prices, volumes and spread and the array will be called: “PriceInformation”. We sort the array downwards from the current candle by using “ArraySetAsSeries” and with “CopyRates” we fill the price information array (PriceInformation) for the current symbol and the currently selected period on the chart. We start with the current candle 0 (zero) and we will fill it with price information for 3 candles. The Envelopes Indicator has 2 bands, the blue one and the red one, so we also need two arrays for the upper band array (UpperBandArray) and the lower band array (LowerBandArray), these will also be sorted by using “ArraySetAsSeries” and now we can create our envelopes definition (EnvelopesDefinition) by using the “iEnvelopes” function that is included with MQL5. This one also needs a few parameters: the first one is for the current symbol on the chart the second one for the period we are using, it will be calculated based on 14 candles and if you click on: “Insert/ Indicators/ Trend/ Envelopes” you will see the 14 candle period here, the shift value is 0 (zero), the deviation is 0.100 and the calculation will be based on the close prices and that’s exactly what we use here for the rest of the parameters. Now we use “CopyBuffer” to fill our upper and our lower band array according to the envelopes definition (EnvelopesDefinition) that we have created here, the first one will be filled for the buffer 0 (zero), the second one for the buffer 1, we start with candle 0 (zero) and copy the data for 3 candles. Let’s calculate the current value for the array values (myUpperBandValue and myLowerBandValue), when you hover with your mouse you will see that there is a value that is constantly changing and that’s what we need so we look at candle 0 (zero) in the upper band array (UpperBandArray) and the lower band array (LowerBandArray) and we use “NormalizeDouble” and a 6 to get only 6 digits behind the dot – that’s what “NormalizeDouble” does, its rounding floating point numbers to a specified accuracy – for the first parameter we say what we want to get rounded, the second parameter is for the number of digits we need and if the last candle closed below the lower b...
    8 min
  • MQL5 TUTORIAL – SIMPLE STANDALONE RANDOM ENTRY EA
    In this video we are going to create a Standalone Expert Advisor that is going to trade random entries. It creates buy and sell signals directly on the chart, here is our first open position and now we are going to find out how to create something like this in and MQL5. To do that please click on the little button here or press F4 in your Metatrader, now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this one: “SimpleStandaloneRandomEA”, click on “Continue”, “Continue” and “Finish” and now we are going to delete everything above the “OnTick” function and the two comment lines here. We start by using the “include” function to include the file: “Trade.mqh” that one comes with MQL5 and it allows us to create an instance of the class “CTrade” that we will call: “trade” and we are going to use it later on to open new positions. Inside of the “OnTick” function we want to calculate the Ask price and the Bid price that’s done by using “SymbolInfoDouble”, the first parameter is for the current currency pair on the chart, the second one will deliver the Ask price or the Bid price, it depends on if we use “SYMBOL_ASK” or “SYMBOL _BID” and “NormalizeDouble” and “_Digits” will make sure that we get the right amount of digits behind the dot. Now we use “MqlRates” that structure stores the information about prices, volumes and spread for the candles. We sort the price info array (PriceInfo) downwards from the current candle by using “ArraySetAsSeries” and with “CopyRates” we fill our price info array (PriceInfo) for the current symbol and the currently selected period on the chart with price data from the current candle 0 (zero) for 3 candles. Now let’s create a string variable called: “signal” that will be used to calculate the signal later on, right now it’s empty and here comes the interesting part; we use ”MathSrand” and “GetTickCount” to initialize the random generator because we want to calculate a random number. We use “MathRand” %2 because “MathRand” can return a pseudorandom integer within the range of 0 to 32,767 but we only need 2 random numbers, so let’s say if the random number is 0 (zero) that would be a buy signal and in that case we would assign the word: “buy” to our signal. Otherwise if the random number is 1 we suppose that this one is a sell signal so we assign the word: “sell” to our signal and whenever the signal equals sell and “PositionsTotal” is below 1 – in other words we don’t have any open positions – we use “trade.Sell” to sell 10 micro lot. Otherwise if the signal equals buy and we have no open positions we use “trade.Buy” to buy 10 micro lot. Finally we use the “Comment” function to output the words: ”The current signal is:” followed by the calculated “signal” directly on our chart. That’s it, please click on the “Compile” button or press F7, now you should see that it went well without any errors and in that case you can click here or press F4 to go back to Metatrader. In Metatrader we click on: “View/ Strategy Tester” or we press CTRL and R, please select the “SimpleStandaloneRandomEA” file, enable the visualization here and start your test. And now you should see the first trade on the chart, let’s speed that up a little bit here and you see that we have made several trades, in this case we have made a profit and if you have no idea which results you could expect from a random entry you should definitely try this out. Okay. In this video you have learned how to create an MQL5 Expert Advisor that is going to trade random entries and output signals directly on the chart and you have coded it yourself with a few lines of MQL5 code.
    6 min
  • MQL5 TUTORIAL – SIMPLE IVIDYA STANDALONE EXPERT ADVISOR
    In this video we are going to create an Expert Advisor to calculate this Indicator; it’s the Variable Index Dynamic Average, whenever the red line is above the price that would be a sell signal and in the other case when the red line is below the candles – like here – that would be a buy signal. Now how can we create an Expert Advisor that is going to actually trade our signal and output our buy and sell signals directly on the chart? To do that please click on 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 (template)” from template, I will call this file: “SimpleIVIDYAStandaloneEA”, click on “Continue”, “Continue” and “Finish”. Now we can remove everything that is above the “OnTick” function and let’s also delete the two comment lines here. We start by including the file: “Trade.mqh”. Sometimes people ask me where the file is? Well, it’s included in MQL5 and we can create an instance of the class: “CTrade” that will be called: “trade” and we are going to use it later on to open positions. Inside of the “OnTick” function we are going to calculate the Ask price and the Bid price. We get the prices by using “SymbolInfoDouble”, we use “SYMBOL_ASK” to calculate the Ask price and “SYMBOL_BID” to calculate the Bid price. With “NormalizeDouble” and “_Digits” we calculate the number of digits behind the dot. Let’s create a string variable that will be called: “signal” and we are going to calculate the signal value later on so we don’t assign a value right now. We also need to create a price array (PriceArray) that is done by using “MqlRates”, lets sort the array from the current candle downwards by using “ArraySetAsSeries” and with “CopyRates” we fill our array (PriceArray) with price data for the current symbol on the chart and the currently selected period, we are going to start from the current candle 0 (zero) and we need the price for 3 candles – that’s for the prices. Now we need to create an array for the Expert Advisor data that will be called: “EAArray”, let’s also sort that one by using “ArraySetAsSeries”. Now we actually can define the Expert Advisor by using the integrated “iVIDyA” function. If you mark that one and hit F1 you will see all the parameters for the Variable Index Dynamic Average Indicator, it has only one buffer and the parameters are: the current symbol on the chart, the currently selected period on the chart, here we have a 9, a 12 and a 0 (zero) – so let’s click on: “Insert/ Indicators/ Trend/ Variable Index Dynamic Average” and you will see that we have a “Period CMO:” that is 9, the “Period EMA:” that is 12 and the “Shift:” value of 0 (zero) and that’s exactly what we use here and the value will be calculated based on the close price (PRICE_CLOSE). So let’s use “CopyBuffer” to fill our array (EAArray) with data according to the IVIDYA definition (iVIDyADefinition) we have created here, it’s for only one buffer – the first buffer is buffer 0 (zero) – the current candle is also candle 0 (zero), we need data for 3 candles so we use a 3 here and save it in our “EAArray”. So far so good; to get the current value we just look into our “EAArray” and we need the value for the current candle – that’s candle 0 (zero) – now we can actually calculate the signal. Whenever the current iVIDyA value (iVIDyAVALUE) is bigger than the close price of the last candle in our price array (PriceArray) that would be a sell signal and that’s when we want to assign the word: “sell” to our signal. In the other case if the iVIDyA value (iVIDyAVALUE) is smaller than the price array 1 close price for candle 1 (PriceArray[1].close) that would be a buy signal and now we assign the word: ”buy” to our signal. Whenever our signal equals sell and “PositionsTotal” is less than 1 – with other words we don’t have any open position – we use “trade.Sell” to sell 10 micro lot. In the other case if the signal equals buy and we have no open...
    8 min
  • MQL5 TUTORIAL- SIMPLE ICCI STANDALONE EA
    In this video we are going to create an Expert Advisor for this signal here; it’s the Commodity Channel Index, and you see that we have already opened a position, it produces buy and sell signals here on the chart. Whenever the signal is below the lower line we are going to buy and when it’s above we are going to sell. Now how can we create an Expert Advisor in MQL5 that is actually able to trade the Commodity Channel Index? To do that please click on the little button here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this one: “SimpleICCIStandaloneEA”, click on “Continue”, “Continue” and “Finish”, now you can remove everything that is above the “OnTick” function and let’s also delete the two comment lines here. First we want to include the file: “Trade.mqh”, it comes with MQL5 and it will help us to create an instance of the class “CTrade” that will be called: “trade” and we are going to use it to actually open positions later on but before we can do that we need to calculate a few things. For example the Ask price. We get that by using “SymbolInfoDouble” for the current symbol and with “SYMBOL_ASK” we will get the Ask price. “NormalizeDouble” and “_Digits” will help us to get the price with either 3 or 5 digits behind the dot depending on the currency pair. Let’s do the same for the Bid price, it’s almost exactly the same but this time we use “SYMBOL_BID”. Now we use “MqlRates” to create an array for the prices, we will call that:”PriceInfo” and we sort the array from the current candle downwards by using “ArraySetAsSeries” and we use “CopyRates” to fill our “PriceInfo” array with price data for the last candles. We will use the current symbol on the chart, the currently selected period and we will start with the prices for candle 0 (zero) – that’s the current candle – and we only need prices for 3 candles and store that result in the “PriceInfo” array. We also need to create a string for the signal that will be called: “signal” and it has no value right now because we are going to calculate that later on. We need to create another array, this time we will call it: “myPriceArray”. Well, this one was for the candle prices (PriceInfo) and the second one will be for the Expert Advisor (myPriceArray). Let’s define the Commodity Channel Index here (CCIDefinition), we are going to use the built-in: “iCCI” function for the current symbol and the current period on the chart, it will be calculated based on 14 candles – that’s also what you see if you click on: “Insert/ Indicators/ Oscillators/ Commodity Channel Index”, you see 14 candles – and our result will be calculated based on the close prices. Let’s sort the price array (myPriceArray) from the current candle downwards by using “ArraySetAsSeries” and with “CopyBuffer” we fill our price array (myPriceArray) according to the Commodity Channel Index definition (CCIDefinition) that we have created here for the first signal buffer, – that’s the line here – we start with candle 0 (zero) – that’s the current candle – and we copy the values of 3 candles and store it in our price array (myPriceArray). Finally we are able to get the value of the current candle by looking at candle 0 (zero) in our price array (myPriceArray) and the result will be stored in the variable called: ”CCIValue” and if the “CCIValue” is bigger than 100 – that would be the case whenever the blue line is above the upper dotted line, you see the 100 mark here – and if that is the case we want to sell so we assign the word: “sell” to our signal. Otherwise if the “CCIValue” is below the -100 (minus 100) line – that is the lower dotted line here – we want to buy so we assign the word: “buy” to our signal. And if the signal equals sell and “PositionsTotal” is less than 1 – with other words we wouldn’t have any open positions on the chart – we want to sell so we use “trade.
    8 min
  • MQL5 TUTORIAL – SIMPLE TRIPLE SMA STANDALONE EXPERT ADVISOR
    In this video we are going to create an Expert Advisor for this Triple SMA, we have just opened the first trade and now we are going to find out how to code this in MQL5. To do that please click on the little button here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleTripleSMAStandaloneEA” (Expert Advisor), click on: “Continue”, “Continue” and “Finish”. Now we can remove everything that is above the “OnTick” function and the two comment lines here. First I want to include the file: “Trade.mqh” and create an instance of “CTrade” that will be called: “trade” and we are going to use it later to open positions. Now we calculate the Ask price and we calculate the Bid price by using “SymbolInfoDouble” for the current symbol, “SYMBOL_ASK” will give us the Ask price, “SYMBOL_BID” will give us the Bid price, ”NormalizeDouble” and “_Digits” will calculate the right number of digits behind the dot because we have different currency pairs here – this one has 3 digits behind the dot and this one 5 – and ”NormalizeDouble” and “_Digits” is taking care of that. Let’s create an array by using “MqlRates”, this one is going to hold the price info (PriceInfo). We use “ArraySetAsSeries” to sort the price array (PriceInfo) from the current candle downwards and by using “CopyRates” we fill our price info array (PriceInfo) for the current symbol, and the currently selected period on the chart from the candle 0 (zero) – that’s the candle on the right side of the chart – for 3 candles. Now we create an empty string for the signal that will be called: “signal” and we don’t assign any value here because we need to calculate it and because we are using three different Moving Averages here. This one is for 100 candles; this one is calculated for 50 candles and this one for 10 candles. We also need to define three different SMA’s. The first one will be the definition for the 10 candle SMA and we use the “iMA” function for the current symbol and the currently selected period for 10 candles, we don’t use a shift value, “MODE_SMA” is used to calculate the Simple Moving Average and that will be done based on the close prices. These two lines here are almost the same, except of the name for the definition and the number of candles. All the three arrays need to be sorted from the current candle downwards by using “ArraySetAsSeries” and you might have guessed it… we use “CopyBuffer” for each of our arrays to fill them with data from the current candle for the first buffer from candle 0 (zero) for 10 candles and store the results in our price arrays (SMA10Array, SMA50Array, SMA100Array). Now we can go ahead and calculate the long signal, so if the “SMA10Array[0]” for candle 0 (zero) is bigger than the “SMA50Array[0]” for candle 0 (zero) and if the “SMA50Array[0]” is bigger than the “SMA100Array[0]” that would be a buy signal so now we assign the word: “buy” to our signal. Otherwise we calculate the short signal. When the “SMA10Array[0]” for the current candle 0 (zero) is below the “SMA50Array[0]” and when the “SMA50Array[0]” also is below the “SMA100Array[0]” that would be a sell signal so now we assign the word: “sell” to our signal. To sell 10 micro lot we check if the signal equals “sell” and if “PositionsTotal” is less than 1 – that would mean that we don’t have any open positions here – and in that case we use “trade.Sell” to sell 10 micro lot. In the other case we want to buy 10 micro lot if the signal equals “buy” and if we don’t have open positions, in that case we use trade.Buy and to buy 10 micro lot. Finally we create a chart output by using the “Comment” function that will output the sentence: “The current signal is:” followed by the calculated “signal”. Okay, that’s it. Please click on the “Compile” button or press F7… Oh, I made a mistake. I have used all these arrays here without any declaration so ...
    7 min
  • MQL5 TUTORIAL – SIMPLE AMA STANDALONE EXPERT ADVISOR
    This time we are going to calculate the Adaptive Moving Average Indicator; this is the red line you see on the chart. Whenever the candles are above the line we have a buy signal, otherwise if the candles are below the red line this is a sell signal. Now we are going to find out how to create an Expert Advisor in MQL5 that is actually going to trade the Advanced Moving Average. To do that please click on the little button here or press F4 in Metatrader and now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleAMAStandaloneEA” (Expert Advisor), click on: “Continue”, “Continue” and “Finish”. Now let’s delete everything above the “OnTick” function and please also remove the two comment lines. We start by including the file: “Trade.mqh”, that file contains a few useful functions and now we can create an instance of “CTrade” that will be called: “trade” and we are going to use it to open positions later on. First we calculate the Ask price and the Bid price by using “SymbolInfoDouble” for the current symbol on the chart, “SYMBOL_ASK” will deliver the Ask price, “SYMBOL_BID” will deliver the Bid price and with “NormalizeDouble” and “_Digits” we automatically calculate the right number of digits behind the dot because this currency pair would have 3 digits while this one has 5 digits and this function (_Digits) is going to handle that. We create an array for the prices by using “MqlRates”, let’s call that array: “PriceInfo”, now we sort the price array (PriceInfo) from the current candle downwards by using the “ArraySetAsSeries” function, we use “CopyRates” to fill the array with price data for the symbol that is currently selected on the chart and the current period on the chart. We start with candle 0 (zero) for 3 candles. Candle 0 (zero) is the current candle. Let’s create a string variable for the signal, we will call it: “signal” and we don’t assign a value here that is going to be calculated now. We need another array (myPriceArray), this one for several prices that contain the Indicator values; the one here was just for the candle prices (PriceInfo). Let’s define the properties for the Adaptive Moving Average value (AdaptiveMovingAverageValue) that is done by using the “iAMA” – it’s an included function in MQL5 – once again for the current symbol and the currently selected period on the chart. You will see three values here; 9, 2 and 30 and when you right click the Indicator and select “Properties” you would see that is calculated for 9 candles, the “Fast EMA:” is 2, the “Slow EMA:” is 30 and the “Shift:” is 0 (zero), that’s exactly what we are using here and the calculation will be based on the close prices. Now let’s sort the second array from the current candle downwards, once again we use “ArraySetAsSeries”, with “CopyBuffer” we fill our price array (myPriceArray) according to the Adaptive Moving Average definition (AdaptiveMovingAverageDefinition) we have created here for the first buffer – that’s the first signal – from candle 0 (zero) for 3 candles and store the result in our price array (myPriceArray). To get the value for the current candle we just look at candle 0 (zero) in our price array (myPriceArray) and with “NormalizeDouble” and the 6 we calculate the current value, as you can see here it has 6 digits behind the dot and if the Adaptive Moving Average value (AdaptiveMovingAverageValue) is above the close price for candle 0 (zero) – that’s actually a shortcut for the current price because close prices are always the last price for a finished candle and candle 0 (zero) isn’t finished so the close price will deliver the current price here – and if this expression is true we want to assign the word: “sell” to our signal. In the other case if the Adaptive Moving Average is below the current price that’s a buy signal and now we assign the word: “buy” to our signal.
    8 min