MQL5 Tutorial

MQL5 Tutorial

By Raimund BauerBusinessTechnologyInvesting
Download on the App Store

All MQL5 Tutorial episodes

  • MQL5 TUTORIAL – SIMPLE ORDER HISTORY PROFIT
    In this video we are going to calculate the history profit for positions that have been already closed, it will output the text: “My last profit was:” followed by the last profit from the first order in the history, as soon as one of the positions here is closed we will see that we get a value. Let’s speed that process up a little bit and here we are and now you can see the profit, the ticket number and the position and the direction of the trade and now we want to find out how we can create an Expert Advisor that is going to calculate the order profit for history trades. To do that please click on the little button here or press F4 on your keyboard and 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 file: “SimpleOrderHistoryProfit” click on “Continue”, “Continue” and “Finish”, and now you can delete everything above the “OnTick” function and the two comment lines here. First we need to include the file: “Trade.mqh”, it contains several trading functions so now we can create an instance of the class “CTrade” that will be called: “trade” and we are going to use it to open positions later on. Inside of the “OnTick” function we are going to calculate the Ask price and the Bid price that is done by using “SymbolInfoDouble” for the symbol on the chart and we use “SYMBOL_ASK” or “SYMBOL_BID” to get the Ask or the Bid price. “NormalizeDouble” and “_Digits” will help us to calculate the right amount of digits behind the dot – this currency pair has 3 digits and this one has 5 digits – and if we don’t have any open positions – so “PositionsTotal” would be less than one – we will use “trade.Buy” to buy 10 micro lot and “trade.Sell” to sell 10 micro lot to open two testing positions, so please don’t do that on your real account but we need some positions to calculate the last profit. That will be done by calling the function: “GetLastProfit” but we have to code that. In the “OnTick” function we will use “Comment” to output the text: “My last profit was:” and in a new line the calculated profit that we are getting from the “GetLastProfit” function, so let’s code that function now! It will return a string, the name is: “GetLastProfit” profit, we don’t have any parameters here, first we need to create a few variables. MQL5 is a little bit complicated, there are deals, orders, positions and ticket numbers. What does it all mean? Well, if you want something that would be an order. The broker can decide if your order is executed or not, if it is it becomes a deal and in that case you will have an open position that will go in one direction or another, so let’s figure that out! First we need to get the history by using “HistorySelect”, we want all the data up to the current time (TimeCurrent), now we go through all the deals, here we use an unsigned integer as counter and as long as it is less than the total number of deals (TotalNumberOfDeals) we will increase it. Now we need to look for a ticket number, that ticket number is the return value for the function: “HistoryDealGetTicket” but we need to use an “if” statement because as long as we have no closed position we don’t have anything inside of the history. The order profit is calculated by using “HistoryDealGetDouble” for the ticket number and we use “DEAL_PROFIT”, we also need to get the order type by using “HistoryDealGetInteger” for the ticket number and this time we use “DEAL_TYPE”. Now we need to get the currency pair, this time we are using “HistoryDealGetString” for the ticket number and to get the symbol we use “DEAL_SYMBOL”. Let’s get the deal entry type to check for close types! Once again “HistoryDealGetInteger” for the current ticket number in combination with “DEAL_ENTRY” will deliver the deal entry type. Now we want to know if the currency pair fits, so if the currency pair for order in the history is equal to the current currency pair on the chart and if we have a buy or a s...
    9 min
  • MQL5 TUTORIAL – SIMPLE SAR STANDALONE EXPERT ADVISOR
    In this video we are going to create a standalone Expert Advisor that is actually able to trade the Parabolic SAR Indicator. This is a very nice Indicator, you can see the dots above and below the price and whenever the direction changes here you will see that we have a buy or a sell signal. The signal output will be in the left upper corner here; this Expert Advisor will be able to automatically trade the signal. We have already an open position here and now we want to find out how we can code such a standalone Expert Advisor in MQL5. 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, “Continue”, I will call this version: “SimpleSARStandaloneEA” (Expert Advisor), click on: “Continue”, “Continue” and “Finish”. Now we delete everything that is above the “OnTick” function and let’s also remove the two comment lines here. We start by importing the file: “Trade.mqh” that will enable us to create an instance of “CTrade” that will be called: “trade” and we use it later on to open the positions. Inside the “OnTick” function we create a price array (PriceArray) that is done by using the “MqlRates” function that is included in MQL5, now we use “ArraySetAsSeries” to sort our price array (PriceArray) from the current candle downwards and with “CopyRates” we fill it for the current symbol on the chart and a currently selected period, we start with the current candle 0 (zero) and we copy price data for 3 candles and store it in our price array (PriceArray). We also need to create a string for the signal, that variable will be also called: “signal” and we don’t assign a value here because we are going to calculate that later on. First we need to get the Ask and the Bid price, that can be done by using “SymbolInfoDouble” for the current symbol, we use “SYMBOL_ASK” to get the Ask price and “SYMBOL_BID” to get the Bid price, and with “NormalizeDouble” and “_Digits“ we calculate the number of digits behind the dot – you see that this currency pair has 3 digits while other currency pairs may have 5 digits – and if you mark “_Digits“ and press F1 you will see that the “_Digits“ variable stores the number of digits after the decimal point. Let’s create another array for our SAR Indicator that will be called my SAR array (mySARArray), it’s a double array so it can hold floating type values and by using a “iSAR” we create an SAR definition (SARDefinition) for the symbol on the chart and the currently selected period. Now you might ask yourself; what these two parameters are? Well, if you click on “Insert/ Indicators/ Trend/ Parabolic SAR” you will see that we have a step value of: 0.02 and a max value of: 0.2, that’s exactly what we are using here. Now it’s time to sort the SAR array (mySARArray) with “ArraySetAsSeries” and we use “CopyBuffer” one more time to fill our SAR array (mySARArray) for the first buffer – that’s the one that is used for the dots here – from the current candle 0 (zero) for 3 candles and store the values in our SAR array (mySARArray). Now we are able to calculate the value for the last candle, I will call it: “LastSARValue”, I use ”NormalizeDouble” here and a 5 because when you pause the Strategy Tester and hold your mouse above one of these dots you will see that we have 5 digits behind the dot of the value – that’s what “NormalizeDouble” does; if I would use “NormalizeDouble” and a 4 we would have 4 digits behind the dot – and we get the value for the last candle by looking at candle 1 in our SAR array (mySARArray). We actually do the same here, this time for candle 2 – that’s the candle before the last candle – so I call it: “NextToLastSARValue” and whenever the “LastSARValue” is below the lowest price of candle 1 – or in other words; whenever the dots are below the candles – and if the “NextToLastSARValue” was greater than the highest price for the candle before t...
    8 min
  • MQL5 TUTORIAL – SIMPLE STOCHASTIC STANDALONE EXPERT ADVISOR
    In this video we are going to create a standalone version for the Stochastic Oscillator, this is a very simple Oscillator; whenever the two lines crossed below the lower line here that would be a buy signal and whenever the two lines cross above that would be a sell signal. This Expert Advisor is actually going to trade and we will see the signal here on the chart. Now how can we create an Expert Advisor for the Stochastic Indicator? To do that please click on a little button here or press F4 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 version: “SimpleStochasticStandaloneEA”, click on “Continue”, “Continue” and “Finish” and now we can delete everything above the “OnTick” function and the two comment lines here. First we want to include the file: “Trade.mqh” and we want to create an instance of the class: “CTrade”, let’s call that one: “trade” and inside the “OnTick” function we create a string for the signal that is called: “signal” and we don’t assign a value now because we need to calculate that later on. To really trade something we need to calculate the Ask and the Bid price, that is done by using “SymbolInfoDouble” for the current symbol on the chart and we use “SYMBOL_ASK” to get the Ask price and “SYMBOL_BID” to get the Bid price and with “NormalizeDouble” and “_Digits” we get the right number of digits – this currency pair has 3 digits and other currency pairs may have 5 digits – and that’s a way to calculate the right value. Now we need 2 arrays; the first one is called K array (KArray) the second one D array (DArray) and when you click on: “Insert/ Indicators/ Oscillators/ Stochastic Oscillator”, you would see the same period values here: 5 for the K period and 3 for the D period, so we use “ArraySetAsSeries” to sort the “KArray” and the “DArray” from the current candle downwards and now we can call the “iStochastic” function that is included with MQL5 for the current symbol and the current period, here are our values: 5, 3 and another 3; that’s K period, D period and slowing. We use ”MODE_SMA” and “STO_LOWHIGH” – that’s the method here – SMA stands for Simple Moving Average and “Price field” is “Low/High”. Let’s fill the array with “CopyBuffer”, the first one is for the first buffer that’s buffer 0 (zero) – that’s for the “KArray”, the second one is for buffer 1 – that’s for the “DArray”. We start at candle 0 (zero) and fill it for 3 candles, and that makes it possible to calculate the current K value (KValue) by looking at the value of candle 0 (zero) of the “KArray” and the current D value by looking at the candle 0 (zero) of the “DArray” and because we need to find out if we had a crossover we also do the same and calculate the same values for candle 1. The buy signal would be true if both values are below 20 – so that’s the case when both lines are below the dotted line here – so “KValue0” would be less than 20 and “DValue0” would also be less than 20 and if the “KValue” has crossed the “DValue” from below – so now “KValue0” would be bigger than “DValue0” but “KValue1” was below “DValue1” – that would be a buy signal so we assign the word: ”buy” to our signal. We also have the sell signal, that’s if both values are above 80, that would be the case when both lines are above the upper dotted line here and if the “KValue” has crossed the “DValue” from above – so “KValue0” is below “DValue0” but “KValue1” was above “DValue1” – that would be a sell signal so we assign the word: “sell” to our signal. And if the signal equals sell and we have no open position – so “PositionsTotal” is less than 1 – we use “trade.Sell” to sell 10 micro lot in the other case if the signal equals buy and we have no open positions we use “trade.Buy” to buy 10 micro lot. Finally we are going to create a chart output by using the “Comment” function that will output the words: “The current signal is:” and the calculated “signal...
    7 min
  • MQL5 TUTORIAL – SIMPLE RSI STANDALONE EXPERT ADVISOR
    In this video we are going to automate the Relative Strength Index also known as RSI and we are going to create an Expert Advisor for Metatrader 5 that is actually going to trade. 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, “Continue”, I will call this version: “SimpleRSIStandaloneEA”, click on “Continue”, “Continue” and “Finish”. Now you can delete everything above the “OnTick” function and the two comment lines here. First we are going to include the file: ”Trade.mqh”, this file comes with MQL5 and it offers a few useful trade functions, so let’s create an instance of the “CTrade” class, that will be called: “trade” and we are going to use it to open positions later on. Before we do that we need to calculate the Ask price and we need to calculate the Bid price; that is done by using “SymbolInfoDouble” for the current symbol on the chart and we use “SYMBOL_ASK” to get the Ask price and “SYMBOL_BID” to get the Bid price and with “NormalizeDouble” and “_Digits” we calculate it for the current symbol – you see that we have two different prices here, some currency pairs only use 3 digits behind the dot and others use 5 digits – and if you mark “_Digits” and press F1 you will see that the “_Digits” variable stores number of digits after the decimal point. Let’s create an array for the price data, in this case it will be called: “myRSIArray” it’s a double array so it can hold floating type values. Now we define the RSI (myRSIDefinition) by using the “iRSI” function for the current symbol and the currently selected period for 14 candles, you see the 14 here in the round brackets and when you click on: “Insert/ Indicators/ Oscillators/ Relative Strength Index” you will also find the 14 candles here, that’s the standard value and the result will be calculated based on the close price. We use “ArraySetAsSeries” to sort the price data (myRSIArray) from the current candle downwards for our RSI array (myRSIArray) and with “CopyBuffer” we fill our RSI array (myRSIArray) according to the “RSIDefinition” that we have created here, we do it for buffer 0 (zero) from the current candle – that’s candle 0 (zero) – for 3 candles – so that’s the 3 here – and after we have done that we are actually able to calculate the current RSI value (myRSIValue), we just look into the value for candle 0 (zero) in our RSI array (myRSIArray) and with “NormalizeDouble” and the 2 here we get a value that has 2 digits behind the dot. If the RSI value (myRSIValue) is above 70 – that’s the upper line here – so if the blue line is above the upper line we consider it to be overbought so we assign the word: “sell” to our signal, otherwise if the RSI value (myRSIValue) is below 30 – so now the blue line would be below the lower dotted line here – we expect it to rise and now we would assign the word: “buy” to our signal. And if the signal equals “buy” and “PositionsTotal” is less than 1 – that would be true if we have no open positions – we use “trade.Buy” to buy 10 micro lot. In the other case if the signal equals “sell” and we have no open positions we would use “trade.Sell” to sell 10 micro lot. And finally we are going to create a chart output that is done by using the “Comment” function, it will output the words: “The current signal is:” followed by our calculated “signal” directly on the chart. Okay. That’s it. Please click on the compile button here or press F7 on your keyboard… …oh I made a mistake! Let’s go back to the beginning of the file! Of course we need to create a string for the signal before we use it, that’s also named: “signal” and we don’t assign an initial value here. Let’s click on the “Compile” button again and this time it worked and in that case you can click here or press F4 to go back to Metatrader and in Metatrader we click on: “View/ Strategy Tester” or press CTRL and R,
    7 min
  • MQL5 TUTORIAL – SIMPLE ICHIMOKU KINKO HYO
    In this video we are going to have a look at this indicator; it’s called: Ichimoku Kinko Hyo, it produces a lot of indicator signals here on the chart and there are several different interpretations how to use this Indicator. In our example we are going to calculate the value of the red line here, it has a value of: 0.811800 and that’s exactly what we have calculated here, so let’s see how we can do that 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: “SimpleIchimoku” click on “Continue”, “Continue” and “Finish” and now we can delete everything above the “OnTick” function and the two comment lines here. Okay, first we need to create an array for the price data, this one will be called: “TenkansenArray” because the red line you see here is the Tenkan-sen line and we use the function: “iIchimoku” that comes with MQL5 for the current symbol on the chart and the currently selected period. These 3 values here can also be found when you click on: “Insert/ Indicators/ Trend/ Ichimoku Kinko Hyo” and here we see that the Tekan-sen has a value of: 9, the Kijun-sen has a value of: 26 and the Senkou Span B has a value of: 52; that’s what we use here 9, 26, 52. Okay, let’s use the “ArraySetAsSeries” command to sort the prices from the current candle downwards for our Tenkan-sen array (TenkansenArray) and with “CopyBuffer” we fill our Tenkan-sen array (TenkansenArray) according to the Ichimoku definition (IchimokuDefinition) we have created here for one line. The first line is for buffer 0 (zero) from the current candle 0 (zero) for 3 candles – that’s the 3 here – and store the result in our Tenkan-sen array (TenkansenArray). To calculate the value for the current candle we simply look at the candle 0 (zero) – that’s the current candle in our Tenkan-sen array (TenkansenArray) – and we assign the result to the Ichimoku value (IchimokuValue) variable here. Finally we need to create a chart output that is done by using the “Comment” function, it will output the word: “IchimokuValue:” followed by the calculated value directly on our chart. When you are done you can click on the “Compile” button or press F7; that worked without any errors here so now we can click here or press F4 to go back to Metatrader. In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, here you want to select the “SimpleIchimoku.ex5” file, mark the visualization option and click on start. …and here we are, we can stop the Expert Advisor at any time and when you point your mouse above the red line you should see a Tenkan-sen value; this one is: 0.8128, that’s exactly what we have calculated here with MQL5. Well, if you would like to calculate one of the other signal lines here you just would have to use another buffer line, you see 0 (zero) stands for the Tenkan-sen line, the 1 (one) would be the Kijun-sen line, let’s stop the Expert Advisor here and when you point to the blue line you should see that it is called: Kijun-sen, that’s what we want to calculate, so let’s exchange buffer 0 (zero) with buffer 1 here, recompile the code and start another test. Now we get other values here, this one is: 0.813225, it’s not the Tenkan-sen line anymore but now it’s the result for the Kijun-sen line. Okay. In this little video you have seen how to calculate values for the Ichimoku Kinko Hyo Indicator and you have coded it yourself with a few lines of MQL5 code.
    6 min
  • MQL5 TUTORIAL – SIMPLE RVI STANDALONE EXPERT ADVISOR
    In this video we are going to create a standalone Expert Advisor that is able to trade the Relative Vigor Index, it’s the little Indicator here, we have an open trade right now. Now we want to find out how to create this EA for Metatrader 5. 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” from template (template), “Continue” (Next). I will call this file: “SimpleRVIStandaloneEA”, click on “Continue” (Next), “Continue” (Next) and “Finish”. Now you can remove everything above the “OnTick” function and the two comment lines here. First we need to include the trade library by including the file: “Trade.mqh”. Afterwards we create an instance for the CTrade class that will be called: “trade”. In the “OnTick” function we create a string variable for the signal that will also be called: “signal” but we don’t assign a value here because we are going to calculate that later on. First we need to get the Ask price and the Bid price. That is done by using “SymbolInfoDouble” for the current symbol on the chart. We use “SYMBOL_ASK” to get the Ask price and “SYMBOL_BID” to get the Bid price and with “NormalizeDouble” and “_Digits” we get the number of digits after the decimal point. You see there are currency pairs with 5 digits and other currency pairs with 3 digits behind the dot and now that we have calculated Ask and Bid price we want to create an array for the price data. The first one will be named: “myPriceArray0”, the second will be named: “myPriceArray1”. Now let’s define the properties for the RVI Expert Advisor by using the built in function: ”iRVI” that comes with MQL5 for the current symbol and currently selected period on the chart for 10 candles. If you take a look at the Indicator here you will also see the 10 and when you click on: “Insert /Indicators /Oscillators /Relative Vigor Index” you will see that the default period here is 10 candles, so that’s what we use here. Now we sort the array0 (myPriceArray0) and the array1 (myPriceArray1) from the current candle downward, that is done by using “ArraySetAsSeries” for our two price arrays. With “CopyBuffer” we fill our price array according to the RVI definition (RVIDefinition) that we have created here for buffer 0 or buffer 1 from the current candle for 3 candles and store the result in our two arrays here. After we have done that we are actually able to calculate the current value for our price array by looking at candle 0. The RVI Indicator uses 3 digits behind the dot, so we once gain use “NormalizeDouble” and a 3 to get that right. And because we need 2 lines, let’s also do that for the 2nd array. Let’s create a buy signal! We want to buy if the RVI value 0 (RVIValue0) is below the RVI value 1 (RVIValue1) and if both values are below the zero line – the zero line is the dotted line here – and in that case we assign the word: “buy” to our signal. Otherwise if the RVI value 0 (RVIValue0) is greater than the RVI value 1 (RVIValue1) and if both values are above the zero line we assign the word: “sell” to our signal. Whenever our signal equals sell and “PositionsTotal” is below 1 – so we have no open position – we want to sell 10 micro lot. In the other case if the signal equals buy and we have no open positions we want to buy 10 micro lot. In the last step we use the “Comment” function to output the word: “Signal:” followed by our calculated signal directly on our chart. Okay. When you are done you can click on the “Compile” button or press F7 on your keyboard; that should work without any errors here and in that case you can click on a button here or press F4 to go back to Metatrader. In Metatrader we click on “View/ Strategy Tester” or press CTRL and R, and here we select the “SimpleRVIStandaloneEA.ex5” file, enable the visualization mode here and click on “Start”. …and here is our Expert Advisor at work and it already made a trade.
    7 min
  • MQL5 TUTORIAL – CROSSOVER STANDALONE EXPERT ADVISOR
    In this video we are going to talk about one of the most popular entries; this is a crossover entry for a Moving Average that is calculated for 50 candles and for 20 candles, and whenever those two lines cross we have a buy or a sell signal. Most people think it’s only about the entry, so let’s create a standalone module to disprove that. 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: “SimpleCrossoverStandalone”, click on “Continue”, “Continue” and “Finish” and now you can delete everything above the “OnTick” function and the two comment lines here. First we need to include the “Trade.mqh” file that is needed to create an instance of “CTrade” that will be called: “trade”. In the “OnTick” function we create an empty string, let’s call this one: “entry”, we don’t assign the value here because we are going to calculate that later. Now we use “SymbolInfoDouble” for the current symbol to get the Ask price and the Bid price. If you click on one of the symbols here you will see that we have two prices; the Ask price is what we need to pay if we buy something and the Bid price is what we get when we sell something and we use “NormalizeDdouble” and “_Digits” to get the right number of digits behind the dot because you see that this currency pair has 3 digits behind the dot and this one has 5. Now we need two arrays for several prices; one for the Moving Average array (MovingAverageArray20) for 20 candles and another one for the Moving Average array (MovingAverageArray50) for 50 candles, now we define the properties for the Moving Average by using the “iMA” function for the current symbol and the currently selected period on the chart. This one is for 20 and this one is for 50 candles, we will use “MODE_EMA” to get an Exponential Moving Average and it will be calculated based on the close price. Let’s sort the price array from the current candle downwards; that is done by using “ArraySetAsSeries” for the Moving Average array (MovingAverageArray20) for 20 and 50 candles (MovingAverageArray50) and with “CopyBuffer” we fill our Moving Average for the first buffer – that’s buffer zero – from candle 0 – that’s the current candle – for 3 candles and store the result in my moving average array 20 (myMovingAverageArray20) and in my moving average array 50 (myMovingAverageArray50) and here comes the entry: if my moving average array 20 (myMovingAverageArray20) for the current candle is bigger than my moving average array 50 (myMovingAverageArray50) for the current candle and if my moving average array 20 (myMovingAverageArray20) for the last candle was below my moving average 50 (myMovingAverageArray50) for the last candle that’s when we have a crossover and now we want to buy. If my moving average array 20 (myMovingAverageArray20) for the current candle is below my moving average array 50 (myMovingAverageArray50) for the current candle and if my moving average array 20 (myMovingAverageArray20) for the last candle was above my moving average array 50 (myMovingAverageArray50) for the last candle that’s a crossover in the other direction and now we want to sell. So far so good, if the entry equals sell and we have less than one position we want to sell 10 micro lot in the other case if the entry equals buy and we have less than one position we want to buy 10 micro lot. So this is it. When you’re done you can click on the “Compile” button or press F7 on your keyboard, that should work without any errors and in that case you can 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, this is the Strategy Tester panel and here you want to select the new file: “SimpleCrossoverStandalone.ex5”, please enable the visual mode here and start your test.
    6 min
  • MQL5 TUTORIAL – SIMPLE WHILE LOOP
    In this video we are going to talk about the “while” statement. Actually “while” is a loop, while “if” is a statement, both are very similar but we want to find out what the differences are and how to create an Expert Advisor that is going to use the “while” or the “if” statement to open a trade. 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. ”Continue”. I will call this file: “SimpleWhileLoop”, click on ”Continue”, ”Continue” and “Finish” and now you can delete everything above the “OnTick” function and let’s also remove the two comment lines here. First we need to create an instance of “CTrade” so we include the file: “Trade.mqh” and create an instance of “CTrade” that will be called: “trade” and now we need to check if we have any open positions, that’s done by using: “PositionsTotal” and we want to know if “PositionsTotal” is equal to zero and in that case we want to open a buy order so let’s create a function that will be called: “OpenBuyOrder.” First we need to get the Ask price that is done by using: “SymbolInfoDouble” and we use “NormalizeDouble” and “_Digits” to get the right amount of digits behind the dot because as you can see here we have currency pairs with 5 digits behind the dot and this one only has 3 digits behind the dot and “_Digits” stores the number of digits after the decimal point for the current symbol on the chart and the Ask price for that symbol and the result will be stored in the variable called: “Ask”. Now that we know the Ask price we want to buy 10 micro lot by using “trade.Buy”, if you want to understand all the parameter here just take a look at the video called: Simple Buy Trade. In our case it’s about the “while” statement here so now we close the second function here and click on the “Compile” button or press F7; that went very well so we can now click on a little button here or press F4 to go back to Metatrader. In Metatrader we click on “View/ Strategy Tester” or press CTRL + R, please select the file: “SimpleWhileLoop.ex5” that we have just created; I will disable the visualization here, pick the whole year 2017 and start a test. …and here is our result, we made a net profit of: 1.080 4 Dollars and 59 Cents, let’s replace the “while” statement with an “if” statement, recompile the code, go back to Metatrader and restart the same test again. This will take a few seconds, here is our result: once again we made a profit 1.084 Dollars and 59 Cents but now we are going to change the statement here. We want to find out if we have less than 10 open positions, compile the code once again, restart the test and this time we made a net profit of: 10.872 Dollars with the “if” statement and now we are going to change that and use the “while loop”, recompile again and start the test, and in this case we made a net profit of: 10.845 Dollars and 90 Cents, but what’s the reason? We only changed the amount of positions to check. To find the answer please mark the visualization button here and start a new test and you will see that we only see one line here because the “while” statement opened all the positions here at exactly the same time. The reason is that within the “OnTick” function the “while” statement will run as long as this expression here is true while the “if” statement behaves differently so let’s do a quick compilation here, stop the test for the last time and now you will see that only one position is opened after the other, so we have opened buy positions at different price levels here and over time this makes a big difference. Okay, in this video you have learned how to open positions by using the “while” statement and you have coded it yourself with a few lines of MQL5 code.
    6 min
  • MQL5 TUTORIAL – SIMPLE CUSTOM TIME FUNCTION
    In this video we are going to talk about a simple custom time function that we have created in MQL5. You see the output here on the chart, we have the date and we have the time including the seconds that is the local time that has been traded right here in the Strategy Tester and now we are going to find out how we can do a custom time function that is going to return the time and this readable format and to do that please click on the little button here 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 file: “SimpleCustomTimeFunction”, click on “Continue”, “Continue” and “Finish” and now you can delete everything above the “OnTick” function and the two comment lines here. Okay, that’s it. First we need to create a string variable for our custom time that will be called: “myTime” and it’s of the type string, so it can contain text and to calculate it we just want to call a custom function called: “GetTime” to calculate the time, so the value of “myTime” will be calculated in the function “GetTime” and when we click on the “Compile” button right now we will see that the “GetTime” function is not defined, so that’s what we are going to do now. To create a custom function you can use a return type, in our case we will use the string return type, because we are going to deliver the time as text. The name of the function is: “GetTime” as we are calling it here and we don’t have any parameters so let’s add two brackets here, we use a local string that will be called: time with seconds (TimeWithSeconds). Now we create a string for the time with seconds (TimeWithSeconds) that’s also a string type variable and the name will be: “TimeWithSeconds”. Here comes the interesting part; we calculate the time with seconds (TimeWithSeconds) for the local time and that is done by using “TIME_LOCAL”. If you mark “TIME_LOCAL” and press the F1 key you will see that it returns the local time of a computer where the client terminal is running, but it’s a datetime variable so we use “TimeToStr” to change it to a text and the function: “TimeToStr” (time to string) is converting a value containing time in seconds since the 1st of January in 1970 into a string in this format. We use “TIME_DATE” that will give us the year, the month and the day – that’s the first part here, this is the pipe sign, it separates the first from the second parameter, the second parameter is “TIME_SECONDS” and that will deliver us the local time including the seconds. Okay. In the last step we want to return the calculated time to the main function by using the “return” function for “TimeWithSeconds” so it will return the calculated value here and assign it to “myTime” and to see it on the chart we use the “Comment” function to create a chart output for the time and when you are done you can click on the “Compile” button here, that should work without any errors now and if that is the case you can click on the little button here to go back to Metatrader. In Metatrader you want to click on “View/ Strategy Tester” or press CTRL and R and now you should see the Strategy Tester here, please select the “SimpleCustomTimeFunction.ex5”, mark the visualization option here and start your test. We have the date and the time directly on the Forex Chart and now you know how to create an Expert Advisor to calculate the formatted time output on your chart and you have done it with a few lines of MQL5 code.
    6 min
  • MQL5 TUTORIAL – SIMPLE TAKE PROFIT OPTIMIZATION
    In this video we want to test the take profit value, in our case we are just testing the 100 point take profit value here. Our little test Expert Advisor is just opening buy trades. Whenever the equity is equal or above the balance its opening lots of positions so please don’t do that on a real account. Now we want to find out how we can create an Expert Advisor to test the take profit value in MQL5. To do that please click on the little button here or press F4 in your Metatrader. Now you should see the Metaeditor window here, and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this one: “SimpleTakeProfitTest”, click on “Continue”, “Continue” and “Finish”. Now you can delete everything that is above the “OnTick” function and let’s remove the two comment lines here. Okay. First we need to create an instance of “CTrade”. “CTrade” is included in the “Trade” library that comes with MQL5 and we will create a new instance called: “trade”. We also want create a user input variable that is done by using the modifier input, it’s an integer variable and we call it: “MyTakeProfitValue” and the initial value will be 100. Now we need to calculate a few things; first we need to get the “Ask” price, that is done by using “SymbolInfoDouble” for the current symbol on the chart, we want to get “Symbol_ask” and by using “NormalizeDouble” and “_Digits” we make sure that we get 3 digits behind the dot for a 3 digit value and other currency pairs have 5 digits behind the dot and “_Digits” will give us the number of digits after the decimal point. We also need the current account balance and we get that by using “AccountInfoDouble(ACCOUNT_BALANCE)”. Now let’s calculate the equity – this is similar – we use “AccountInfoDoulbe(ACCOUNT_EQUITY)”. Now we want to create a simple buy signal, when the equity is equal or above the balance; so if equity is greater or equal balance we are going to buy 10 micro lot of the currency pair. Now here is our profit value variable that we have created as a user input and I will show you what we are going to do with it but before we do that we want to create a chart output by using the “Comment” function that will output the “Balance”, the “Equity” and the take profit value (MyTakeProfitValue) here. Okay. When you are done please click on the “Compile” button, we don’t have any errors here so now we can click here or press F4 to go back to Metatrader. In Metatrader we click on: “View/ Strategy tester” or you can also press “CTRL and R”, let’s select the file: “SimpleTakeProfitTest.ex5”. I have randomly chosen a currency pair and the period here and now I want to disable the visualization mode here to speed things up and start a test. This should only take a few seconds, as soon as you see the “Results” tab the test finished. We have made 2.863 Dollars in this simple test. This is the graph, and now we click on: “Inputs” and we see that our take profit value (MyTakeProfitValue) can be changed here. Right now its 100, we could also change it to 200, start another test; this time we made a profit of 37.732 Dollars and 70 Cents. Looks good for me but now comes the interesting part; I want to Optimize. So I choose “Optimization/ Fast Generic Based Algorithm” here, click on the “Inputs” tab, set the original value to 100, the start value to 100, the step value to 20 and the stop value to let say 300. Now what this is going to do is: it’s starting with 100 take profit points and the next one will be increased by 20 points so the second one will have 120 take profit points and it will stop when we have reached 300 take profit points. We also need to mark the value we want to optimize for and start a new test without visualization. Now you should see that several agents are going to calculate everything the first ones are already finished here and in a few seconds we should have all the results. Please remember this is just a quick test and now it’s finished.
    7 min