MQL5 Tutorial

MQL5 Tutorial

By Raimund BauerBusinessTechnologyInvesting
Download on the App Store

All MQL5 Tutorial episodes

  • MQL5 TUTORIAL BASICS – 22 HOW TO CODE AN RSI EXPERT ADVISOR
    In this video, we are going to create an Expert Advisor to automatically calculate and trade the Relative Strength Index also called RSI, so let’s find out how to do that with MQL5. First you need to 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 File/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleRSIEA”, click on “Continue”, “Continue” and “Finish”. Now you can delete everything above the “OnTick” function and the two comment lines here. We start with an include statement, we want to include the file: “Trade.mqh”, it comes with trading functions and it is included in MQL5, so now we can create an instance called: “trade”. Inside of the “OnTick” function we want to calculate the Ask price and the Bid price because we are going to buy and sell, so we use “SymbolInfoDouble”, “_Symbol“, “SYMBOL_ASK” to calculate the Ask price and with “SymbolInfoDouble”, “_Symbol“, “SYMBOL_BID” – all in capital letters – we will get the Bid price. “NormalizeDouble” and “_Digits” is a way to automatically calculate the number of digits behind the dot because the “_Digits” variable stores the number of digits after the decimal point that might be 3 digits – like in this case – or 5 digits. Let’s create a variable for the signal, the name is also “signal”, please don’t assign a value right now because we need to calculate that. We need to create an array for the price data that will be called: “myRSIArray”, it’s a double array, so let’s define the properties of the RSI (myRSIDefinition), we have an included function in MQL5 that is called: “iRSI”, it takes a few parameters; The first parameter is for the current symbol on the chart, The second one for the period, This value; 14, is also what you see if you click on: “Insert/ Indicators/ Oscillators/ Relative Strength Index”, here you have a period value for 14 candles and it is applied to the close prices. This is how the Indicator looks like. Until the last major update, it was automatically shown on the chart whenever I calculated it, but now I need to right-click into the chart, select “Templates/ Save Template” and save it as: “tester.tpl” because this is the template that is going to be used in the backtest and the default template is the one that you will see when you drag any Expert Advisor on the chart, so let’s choose “tester.tpl” and save it. Yes, I want to override it, so this parameter is for the period, This one is to calculate values based on the close price. Now I use “CopyBuffer” to fill the price array (myRSIArray) that we have created here with data according to the definition (myRSIDefinition) that we made here. This parameter here stands for the buffer. The buffer is in our case the blue line, this parameter here stands for the current candle 0 (zero), we want to calculate the values for 3 candles and store them in the RSI array (myRSIArray) and that will make it possible to calculate an RSI value (myRSIValue) for the current candle by simply looking into candle 0 (zero) of our RSI array (myRSIArray), “NormalizeDouble” and comma 2 is used to cut the output to two digits like in the Indicator and if the RSI value(myRSIValue) is bigger than 70 – that’s the case when it crosses this line – and is above we consider the value to be overbought, so we want to assign “sell” to our signal. In the other case if the RSI value(myRSIValue) is below 30 we want to buy because when it’s below this line we think that it is oversold. So if the signal equals “sell” and if “PositionsTotal” is below 1 – so we have no open positions – we want to 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. Finally, we use the “Comment” statement to output the text: “The signal is now:” followed by the current signal. Okay, that’s about it.
    8 min
  • MQL5 TUTORIAL BASICS – 21 – HOW TO CODE THE MACD EXPERT ADVISOR
    In this video, we are going to create an Expert Advisor that is actually going to trade the MACD. The MACD is an oscillator and now we want to automate it 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 File/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleMacDEA”, click on “Continue”, “Continue” and “Finish”. Now you can remove everything above the “OnTick” function and the two comment lines here. We start by including the file: “Trade.mqh”, this one will give us some trading functions, afterwards we will create an instance of “CTrade” that will be called: “trade” and we are going to use it to open positions later on. To be able to do that we need to calculate the Ask price and the Bid price that is done by using “SymbolInfoDouble” for the current symbol on the chart, we use “SYMBOL_ASK” or “SYMBOL_BID” and with “NormalizeDouble” and “_Digits” we make sure that we are getting either 3 or 5 digits behind the dot depending on the currency pair we used that might be different. I would like to create a string for the signal that will be called: “signal”, it doesn’t have any value right now, we need to calculate that value later on. Let’s create an array for several prices that will be called: “myPriceArray“, now we defined the properties for the MACD Expert Advisor (MacDDefinition), MQL5 comes with an included function called: “iMACD”, it takes a few parameters: The first one is for the current symbol on the chart, The second one is for the period that is selected on that chart, Here we have 3 values and the MACD value should be calculated based on the close price, so let’s find out what these 3 values mean. On any chart click on: “Insert/ Indicators/ Oscillators/ MACD” and you will see the 3 values: 12, 26 and 9. 12 is the value for the fast Exponential Moving Average (Fast EMA), 26 is the value for the slow Exponential Moving Average (SlowEMA) and there is also a value of 9 for the MACD Simple Moving Average (MACD SMA), if you are interested in the exact formula you could go to sites like investopedia.com. What’s important for now is that the 3 values are used here to calculate one value, we use “CopyBuffer” to copy the price data into our price array (myPriceArray), we do it according to the MACD definition (MacDDefinition) that we have created here for the first buffer – let’s actually call butter – we start with the current candle 0 (zero) and we copy the values for 3 candles. From time to time people ask me why I always use 3 candles, well that’s handy if you want to calculate a crossover value and it’s much faster than it would be to use all the candles on the chart, so we can save some processing power here. Now I can get the value for the MACD by looking into the value of candle 0 (zero) of our price array (myPriceArray), for this simple example let’s assume that we have a sell signal whenever the MACD value (MacDValue) is above the 0 (zero) line – this is the zero line – and when the MACD value (MacDValue) is below the zero line that would be a buy signal and when our signal equals sell and “PositionsTotal” is below 1 – that would mean we have no open position – we would use “trade.Sell” to sell 10 micro lot. In the other case if the signal equals buy and we have no open positions we would use “trade.Buy” and buy 10 micro lot. Finally, we want to create a chart output, I would like to see the text: “The signal is now:” followed by the signal that we have created. That’s about it, so let’s click on the “Compile” button here, I have one warning because I use a float variable here, this one will give me fewer digits behind the dot but it’s faster and it’s enough for our simple example here, so if you don’t have any errors you can click on the little button here or press F4 to go back to Metatrader. And in Metatrader we click on: “View/ Strategy Tester” or pre...
    7 min
  • MQL5 TUTORIAL BASICS – 20 HOW TO CODE A SIMPLE CROSSOVER EXPERT ADVISOR
    In this video we are going to create a standalone version for a crossover Expert Advisor that is producing buy and sell signals directly on a chart and it is actually going to trade those signals whenever crossover happens, we will get a signal here, so let’s find out how to do that with MQL5. First you need to 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: “SimpleCrossoverEA”, click on “Continue”, “Continue” and “Finish”. Now you can delete 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” that will help us to create an instance of the class: “CTrade” that will be called: “trade”. Now we want to create two input values, if you mark “input” and press F1 you will see that it defines an external variable, basically it will create a user input. We need two; one small Moving Average (SmallMovingAverage ) and one big Moving Average value (BigMovingAverage). Now let’s calculate the Ask price and the Bid price that is done by using “SymbolInfoDouble” for the current symbol on the chart, we use “SYMBOL_ASK” or “SYMBOL_BID”. “NormalizeDouble” and “_Digits” will give us the number of digits behind the dot. Let’s create an empty value for the signal (signal) that needs to be calculated. We start by creating an array for several prices; one for the small Moving Average (SmallMovingAverageArray) and one for the big Moving Average (BigMovingAverageArray), first we need to define the properties of the small Moving Average (SmallMovingAverageDefinition) – I need to reduce the font size here – we use the function “iMA” for the current symbol on the chart and the currently selected period on that chart, this is the number of candles that we want to use to calculate the small Moving Average – in our case it’s 20 candles – we don’t use a shift value and with “MODE_SMA” and “PRICE_CLOSE” we define that we want to calculate a Simple Moving Average that is based on the close price. Now let’s repeat that for the big Moving Average (BigMovingAverageDefinition), this is basically the same line – that is 50 by default – afterwards we use “CopyBuffer” to fill our small Moving Average array (SmallMovingAverageArray) according to the definition (SmallMovingAverageDefinition) that we have created here for the buffer 0 (zero) – that’s the only line the Moving Average has – we start with candle 0 (zero), copy the value for 3 candles and store them in a small Moving Average array (SmallMovingAverageArray), afterwards you might have guessed it, we do the same thing for the big Moving Average according to the definition (BigMovingAverageDefinition) we have created here, we store the values in our big Moving Average array (BigMovingAverageArray) and if the big Moving Average is greater than the small Moving Average for candle 1 and it was below the small Moving Average for candle 2 that would be a crossover, now we want to buy so we assign the word: “buy” to our signal. Otherwise if the big Moving Average is below the small Moving Average for candle 1 and if the big Moving Average was above the small Moving Average for candle 2 that’s a crossover in the other direction, now we would assign the word: “sell” to our signal and if our signal equals sell and if we have no open positions we use “trade.Sell” to sell 10 micro lot. Otherwise if the signal equals buy and we have no open positions we would use “trade.Buy” to buy 10 micro lot. Finally we want to use the “Comment” function, this will output “The signal is now:” followed by the signal that we have calculated. That’s about it, if this is too fast for you you might consider to become a member of the premium course or repeat the other basic videos in this series. When you’re done you can click on the “Compile” button or press F7 on your keyboard,
    9 min
  • MQL5 TUTORIAL BASICS – 19 HOW TO TRADE A SIMPLE MOVING AVERAGE
    In this video we are going to create a simple Expert Advisor that is going to trade this Moving Average. It’s a Moving Average for 20 candles, so let’s find out how to do that with MQL5. First you need to 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: “SimpleMovingAverageEA”, click on “Continue”, “Continue” and “Finish”. Now you can remove everything above the “OnTick” function and the two command lines here. We start by including the file: “Trade.mqh” that comes with MQL5 and it provides easy trade functions, so let’s create an instance of “CTrade” that will be called: “trade” and we are going to use it to open our positions later on. As we want to trade in both directions we need to get the Ask price and the Bid price, that is done by using the function “SymbolInfoDouble” for the current symbol, for the Ask price we use “SYMBOL_ASK” – all in capital letters – and for the Bid price we use “SYMBOL_BID” also in capital letters, I like to use “NormalizeDouble” and “_Digits” to automatically calculate if we have a currency pair with 3 digits or 5 digits. Now let’s continue, we create an array for the price data that will be called: “PriceInfo“ and we use the function: “MqlRates” to create it, because “MqlRates” stores the information about prices, volumes and spread. Now we want to fill the array (PriceInfo) with price data that is done by using “CopyRates” for the current symbol on the chart and the currently used period on that chart, actually this and this statement are equal, we could also use “_Symbol” and “_Period”, we start with the current candle 0 (zero) and we want to copy the price data for 3 candles and store it in our price info (PriceInfo) array. Let’s create a string for the signal that will also be called: “signal” and we don’t assign any value here because we are going to calculate that later on. Now we need another array, this time for the Moving Average it will be called: “myMovingAverageArray”, so let’s define the properties for the Moving Average (movingAverageDefinition). We use the included “iMA” function of MQL5 for the current symbol on the chart and the currently selected period on that chart, I want to calculate the Moving Average for 20 candles in the past, this is a shift value that could be used to move the Moving Average to the right or to the left, I don’t want to use it so I select 0 (zero), we want to calculate a Simple Moving Average so we use “MODE_SMA” – all in capital letters – and “PRICE_CLOSE” also in capital letters is used to calculate the value based on the close price. So let’s use “CopyBuffer” to fill the array (myMovingAveargeArray) with data for the buffer 0 (zero) – that’s the red line here, this Indicator only has one buffer – we start with candle 0 (zero) and we want to copy the price data for 3 candles and store it in the my Moving Average array (myMovingAverageArray) according to the Moving Average definition (movingAverageDefinition) that we have created here. Let’s define the buy conditions, we want to check if the close price for candle 1 is bigger than the value for the Moving Average for candle 1 and if the close prize for candle 2 was below the Moving Average value for candle 2 that would be a cross over and now we want to buy so we assign the word: “buy” to our signal. In the other case if the close price for candle 1 is now below the Moving Average for candle 1 and the close price for candle 2 was above the Moving Average for candle 2 before that would be a sell signal and now we assign the word: “sell” to our signal, and if our signal equals sell and “PositionsTotal” is below 1 we want to open a sell trade for 10 micro lot, otherwise if the signal equals buy and we have no positions that would mean that we want to open a buy trade for 10 micro lot. If this is too fast for you I would suggest to...
    8 min
  • MQL5 TUTORIAL BASICS – 18 HOW TO TRADE SMALL ACCOUNTS WITHOUT BEING STOPPED OUT
    In this video we want to talk about how to trade small accounts and how to manage risk so let’s find out how to do that. If you open the “Navigator” on your Metatrader and go to the “Accounts” section you most likely already see one of these green accounts here, this one is yellow because it’s a real account, the account number is: 5289348, this is the “Members Area” of one of my favorite brokers and when I want to open a new trading account I can pick if I want to use Metatrader 4 or Metatrader 5, and one of the account types here is called: “Pro-Cent”. I can pick US Dollar or Euros, this one still offers leverages go up to 1 to 2,000, usually I trade 1 to 500, now I can pick a password for myself and one for the investor. This investor password comes in handy later on for my statistics. I could also pass an affiliate code here and this is how my account looks like and you will notice that it is traded in US Dollar cents, so what actually does that mean? Well, if you look to the left side you can see that I have made a deposit, the deposit was 300 Dollars, I can use all those money services here but there is also the option to do an internal transfer, so now I can pick my account, transfer money to one of the other accounts and this one has a balance of: 30,629 US Dollar cents, here is the account balance and it says: 30,629 US Dollars, I’m actually trading this account live with my real money but I didn’t need to invest 30,000 Dollars I just invested 300 Dollars for this account, so when you move from your demo account to a real account you can pick a cent account, invest 300 to 500 Dollars and that would become 30,000 cents or 50,000 cents that are actually traded within Metatrader like Dollars. That makes trading much more scalable because I can now trade all these positions here and I still have a free margin of 29,726 cents. There are some brokers who might offer cent accounts for you, this one is the one that I have used in this case, I don’t know if this website is updated on regular bases but you might be able to find out yourself. You should be able to find a broker that offer cent accounts for your country and that’s a good way to trade real money under real conditions, you will make real profits and you will pay real fees, this is the account statistics on FX Blue, I would encourage you to use a statistic service right away. Today is April the 23rd, so far the account is performing well so now you know how to set up a cent account and do a lot of trades with real money even with a small budget and in this little video you have learned how to set up a cent account in a few minutes.
    5 min
  • MQL5 TUTORIAL BASICS – 17 HOW TO PLACE A SELL STOP
    In this video we are going set a sell stop that is a pending order. You don’t see it right now but when we zoom into the chart you see that we have a pending order far below the current price, so let’s find out how to do that with MQL5. We start by clicking on the little button here, you can also 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: “SimpleSellStop”, click on: “Continue“, “Continue“ and “Finish”. Now we can delete everything above the “OnTick” function and the two comment line here. We are stating by importing the file: “Trade.mqh”, this one comes with MQL5 and it provides several trading functions, so let’s create an instance of “CTrade” that will be called: “trade” and we are going to use it later on to open our sell stop. First we need to get the Bid price, that is done by using “SymbolInfoDouble” for the current symbol on the chart, we use “SYMBOL_BID” – all in capital letters – and I also like to use “NormalizeDouble” and “_Digits” that will give us the right number of digits behind the dot, in this case it would be 3 digits while other currency pairs have 5 digits and for this simple example I just want to check one thing and that’s if we have no open orders or positions, so we use “OrdersTotal” and if that equals 0 (zero) that means that we have no pending orders and “PositionsTotal” will deliver us the number of open positions and if that is also 0 (zero) we have no orders and no positions. An order is something that you place into the market and once it is executed it becomes a position. And now we want to use “trade.SellStop” to place our pending order, we have to pass several parameters, so let’s go through them one by one; The first one here is for the lot size, we are going to open a pending order for 10 micro lot. The second one is for the entry price, we want to enter as soon as the price is 200 points below the current Bid price – that’s where we place the pending order . “_Symbol” will give us the currency pair that is on our chart, in my case it’s the Euro-US Dollar. This parameter here could be used to set a stop loss, I dint use that one but I have a take profit, so when the price is 200 points below the current Bid price we will enter and when it goes to 400 points below the current Bid price we will take our profit. “ORDER_TIME_GTC” stands for the expiration date, GTC means that “it is good till cancel”, so the order will not expire. You could specify that it is only valid for the current day (ORDER_TIME_DAY) and you could also set a time and date (ORDER_TIME_SPECIFIED) but we don’t do that so the next parameter for the expiration time is also 0 (zero). And this one is actually wrong, it would be correct to write “NULL” – all capital letters – because the last parameter is a string value, if you want to set a comment to identify the Expert Advisor on your chart you could use something like: “my comment” here and that’s about it. So let’s click on the “Compile” button or press F7, we don’t see any errors here and we have no warning and if that is the case you can click on the little button here or press F4 to go back to Metatrader. In Metatrader you want to click on: “View/ Strategy Tester” or you can press CTRL and R. Inside of the “Strategy Tester” please pick the new file: “SimpleSellStop.ex5”, mark the visualization option here and start a test. Here we are… This is our pending order, here is our comment string so let’s zoom in and here is the pending order and in this little video you have learned how to place an automated sell stop with MQL5 and you have coded it yourself with a few lines of MQL5 code.
    6 min
  • MQL5 TUTORIAL BASICS – 16 HOW TO SET A SELL LIMIT
    In this video we are going set a sell limit. A sell limit is a pending order far above the current Bid price, so let’s find out how to do that with MQL5. In Metatrader first we need to click on a little symbol here or press F4. 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: “SimpleSellLimit”, click on: “Continue“, “Continue“ and “Finish”. Now we can delete everything that is above the “OnTick” function and let’s also remove the two comment line here. We are stating with an import, we include the file: “Trade.mqh”, this one comes with MQL5 and Metatrader 5 and we use it to create an instance of “CTrade” that will be called: “trade” and we are going to use it later on to open our sell limit. First we need to find out about the current Bid price, we do it by using “SymbolInfoDouble”; “_Symbol” is the first parameter, “SYMBOL_BID” – all in capital letters – is the second parameter, “NormalizeDouble” and “_Digits” that will automatically calculate the number of digits behind the dot, so for this currency pair it would return three digits behind the dot while other currency pairs would return five digits behind the dot. Now that we have the Bid price we need to check another condition; I would like to know if we have any open orders or open positions, so if “OrdersTotal” equals 0 (zero) that would mean that we have no other pending order and if “PositionsTotal” equals 0 (zero) that means that we have no open positions and in that case we want to set our sell limit, so let’s add two curly braces here, we can use “trade.SellLimit” because we have created an instance called: “trade” and we need to pass a few parameters here; The first parameter here is for the lot size, we are going to open a sell limit for 10 micro lot, The second parameter is the price, our sell limit should be opened whenever the price rises 200 points above the current Bid price, Parameter 3 will be used to automatically get the current symbol on the chart, in my case it’s the Euro-US Dollar, The next parameter would be the stop loss, you could set it to over 200 points above the current Bid price but in this case I don’t use it so I use 0 (zero), what we do use is the take profit, we want to close the position when the price falls from 200 points above the Bid price to 100 points above the Bid price, “ORDER_TIME_GTC” stands for: “it is good until we cancel the order”, “ORDER_TIME_DAY” would be used if you would like to place an order that is just for the current trading day, this also is all in capital letters. We have no expiration date and so we have no expiration time, so this parameter is also 0 (zero). And the last one could be used to set a comment, if you don’t pass a string variable here you can use “NULL” – this is also in capital letters – and we use it instead of a string value, that’s basically it, so when you are done you can click on the “Compile” button here or press F7, you shouldn’t get any errors and if that is the case you can click on a little button here or press F4 to go back to Metatrader. In Metatrader please click on: “View/ Strategy Tester” or press CTRL and R, here we want to pick the new file: “SimpleSellLimit.ex5”, please mark the visualization option here and start a test. Now the Expert Advisor is running and we can see our sell limit here, it’s for 10 micro lots and when we zoom into the chart we see that it is far above the current Bid price and in this little video you have learned how to automatically set a sell limit and you have coded it yourself with a few lines of MQL5 code.
    6 min
  • MQL5 TUTORIAL BASICS – 15 HOW TO PLACE A SIMPLE BUY STOP
    In this video we are going to talk about a buy stop. You don’t see it here but when you zoom into the chart you will see that we have placed a pending order above the current market price, it will be executed when the price crosses this green line here, so let’s find out how to do that with and MQL5. Please click on the little button here or press F4 on your keyboard. Now you should see the Metaeditor window and here we click on: “File/ New/ Expert Advisor (template)” from template, “Continue“, I will call this file: “SimpleBuyStop”, click on: “Continue“, “Continue“ and “Finish”. We remove everything that is above the “OnTick” function and let’s also delete the two comment line. First we need to import the file: “Trade.mqh”, this one comes with MQL5, it contains trading functions and now we use it to create an instance of the class “CTrade” that will be called: “trade” and we are going to use it to open our buy stop later on. To open a buy stop we need to know the Ask price that is calculated by using “SymbolInfoDouble” for the current symbol, the second parameter is “SYMBOL_ASK” to get the Ask price. According to the Reference “SYMBOL_ASK” will give us the best buy offer. I like to use “NormalizeDouble” and “_Digits” to get the Ask price with either five digits or three digits depending on the currency pair I use. Let’s add a simple condition here; I want to check if we have no open orders and no open positions. “OrdersTotal” will deliver the number of orders, an order is something you place into the market that can become a position and “PositionsTotal” will deliver the number of existing positions where those orders have been executed, this one is still an order because it wasn’t executed so far but once it reaches the price that we have defined the order will become a position. Okay. So if my conditions are true I want to place a buy stop and here is how we do that; we use “trade.BuyStop” with a few parameters; The first parameter is for the lot size, the second one is for the price level. We want to buy when the price rises 200 points from the current Ask price. This third parameter is for the currency pair and “_Symbol” will deliver the current currency pair on the chart, in my case it’s the Australian against the Canadian Dollar. This one would be for the stop loss, I did not set a stop loss here, and it could be something like 100 points above the Ask price. I have defined a take profit and I would like to close the position for a profit when the price rises 400 points from the current Ask price that would be 200 points profit because the entry is executed 200 points above the Ask price. The next parameter is the date when we would like to close the pending order. ”ORDER_TIME_GTC” stands for “good until we cancel the order”, “ORDER_TIME_DAY” would be “good until the day is over” and you could also specify (ORDER_TIME_SPECIFIED) a time and date, in that case you would use the next parameter to set an expiration time, we don’t do that. The last parameter would be a comment, I don’t use one so I use “NULL”, all capital letters here. Actually I could also change that; let’s say: “My comment”. Actually we are done, so please click on the “Compile” button and if you don’t get any errors you can click here or press F4 to go back to Metatrader. And in Metatrader please click on: “View/ Strategy Tester” or press CTRL and R, now you should see the “Strategy Tester” panel and here you want to pick the file: “SimpleBuyStop.ex5”, please enable the visualization here and start a test. And this is what it should look like, here is actually our new comment, the buy stop was placed but it wasn’t executed so far, it is for 10 micro lot, here is the price that we have defined and the take profit actually is 200 points above the entry price and if you zoom into the chart you will see the buy stop is here, so our little Expert Advisor is working and in this little video you have learned how to automatically place a buy stop above the cu...
    6 min
  • MQL5 TUTORIAL BASICS – 14 HOW TO PLACE A BUY LIMIT
    In this video we want to find out of how to set a buy limit, right now you don’t see it on the chart, but if you zoom in you can see that we have this buy limit here below the current price and now we are going to find out how to do that with 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: “SimpleBuyLimit”, click on “Continue”, “Continue” and “Finish“. Now we can delete everything above the “OnTick” function, lets also remove the two comment lines, we start by importing the file: “Trade.mqh”. This file comes with MQL5 and it includes a few trading functions and we used it to create an instance of “CTrade” that will be called: “trade” and we are going to use it later on to open our buy limit. Inside of the “OnTick” function we start by getting the Ask price that is done by using “SymbolInfoDouble” for the current symbol on the chart we use “SYMBOL_ASK” all in capital letters and I like to use “NormalizeDouble” and “_Digits” to automatically calculate the right number of digits behind the dot because some currency pairs only have three digits behind the dot while others have five digits. For this simple example we are going to only check if we have open orders or open positions. In MQL5 we do that by using two different functions; the first one is “OrdersTotal”, that one will deliver the number of open orders that could become a position, the second one is for the positions (PositionsTotal) that we have created and only if both conditions are true we are going to set our buy limit. That’s done by using: “trade.BuyLimit”, we need to pass a few parameters here in round braces. I always do both braces at the same time so I don’t forget one and if you mark one of the braces here you will see that the other one is highlighted in grey, that’s a good way to find out if you have any errors here. The first parameter is for the lot size, we want to set the buy limit for a 10 micro lot trade. Parameter 2 is used to tell Metatrader the price for the buy limit, we would like to get the current Ask price and place our buy limit 200 points below the Ask price, we use “_Symbol” to automatically get the symbol on the chart, the next parameter would be for the stop loss, I don’t use it here but if you would like to use it you can say something like: Ask minus 400 points, but I do use a value for the take profit. I would like to take the profit when the price rises 200 points above the current Ask price. The next parameter: “ORDER_TIME_GTC” all in capital letters, is used for the expiration date, “GTC” stands for: “Good to cancel” – we don’t have any expiration date here – so let’s set the next parameter to 0 (zero), that would be the expiration time. Finally we have one last parameter, which is set to “NULL” all capital letters, we could use it to pass a comment, which might be helpful if you are going to use several Expert Advisors on one and the same chart, but for this simple example that’s it. Let’s click on the “Compile” button or press F7… Ooh… we missed a semicolon that needs to be placed behind the closing round brace here because you could write all these code lines here in a single line and the semicolon is the end of the statement, so let’s recompile and this time it worked without any errors and we also don’t have any warnings 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, please pick the new file: “SimpleBuyLimit.ex5”, enable the visualization here and start a test. And now you should see that we have an entry for a buy limit for 10 micro lots and if you click on the chart and press the minus key on your keyboard you should see the buy limit here, let’s speed the process up and we should see a buy trade. Right here our buy limit was executed,
    7 min
  • MQL5 TUTORIAL BASICS – 13 HOW TO OPEN A SIMPLE SELL TRADE
    In this video we are going to find out how to create a sell order, whenever the first red candle appears on the chart, so let’s find out how to do that with MQL5. First 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: “SimpleSellOrder”, click on “Continue”, “Continue” and “Finish”. Now you can remove everything above the “OnTick” function and let’s also remove the two comment lines here. We start by creating an instance of “CTrade” so we include the file: “Trade.mqh”, this one comes with MQL5 and it provides simplified trade functions, so let’s create an instance called: “trade” that we are going to use later on to open our sell trade. When we sell something we need to find out the Bid price, that is done with “SymbolInfoDouble” for the current symbol on the chart; this one is from the last tutorial, actually we need “SYMBOL_BID” here, you see this capital letters here are constants and when I mark “SYMBOL_BID” and press F1 we learn that we will get the sell offer and that’s what we need. “NormalizeDouble” and “_Digits” is used because we have some currency pairs with three digits behind the dot while others have five digits behind the dot, “NormalizeDouble” is a way to tell MQL5 how many digits we want and “_Digits” is a way to automatically calculate the number of digits that we need for this particular currency pair. Let’s continue with “MqlRates” and “MqlRates” has all the information about prices, volumes and spread and we will store it in an array that is called: “PriceInfo”, let’s use “ArraySetAsSeries” to sort the array (PriceInfo) from the current candle downwards and now we can use “CopyRates” to fill our array (PriceInfo) with price data for the current symbol on the chart and the currently selected period, we start with candle 0 (zero) – that’s the current candle – and we use price data for 3 candles and store it in the “PriceInfo” array. For this very simple example we only need data for candle 1 because we are going to find out if the close price for candle 1 in our price info array (PriceInfo) is below the open price for candle 1 in the price info array (PriceInfo), that’s the case here; the opening price was higher than the close price so the candle is red. Let’s check another condition because we only want to open a new position when we have no other open positions, so if “PositionsTotal” equals 0 (zero) – that would mean that we have no other open positions – and in that case we are going to sell. To do that we use “trade.Sell”, remember this is the instance that we have created here, we need to pass a few parameters. The first one is for the lot size, how much we are going to sell. The second one is all capital letters for the current currency pair. The next parameter is the current Bid price, we use the Bid price because we are going to sell, if we would like to buy something we would use the Ask price and I would like to have a stop loss that is 300 points above the Bid price, that’s parameter 4 and I would like to have the take profit 150 points below the Bid price. The last parameter has no value in my case, you could use it as a comment and pass a string variable, I rarely do that but if you use several Expert Advisors on the same chart that might be interesting. Okay, that’s it so far. Please click on the “Compile” button here and now you should get no errors and no warnings and in that case please click on the little button here or press F4 to go back to Metatrader. And in Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, let’s pick the new file: “SimpleSellOrder.ex5”, mark the visualization option here and start a test. Here we are, so far only bullish candles, here is the first red one and now we have an automated sell trade, so our little Expert Advisor works and in this short video you learned how ...
    6 min