MQL5 Tutorial

MQL5 Tutorial

By Raimund BauerBusinessTechnologyInvesting
Download on the App Store

All MQL5 Tutorial episodes

  • MQL5 TUTORIAL BASICS – 49 SIMPLE WILLIAMS PERCENT RANGE EA
    In this expert advisor, we are going to trade this oscillator, it’s the Williams Percent Range oscillator, we are going to create buy and sell signals to open trades, 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 one simple Williams percent range ea, click on continue, continue and finish. Now you can delete everything above the on tick function and let’s also remove the two comment lines here. We start with an include statement to include the file trade dot mqh this one is included in mql5 and it will give us some trade functions that we are going to use to open and close positions. First, we need to create an instance of the class ctrade that will be called trade. To open a position we need to know either the ask or the bid price, we can calculate that by using symbol info double for the current symbol on the chart, we either use symbol ask or symbol bid – all in capital letters – and with normalize double and underscore digits we calculate the right number of digits behind the dot because that could be three digits or five digits depending on the currency pair. Now we create a string variable called signal for our signal, we don’t assign a value here because that is going to be calculated later on. Let’s create an array, I will call this one wpr array and here is the definition, we use the included function iwpr, that comes with mql5 for the current symbol on the chart and the currently selected period on that chart. And I would like to calculate the result for fourteen candles, that’s also what we see here and it’s the default value. We sort the array from the current candle downwards by using array set as series for our wpr array and with copy buffer, we fill our wpr array according to the definition that we have created here. We do that for buffer zero – that’s the blue line here. We start with the current candle zero and we copy the prices for three candles and store them in the array. To calculate the current value we just have to look inside our array and grab the value for candle zero, once again I use normalize double and I would like to have a value with two digits behind the dot because that’s how the oscillator is calculated here. Now let’s define the buy signal. We would have a buy signal if the current wpr value is below minus eighty but above minus one hundred, so that is the range here below the lower dotted line and if this is true we want our signal to have the value buy. Otherwise, if the wpr value is bigger than minus twenty but below zero and that would be true when it’s above the upper dotted line here in this range that’s when we want to sell so now we assign the word sell to our signal. And when our signal equals sell and positions total is below one – or in other words, we don’t have any open positions – we use trade dot sell to sell ten micro lot. Otherwise, if the signal equals buy and we also have no open positions we use trade dot buy and buy ten micro lot. Finally, we use comment to output the text, the signal is now followed by the calculated signal. That’s about it. If this was too fast for you or if you have no clue what all the code here does, maybe you want to watch one of the other videos in this basic video series or maybe even the premium course might be interesting for you, you can find that one on our website. For now, if you are finished please click on the compile button or press F7, in my case I have no errors and if this is true for you you can click on the little button here or you can press F4 on your keyboard to go back to Metatrader. And in Metatrader we click on view, strategy tester or press control and r, please pick the new file simple williams percent range ea dot ex5, please enable the visual mode here and start a test.
    7 min
  • MQL5 TUTORIAL BASICS – 48 SIMPLE CHART OBJECT
    In this video we are going to create an expert advisor that is able to draw an object on the chart like in this case a rectangle, 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 chart object, click on continue, continue and finish. Now you can delete everything above the on tick function and we also remove the two comment lines here. We start by creating two variables for the highest candle and the lowest candle, we also need two arrays, the first one is called high, the second one low. We use array set as series to sort the arrays from the current candle downwards and afterwards we use copy high for the current symbol on the chart and the currently selected period to fill the array called high from candle zero up to candle thirty with the high values, that’s why we use copy high and to get the lowest values we use copy low. You could also use something like copy open or copy close if you would like to get the open and close prices. To get the highest candle we use array maximum. This is also a function that is included in mql5 and it looks into our high array and finds the highest value from candle zero to candle thirty. So let’s repeat that for the lowest candle, this time we use array minimum to look inside the array called low and find the lowest value from candle zero to candle thirty. We also need to have some price information, so we use mql rates, that is a structure that stores information about prices, volumes and spreads. We call the array price information and once again we use array set as series to sort it from the current candle downwards and now we use copy rates to fill our array for the current symbol on the chart and the currently selected period. By the way, this is the old way to do that, it is equal to this notation, so let’s actually exchange that. We will start at the current candle zero and this expression here is able to get all the candles on the chart but it consumes a lot of calculation power so if your system is a little weak you could exchange that with a number of candles that is fixed and now we have some price information that is saved in our price array. The next step is to use object delete to delete the former rectangles for the current symbol on the chart, the object name is rectangle, it will not delete anything when you run the expert advisor for the very first time. And afterwards, we use object create for the current symbol on the chart to create an object called rectangle, therefore we use the object type obj underscore rectangle and if you mark that and press F1 you will see that there are a lot of other object types. You could, for example, use a button or a label, each of these object types has different settings and values, in our case, the next parameter is zero that is the main window because the rectangle is drawn here where the candles are. For oscillators, you would see a separate window below. The object should be drawn from candle thirty to candle zero and we want the upper border to be on the highest high of the highest candle. The lower border will be on the lowest low of the lowest candle within these thirty candles. Now we use object set integer on the main window. To set the color for the object with the name rectangle we use object property color for this and set the color to blue and to fill the rectangle object we use object property fill to also use blue to fill the rectangle and that’s about it. Now if this was a too fast for you or if you don’t know what all the code here does, you maybe want to watch one of the other videos in this basic video series first or maybe even the premium course might be interesting for you. You can find that one on our website. But for now, please click on the compile button or press F7,
    7 min
  • MQL5 TUTORIAL BASICS – 47 SIMPLE STOCHASTIC EA
    In this video we are going to create an expert advisor to trade this oscillator here, this is the stochastic oscillator 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 stochastic ea, click on continue, continue and finish. Now you can delete everything above the on tick function and the two comment lines here. We start with an include statement for the file trade dot mqh, this one comes with mql5 and we will use it to open buy or sell positions. Therefore we start by creating an instance of the class ctrade that will do it for us, we will call it trade. Inside of the on tick function, we start by creating a string variable called signal. We don’t assign a value here that is going to be calculated later on. As we are going to trade in both directions we need the ask price and the bid price, we can calculate both with the function symbol info double for the current symbol on the chart we use symbol underscore ask – all in capital letters – to get the ask price and with normalize double and underscore digits we automatically can calculate if the currency pair has five or three digits behind the dot. To get the bid price we do the same but this time we use symbol underscore bid. Once that is finished we create two arrays, one is the k array, the other one is the d array because the stochastic oscillator has two lines here we need two arrays, we use array set as series for both arrays to sort them from the current candle downwards and with the integrated function istochastic we can create our definition for the expert advisor. The first parameter is for the current currency pair on the chart, the second one is for the selected period on that chart and these three values here also show up here in the oscillator. When we click on properties we will see that the default value for the k period is five, the value for slowing is three and the d period is also three so that’s what we use here. The last two parameters, mode underscore sma and sto underscore low high stand for the simple moving average method and for the price field so let’s continue to fill the arrays that is done by using copy buffer according to the stochastic definition we have created here. For the k array we use buffer zero, and for the d array buffer one, and we copy the values from candle zero to candle three and store it in the k array or the d array. Now we can get the current value for the k array by looking into the k array, especially at the value in candle zero. To get the d value for the current candle we do the same for the d array and to get the values for the candle before we do the same for candle one, so now we know the current and the last values for both arrays. And we would have a buy signal if those values are below twenty – that’s the dotted line here – and if k value zero is below twenty and d value zero is below twenty we will check our next condition and that is, we want to know if the current k value zero is bigger than the d value zero and if the last k value was below the d value we have a cross over and if that is the case we assign the word buy to our signal. Otherwise, if the current k value and the current d value are bigger than eighty – and that is the case when the two lines are above the upper dotted line here – that’s when we check if the current k value is below the current d value and if k value one was above d value one for the candle before we have a sell signal so now we assign the word sell to our signal. Now if our signal equals sell and if the return value for positions total is below one – or in other words, we don’t have any open position – we use trade dot sell to sell ten micro lot. Otherwise, if the signal equals buy and we have no open positions we use trade dot buy to buy ten micro lot.
    8 min
  • MQL5 TUTORIAL BASICS – 47 SIMPLE STOCHASTIC EA
    In this video we are going to create an expert advisor to trade this oscillator here, this is the stochastic oscillator 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 stochastic ea, click on continue, continue and finish. Now you can delete everything above the on tick function and the two comment lines here. We start with an include statement for the file trade dot mqh, this one comes with mql5 and we will use it to open buy or sell positions. Therefore we start by creating an instance of the class ctrade that will do it for us, we will call it trade. Inside of the on tick function, we start by creating a string variable called signal. We don’t assign a value here that is going to be calculated later on. As we are going to trade in both directions we need the ask price and the bid price, we can calculate both with the function symbol info double for the current symbol on the chart we use symbol underscore ask – all in capital letters – to get the ask price and with normalize double and underscore digits we automatically can calculate if the currency pair has five or three digits behind the dot. To get the bid price we do the same but this time we use symbol underscore bid. Once that is finished we create two arrays, one is the k array, the other one is the d array because the stochastic oscillator has two lines here we need two arrays, we use array set as series for both arrays to sort them from the current candle downwards and with the integrated function istochastic we can create our definition for the expert advisor. The first parameter is for the current currency pair on the chart, the second one is for the selected period on that chart and these three values here also show up here in the oscillator. When we click on properties we will see that the default value for the k period is five, the value for slowing is three and the d period is also three so that’s what we use here. The last two parameters, mode underscore sma and sto underscore low high stand for the simple moving average method and for the price field so let’s continue to fill the arrays that is done by using copy buffer according to the stochastic definition we have created here. For the k array we use buffer zero, and for the d array buffer one, and we copy the values from candle zero to candle three and store it in the k array or the d array. Now we can get the current value for the k array by looking into the k array, especially at the value in candle zero. To get the d value for the current candle we do the same for the d array and to get the values for the candle before we do the same for candle one, so now we know the current and the last values for both arrays. And we would have a buy signal if those values are below twenty – that’s the dotted line here – and if k value zero is below twenty and d value zero is below twenty we will check our next condition and that is, we want to know if the current k value zero is bigger than the d value zero and if the last k value was below the d value we have a cross over and if that is the case we assign the word buy to our signal. Otherwise, if the current k value and the current d value are bigger than eighty – and that is the case when the two lines are above the upper dotted line here – that’s when we check if the current k value is below the current d value and if k value one was above d value one for the candle before we have a sell signal so now we assign the word sell to our signal. Now if our signal equals sell and if the return value for positions total is below one – or in other words, we don’t have any open position – we use trade dot sell to sell ten micro lot. Otherwise, if the signal equals buy and we have no open positions we use trade dot buy to buy ten micro lot.
    8 min
  • MQL5 TUTORIAL BASICS – 46 SIMPLE COMMODITY CHANNEL INDEX EA
    In this video we are going to create an expert advisor to trade this oscillator, it’s the commodity channel index, usually, it’s used as a filter but in this case, we are going to use it to create buy and sell signals, 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 and here you want to click on file, new file, expert advisor from template, continue, I will call this file simple commodity channel index, click on continue, continue and finish. Now you can delete everything that is above the on tick function and let’s also remove the two comment lines here. We start with an include statement to include the file trade dot mqh, this one comes with mql5 and it will give us some simple options to open positions. We want to use a class called ctrade and we are going to create an instance called trade. Inside of the on tick function we use symbol info double for the current symbol on the chart to get the ask price, that is done by using symbol underscore ask, please all in capital letters and with normalize double and underscore digits we calculate the right number of digits behind the dot. Let’s repeat that for the bid price. This time the variable name is different and we use symbol underscore bid but everything else is pretty much the same. With mql rates we create an array for the price data and we use array set as series to sort the array from the current candle downwards and with copy rates we fill the array for the current symbol and the currently selected period on the chart with price data for three candles. Actually, this expression and this one are the same, if you mark this function and press F1 you will see both expressions do the same, so let’s actually do that and this is also true for the period. Now we need an empty string that will be called signal because it will contain the signal, we don’t assign a value here because that’s’ what we are going to calculate now. So let’s create a price array and define the commodity channel index that can be done with the included function icci that comes with mql5. We do it for the current symbol on the chart and the currently selected period on that chart. It will be calculated based on fourteen candles and on the close price and that’s actually what you will see when you right-click the oscillator and click properties. Fourteen candles and the close price are the standard parameters. We use array set as series to sort this one also from the current candle downwards and with copy buffer we fill our price array according to the definition that we have created here. We do it for buffer zero – that’s the indicator line here – and we start with the current candle zero, we just need data for three candles and store everything in the price array. That makes it possible to calculate the current cci value by looking into candle zero of the price array and if the current value is above the plus one hundred line – that is this dotted line here – we consider that to be a sell signal so we assign sell to our signal. Otherwise, if the value is below the lower dotted line – that would be this one – we want to buy so now we assign the word buy to our signal. And if our signal equals sell and if the return value for positions total is below one, or in other words, we don’t have any open positions we use trade dot sell to sell ten micro lot. In the other case if the signal equals buy and positions total is below one we use trade dot buy and buy ten micro lot. Finally, we want to create a chart output, we use the comment function to do that. It will output the text the current signal is and the calculated signal. That’s about it. If you don’t understand what all the code here does or if this was too fast for you, you probably want to watch the other videos in this basic series first or maybe even the premium course on our website might be interesting for you. For now please click on the compile button or press F7,
    7 min
  • MQL5 TUTORIAL BASICS – 45 SIMPLE SAR SELL TRAILINGSTOP
    In this video we are going to create a sell stop for this parabolic sar indicator, so let’s find out how to do that with mql5. To get started 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 file, expert advisor from template, continue, I will call this file simple sar sell trailing stop, click on continue, continue and finish. Now you can delete everything above the on tick function and the two comment lines here, we start with an include statement to include the file trade dot mqh, afterwards we create an instance of the class ctrade that will be called trade and we needed to open a test position, so that we have something where we can place the trailing stop. As we are talking about a sell trailing stop we need to calculate the bid price that is done by using symbol info double for the current symbol on the chart, we use symbol underscore bid – all in capital letters – and with normalize double and underscore digits we make sure that we have the right number of digits behind the dot. Depending on the currency pair that could be either three digits or five digits and now we want to check if the number of current positions is below one, so if we don’t have any open positions we use trade dot sell to open a sell position for ten micro lot. Please remember we only do this on a demo account, we wouldn’t do that on a real account. Now let’s create an array and once that is done we use the integrated function isar for the current symbol on the chart and the currently selected period on that chart and these two parameters here are what we see if you click on insert, indicators, trend, parabolic sar, here we have a step value of zero point zero two and a max value of zero point two, so let’s click on ok, right-click into the chart, select templates, save template and save it as tester dot tpl because this is the template that we are going to use in the strategy test, so let’s click on ok, we can replace the old value here and go back to the Metaeditor, so let’s use array set as series to sort the array from the current candle downwards and in the next step we can use copy buffer to fill our sar array according to the sar definition that we have created here. We fill it for buffer zero – that’s the first indicator line – in our case that’s the dotted line here, and we fill it from candle zero for three candles and store the results in our sar array because that makes it possible to calculate the current sar value by looking into candle zero in our sar array and with normalize double and comma five we make sure that the value will be calculated according to the definition of the parabolic sar. You see here is the value zero point eight eight nine nine seven, that’s five digits behind the dot and that’s also what we get when we do it this way. And the last part of the on tick function is to call a function called check sar sell trailing stop, this one doesn’t exist so far so we need to code it now and we will pass the bid price and the sar value as parameters. So this is the function, it is called check sar sell trailing stop, we take the two parameters here, the first one for the bid price is a double value and that’s also true for the second parameter the sar value. Now we use a for loop to go through all the open positions, first we use position get symbol for the current counter value to get the currency for the current position, afterwards we want to check if the current symbol on the chart and the position symbol are equal, and if that is the case we use position get integer and position underscore ticket – all in capital letters – to get the ticket number for the position. We use position get double, position underscore sl to get the current stop loss and we only want to modify our stop loss if the current stop loss is above the current sar value or if the current stop loss equals zero,
    9 min
  • MQL5 TUTORIAL BASICS – 44 SIMPLE SAR BUY TRAILING STOP
    In this video we are going to create a buy trailing stop for the parabolic sar indicator you see the little dots here, our stop loss is trailing as our price is rising, so let’s find out how to code a stop loss that moves based on an indicator value. To do that please click on view, toolbars, standard and click this 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 sar buy trailing stop, click on continue, continue and finish. Now we can delete everything that is above the on tick function and let’s also delete the two comment lines here. We start with an include statement to include the file trade dot mqh, afterwards, we create an instance of the class ctrade that will be called trade. Inside of the on tick function we need to calculate the ask price, that is done with symbol info double for the current symbol on the chart, we use symbol underscore ask and with normalize double and underscore digits we calculate the right number of digits behind the dot. For our little demo account, we want to create a test position, you wouldn’t do that on your real account but we need something to test, so if we have no open positions and positions total delivers a return value below one we use trade dot buy to buy ten micro lot. Afterwards, we create an array for the sar and we use the integrated function isar for the current symbol on the chart and the currently selected period on that chart. These two parameters here is what you see when you click on insert, indicators, trend, parabolic sar. Here we have a step value of zero point zero two and the maximum value of zero point two, these are the default values so that’s what we use here. Let’s use array set as series to sort the sar array from the current candle downwards and now we use copy buffer according to the sar definition that we have created here to fill our array for three candles starting with candle zero. This parameter here is for the buffer, this indicator only has one buffer because it only produces one line of dots. And now we can actually calculate the sar value by looking into our array especially at candle zero. I use normalize double and the value five because each of those dots here has five digits behind the dot. And now we want to call a function called check sar buy trailing stop, we pass two parameters, the first one is the ask price and the second one is the sar value and as this function does not exist so far we need to create it now. Our trailing stop function will be called check sar buy trailing stop, it will take two parameter values from the main function, the first one is the ask price and the second one is the sar value. Now we use a for loop to go through all the open positions, first we need to get the symbol of the current position, that is done by using position get symbol for the current counter value and we only want to continue if the symbol on the chart and the position symbol are equal. In that case we use position get integer for the position ticket – all in capital letters – to get the ticket number and with position get double we can get the current stop loss by using position underscore sl. And if the current stop loss is below the sar value we use trade dot position modify for the current position ticket to move the stop loss to the sar value. This last parameter here is for the take profit value, we don’t modify that, actually we could but this is something for another video. Finally, we close all the loops here and that’s about it. Well, if you don’t understand what all the code here does or if this was too fast for you maybe you want to watch the other videos in this video series or maybe even the premium course on our website is interesting for you, for now, please click on the compile button… I made a mistake here, I think I have missed a curly bracket here,
    8 min
  • MQL5 TUTORIAL BASICS – 43 SIMPLE IOSMA EA
    In this video we are going to create an expert advisor to trade this little indicator here, it’s the moving average of oscillator so let’s find out how to do that with mql5. To get started please click on a little button here or press the F4 key on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, I will call this file simple iosma expert advisor, click on continue, continue and finish. Now you can delete everything above the on tick function and the two comment lines here, we start with an include statement for the file trade dot mqh, afterwards, we create an instance of the class ctrade that will be called trade and we use it to open our trades later on. Inside of the on tick function we calculate the ask price, that is done by using symbol info double for the current symbol on the chart, we use symbol ask for the ask price and with normalize double and underscore digits we make sure that our expert advisor is calculating the right number of digits behind the dot. Let’s repeat that for the sell price or the bid price, it’s basically the same but this time the variable has the name bid and we use symbol underscore bid – all in capital letters –, we also need to create a variable for our signal that one will be a string variable and we don’t assign any value because that is going to be calculated later. Let’s create an array, afterwards, we use array set as series to sort the array from the current candle downwards, so let’s define what we want, we want to use the function iosma for the current symbol on the chart and currently selected period. We use twelve, twenty six and nine because when you click on insert, indicators, oscillators, moving average of oscillator you will see that these are the default values. Twelve is for the fast ema, twenty six for the slow ema and the nine is the value for the so-called macd sma. The prices are calculated based on the close price and when you click on okay you will see that the oscillator is appearing in a separate window below the candles and here are the values, twelve, twenty six, and nine, and that’s actually what we use here. The last parameter is price underscore close, now use copy buffer to fill our array with three candles according to the definition that we have created here, we do that for the first buffer, that’s buffer zero and we start with candle zero. Sometimes people ask me why I always use three candles? that’s very handy for crossover settings like in this case, because now we can calculate the current value by looking into candle zero and we do the same thing for candle one and assign this value to a variable called my last iosma value and to find out if we had a crossover we can now check if the current value is below zero and if the last value was above zero – this little dotted line here is the zero line – and if this expression here is true the value has crossed the zero line from above and that’s when we assign the word sell to our signal because now we want to sell. In the other case if the current value is above zero and if the last value was below zero that’s when we assign the word buy to our signal, so if our signal equals sell and if our positions total function delivers a value below one or in other words we don’t have any open positions we use trade dot sell to sell ten micro lot otherwise if the signal equals buy and we have no open positions we use trade dot buy to buy ten micro lot. Finally, we want to use the command statement to create an output on our chart that will show us the text, the signal is now, followed by our current signal. That’s about it. If you don’t know how this works or if this was too fast for you maybe you want to watch one of the other videos in this basic course or maybe even the premium course on our website might be interesting for you, for now please click on the compile button or press F7 that worked without any errors and if that is the ca...
    8 min
  • MQL5 TUTORIAL BASICS – 42 SIMPLE Accumulation Distribution EA
    In this video we want to find out how to calculate this accumulation distribution oscillator and how to use it as a filter for a simple moving average, so let’s find out how to do that with mql5. To get started 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 file, expert advisor from template, continue, I will call this file simple IADEA, now let’s click on continue, continue and finish. Now you can delete everything above the on tick function and the two comment lines here, we start with an include statement for the file trade dot mqh, afterwards, we create an instance of ctrade that will be called trade. Inside of the on tick function we need to get the ask price and the bid price, that is done by using symbol info double, either for symbol underscore ask or underscore bid. We also create an empty string variable called signal, afterwards we need to create two arrays, the first one is for the price data and the second one is for the moving average, so let’s use array set as series to sort the price array from the current candle downwards, afterwards we define the moving average that is done by using the ima function. Now if you don’t know how this works you maybe want to watch the other videos in this basic course video series, in our case we use a simple moving average for twenty candles, let’s also sort this one from the current candle downwards and now we use copy buffer to copy the values for three candles according to the my moving average definition that we have created here and store it in the array. And to calculate the current simple moving average value we just need to look into candle zero of the moving average array and that value will be assigned to a variable called my moving average value. Let’s also calculate the two values we need to find out of the accumulation distribution oscillator is rising or falling, that is done by using the IAD function for the current symbol and the current period on our chart, we use volume tick as the third parameter, afterwards we use copy buffer to fill the price array with price data for eleven candles according to the IAD definition that we have created here. To get the current IAD value we just have to look for the value of candle zero in our price array and we get the value for the last IAD by looking into candle ten of our price array, so now we can define a few conditions, the first one if the ask price is above the moving average, that is the case when the candles are above the simple moving average line here that would be an uptrend and in that case I would like to know if the current IAD value is bigger than the last one, that would be true when the accumulation distribution oscillator is rising and if both conditions are true we assign the word buy to our signal. Otherwise, if the bid price is below the moving average value, like here where the candles are below the simple moving average and if the current IAD value is below the last one, that is true if the value is falling, that’s when we want to assign the word sell to our signal. And if our signal equals sell and positions total is below one or in other words we have no open positions we use trade dot sell to sell ten micro lot. Otherwise, if the signal equals buy and we also have no open positions we use trade dot buy to buy ten micro lot. Finally, we use the comment statement to output the text, the current signal is, followed by the calculated signal. Now if this was too fast for you or if you don’t know what all the code here does maybe you want to watch the other videos in this basic series, otherwise please click on the compile button now or press F7, you shouldn’t get any errors here and if this is the case please click on a little button here or press F4 to go back to Metatrader. And in Metatrader, we click on view, strategy tester or press control and r, please pick the new file simple IADEA dot ex5,
    7 min
  • MQL5 TUTORIAL BASICS – 41 SIMPLE SELL BREAKEVEN STOP
    In this video, we are going to find out how to set a breakeven stop for sell trades when the price is running into our direction. You see the little red line here that is the breakeven stop that has been placed, so let’s find out how to do that with mql5. To start please click on the little icon here or press F4 in your Metatrader, now you should see the Metaeditor window and here you would want to click on file, new file, expert advisor form template, continue. I will call this file simple sell breakeven stop, click on continue, continue and finish. Now you can delete everything above the on tick function, and the two comment lines here. First, we need to use the include statement to include the file trade dot mqh. This one comes with mql5 and it will give us some simple trading functions that we need to open a sample trade. We create an instance of the class ctrade that will be called trade. Inside of the on tick function, we first need to get the bid price because we are going to open a sell trade. To get the bid price we use symbol info double, for the current symbol on the chart, the parameter here is called symbol underscore bid – all in capital letters – and I also use normalize double and underscore digits to automatically calculate the right number of digits behind the dot. And if we have no open positions, so if positions total equals zero, we use trade dot sell to open a sell trade for ten micro lot. You wouldn’t do that on a real account, but we need something to test. And now we are going to call a function called check sell breakeven stop and pass the bid price as a parameter. So far, this function does not exist, so we need to code it now. Our function will not have any return value. We start with the for loop and go through all the positions. For each position, we want to find out the ticket number. That is done by using the position get integer, and the parameter to get the ticket number is position underscore ticket – all in capital letters. We also want to know the open price, or in my case I have called it buy price. That can be done by using position get double, position underscore price underscore open. To get the position stop loss we use position get double, and we use position underscore sl. And that is also the same for the take profit, we use position get double, and this time position underscore tp. To find out if it’s a sell position, we need to find out the position type by using position get integer, position underscore type. And to get the position symbol we use position get symbol for the current counter value, because in the next step we are going to check out if the current symbol on the charge equals the position symbol, and if that is true, we check if the position type equals position type sell, so that’s a sell position. We only want to set a breakeven stop if the current position stop loss still is above the position buy price, and in my case I want to set the stop loss if the current bid price is at least thirty points below the position buy price. And if all those conditions here are true, we can now modify the stop loss. That is done by using trade dot position modify for the current position ticket. I want to place the breakeven stop four points below the position buy price, and we leave the position take profit value as it is. Finally, we need to close the for loop and the function, and that’s about it. If you don’t know what all the code here does, or if this was too fast for you, maybe you want to watch one of the other videos in this basic course, or maybe even the premium course on the website is interesting for you. For now, please click on the compile button here or press F7. We did not get any errors here, and if that is the case, you can click on the little button here or press F4 to go back to Metatrader. And in Metatrader, please click on view, strategy tester or press control and r. Please pick the new file simple sell breakeven stop dot ex5,
    7 min