MQL5 Tutorial

MQL5 Tutorial

By Raimund BauerBusinessTechnologyInvesting
Download on the App Store

All MQL5 Tutorial episodes

  • MQL5 TUTORIAL BASICS – 113 SIMPLE SELL PERCENTAGE RISK
    In this video, we are going to create an Expert Advisor for Sell positions,  that is able to close positions when a defined risk percentage value has been reached. So let’s find out how to do that with MQL5. To get started, please click on a 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 from template, continue. I will call this file Simple Sell percentage risk.   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 to include the trade Library. This is the file Trade.mqh. It is part of MQL5 and it will help us to use some functions to open trades. Therefore, we create an instance of the class Ctrade that is called Ctrade, our goal for this Expert Advisor is to create an input variable for the user that will be called percentage risk value.   This is the start value and it can be changed by the user without Recompilation. Because if you mark input and press F1, you will see that values of input variables can be changed by the user in the properties window of the Expert Advisor. Now let’s add another variable called Max Position Loss. This is the max loss that we are going to accept. Inside of the ontick function we start by calculating the Bid price. That is done by using Symbolinfodouble for the current symbol on the chart.   We are using symbol  underscore Bid and with normalize double and underscore digits, we can automatically calculate the right number of digits behind the dot. The second thing I want to calculate is the balance. That can be done by using account info double and we are going to use account underscore balance in this case. If you mark that and press F1, you will see that you can get all kinds of information about your account. And as we want to close sell position, we need to have a test position. So we check if PositionsTotal is less than one, that would mean we have no open positions and now we can use trade.Sell to open a 10 Microlot position. You wouldn’t do that on a real account, but that’s what demo accounts are for. And without a position, we cannot close anything. On a real account we could have more than one position, so we would use a for loop to go through all the open positions. We use buy position getticket for the current counter value to get the ticket number for the position.   Afterwards, we use position get double to get the position profit, because if we have the position profit, we also know the position loss and now we can calculate the max position loss.   We are going to divide the balance by 100 to get one percent. That will be multiplied with the percentage risk value. And if you wonder what that part is for, we need to have a negative value because a position loss as a negative value. Let’s add two print outputs, so we can see what’s going on in the journal. And if the position profit is below the max position loss that we have defined, or in other words, if the position loss is bigger than the accepted loss, we use trade position close for the current ticket to close the current position.   Let’s end the for loop here and to see what’s going on, I would like to add a comment statement that will output the balance, the max position loss and the percentage risk value directly on our chart. That’s about it. If this was too fast for you or if you have no idea what all the code here does, maybe you want to watch one of the other videos in the basic video series or maybe even the Premium Course on our website might be interesting for you.   That’s also the place where you can find offers to download source codes like this one. But for now, please click on View, Toolbar. Now click on the Compile button or press F7 on your keyboard. You shouldn’t see any errors. And if that is the case, you can click on a little button here or press F4 to go back to Metatrader.
    7 min
  • MQL5 TUTORIAL BASICS – 112 SIMPLE BUY PERCENTAGE RISK
    In this video, we are going to create an Expert Advisor that is able to close positions based on a risk percentage value. So let’s find out how to do that with MQL5.   To get started, please click on the little icon 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 from template, continue. I will call this file simple buy percentage risk. Click on continue. Continue and finish, now you can delete everything above the Ontick function and the two Comment lines here. We start with and include statement to include the file Trade.mqh. That will make it possible to create an instance of the class  Ctrade, that is called trade.   We also want to create an input variable that is a user changeable variable for the percentage risk. In our case, we have set a percentage of zero point five percent based on the balance and we also need to define a max position, loss variable. So let’s continue with the calculation. First, we need to calculate the Ask price. That is done by using Symbolinfodouble for the current symbol on the chart, we use simple underscore ask. And with normalize double and Underscore digits, we make sure to automatically calculate the right number of digits behind the dot.   We also need to calculate the current account balance. We can do that by using account info double. And we use the parameter account underscore balance here. Now we need to open a test position. Of course we only do this on a demo account for testing purposes.   So when we have no open positions, we want to open one buy position. Let’s change that to 10 Microlot here so it will work on most accounts. Some brokers do not support one Microlot position sizes. And this will be our test position and we want to close it as soon as the loss is higher than the percentage value that we have defined here. So let’s create a for loop that will go through all the open positions until we have no positions left.   We use position get ticket for the current counter value of the For loop to get the position ticket number, and afterwards we will calculate the position profit. That can be done by using position get double and we use position underscore profit here. If you mark that and press F1 you will see that it says it will give you the current profit. That is not really the profit because you might also want to include position swap in the calculation if you hold positions for longer than one day.   But for this simple example, it’s good enough. Now let’s calculate the maximum position loss. We use the current balance. One percent of the balance would be  balance divided by one hundred. And we multiply it with the percentage risk value that we have defined here. In our case, it’s zero point five percent. And if you wonder about this part here, well we need to calculate the negative value for this variable, because a loss is actually a negative value.   Now, let’s add to print statements. That will output the position profit and the max position loss each time a new tick comes in. And if the position profit is below the max position loss, that would mean that we have a greater minus value here than the negative value for the max position loss. That’s when we use trade position close for the current ticket number. Please don’t forget to close the For loop and to see what’s going on, I would like to add a comment statement to output the balance, the max position loss and the percentage risk value that we have defined. And that’s about it.   Well, if this was too fast for you or if you have no idea what all the code here does, maybe you want to watch one of the other basic videos or maybe even the Premium Course on our website might be interesting for you. This actually was a suggestion of a German Premium Course member. And if you are a Premium Course member and have an idea for a video like this one, please just let me know. On our website we have also offers for source codes l...
    8 min
  • MQL5 TUTORIAL BASICS 111 – SIMPLE STANDARD DEVIATION EA
    In this video, we are going to calculate the standard deviation value for two hundred candles, so let’s find out how to do that with MQL5. To get started, please click on a little icon 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 from template, continue. I will call this file simple standard deviation. Click on continue, continue and finish. Now you can delete everything above the ontick function and let’s also remove the two comment lines here. We start with an include statement to include the file Trade.mqh. This one comes with some simplified trading functions. And we create an instance of the class Ctrade and call it trade. Inside of the ontick function, we want to create a variable for the signal.   But we are not going to assign any value, because we are going to calculate that later. Afterwards, we want to calculate the Ask price and the Bid price, that is done by using Symbolinfodouble for the current symbol on the chart, we use either symbol underscore, ask or underscore bid. And with normalize double and underscore digits, we make sure that we calculate the right number of digits behind the dot. Let’s continue and create an array for the standard deviation Expert Advisor.   We can use the integrated function iSTDDEV for the current symbol on the chart and the currently selected period on that chart.   We want to calculate the results for twenty candles, without any shift for a simple moving average. That is why we use ModeUnderscoreSMA and the result should be calculated based on the close price. Now we use arrays set as series to sort our array from the current candle downwards.   And with copybuffer we fill our standard deviation array according to the definition that we have created here for buffer zero.   We start with the current candle zero and we will copy the values for two hundred candles. So let’s actually write that in the comment. That makes it possible to now calculate the highest candle number by using array maximum. Array maximum will give us the largest element in our array and actually that is the candle number, not the value. We want to search in the standard deviation array starting from Candle zero and we want to search in the whole array. Let’s also do that for the lowest candle number, that can be calculated by using array minimum.   The rest is the same and we need one more thing, and that is the standard deviation value for the current candle.   We get that one by using Normalize double for the standard deviation array. We want the value for candle zero with six digits behind the dot. Now we can check if the lowest candle number equals zero.   That would mean that the deviation value for candle zero is the lowest of two hundred candles. If that is the case we consider that to be a buy signal. So we assign the word buy to our signal. Otherwise, if the value for candle zero is the highest, that would mean we have a sell signal and now we assign the word sell to our signal. And if we have a sell signal and the return value for PositionsTotal is below one, that would mean that we have no open positions and a sell signal. And now we use trade.Sell to sell 10 Microlot. In the other case, if the signal equals buy and we also have no open positions, that’s when we use trade.Buy and by 10 Microlot.   Finally we add a comment statement that will output the current deviation value, the number of the highest and the lowest candle and the calculated signal. That’s about it, if this was too fast for you or if you have no idea what all the code here does, you maybe want to watch one of the other videos in the basic video series. Or maybe even the Premium Course might be interesting for you. You can find that one on our website. And that’s also where we now have some offers for source codes for the basic videos like this one.   For now, please click on View, toolbar and press the compile button or press F7 on yo...
    7 min
  • MQL5 TUTORIAL BASICS – 110 SIMPLE MULTIPLE INPUT
    In this video, we are going to create a list of predefined values that can be picked by the user to pick an entry. So let’s find out how to do that with MQL5. To get started please click on a little icon here or press F4 on your keyboard. Now, you should see the Metaeditor window until you want to click on file. New file, Expert Advisor from template, continue. I will call this file simple multiple input. Click on continue, continue and finish. Now you can delete everything above the ontick function and let’s also remove the two comment lines here, we start by creating a list. This is an enumeration, as you can see. The keyword here is Enum. And if you mark it and press F1, you will see that enumerations are a limited set of data that is predefined. For example, you could use it for 12 months and it is created like this. We have several items and we can also assign a value here.   The items are comma separated and now we are going to use this collection of data to create an instance. The trick is to use the input modifier because this will make our complete choice list available for the user input. The instance is called choice and we assign a default value here called random for random entries. I also would like to have a readable version, a string variable called entry. And inside of the ontick function, we can now check if the value for our choice is zero.   If that is the case, we have picked a random entry, so we assign the word random to our entry for the other entries. It is very similar and that’s how we check for the value. Let’s add a comment statement that will output the entry and the choice so that we see something on our chart.   Usually we would create some functions here, right now, we can pick entries, but nothing is happening as this video is just about how to make the enumeration available for the user input. So that’s basically it. If this was too fast for you or if you have no idea what all the code here does, you may be one to watch one of the other videos and the basic video series, or maybe even the Premium Course on our website might be interesting for you.   That’s also where you can find some offers for those source code files, in case you don’t want to type everything. For now, please click on View toolbar and click on the Compile button or press F7 on your keyboard.   You shouldn’t get any errors here. And if that is the case, you can click on a little button here or press F4 to go back to Metatrader. And in Metatrader we want to pick an empty chart. Please navigate to the simple multiple input file and drag it on the chart and here comes the user input.   Now you can pick a choice. Let’s pick the EMA and click on, OK? And when the next tick comes in, we should see that we have chosen the EMA, the value for choice, this one, if you press F7, you can change the value. So now let’s pick the ADX and click on, OK? And now the entry is ADX. The value for choice is three. By the way, this once again was an idea of a Premium Course member, and if you are a Premium Course member and have an idea for a video like this one, just send me an email.   For now, you have learned how to create an Expert Advisor that is able to use an enumeration for user defined input variables that can be changed without the need to recompile the code, and you have coded it yourself with a few lines of MQL5 code.
    6 min
  • MQL5 TUTORIAL BASICS – 109 SIMPLE FRACTALS EA
    In this video, we are going to create an Expert Advisor that is able to calculate this indicator here, it’s the fractals indicator. So let’s find out how to do that with MQL five. To get started, please click on a little icon 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 from template, continue. I will call this file simple fractals Ea. Click on Continue, continue and finish.   Now you can delete everything above the ontick function and let’s also remove the two comment lines. We will start with and include statement that will include the file Trade.mqh. This one is part of MQL five and it will allow us to create an instance of the class Ctrade, that is called trade, and we are going to use it to open some positions later. Inside of the ontick function 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 either symbol ask or symbol bid, and with normalize double and underscore digits, we can automatically calculate the right number of digits behind the dot for each currency pair. The next thing we need is a variable for the signal. This will also be called signal, but we don’t assign a value here. Afterwards, we use MQLRates to create a price array and with arraysetasseries, we sort our price array from the current candle downwards.   And now we can fill the array with price data, that is done by using CopyRates for the current symbol on the chart and the currently selected period on that chart, copy the data for three candles and store them in the price array. But let’s use the underscore. It doesn’t really matter because when you mark that and press F1, you will see that it is interchangeable. We need two more arrays, one for the upper fractals and one for the lower fractals.   The definition is very easy. We use the function I fractals for the current symbol on the chart and the current period on that chart. Our two fractal arrays need to be sorted by using arrays set as series as we did here. Now we can fill in some data, that is done by using copybuffer for the fractals definition that we have created here. The first one is for the upper line, we are starting with candle one for three candles and store the values in the upper fractals array, for the lower fractals array it’s very similar, but this time we are using lower underscore line and store the values in the lower fractals array. Here comes something that is very uncommon, because if the upper fractals value is equal to empty underscore value, we will assign the value zero to our upper fractals value. And we do the same thing here for the lower fractals value. If you missed that part, you might see some strange exponential values on your chart. So let’s don’t forget that. Now we can check the entry conditions.   Because if the lower fractals value is not zero, and if the lower fractals value is below the lowest price for candle one in our price array, we would consider that to be a buy signal. Therefore, we assign the word buy to our signal. Otherwise, if the upper fractals value is not zero and if the upper fractals value is higher than the highest price for candle one in our price array, that would be a sell signal. And now we assign the word sell to our signal.   And when we have a sell signal and PositionsTotal is less than one, that is equal to we have no open positions. That’s when we want to sell. And that’s when we use trade.Sell to sell 10 Microlot. In the other case, if the signal equals buy and we have no open positions, that is when we use trade.Buy to buy 10 Microlot.   Finally, we want to add a comment statement on our chart, that will output the signal, the upper and lower value for the fractals indicator, and that’s about it. Well, if this was too fast for you or if you have no idea what all the code here does, maybe you want to watch one of the other videos in the basic vi...
    8 min
  • MQL5 TUTORIAL BASICS – 108 SIMPLE IOBVEA OSCILLATOR
    In this video, we are going to create an Expert Advisor that is able to open buy and sell trades based on minimal or maximal values of this oscillator here. So let’s find out how to do that with MQL5. To do that, please click on a 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 from template continue. I will call this file simple IOBVEA, click on continue, continue and finish.   Now you can delete everything above the ontick function and let’s also remove the two comment lines here. We start with an include statement to include the file Trade.mqh. This one comes with some simplified trading functions and it makes it possible to create an instance of the class Ctrade. That is called trade and we are going to use it to open positions. Inside of the ontick function we want to create a string variable called signal, but we don’t assign a value here.   Now we want to calculate the Ask price and the Bid price. That is done by using Symbolinfodouble for the current symbol on the chart. We use either symbol underscore ask or symbol underscore bid. And with symbolinfodouble and underscore digits, we make sure that we automatically calculate the right number of digits behind the dot. Let’s create an array for the price data and we use ArraySetAsSeries to sort it from the current candle downwards.   And now we use IOBV for the current symbol on the chart and the currently selected period on that chart. The third parameter is volume tick. And this function will give us the onbalance volume indicator, that is an oscillator. We use copybuffer to copy data into our array according to the definition that we have created here. We want to do it for buffer zero. We start with the current Candle zero and copy the data for 100 candles to store them in our array.   Now we can calculate the current value by simply looking into candle zero of the array. Let’s calculate the number of the candle that contains the highest value. That is done by using array maximum and array maximum will deliver the number of the candle in the current array, starting from candle zero for the whole array that contains the highest value. There is a similar function that is called array minimum. That will do the same and calculate the number of the candle with the minimum value. And to actually get the value, we need to get the value for the candle number that we have just calculated.   Let’s repeat that for the minimum value so that we can define the signal. Because if the current value is bigger or equal the max value, we consider that to be a buy signal. So we assign the word buy to our signal. Otherwise, if the current value is below or equal to the minimum value, we consider that to be a sell signal and now we assign the word sell to our signal. So if our signal equals sell and the return value for PositionsTotal is below one, that would mean we have no open Orders and a Sell signal.   And that’s when we use trade.Sell to sell 10 Microlot. Otherwise, if the signal equals buy and we have no open positions, that’s when we use trade.Buy to buy 10 Microlot. Finally, we want to use the comment statement to output the current the max and the MIN values directly on our chart. So this is about it. If this was too fast for you or if you have no idea what all the code here does, maybe you want to watch one of the other videos in the basic video series.   Or maybe even the Premium Course on our website might be interesting for you. That’s where you also can find a link for courses that contain the source codes here. For now, let’s click on View, toolbar and click on compile or press F7. That should work without any errors, and if that is the case, you can click on a little button here or press F4 to go back to Metatrader. And in Metatrader, we use a little trick and click on insert, indicators, volumes, on balance volume.   We will go with the default values. Click on OK,
    7 min
  • MQL5 TUTORIAL BASICS – 107 SIMPLE ARRAY REFERENCE
    In this video, we are going to find out how we can pass a whole array as a parameter to a different function, this is possible.   So let’s find out how to do that with an Mql5. To get started, please click on a little icon 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 from template, continue. I will call this file simple array reference. Click on continue. Continue and finish. Now you can delete everything above the Ontick function and the two comment lines here. We start by creating a simple small array for integer values, because this is just a simple example to show the concept. And it makes a lot of sense to create small programs like this one to find out how things work.   So now we assign values to our array. Please remember the first value can be found when you access index zero. All the array values are whole numbers. We want to use those numbers for a simple calculation. So we want to pass the whole array to a function that is called addvalues. And please remember, usually we are going to pass parameters, but this is a whole array and the returned result should be shown on the chart. So let’s add a comment statement here that will show us the text.   The returned result is followed by the result that we have calculated. But this function doesn’t exist, so we need to create it now. And here is where we need to add this little symbol here. It’s the ampersand symbol inside of our user defined function. We want to add the values for the array for index zero, one and two. And afterwards we are going to return the results to our main function, and that’s about it. If you have no idea what all the code here does, all of this was too fast for you, you probably want to watch one of the other videos in the basic video series first. By the way, on our website, we have a link for additional courses where you can get the source codes for this basic video stuff. That’s also where you can find the link for the Premium Course. This was a suggestion of a Premium Course member, and if you are a Premium Course member and have an idea for a video like this one, please let me know.   For now, let’s click on View toolbar and press the Compile button. Or you could also press F7 on your keyboard. You shouldn’t get any errors. And if that is the case, you can click on a little icon here or press F4 to go back to Metatrader. And in Metatrader you want to click on View Strategy tester or Press Control and R. Please pick the new file simple array reference dot .ex5. Mark, the option for the Visual mode, if you can’t see that, you might need to drag this little bar upwards.   So now please click on start. And here is our returned result. It is fifty six. We have added the first three values here. That is eight plus 16. So this would be twenty four. And when we add this value, this will be fifty six. So let’s add actually another value here. Recompile the code and when we stop the test and start a new one, you should see a new value that is returned. This time it is one hundred and twenty.   So our little Expert Advisor works as expected. And in this little video you have learned how to pass an array to a user defined function and you have coded it yourself with a few lines of MQL5 code.
    5 min
  • MQL5 TUTORIAL BASICS – 106 SIMPLE ICHIMOKU KINKO HYO
    In this video, we are going to create an Expert Advisor that is able to calculate the values for this indicator. It is called the Ichi Moku Kinko Hyo. So let’s find out how to do that with MQL5. To get started, please click on a 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 from template, continue.   I will call this file simple Ichi Moku Kinko Hyo. He or 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 import statement that will import the file Trend.mqh.   This one comes with MQL5 and it provides a few additional trend help classes.   One of those classes is this one. It is called C I Ichi Moku.   Here is the declaration and some additional information.   We have methods that have been inherited from other classes. Here is the class hierarchy. Basically, it’s an object and we want to create an instance that is called I ichi Moku. This is actually not a mistake. Please don’t forget to use this asterisk. Now, we are going to continue with the on init function.   We want to create the I ichi Moku only once that is done by using new C I ichi Moku. Here comes the actual creation. We create an instance for the current symbol on the chart and the currently selected period on that chart, followed by three values: nine, twenty six and fifty two. And you will see the same values when you click on insert indicators, trend, Ichimoku Kinko Hyo. The Tenkasen value is nine. The Kijunsen value is twenty six and the value for Sekuspan B is fifty two.   And that’s exactly what we are going to use here. So now we have created our indicator object. We want to use Ichimoku.Refresh. That is one of the inherited methods from C Indicator and they say it updates the indicator data and it is recommended to call this method before we get the data. Now let’s calculate the values. The Tenkansen value is calculated by using Ichimoku.Tenkansen  for candle zero. The Kijunsen value is calculated by using Ichimoku.Kijunsen for the current candle. The Senkouspan A value is calculated by using Ichimoku.Senkouspan A four minus twenty six candles. And that might look a little bit confusing. But if you take a closer look, you will see that this cloud here is calculated ahead of the current candle.   Therefore we need to use this negative value here. Otherwise, it would be calculated for the current candle and that would be wrong. That can be really confusing, especially in the beginning, because you would get wrong values. Let’s continue with the Sekuspan B value. That’s basically the same. This one is also calculated for minus twenty six candles, and the last one is the Chinkospan value. To calculate this, you use Ichimoku.Chinkospan and this time we are going to use twenty six candles because this Chinkospan value is calculated for twenty six candles in the past.
    9 min
  • MQL5 TUTORIAL BASICS – 105 SIMPLE SELL GRID SYSTEM
    In this video, we are going to create a sell grid system that will open a sell position when the next sell price is reached. So let’s speed that up a little bit and you see that we have several positions. So let’s find out how to create something like this with MQL5. To do that, please click on a little icon 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 from template, Continue. I will call this file Simple sell Grid System. Click on Continue, continue and finish. Now you can delete everything above the Ontick function. And let’s also remove the two comment lines. We start with an include function to include the file Trade.mqh. This one will enable us to create an instance of the class Ctrade. It gives us simplified trading functions and it will be called trade.   We also want to create an array for  the price data, that is called price info and it is created by using MQLRates. And let’s create a string variable for the signal, that is also called signal, but we don’t assign a value here because we are going to calculate that later. Inside of the ontick function we want to start by creating a static double variable for the next sell price. Static is a modifier that will remain the value for as long as the Expert Advisor is running, even if we leave the function. Now let’s calculate the Bid price. That is done by using Symbolinfodouble for the current symbol on the chart, we are using symbol underscore bid and with normalize double and underscore digits, we make sure that we automatically calculate the right number of digits behind the dot.   Because some currency pairs have five digits behind the dot and the other ones have three digits behind the dot. Now let’s use arraysetasseries for the price and for array to sort the current array from the current candidate downwards.   And with CopyRates, we can now fill the array with data for the current symbol on the chart and the currently selected period on that chart, starting from candle zero for three candles. We store the data and the price info array, and we can also use underscores here like we did in this line. Now, if we have no open positions, that would be true when PositionsTotal has a value of zero, we want to set our next sell price to zero.   That is a reset. Afterwards, we want to check our entry signal, by calling a function that is called check entry signal. That function doesn’t exist so far, so we need to create it in a few minutes, but before we do that, we want to check if the Bid price currently is below the next sell price or if the next sell price equals zero. If that is the case, we want to open a sell position, that is done by using trade.Sell for 10 Microlot.   And right after we have done that, we want to set the next sell price 50 points below the current sell price level. Let’s also add a comment statement to create an output for the Bid price and for the next sell price right on our chart. So far, so good. That’s the ontick function. But now we need to create this function here. It is called check entry signal and it will return a string value. Because when the close price for the last candle in our price info array is above the open price for candle one in our price info array, we consider that to be a buy signal.   So now we assign the word buy to our signal. Otherwise, when the close price for candle one in our price info array is bigger than the open price for candle one in our price info array that would be a sell signal. And now we assign the word sell to our signal. Finally, we want to return our signal to the main module and close this function. And that’s about it, if this was too fast for you or if you have no idea what all the code here does, maybe you want to watch one of the other basic videos in this video series. And on our website, you can now find a link for all these videos, including the source codes in our shop. For now,
    7 min
  • MQL5 TUTORIAL BASICS – 104 SIMPLE SELL CROSSOVER STOP LOSS
    In this video, we are going to create an Expert Advisor for a simple moving average crossover Sell Stop Loss. So let’s find out how to do that with MQL5. To get started please click on a 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 from template continue, I will call this file simple cross over Stop Loss.   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 to include the file Trade.mqh. This one is part of MQL5 and it will help us to use some simplified trading functions.   Therefore, we create an instance of the class Ctrade and call it trade. Inside of the ontick function we start by calculating the Ask price. That can be done by using symbolinfodouble for the current symbol on the chart, we are using symbol underscore ask and with normalize double and underscore digits we automatically calculate the right number of digits behind the dot. Now let’s repeat that for the Bid price. This is very similar. But to calculate the Bid price, we are going to use Simbol underscore bid. For the signal, we use a string variable.   This is called signal, but we don’t assign any values here because we are going to calculate that later.   Now, let’s create two arrays, one for the small moving average array and one for the big moving average array. The small moving average is defined by using the IMA function for the current symbol on a chart and the currently selected period on that chart. We are going to calculate it for twenty candles. The shift value is zero. We use ModeUnderscore SMA for simple moving average and the result should be calculated based on the close price. Let’s repeat that for the big moving average, this time we are going to use the same function, but for 50 candles, everything else is similar.   And with Copybuffer, we now can fill the small moving average array according to the definition that we have created here for buffer zero, starting from the current candle zero, we copy the values for three candles and store them in the small moving average array. Now let’s do the same for the big moving average array.   Once we have done that, we can now find out if the big moving average array for candle one is below the small moving average array for candle one. If that is the case, we consider that to be a buy signal, so we assign the word buy to our signal. In the other case, if the big moving average array for candle one is bigger than the small moving average array for candle one, that would be sell signal. And now we assign the word sell to our signal.   And if our signal equals buy and we have open positions, we are going to use a function that is called close all sell position this pair. Otherwise, if the signal equals sell and the return value for PositionsTotal equals zero, that would mean that we have no open position. So now we need to open a test position, that is done by using trade.Sell. We are opening a position for 10 Microlot. Please remember, this is just a test position. You wouldn’t do that on a real account.   And to see what’s going on, I would like to add a comment statement that will output the text the signal is now followed by the signal. That’s it for the main part, but to use this function, we need to create it. Now, the name of the function is close all sell position this power we are using void because we don’t need to return value. Now we need to use a For loop for all the positions and go through each and every position to get some details.   The first detail is the ticket number. We can get that by using position get ticket for the current counter value of the For loop. And we also need to get the currency pair for the position. That is done by using position get symbol for the current counter value. And as we only want to close sell position,
    9 min