MQL5 Tutorial

MQL5 Tutorial

By Raimund BauerBusinessTechnologyInvesting
Download on the App Store

All MQL5 Tutorial episodes

  • MQL5 TUTORIAL BASICS – 40 SIMPLE BUY BREAKEVEN STOP
    In this video we are going to create break-even stops for buy trades, this is the initial stop-loss for this trade and when the price rises as soon as it passes the buying price you will see the little red line appears on the chart so let’s find out how to do that with mql5. We start by clicking on this little icon here or we can also press F4 on the keyboard, now you should see the Metaeditor window and here you can click on file, new file, expert advisor from template, continue, I will call this file simple buy break-even 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 by including the trade dot mqh file that comes with mql5 and we will use an instance of ctrade called trade to open a test position. You wouldn’t do that on a real account but we need to open a test position so we can set the break-even stop. For a buy position we need to find out the ask price, that is done by using symbol info double for the current symbol on the chart, we use symbol underscore ask – all in capital letters – and with normalize double and underscore digits we automatically calculate the right number of digits behind the dot. I only need one test position so I check if the return value for the function positions total equals zero and if that is true we use trade dot buy to open the test position for ten micro lot. We also set a stop loss and take profit value here and at the end of the on tick function, we want to call another function that shall be called check buy break-even stop, we passed the ask price as a parameter and this function doesn’t exist so far so we need to code it now. Our custom function has no return value and it will take the ask price as a parameter, we will use a for loop, our counter is an integer for the number of the position and we will count down from positions total until there are no other positions left. For each position we need to get the ticket number that is done by using position get integer, position underscore ticket, that value will be stored in a variable called position ticket, that’s an unsigned long variable and that’s a little bit crazy because we use position get integer so you would expect an integer value here but when you mark position ticket and press F1 you will see that it actually returns a long value here. We also need to get the position buy price or maybe I should say the open price for the position that is done by using position get double and we use position underscore price, underscore open, let’s repeat that two more times because we also want to know the position stop loss and the position take profit value, both can be calculated by using position get double and to get the direction for the position we use position get integer for position underscore type, that will give us the direction because we need to know if it’s a buy or a sell position. To make sure that the position belongs to the current chart we use position get symbol for the current position number and now we can check the conditions, we only want to move the stop-loss if the current symbol on the chart equals the symbol of the position, we also want to make sure that the position type is equal to position underscore type, underscore buy, that is the case when we have a buy trade, we only want to move the break-even stop if the current position stop-loss is below the position buy price. And finally, I want to know if the current ask price is above the position buy price plus thirty points that’s when I want to set my breakeven stop. If all those conditions are true I use trade dot position modify to modify the current position for the current position ticket and want to move the current stop-loss to four points above the position buy price, that’s the price that we paid when we opened the buy position and in this case I leave the position take profit value unchanged. Let’s reformat the code here,
    9 min
  • MQL5 TUTORIAL BASICS – 39 SIMPLE CHART LABEL
    In this video we want to create a dynamic label like this one, it shows the current ask price, this is actually an object, we can modify the properties, so let’s find out how to do that in 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 chart label, click on continue, continue and finish. Now you can delete everything that is above the on tick function and let’s also delete the two comment lines here. We start by getting the ask price, that is done by using symbol info double for the current symbol on the chart, we want to know the ask price so we use symbol underscore ask – all in capital letters – and with normalize double and underscore digits we make sure that the ask price is shown with the number of digits for this currency pair. Actually, that doesn’t work always, most of the time it will show five digits behind the dot, that’s because this currency pair has five digits behind the dot, but from time to time you will see that it shows additional digits here. To create such a label we use object create for the current symbol, the name of the object will be label one and we use obj underscore label to define what kind of object we like to be drawn, the last three parameters here are used to define the window, I use zero for the main window, I don’t have any date time and any price value. Let’s pick a font, that is done by using objects set string for the object on the main chart, that is called label one, I would like to change the object property font and set it to the font type I like, and with object set integer on the main chart for the property for the object label one I would like to set the font size to twenty four, let’s also add some text here, that is done by using object set string, once again for the chart window for label one, this time it’s object property text, I think we can leave out this zero here and I would like to have the text, ask price followed by the calculated value for the ask price. To set a distance we use object set integer object property x distance, let’s set five pixels for the distance to the left border and finally, I want to set the y distance, that’s the distance from the upper border to ten pixels. Okay, that’s it. If this was too fast for you or if you don’t know what all the code here means maybe you want to watch the other videos in this basic video series or maybe even the premium course that you can find on the website might be interesting for you. If you are finished you can click on the compile button here or press F7, that should work without any errors. I have one inconsistency here, that’s actually the chart id, let’s recompile the code, here we have another warning, conversion from number to string, that’s okay for this simple example and when you’re done you can click on the little button here or press F4 to go back to Metatrader. And in Metatrader we pick an empty chart, make sure that the navigator window is visible, otherwise you could press ctrl end to show it, now we can pick the new expert advisor that is called simple chart label and drag it on the chart, and as soon as the price changes you should see your new label here. Let’s go back, change the font, re-compile the code and now it looks different, and in this little video you have learned how to create an object-oriented label right on the chart and you have coded it yourself with the few lines of mql5 code.
    6 min
  • MQL5 TUTORIAL BASICS – 38 HOW TO CATCH A SIMPLE KEY EVENT
    In this video we are going to create a little expert advisor that is able to recognize keystrokes, in this case, we have a simple chart here, this is the expert advisor that we have coded, it is called simple key event and when I click into the chart and press any button I will get the notice that a key was pressed. I have pressed the key a, now I have pressed the key b and this time the key was c, so let’s find out how to code that in mql5. To do that please click on the little button here or press F4 on your keyboard, now you should see the Metaeditor and here you want to click on file, new file, expert advisor from template, continue, I will call this file simple key event, 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. So usually we are using the on tick event, this is the event that is triggered whenever the price changes on the chart but in this case, we need another kind of event because we want to find out if a key was pressed and to do that we use the on chart event. It takes a few parameters, we have already done that for the mouse click but let’s repeat the parameters. The first one is an integer value, it’s called event id and it will return the id of the event, the second one is a long parameter, this symbol here is not a mistake it’s how we can pass a value by reference in mql5, it’s a long value so we call it lparam, the next one is a double parameter, also passed as a reference so it’s called dparam, and the last parameter is a string parameter, this is also a reference and this one is called sparam. Depending on what you do you need to find out what each of those parameters contains, in our case it’s easy because we only need the event id to identify if we have a keystroke and that is the case when the event id equals, it’s called chart event underscore key down – all in capital letters – now we want to translate the key code – that’s a number into a letter – that is done by using translate key for the value that’s inside of the lparam, it is an int value, that’s a little bit strange because lparam is usually a long parameter but actually key codes only have integer values and now we want to create an output for the key that was pressed, we use a message box, it will output the text: the key was, followed by the converted value for the key that was pressed, this is the heading and our message box will have an ok button just for confirmation, you could also use other buttons like yes or no and cancel, in our case the ok button is enough. Finally, we have to add the two closing brackets here and that’s about it. If this was too fast for you or if you don’t understand what all the code here does maybe you want to watch the other videos in this video series or maybe even the premium course is interesting for you but for now please click on the compile button… I have messed up something here… Actually, I missed the curly bracket here, so let’s re-compile the code and this time it works, so let’s find out if it does what we want. Please click on the little button here or press F4 to go back to Metatrader, let’s pick an empty chart, drag the newly created expert advisor onto that chart and press a key and now here is our message box, the key that was pressed is k, let’s press the b key and as you see it works as designed, while it will not be able to recognize every possible combination, for example, if I press the shift key I will get an empty return code, it’s also possible to do that but in this little video you have learned how to recognize a pressed key on your chart and you have coded it yourself with a few lines of mql5 code.
    7 min
  • MQL5 TUTORIAL BASICS – 37 SIMPLE SOURCECODE IMPORT
    In this video we want to find out how to import external source code files, in this case, we have an external source code file for the macd indicator, so let’s find out how to do that with mql5. To get started please click on the little icon 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 from template, continue, I will call this file simple source code import, click on continue, continue and finish, now you can delete everything above the on tick function and the two command lines here. We start with an include statement, you may already know that from other videos where we have included the file that is called trade dot mqh. Trade dot mqh is what we call a library, basically, it’s a source code file, it is one of the files that are provided with mql5, here it is and it is located in the folder mql5, include, trade. As you can see there are lots of other mqh files, like in this folder and this is also true for the other folders here, it is possible to open one of these files with any kind of editor and you will find hundreds and hundreds of lines with source code and in our case we want to import a self-created mql5 source code file, the name is check entry underscore macd, it is located in the folder import that I have created below the folder where I have stored the file for the simple source code import. And what this file basically does is it will use the macd indicator to calculate a signal and it will return a buy or a sell signal, and to get that signal I just use a function that is contained inside of the source code, it is called check entry and we create a string variable called signal that will be filled with the return value of the check entry function, it will output the signal on the chart by using the comment statement so we will see the text signal and the calculated signal and that’s basically all. I can now click on the compile button, the code is compiled successfully but to make this work for you, you would need to have the right source file here, let me show you something, these are all source files for different entries that are used in the professional version for the robot trading system. This is the macd and I would like to also copy the rsi to show you how it’s done, and now I paste both files into the import folder so we have already compiled the code I can now click this little button to go back to Metatrader, here we click on view, strategy tester or press ctrl and r, here I pick the new file simple source code import dot ex5, I mark the visualization option here and I start a test, and here you can see that the expert advisor is calculating buy and sell signals for the macd entry, so let’s click on stop. Now I want to import the rsi module, I click on compile, this also worked without any errors so now I go back to Metatrader, I stop the current test, start a new one, and now it’s calculating the rsi signal. And this is actually possible for all the modules that you see here, I have coded lots of them and that’s the power of the include statement, it makes it possible to import source code files that have been created by anybody else or maybe even by you, these are modular files that you just have to code once and you can use them over and over again, and now you know how to do that for an mql5 expert advisor and you have coded it yourself with a few lines of mql5 code.
    7 min
  • MQL5 TUTORIAL BASICS – 36 SIMPLE MOUSE CLICK EVENT
    In this video, we want to talk about chart events, in this case about a mouse click. When I click on the chart nothing happens, now let’s drag the little expert advisor, a simple mouse click on that chart, click on ok and when I click into the chart now I will get this little message box here, it says that the chart was clicked, the x value is two hundred and sixty three, and the y value is three hundred and eleven, let’s close that box and click again in the lower right corner and now you see that the values are much bigger, these numbers here are actually the mouse coordinates, so let’s find out how to fire a chart event with mql5. To do that, please click on the little button here or press F4, now you should see the Metaeditor window and here you want to click on file, new, expert advisor from template, continue, I will call this file simple mouse click, click on continue, continue, and finish, now you can delete everything above the on tick function and the two comment lines here and most of the time we would do anything inside of the on tick function, this one is also an event, it’s called whenever the price changes on the chart and that’s the most interesting event, but we can also use other events like in our case a mouse click. Actually a mouse click belongs to what is called a chart event because it happens when we click anywhere on the chart and this chart event takes a few parameters. We use the expression const here for constant or not changeable, the first parameter is an int parameter, it is called id, and it belongs to the event id, the second one is a long parameter – this is not a typo here, it’s actually the way to pass a reference instead of a value – the parameter is called lparam for long parameter, the next one is a reference and this one is called dparam for double parameter, because it’s a double value and the last one is called sparam because it’s a string, that’s basically it. Now what can we do with the on short event function? We can check if the id equals something, in our case we want to use chart event click and if that is the case we want to create an output. This is a message box, it will show the words: chart was clicked, the x value, the y value, this is the title of the message box, and this is the button that we are going to use so let’s compile the code. If you don’t have any errors here you can now click on the little button here or press F4 to go back to Met trader, and in Metatrader we simply drag the expert advisor on a live chart, click on ok and now when you click you will see the little message box here, so now you know how to get the current position of the mouse click and you have coded it yourself with a few lines of mql5 code.
    5 min
  • MQL5 TUTORIAL BASICS – 35 HOW TO CALCULATE THE LOWEST CANDLE PRICE
    In this video, we are going to find out how to find the lowest of the last one hundred candles and we are also going to draw a horizontal line on the chart, so let’s find out how to do that with mql5. To start 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 lowest candle, click on continue, continue and finish, now you can delete everything above the on tick function and the two comment lines here. We start by creating an array for the price data, the array will be called price information and we use mqlrates to create it, afterwards we sort the array using array set as series that will be done from the current candle downwards for our price information array and now we can use copy rates to fill our price information array with data for the current symbol on the chart and the currently selected period on that chart. We will start at candle zero – that’s the current candle – and we need the price information for one hundred candles, let’s create a variable for the lowest candle, that will be also called lowest candle, it’s an integer value because it would just give us the number of the lowest candle. We also need an array for the price data for the lowest prices, that will be called low, let’s also sort that one by using array set as series and to fill this array with price data we use copy low. Copy low will give us the minimal bar prices for the selected period for the current symbol on the chart and the period that is selected on that chart, once again we start with the current candle zero, we want the price data for one hundred candles and we want to store the values in our array called: low, and now we can use another function called array minimum, it searches for the lowest element in our array, the array is the first parameter, the second parameter is the start index, and the third parameter is the number of checked elements. We are going to start at the current candle zero and we will look into one hundred candles to find out which candle is the lowest one. Now we can use that information to create an object, that is done by using object create, object create requires a lot of parameters, the first one is for the current symbol on the chart, the second one is the name of the object, in our case it’s named line one, we use obj hline, that’s for a horizontal line, you could also draw vertical lines or a lot of other objects, depending on what you are going to draw you will need different parameters, in our case we need to define in which window we want the horizontal line to appear, the value zero will draw the horizontal line on our chart, the next one is the anchor point, the first anchor point is candle zero and the line should be drawn on the lowest price of the lowest candle in our price information array. We can also use object set integer to set properties, in our case it’s the color, the first parameter is for the chart id, in our case once again this is zero because zero stands for the main chart, the name of the object is line one, the property we want to change is object property color – all in capital letters – and we want to use the color clr magenta, this is just one of several predefined colors that we can use, let’s also set the object width, this is very similar, we use objects set integer on the main chart for the object line one, this time the property we want to change is object property width and it should be three pixels. Finally, we use object move to move the created object line, for the current symbol on a chart, for the object with the name line one, on the main chart starting at anchor point candle zero, to the low of the lowest candle in our price information array. Object create and object move are very similar, you could also use object create and object delete and create another object for each cycle but for this...
    8 min
  • MQL5 TUTORIAL BASICS – 35 HOW TO CALCULATE THE LOWEST CANDLE PRICE
    In this video, we are going to find out how to find the lowest of the last one hundred candles and we are also going to draw a horizontal line on the chart, so let’s find out how to do that with mql5. To start 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 lowest candle, click on continue, continue and finish, now you can delete everything above the on tick function and the two comment lines here. We start by creating an array for the price data, the array will be called price information and we use mqlrates to create it, afterwards we sort the array using array set as series that will be done from the current candle downwards for our price information array and now we can use copy rates to fill our price information array with data for the current symbol on the chart and the currently selected period on that chart. We will start at candle zero – that’s the current candle – and we need the price information for one hundred candles, let’s create a variable for the lowest candle, that will be also called lowest candle, it’s an integer value because it would just give us the number of the lowest candle. We also need an array for the price data for the lowest prices, that will be called low, let’s also sort that one by using array set as series and to fill this array with price data we use copy low. Copy low will give us the minimal bar prices for the selected period for the current symbol on the chart and the period that is selected on that chart, once again we start with the current candle zero, we want the price data for one hundred candles and we want to store the values in our array called: low, and now we can use another function called array minimum, it searches for the lowest element in our array, the array is the first parameter, the second parameter is the start index, and the third parameter is the number of checked elements. We are going to start at the current candle zero and we will look into one hundred candles to find out which candle is the lowest one. Now we can use that information to create an object, that is done by using object create, object create requires a lot of parameters, the first one is for the current symbol on the chart, the second one is the name of the object, in our case it’s named line one, we use obj hline, that’s for a horizontal line, you could also draw vertical lines or a lot of other objects, depending on what you are going to draw you will need different parameters, in our case we need to define in which window we want the horizontal line to appear, the value zero will draw the horizontal line on our chart, the next one is the anchor point, the first anchor point is candle zero and the line should be drawn on the lowest price of the lowest candle in our price information array. We can also use object set integer to set properties, in our case it’s the color, the first parameter is for the chart id, in our case once again this is zero because zero stands for the main chart, the name of the object is line one, the property we want to change is object property color – all in capital letters – and we want to use the color clr magenta, this is just one of several predefined colors that we can use, let’s also set the object width, this is very similar, we use objects set integer on the main chart for the object line one, this time the property we want to change is object property width and it should be three pixels. Finally, we use object move to move the created object line, for the current symbol on a chart, for the object with the name line one, on the main chart starting at anchor point candle zero, to the low of the lowest candle in our price information array. Object create and object move are very similar, you could also use object create and object delete and create another object for each cycle but for this...
    8 min
  • MQL5 TUTORIAL BASICS – 34 HOW TO CALCULATE THE HIGHEST CANDLE PRICE
    In this video, we are going to find the highest of the last one hundred candles so let’s find out how to do that with mql5. To start 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 highest candle, click on continue, continue and finish, and now you can delete everything that is above the on tick function and let’s also delete the two comment lines here. We start by creating an array for the price data that will be called price information and we create it using mql rates, afterwards we sort the array from the current candle downwards, that is done by using array set as series for the price information array, and now we use copy rates for the current symbol and the currently selected period on the chart to fill the price information array with price data from candle zero up to candle one hundred, so that’s it for the price data. Let’s create a variable for the highest candle, that will also be called highest candle, it’s an integer because we want the number of the highest candle, we also want to create an array for the price data for the highest prices that will be called high, let’s also sort that one with array set as series and now we can use copy high for the current symbol on the chart and the currently selected period to copy the highest prices for each candle starting from candle zero up to candle one hundred, and to get the highest candle we use the function array maximum, this one searches for the largest element in the first dimension of a multi-dimensional numeric array and this one will simply give us the number of the highest candle and now that we know all that, we can use object create for the current symbol on the chart, I want to create an object and name it line one, I use obg underscore hline for our horizontal line, that’s the object type here, the next parameter is for the window, I use zero for the current chart, we want to start with candle zero and the line should be drawn on the high of the highest candle inside of our price information array. Let’s select a color, in my case I will use magenta, object properties can be defined using object set, in our case object set integer for the current chart, the second parameter is the name of the object, in our case it’s line one, I want to set the color for the object and this is the value, clr magenta is one of the countless colors that are predefined, I can also set the property for the object width, that’s almost the same but now we simply use a value of three pixels and for each time the price changes we use object move to move our object to the new high. Finally, we use comment to create an output that would show us the number of the highest candle and the price of the highest candle directly on our chart. That’s about it, please click on the compile button or press F7 on your keyboard, we don’t have any errors here just two warnings for typecasting from string to number, if you don’t understand all the code here or if this is all too fast for you maybe you want to watch the other videos in this basic series or maybe even the premium course might be interesting for you, for now please click on the compile button and if you don’t have any errors you can click the little icon here or press a 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 highest candle dot ex5, mark the visualization mode here and start a test. And here we are! The expert advisor is already working, it draws a line above the current high of the last one hundred candles and now you know how to code that with mql5 and you have coded it yourself with a few lines of code in the Metaeditor.
    6 min
  • MQL5 TUTORIAL BASICS – 33 SIMPLE SELL POSITION MODIFIER
    In this video we are going to find out how to reduce the position size for an existing sell position, here we have nine micro lot and now the position size is reduced so let’s find out how to code that in mql5. To do that 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 sell position modifier, click on continue, continue and finish. Now you can delete everything above the on tick function and the two comment lines here, we start by including the file trade dot mqh, this one comes with mql5 and we use it to open trades, to do that we create an instance of ctrade that will be called trade. As we are only going to close sell positions we need to calculate the bid price, this is done by using symbol info double for the current symbol on the chart, we use symbol underscore bid as the second parameter and I also like to use normalize double and underscore digits because this will automatically calculate the right number of digits behind the dot. Afterwards, we want to check if we have no open positions, that would be the case when positions total delivers a return value of zero and in that case we use trade dot sell to open a test position for ten micro lot. Actually, this is a sell position not a buy position and now we want to reduce the position size, so we call the function change position size with the bid price but before we can do that we need to code that function here. We use the return type void, the name of the new function is change position size and here we will use the double value for the bid price that we have passed here. First we want to calculate the balance that is done by using account info double, the parameter we use is account underscore balance – all in capital letters –, let’s repeat that for the equity, this is almost similar, once again we use account info double but this time the parameter is account underscore equity. Afterwards, we use a for loop to go through all the open positions, in our case it’s only one test position, you wouldn’t do that on a real account, on a real account you would use the for loop to go through all the open positions. Afterwards, we need to get the order symbol for the current position that is done by using position get symbol for the current counter value in the for loop, so now we can check if the current symbol on the chart equals the order symbol and if that is the case we need to find out the position ticket because we will need the ticket number to close a part of the existing position. We do it by using position get integer, the parameter is position underscore ticket and as we are only going to close sell positions we also want to get the position direction that’s done by using position get integer for the position type, so let’s find out if this is a sell position, that would be the case if the position direction has the value position type sell, and finally we want to check if the equity is ten points or more above the balance, that’s the case when we make a profit and in that case we are going to use trade dot position close partial for the current position ticket, we want to reduce the current position size by one micro lot, this would be a tolerance value, we use minus one in this case, let’s close all the open loops here and that’s about it. 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 video series or maybe even the premium course is interesting for you, for now please 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 the little button here or press F4 to go back to Met trader. And in Metatrader you want to click on view, strategy tester or you could press control and r on your keyboard, this is the new strategy tester,
    7 min
  • MQL5 TUTORIAL BASICS – 32 SIMPLE BUY POSITION MODIFIER
    In this video we want to find out how to modify an existing position, in our case we have a ten micro lot position here, it’s a buys position and as soon as the equity is higher than the balance the position size will be reduced. Let’s speed up this process a little bit, and now you should see that this volume is going to decrease, so let’s find out how to code that in mql5. To do that please click on the little button here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new, expert advisor from template, continue, I will call this file simple buy position modifier, click on continue, continue and finish. Now we can delete everything above the on tick function and the two comment lines here. First, we need to use the include statement for the file trade dot mqh that comes with mql5, we use it to create an instance of the class ctrade that will be called trade, first, we need to get the ask price because we are going to use test buy positions. To calculate the ask price we use symbol info double for the current symbol on the chart, we use symbol underscore ask and with normalize double and underscore digits we want to automatically calculate the right number of digits behind the dot. Now we use positions total to calculate if we have no open positions, that would be the case if the return value is zero and if that is the case we use trade dot buy to open a test buy position, the position size will be ten micro lot and afterwards we want to use a new function called change position size and we pass the ask price as a parameter here, we use void as we don’t need to return any values, the name is change position size, and this is the parameter that we have passed here. First we calculate the balance that is done by using account info double, the parameter is account underscore balance – all in capital letters – we also want to calculate the equity, this is done with account info double and the parameter account equity – also in capital letters – now we use a for loop to go through all the open positions, we want to get the symbol of the position that is done by using position get symbol for the current position in the loop and now we compare it to the symbol on the chart and if the symbol on the chart and the symbol of the currency pair are equal we need to get a few things; the first one is the position ticket, we get the ticket by using position get integer and the parameter here is position underscore ticket, we want to get the position direction, the position direction can be calculated by using position get integer, position type – this one is also in capital letters – and if the position direction equals position type buy that means we have a buy position, so let’s check if the equity is ten points above the current balance and when all those conditions are true we use trade dot position close partial for the current position ticket, we want to close one micro lot each time we call this function. For the tolerance you could define a few points here, we use minus one and finally we need to close the if loop, the for loop and the function, and that’s about it. If you don’t know what the code here does or if it was too fast for you, you might want to watch the other videos of this basic series first or maybe even the premium course is interesting for you, for now we click on the compile button or press F7, we don’t have any errors, so now we can click here or press F4 to go back to Metatrader. In Metatrader we click on view, strategy tester or press control and r, here comes up the new strategy tester wizard, we select “Run single test on an Expert Advisor”, let’s select the new file: simple buy position modifier dot ex5, we also need to enable the visual mode here and start a test. Here is our test position, we have a ten micro lot test position and as soon as the price crosses the green line here we should see that the position size is going to be reduced,
    7 min