MQL5 Tutorial

MQL5 Tutorial

By Raimund BauerBusinessTechnologyInvesting
Download on the App Store

All MQL5 Tutorial episodes

  • MQL5 Tutorial – Simple MQL5 New Candle Robot
    Sometimes it is helpful to count the new candles on the chart. The output in the left upper corner on the chart says we had four candles since we started this Expert Advisor,. One, two, three, four, this is the fifth one. Now you might ask yourself why is this important? Well an Expert Advisor can trade several positions on a chart, and you also can trade several currency pairs and every single candle on the chart can have lots of different price moves. And every time a price changes the OnTick function will be triggered but a lot of trading strategies use close prices. So instead of maybe fifteen calculations within this candle, you would only need one calculation for the close price. Even a high tech computer may have a resource problem, if you try to calculate several dozen trades on several dozen charts because when the market starts to move really fast your computer might be overwhelmed and it may cause a crash. So if you want to avoid computer crashes, it’s a good idea to save resources. To do that you can simply check if a new candle is created and now we will create a simple little Expert Advisor to do that. So let’s click the little button over here or hit the F4 key to bring up the Meta Editor. Select File, new Expert Advisor from template, continue, and we will give it the name Simple New Candle. Now click on continue, continue, finish, and remove everything above the OnTick function. Let’s delete the two comment lines here. Okay now the first thing we need is the price array to hold our price data. So let’s create that, that can be done with MQLRates and we will call a price data. Now we want to sort our array from the current candle downwards with arraysetasseries and in the next step we need to fill it with data. That is done by using copyrates for the current currency pair on the chart and the current selected period. For example I prefer a one minute charts, we want to start with the current candle zero, and copy the price for three candles into our little array price data. To count the number of new candles since the last start of the Expert Advisor, we need a new candle counter. This candle counter will be an integer and it will be static, that means it will never lose its value even if we would like to leave the function. Okay let’s move on, now we are creating datetime variable that will hold a time stamp; this one will hold the time stamp for the last time we checked and it is also static so it will never lose its value. And for the current candle we need another datetime variable and that will be called time stamp current candle and we will fill it by getting the time value of the current candle from our price data array. And if the time stamp for the current candle is different from the timestamp the last time we checked, we want to remember the current timestamp for the next time by assigning it to the timestamp last check variable. Now we want to add the value one to our current candle counter and the last thing we need us the output on the chart. So let’s compile the little Expert Advisor here and we have zero errors and zero warnings. So let’s hit the F4 key or click this button to go back to MetaTrader, select View Strategy Test or hit the control and the R key. Look out for the simple new candle.ex5 file. Mark the visualization option and click on start. And now you should see your candles on the chart being counted and that can be done very fast and you did it in five minutes by creating this little Expert Advisor here.
    5 min
  • MQL5 Tutorial – Simple MQL5 Bollinger Bands Robot
    If you like to use indicators, you might have heard of the Bollinger bands. This little Expert Advisor here can calculate all three bands. If you put your mouse cursor over the middle band here you see a value of 0.712078 and our calculated value for the middle band is 0.7120775, this calculation even is a little bit more accurate. The value for the upper Bollinger band is 0.712457 and here we have 0.712457. And for the lower Ballinger band it’s the same 0711698, 0.7116979. This value is even more accurate because this one is rounded. So what can we do with a Bollinger Bands? Well the idea is as the price breaks the upper Bollinger band we want to get in at the next entry and we would like to open a short trade here, because we expect the price to fall. In the other case if the price breaks the Bollinger band here and it gets back into the bands we would go for a long trade because the price might rise. So if you can create an automated Expert Advisor that is able to compare these values over here with the current price, you can fully automate this. Your computer can trade the Bollinger bands automatically and you don’t have to sit in front of your screen all the time. So let’s create a little Expert Advisor! To do that please hit the F4 key or click this little button over here and that will open the Meta Editor. Here we click on new Expert Advisor from template, continue, we will call it simple Bollinger bands, click on continue, continue, finish. Remove everything above the OnTick function, remove the comments and the first thing we need is an array for each of the Bollinger bands one, two, three times a double array. Now we need to sort our three arrays to hold prices from the current candle downwards and we do that with the function arraysetasseries. In the next step we need to define the Bollinger bands. We want our Expert Advisor to use the current chart for the current time period used on the chart. It should calculate the Bollinger bands for twenty candles without any shift and with a standard deviation of two, that all should be calculated for the closed price of the candle. Afterwards we need to copy the price information into three arrays and now it’s time to calculate the actual value for the current candle and to store it in a variable for each of the three bands. That’s it for the calculation. Now we want to output our calculated values on the chart, we do that with the comment function and it will output the name of the variable and the value we have calculated in this block. Okay let’s compile the code. We have no errors and no warnings. So let’s click the little button over here or hit the F4 key to bring up the Meta Trader, to make the Bollinger band visible on the screen we will use a little trick. Please click on insert, indicators, Bollinger bands, select the period of twenty candles with a shift value of zero and the deviation value of two, we want to apply all this to the closed price of the candles and now you can pick a color and style for the line. I prefer these thicker lines. Let’s click on okay and you will see the visual representation of the Bollinger bands on the chart. Now right click the chart select templates, save template and save it as Tester.tpl to make it the default chart for strategy tester. And if you save it as default.tpl it will also become the default template if you open a new chart in life trading. So let’s bring up the Strategy Tester by clicking on View Strategy Tester or hitting the control and the R key and select the simple Bollinger bands.ex5 file, make sure to and enable visualization here and start the test. And this is what you should see now, let’s stop it here and check the values. This is 0.706251, 0.7062505, thats okay. The upper Bollinger band is 0.706916, 0.706916, that’s also okay and the lower Bollinger band is 0.705585, and that’s exactly what’s calculated here, so it works. In this video you learned to calculate all the Bollinger bands by creating this lit...
    6 min
  • MQL5 Tutorial – How to simply count positions with MQL5
    In this video we want to create a little Expert Advisor that will tell us how many positions we have opened for a currency pair. An Expert Advisor like this is useful if you want to handle more than one position for each currency pair as I do, this is called a Multi-currency Bot. As you can see I have opened one, two, three, four, five, six positions for the Euro U.S. dollar. So if I click on Euro U.S. dollar and drag it on the chart, it will say positions for this currency pair six. Now how can we do that? First of all please click on this button here or hit the F4 key to bring up that Meta Quote language editor also called Meta Editor. Now we need to click on new and select Expert Advisor from template, click on continue, I will give it the name Simple Position Count. Click on continue, and finish. You can delete everything above the OnTick function. We also will remove the two comment lines here and the first thing below is a variable but it will be a local variable to count the positions for this currency pair. Now we are going to count all the open positions, that is done by using the function PositionsTotal, subtract one and count down to zero until we have no open positions. We will need to open a bracket for loop, and a closing bracket, and between these two brackets we will get the symbol of the open position by using the function PositionGetSymbol. And if the symbol position is equal to the symbol on the chart, we want to add the value one to the current positions for this currency pair. And after we have counted all positions, we want to output the current positions for this currency pair on the chart. This is the text, this is the value and we are using the Comment function. So this is it. Let’s see if it compiles. Okay that worked without any errors or warnings. So let’s hit the F4 key, open a chart window and drag the EA simple position count on your chart. And it says we have two open positions for this currency pair. This is one and this is the other one. So let’s switch to Euro U.S. dollar, and now it says we have six open positions. Let’s check the Australian against the Canadian dollar and it says we have one open position, and this is this one. In this little video you learned how to create a simple MQL 5 Expert Advisor to count the positions for a currency pair and output the number on the chart.
    4 min
  • MQL5 Tutorial – How to simply adjust user input with MQL5
    In one of our other videos we have created a simple buy trade system and this is the result of a whole year. It made seventy three trades, fifty four were short trades and nineteen were long trades. You see under the input tab we have absolutely no values here so we cannot set anything that is not hardcoded in our source code. Now we want the user to be able to adjust the Lot Size of a trade here, to do that we simply click on Simple Buy Trade and click Modify. This is how our short E.A. looks like and this is the Lot Size. To create a dynamic value that can be changed by the user you have to make it an input variable and will be a double variable, and we will call it MyLotSize. Let’s assign ten Micro Lot here, copy the name and exchange the fixed ten Micro Lot Size here with our new variable. I will save it as Simple Buy Trade with input. Let’s compile the code and hit F4 to bring up the Meta Trader. I will select the system here, check the visualization option and start the test. This is our first trade and its ten Micro Lot. Let’s stop the test, go to inputs and change it to 0.2, now it is 20 Micro Lot. If you make something adjustable by the user, you can do it by making it an input. Afterwards the value can be simply changed without recompiling the code.
    2 min
  • MQL5 Tutorial – How to get Forex Prices with MQL5
    Human traders can see if the price is going up or down, but we need MQL 5 to calculate it for our Expert Advisor. So now we will create a simple Expert Advisor that will bring out the text: “It is going down”, when it’s going down and when the direction changes it will say “it’s going up”. Now how can we do that? First of all we need to create a new Export Advisor. I will call it Simple Price Info, continue, continue and finished. We will delete everything above the Ontick function and the two comment lines, and the first thing we need is an array to hold our price information that can be done by MQLrates. This will create our array, afterwards we want to sort it from a new to the oldest candle that is done with the arraysetasseries function and it looks like this and now we want to fill our array with the price information, that is done with the function CopyRates and this is how we do it. So now MQL 5 knows each price for every candle on the chart and if the last candle is higher than the candle before, we want our expert advisor to print out a line, “It is going up.” And this is how we do that, but if the close price of the last candle is lower than the candle before, we want it to print on the chart, “It is going down.” And this is how it can be done in MQL 5. And this is how we do that, so let’s compile the code, hit the F4 key, bring up the Strategy Tester and select Simple Price Info.ex5. Please set the check mark here for the visualization and start the test. And here is our little Expert Advisor at work. So now you know how you get the price of each and every candle and how to print out the current direction of the price on your chart.
    3 min
  • MQL5 Tutorial – WARNING: How to ruin your account in 5 minutes
    Okay now we are going to use something that I consider to be the biggest advantage that you can get in trading! It’s your ability to make thousands of trades within minutes. Please click on View Strategy Tester or hit the control of the R key. And now you should see the Strategy Tester Panel, we will pick the moving averageEX5 file, select one minute and the first currency pair you can get. And we want to test for a custom period, let’s pick January 2016 until January 2017 here, every tick is a little bit too much because it will take too long for this little demonstration video, we will choose OHLC that is Open High Low and Closed price data. I would like to have one hundred thousand dollars and the biggest leverage I can get, and because I want to see something I have to mark the visualisation option here. If you are finished with your settings please click on the Start button. Now the Meta Trader will download some history data and after a few seconds your strategy test should start, obviously our Meta Trader is going to buy and sell. Let’s speed that up a little bit and here is what happened to our account. Obviously the automated trading with this predefined Expert Advisor is the best way to get broke immediately. So now you might ask yourself why the hell have they included an Expert Advisor that will ruin your account? Let’s try another currency pair here like the Euro US dollar and start another test, let’s speed that up also. And once again you can see your account going to hell immediately. This is predictable loss! This is predictable disaster! If you had used that you would have lost over $50,000 and less than a month. Why should anybody include an Expert Advisor that loses all your money and risk rookies to use it to trade thousands of dollars on a real account without knowing what they do? So please before you do anything else go right to your Meta Trader 5 installation and test the moving average.ex5 file in the examples folder by yourself. If you can find the time do as many tests as you can! You can also disable the visualisation mode here to speed things up. So let’s start a test for the currency pair British pound against Swiss Franc and watch your account going to hell on the third currency pair. You ask yourself why I want you to do that? Well the reason is very simple! If you experience that it is possible to ruin your account predictably with an automated trading system that is included in the default installation of Meta Trader 5, you will start to understand that trading is a game that is set up for most people to lose by default! In the next video we are going to turn this around. Would that be something you would like to see?
    4 min
  • MQL5 Tutorial – Simple Moving Average Robot with MQL5
    If you have been trading for a while, chances are high that you have used or maybe you have seen a moving average. This is a moving average and it is a line and you can use it to find out if the current price is above or below the average value of a particular number of candles. This is a moving average for the last twenty candles and if you right click on it, you can change the properties here. So let’s change the period to one and now you see that the moving average for one candle is connecting all the closed prices. This is a bearish candle so it closes here. This is a bullish candle so here is the closing price. Let’s look again at the properties and select one hundred and now you don’t even see the moving average because it’s far above the candles. As traders we use moving averages to find out if it’s going down and that would be the case when the current prices crosses the moving average from the above. Or if it’s going up and that would be the case when the current price crosses the moving average from below. Now how can we create an Expert Advisor for this? Now if you point your mouse on any point of the moving average, you will see a value and you will see that the value would change with very candle. Here it is 0.988749 and the closing price is 0.98848. So let’s create an Expert Advisor for that, to do that please hit this little button here or press the F4 key to bring up the Meta Editor. Now you need to click on new Expert Advisor from template, continue. We will give it the name simple moving average, continue, continue, finish and remove everything above the OnTick function. Let’s remove the two comment lines and the first thing we need is an array to hold our price values. So we will create an array with the name my moving average array and in the next step we need to define how the moving average should look like. We want to give it the name moving average definition and it should be for the current symbol on the chart and for the current used period on the chart. I would like to use the last twenty candles for the calculation. I don’t want to shift it to the right or to the left direction and it should be a simple moving average. That’s what MODE_SMA does for closed price of the candle. Now that we have defined how our simple moving average should look like, we need to sort the price range from the current candle downwards. This is done with arraysetasseries, we do it for the moving average array. And now we want to fill it with prize data, that is done with copy buffer for the defined EA moving average definition. It has only one line so we places a zero here, we want the current candle, this is another zero and we want to copy the prize for three candles so let’s put in a three. And MQL 5 should store the results in the my moving average array. So let’s calculate the current value by assigning the value of my moving average array for the current candle to the my moving average value and the last thing we want to do is to output the current EA on the chart so we use the comment function to print out the text moving average value and the calculated value we created here. Okay let’s compile the Expert Advisor, click this little button or hit the F4 key. And now we want to do a little trick, we now want to insert an indicator for the trend and it should be a moving average and for our calculation we use the twenty candles period, a shift value of zero. It should be a simple moving average and it should be applied to the close price. This is the color I want to choose for the A and I want to have these kinds of lines. So let’s click on okay and here is our visual representation. Now we right click on the chart, go to templates and save the template as tester.tpl because this is the template the Strategy Tester is going to use, when we test our little Expert Advisor. Bring up the Strategy Tester by choosing it from the menu or clicking control and R, select the simple moving average.ex5 file,
    7 min
  • MQL5 Tutorial – Simple Sell Trade with MQL5
    This is how you would have opened a sell trade in the early days of MQL 5. This is called the netting mode. Netting mode means that you could only open a single position for a currency pair. But now it is possible to have multiple positions per symbol. So let’s do that was a simple EA! Click on New, Expert Advisor, call it simple sell trade, continue and delete everything above the OnTick function. You can also delete these two comment lines and the first thing we need to do is to import an MQH file. We do it with the include command, it’s the trade MQL file and it enables us to create a CTrade with the name trade. To create a selling position we first need to know about the bit price, and this is how we can get the bit price in MQL 5. To create an entry we will also need our account balance and the account equity. And we want to enter a trade if the equity is equal or greater than the account balance. To do that we will use Trade.Sell, let’s do it for ten micro lot, let’s compile that code. Bring up the MetaTrader, go to the Strategy Tester and select the simple sell trade ex5 file. We will trade the whole year 2016, and please enable the visible mode. Let’s start it and now it starts to trade and if the equity becomes greater than the balance it will open several positions. Okay I will switch to full speed now, let’s see how it performs. And now it should be finished soon. Okay this is it. Let’s click on the results tab as far as it comes up. Here it is and we made total net profit of eleven thousand, two hundred and fifty seven bucks.
    3 min
  • MQL5 Tutorial – How to get the highest of 10 candles
    A simple MQL5 Expert Advisor can help you to find the highest candle for your forex trading entry. If you know about charting, it might be interesting for you to find the highest candle in a range of candles on the chart. It might be the highest candle on the chart or the highest candle within a defined range of candles on the chart. So with this little MQL5 Expert Advisor you can find the last High.
    4 min