MQL4TUTORIAL.COM

MQL4TUTORIAL.COM

By MQL4TUTORIAL.COMBusinessInvesting
Download on the App Store

MQL4TUTORIAL.COM episodes

  • MQL4 TUTORIAL BASICS – 38 HOW TO CATCH A KEY EVENT
    In this video, we want to find out how to catch a key event with mql4. A key event is whenever you press a key on your keyboard that is a so-called chart event, so this is the chart, you don’t see very much right now but when I press the key a I will get a message box that says a key was pressed and the key was a. So let’s confirm that with okay. Now I will press the b key and here is the next message box, the key that was pressed is key b and now we want to find out how to code that in mql4. 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, expert advisor from template, continue, I will call this file simple key event, click on continue, continue and finish, now we can delete everything that is above the on tick function and let’s also remove the two comment lines here. So most of the time we would use the on tick function because this is the function that is automatically called whenever the price changes on the chart but in our case, we are looking for a key that is pressed, so we need another event. This one is called on chart event and we need to pass a few parameters, we already have done that before for the mouse click event but let’s repeat the parameters. The first one is an integer value, it’s called event id and it will give us the event that happened, in our case we are looking for a key that was pressed. An event could be something like an object that was created or a mouse-click. The second parameter is a long value and this is no mistake, it’s the way to pass a value as a reference and because it’s a long value its called lparam, actually we will see that what we need is not a long value but it is passed in the lparam. The next parameter is a double value so it’s called dparam and the last one also passed as a reference is called sparam because it’s a string. That’s it for the parameters, so let’s find out what we can do. We want to check if the event id equals chart event underscore key down – all in capital letters – and if that is the case we use the function translate key to get the value of the lparam, we convert it to an integer value here and we also want to create a message box for the output, that is done with the message box function that comes with mql4. We want to output the text: key was, followed by the converted key code, this is the heading and here we use an ok button to confirm the message box, it is actually able to handle a lot of flags, you could use something like a yes/no and a cancel button, these are all the return codes, so you can actually check which of the contained buttons was pressed, we don’t need that for this simple example, please don’t forget the closing brackets here and now you can click on the compile button, that should work without any errors but if you don’t understand 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 could be interesting for you. In my case the compilation process was successful, so now I can click on a little button here or press F4 to go back to Metatrader. This is an empty chart window and now I simply drag the expert advisor from the navigator panel onto the chart, click on ok, now you should see that the simple key event is enabled here, so let’s press the m key and now the message box comes up and says that a key was pressed, the key was m and in this little video you have learned how to recognize key codes with mql4 and you have coded it yourself with a few lines of mql4 code. MQL4 Tutorial searches:on key press mql4
    7 min
  • MQL4 TUTORIAL BASICS – 37 HOW TO IMPORT EXTERNAL SOURCECODE
    In this video I would like to show you something, it’s how to import external source codes. This is a strategy test and when I click on stop you will see that we have an rsi signal here that’s actually what’s calculated inside of the strategy test but you don’t see anything because I didn’t change the template so far and its possible to exchange the module, the external source code. If you click on the include folder inside of your Metatrader installation directory, you will see files that have the extension mqh. There are lots and lots of files inside of those folders and you can open them with Metaeditor or with any editor and this is what you see. This is all source code, its readable for humans, these are hundreds and hundreds of lines of source code, they are provided by Metaquotes but we want to find out how to import our own source codes, so 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 source code import, 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 using an include statement, I would like to use a file that is located inside of a folder that is called import. I have created this folder and the file I would like to import is called check entry underscore macd, this is an mq4 file so it’s a source code file, so I don’t have to write all the source code again, I just do it once for each entry and I can import it over and over again in all of my systems. Now we will create a string variable that is called signal and it will call a function that is called check entry to get the signal for the macd, and finally, we will use the comment statement to output the word signal, followed by the calculated signal and that’s all. Now I can click on the compile button, and it works without any errors but remember you would need to have the same source code file here otherwise the compilation process will not work. In my case, I can click on the little button here or press F4 to go back to Metatrader. Now I click on view, strategy tester or press ctrl and r, I pick the new file simple source code import dot ex4, enable the visual mode here and start a test. Here is the expert advisor, it creates buy and sell signals and when I stop it you will see that it actually has calculated the macd indicator, so let’s exchange that and use rsi in the source code, I re-compile the code, restart the test and when I stop it this time you will see this is the rsi indicator. Actually, I have created these import files for several expert advisors and indicators and once you have created one of these files here you can use them over and over again. Let’s copy this one, that’s for the bollinger bands, I will paste the file inside of the import folder that I have created, let’s close this one, exchange the entry, click on compile and when I start a new test and stop it you will see the bollinger bands are calculated. This is a very easy way to code something once and re-use it again and again, and in this little video you have learned how to include external source code files and you have coded it yourself with a few lines of mql4 code.
    6 min
  • MQL4 TUTORIAL BASICS – 36 SIMPLE MOUSE CLICK EVENT
    In this video we are going to find out how to use a mouse click to bring up a message box, when I click on the chart nothing happens, let’s drag the expert advisor on to a live chart, click on ok, and when I now click into the chart I will get a message box telling me the chart was clicked, we also see the coordinates here, so let’s find out how to code that in mql4. 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, 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 let’s also remove the two comment lines here. Well usually we like to use the on tick event, that’s also an event but this one is automatically caused whenever the price changes, but this time we are going to use the on chart event because when I click on the chart the event is fired. This one takes a few parameters. The first one is for the event id, you see that we use the term const because it’s a constant value – in this case, an integer value – and we are going to find out the kind of event id that is fired and check if it is a mouse click. The second parameter is a long parameter so it’s called lparam for long and this is not a mistake, it’s the way that you can pass values by reference in mql4, the third parameter is called dparam because it’s a double value, and the last one is called sparam because it’s a string value, and that’s basically it. So what can we do? We want to check if the id equals chart event click – all in capital letters – and if that is the case we will use a message box that will contain the text: “the chart was clicked”, it will output the x value that is what is inside of the l parameter, we also want to know the y value that’s inside of the d parameter, this message here is just their heading and our message box will only have one button, that’s the okay button and that’s about it. When you are done you can click on the compile button here or press F7 on your keyboard… Oh, I did something wrong because this one needs to be the event id, so let’s recompile the code and this time it worked. If this is also the case for you, you can click on a little button here or press F4 to go back to Metatrader. And in Metatrader right now when I click on a live chart nothing is happening so let’s drag the expert advisor right on to the live chart, click on ok, now you should see the little symbol here and when you click on to the chart the message box comes up right away. By the way that wouldn’t work in the strategy tester, if you would like to do something like this and use the strategy tester you would need to use the comment statement because the message box is not supported but it works on our live chart and in this little video you have learned how to get the coordinates for a mouse click on the chart and show a message box with the values and you have coded it yourself with a few lines of mql4 code. MQL4 Tutorial searches:mql button with 2 line
    6 min
  • MQL4 TUTORIAL BASICS – 35 HOW TO GET THE LOWEST CANDLE PRICE
    In this video, we are going to find out the lowest of the last one hundred candles and we are also going to draw a horizontal line below it, so let’s find out how to do that with mql4. 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 until you want to click on file, new, expert advisor from template, continue, I will call this file simple lowest candle, click on continue, continue and finish, now we can delete everything above the on tick function and the to comment lines here. First, we want to find the lowest of the last one hundred candles, that will be the number of the lowest candle and it will be assigned to a variable that is also called lowest candle, we can get the candle number by using i lowest. I lowest is a function that returns the lowest value over a specific number of bars. In our case we pass the parameters for the current symbol on the chart and the selected period on that chart, the next parameter is mode underscore low – all in capital letters – mode underscore low stands for the lowest price in the candle, we want to have the lowest candle number for one hundred candles and the last parameter here is the starting point or the starting index as they call it, and the starting point in our case will be the current candle zero, now we use object delete for our line that we are going to draw, that might seem a little strange but in case we have already a line on the chart we first want to delete the old one, and afterwards we use object create to create a new one. This object will be called line, that’s a string value for the name of the object, that can be anything you like, the second parameter is the kind of object we are going to use, I’m using obj h-line here – all in capital letters –, let’s mark that and press F1 and you will see that it stands for a horizontal line, there are several other object types available, depending on what kind of object you are going to draw you will need to pass different parameters. In our case for the horizontal line the third parameter here is for the sub-window we are going to use, zero stands for the main window so the horizontal line will be drawn on our main chart, oscillators like the MACD are often drawn below the main chart. The next parameter is the anchor point, we want to start to draw the horizontal line, right away from candle zero – that’s the current candle – and it should be drawn at the lowest price of the lowest candle within our hundred candles. Finally, we add a chart output using the comment function, this will create the output “The lowest of the last one hundred candles”, “is candle number” followed by the calculated number for the lowest candle, and in a line below we want to see “The lowest price is” and the calculation for the lowest price within that one hundred candles. Please don’t forget the closing bracket here and that’s basically 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 basic videos in this video series first or maybe even the premium course might be interesting for you, you can find that one on the website but if you are done you can now click on the compile button, you shouldn’t get any errors and if that is the case please click on the little icon 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 lowest candle dot ex4, enable the visual mode here and start a test. And here we are! The expert advisor is running, we see the horizontal line is drawn below the lowest candle, the output also is working and in this little video you have learned how to calculate the lowest of the last one hundred candles and you have done it yourself with a few lines of mq4 code. MQL4 Tutorial searches:highest candlesticks mql4
    6 min
  • MQL4 TUTORIAL BASICS – 34 HOW TO GET THE HIGHEST CANDLE PRICE
    In this video, we are going to find out how to calculate the highest of the last one hundred candles and we are also going to draw a horizontal line above the highest price, so let’s find out how to do that for Metatrader 4. 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, expert advisor from template, continue, I will call this file simple highest candle, 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. To calculate the highest of the last one hundred candles we use the function i highest, it will return the maximum value over a specific number of bars and it takes a few parameters, we want to do it for the current symbol, so we use underscore symbol, the second parameter is underscore period, that will automatically calculate the current period for the selected chart, we use mode underscore high – all in capital letters – the count parameter is for the number of bars that we are interested in. In our case we want to calculate the highest of one hundred candles, started from candle number zero –that’s the current candle –, we start with object delete, that’s a little bit strange here because we have not created the object so far but once we have done that the old instance will be deleted each time the on tick function is called, please remember on tick is the function that will be called whenever the price changes, so let’s actually create the object that is done by using object create, the name of the object will be line, I use obg hline, let’s mark that and press F1 and you will see all the different types of objects that can be created, we have lots of them. After the object type we need to define the window where the object should be drawn, window zero stands for the main chart, this is the first anchor point, we are using candle zero and the line should be drawn on the high of the highest candle that we have calculated here. Finally, let’s create a chart output by using the comment function, this will output the text: “The highest of the last one hundred candles”, “is candle number” followed by the number of the highest candle and in a new line we want to see: “The highest price is” and the calculated price. That’s almost it, please click on the compile button or press F7, we don’t have any errors and we don’t have any warnings, so in my case everything is working, if this was too fast for you or if you don’t know 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 could be interesting for you, you can find that on the website but if the compilation worked for you, you now want to click on the little symbol here or press F4 to go back to Metatrader. And in Metatrader we want to click on view, strategy tester or press control and r, please pick the new file simple highest candle dot ex4, please enable the visual mode here and start a test. Here we are, the expert advisor is counting, it has drawn the line above the highest high, let’s zoom in and as the price is rising we see that the candle number is changing here, now it says the highest candle is candle number one, that’s correct, this is candle zero and this would be candle one, so our little expert advisor is working and in this little video you have learned how to calculate the highest of one hundred candles and how to draw a line for the highest price and you have coded it yourself with a few lines of mql4 code. MQL4 Tutorial searches:mql4 highest candle between 12 and 3 pm
    6 min
  • MQL4 TUTORIAL BASICS – 33 HOW TO MODIFY A SELL POSITION SIZE
    In this video we are going to find out how to reduce existing selling positions when the price goes down, so let’s find out how to calculate that in mql4. 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 sell position modify, click on continue, continue and finish. Now we can delete everything above the on tick function and the two comment lines here, the first thing we do is to check if we have no open orders, that would be the case if orders total delivers a return value below one and in that case we are going to open a demo sell trade that is done by using order send for the current symbol, we want to sell ten micro lot. Now that we have a test position we want to go through all the open orders, that’s done by a for loop, orders total will deliver the number of open orders and we go through all the orders until we have no order left, we select each order that is done by using order select for the current value of the for loop counter, we use select by position and mode trades as parameters and now we check if the current order symbol is equal to the symbol on the chart and as we only want to modify sell trades we also want to know if the return type of order type is op underscore sell – all in capital letters – and if that is the case we have a sell position, so let’s check if the equity is above the balance that would be the case if the account equity is bigger than the account balance and if all these conditions here are true we want to close a part of the existing position that is done by using order close for the current order ticket, we want to close one micro lot each time for the current ask price, the slippage tolerance is three, and the last parameter here stands for a color – we don’t need a color – so we use clr underscore none, that’s almost it. Let’s close the two loops here and now you can click on the compile button or press F7, you should not get any errors but 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 of this video starter serious or maybe even the premium course is interesting for you but for now if you don’t have errors please click on the little button here or press F4 to go back to Metatrader. And in Metatrader we click on view, strategy tester or press control and r please select the new file: simple sell position modify dot ex4, mark the visual mode here and start a test. Let’s save the file as simple sell position modify dot mq4, pick this one and this time the expert advisor is working, it was just the filename, simple error can take hours to find but in this little video you have learned how to create an expert advisor that is able to modify existing sell positions and you have coded it yourself with a few lines of mq4 code.
    5 min
  • MQL4 TUTORIAL BASICS – 32 HOW TO MODIFY A BUY POSITION SIZE
    In this video we want to find out how we can modify the size of an existing buy position, this one is reduced right now, you see that the position size is changing and we want to find out how to code that in mql4. 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 modify, click on continue, continue and finish. Now you can delete everything above the on tick function and we also delete the two comment lines here. Inside of the on tick function we want to check if we have no open orders, that would be the case when the return value for the function orders total is below one and if that is the case we want to open a test position. You wouldn’t do that on your real account but we need to open a position here so we have something that we can modify later on, so we use the order send function to open a position for the current symbol on the chart, we use op buy and the position size will be ten micro lot, so now that we have opened a position we use a for loop to go through all the open orders, in our case it will be only one order but on your real account it would check the other positions too and for each order we use order select for the current counter value, we want to use select by position and mode underscore trade – all in capital letters. Afterwards, we want to check if the order symbol belongs to the current currency pair on the chart and one more thing we want to know is if the order type equals op underscore buy – all in capital letters here – if that is the case we have a buy position. Now we want to check if the account equity is higher than the account balance and if all these conditions are true we use order close for the current order ticket number to close a part of the position, just one micro lot for the current bid price, the slippage of three, and we don’t need any color, let’s close the brackets here, this one is for the if statement and this one is for the for loop and that’s about it. 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 course or maybe even the premium course is interesting for you, for now, let’s click the compile button or press F7, I don’t have any errors here, so now I can click on the little button here or press F4 to go back to Metatrader and in Metatrader we click on view, strategy tester or press control and r, please pick the new file: simple buy position modify dot ex4, enable the visual mode here and start a test. And here we are! We have a buy position of ten micro lot and as soon as the price is above the green line the position size is reduced, so our little expert advisor is working as desired and you have coded it yourself with a few lines of mql4 code.
    5 min
  • MQL4 TUTORIAL – 31 HOW TO CLOSE ALL SELL POSITIONS
    In this video we are going to find out how to close all the open sell positions at the same time, this little expert advisor is going to open positions and as soon as we have ten open positions on the chart it is going to call a custom function to close them at the same time, so let’s find out how to code that in mql4. 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 sell position closer, click on continue, continue and finish, now you can delete everything above the on tick function, let’s also remove the two comment lines. First, we want to use the orders total function to find out if we have less than ten open positions, if you mark orders total and press F1 you will see that it actually returns the number of market and pending orders but we are not going to use pending orders in this case, we use order send to open test sell positions as we need them to have something to close and we do that until orders total equals ten, that’s when we want to close all the open sell positions, so we call a function that is called: close sell positions that doesn’t exist right now so we need to create it. We use the return type void here as we are not going to return anything, the name will be: close sell positions, we are going to use a for loop to go through all the open positions. First, we select an order by using order select for the current number in the for loop. We use select by position and mode trades. First we need to find out the currency pair for the position that is done by using order symbol and now we need to find out if the symbol on the chart and the currency pair are the same, we also want to find out if the order type equals op sell, if that is the case we have a sell position and now we use order close to close the current position for the order ticket that we have calculated. We are going to find out the lot size by using order lots, the order will be closed for the current ask price, we have a slippage of three, the last parameter here is for a color – we don’t need one – so finally we close the for loop and we close the function and that’s it. Let’s click on the compile button or press F7, we don’t have any errors here so let’s click on the little symbol 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 sell position closer dot ex4, mark the option for the visual mode here and start a test. Here we are! The expert advisor is going to open and close positions, so it works and you have coded it yourself with a few lines of mql4 code.
    5 min
  • MQL4 TUTORIAL BASICS – 30 HOW TO CLOSE ALL OPEN BUY POSITIONS
    In this video, we are going to create an expert advisor that is able to close all the buy positions at once, as soon as we have ten open test positions all the positions will be closed, so let’s find out how to do that with mql4. To start 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, expert advisor from template, continue, I will call this file: simple buy position closer as we are only going to close buy positions in this video. So let’s click on continue, continue and finish, now we can delete everything above the on tick function and let’s remove the two comment lines here. We start by checking if orders total is below ten, that would mean that we have less than ten positions, to be exact orders total would also deliver the number of pending orders so we need to check the order type later on and if we have less than ten positions we use order send for the current chart to open buy positions. These are just test buy positions, you wouldn’t do that on a real account but we need to open a few positions here to have something to close. And if orders total equals ten that will mean that we have exactly ten positions and that is when we want to close all open buy positions by calling a function called: close buy positions. So far this function does not exist so we need to create it now. We don’t use a return type here and the name of the function will be close buy positions. First, we use a for loop to go through all the open orders and as long as we have found an order we want to use order select to select it. This is the current counter of the for loop, we use select by position and mode underscore trades to be sure we want to get the current currency pair for the order otherwise, it might be possible to close orders and other currency pairs and you don’t want to do that. So if the current symbol on the chart and the current order currency pair match and if the current order type equals op underscore buy – that means that we have a buy position – that is when we actually want to close the order by using order close for the current order ticket, we want to close the current position size of the order for the current bid price, the slippage is three so when the price changes during this operation we have a little tolerance. The last parameter here would be a color but we don’t need that, so finally we want to close the for loop and the function. If you don’t know what all the code here does or if this was too fast for you 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, let’s click on the compile button or press F7, that should work without 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, you want to click on view, strategy tester or press control and r, please pick the new file, simple buy position closer dot ex4, mark the visual mode here and start a test. And here we! The expert advisor is opening positions whenever a tick comes in and as soon as we have reached ten positions it is going to close them all at once. This video was an idea from a premium course member and if you have any idea that should become such a video here just send me an email because in this little video you have learned how to create an automated expert advisor that is able to close all the open buy positions automatically and you have coded it yourself with a few lines of mql4 code. MQL4 Tutorial searches:how to close all sell position mql4how to close a buy position in mql4
    6 min
  • MQL4 TUTORIAL BASICS – 29 HOW TO COUNT THE OPEN SELL POSITIONS
    In this video we want to find out how to count the number of open sell positions, right now our expert advisor is telling us that we have six open sell positions and when we sort by type we will see one, two, three, four, five, six positions, so let’s find out how to code that in mql4. 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 sell position counter, click on continue, continue and finish. Now you can delete everything between the on tick function and the on init function here, we also delete everything above the on init function and we want to remove the comment lines. Inside of the on init function I want to call a custom function called: open test positions, you wouldn’t do that on your real account but I need some positions that I can count later on and in the on tick function we use the comment statement to output the text: the number of sell positions and this little function here will deliver the current number of open sell positions. We need to still create it now, so let’s get started. I don’t need a floating type value here, the name of the current function will be count sell positions and we start by creating an integer variable called: number of sell positions, the first value will be zero because we need to calculate it later on. Now we use a for loop and the function orders total that comes with mql4 and will give us the number of orders. To go through all the open orders and for each of those orders we need to get the current currency pair that will be delivered by the function order symbol, first we use the order select function to select the order for the current number, we want to use select by position and mode underscore trades –all in capital letters. Afterwards I would like to get the current currency pair of the order that is delivered by the function order symbol because that will be helpful now when we want to check if the current symbol on the chart equals the currency pair for the open order and if that is the case we want to check if the order type equals type op sell. Op underscore sell – all in capital letters – will give us the sell trades, we are not interested in pending orders in this example so op sell is what we need and if all those conditions are true we want to take the current number of sell positions and increase it by one. Finally, we close the for loop here and use the return statement to return the number of sell positions to our main module. I would like to show you the open test positions function that I used, this is the initialization process for a random generator, it will create a random number of test positions and we will use that number to open buy and sell trades here and as I said you wouldn’t do that on a real account! So when you’re done you can click on the compile button here or press F7. If this is not the case or if you don’t know what all the code here does I would suggest to go through the other videos in this starter series or maybe even the premium course is interesting for you, in my case the compilation process was successful so now we can click on the little button here or press F4 to go back to Metatrader. In Metatrader you want to click on view, strategy tester or press control and r, here we pick the new file simple sell position counter dot ex4, please enable the visual mode here and start a test. Here is our expert advisor, it currently says that we have four open sell positions, so let’s see if that is true. Let’s change the order here and we have one, two, three, four sell positions so our little expert advisor is working. If you want to suggest anything to become a video like this one just write me an email but for now you have learned how to create a simple expert advisor in mql4 to automatically calculate the current number of open sell positions and you have cod...
    6 min

About MQL4TUTORIAL.COM

From the publisher's feed

MQL4 Tutorial for Metatrader 4 or MT4 about automated trading, trading robots, MQL 4 and MQL programming