
Escuchar MQL4 TUTORIAL
Podcast de MQL4 TUTORIAL
MQL4 Tutorial for Metatrader 4 or MT4 about automated trading, trading robots, MQL 4 and MQL programming
Empieza 7 días de prueba
$99.00 / mes después de la prueba.Cancela cuando quieras.
Todos los episodios
114 episodios
In this video, we are going to create an expert advisor that is able to create a buy trailing stop for a simple moving average, so let’s find out how to do that with mql4. To get started please click on a little icon here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. We start by creating a static variable and we call it last stop moving average value. To have something that we can use with a buy trailing stop we need to create a test buy position so whenever we have no open orders we use order send to open a buy position for ten micro lots, please don’t do that on a real account, this is just for testing purposes, so let’s calculate the stop moving average value that is done by using the ima function for the current symbol on the chart and the currently selected period on that chart, I will use a simple moving average for five hundred candles, we don’t use a shift value, the mode is for sma simple moving average, the calculation will be based on the close price for candle one. Now we want to check if the current value for the stop moving average is bigger than the last value and the second condition will be if the stop moving average value is below the current bid price and if that is the case we want to try to change the stop loss that’s done by calling a function check sma buy trailing stop and we are going to pass the stop moving average value that we have calculated here. And afterwards, we want to assign the stop moving average value to the variable for the last stop moving average value for the next time we will check it because the current one will be the last one the next time we check it. So far so simple, but this function doesn’t exist so far, so we need to create it now, the name of the function is check sma buy trailing stop, we don’t return a value so we use void and we will pass a parameter called stop moving average value from the main function, we use a for loop to go through all the open orders, each order is selected for the current counter value that we have used here in the for loop. The next condition we need to check is if the current order symbol is equal to the symbol on the chart and as we are going to create a buy trailing stop we also want to check if the order type equals op underscore buy, we also want to know if the current order stop loss for the position is below the stop moving average value that we have calculated, and if all the conditions are true we use order modify for the current order ticket, we don’t change the order open price but we try to change the stop loss value here, we leave the other options unchanged, now let’s close the if statement and the for loop, and finally the function, and that’s about it. Well, if this was too fast for you or if you have no idea what all the code here does maybe you want to watch the other videos in this basic video series 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, I have messed up one of the braces here so let’s recompile now, and this time we have no errors, please notice that we have two warnings here, order send and order modify should be checked, there are several conditions when we will not be able to modify anything, for example, if we try to replace the stop moving average value with something that the broker is not accepting or if we use the same value twice. I will show you the modify errors in the journal. To get back to Metatrader please click on a little icon here or press F4 on your keyboard, now you should see the Metatrader window and here you want to create a new chart, click on insert, indicators, trend, moving average, please pick a period for five hundred candles for a simple moving average applied to the close price,

In this video, we will create an expert advisor that is able to calculate the current spread and find out what the lowest and the highest spread was for a certain currency pair, so let’s find out how to do that with mql4. To get started please click on a little icon here or press the 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 spread checker, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. We start by creating a double variable called lowest spread value and we give it an initial value of one hundred thousand, this is definitely not the lowest spread value that you will see. Now let’s create another variable called highest spread value and this one will have a value of zero and that’s definitely also not the highest value that you will see. Inside of the ontick function, we want to calculate the current candle spread that can be done by using symbol info integer for the current symbol on the chart and we use symbol underscore spread – all in capital letters – and if the current candle spread is below the value that we have for lowest spread value, we want to save it as the lowest spread value that we have seen, so we assign the value for current candle spread to our variable called lowest spread value. In the other case if the current candle spread is bigger than the highest spread value we have seen, so far we want to store the value for the current candle spread as the highest spread value. And finally, we want to use the comment statement to create a few outputs for the current spread, the lowest spread and the highest spread for our currency pair. That’s about it. If you have no idea 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 on our website might be interesting for you. Actually, this was an idea of a premium course member and if you are already a premium course member and have an idea for a video like this one please let me know but for now please click on the compile button or press F7, you shouldn’t get any errors here and if that is the case you can click on a little button here or press F4 to go back to Metatrader. And in Metatrader, we need to open an empty chart, please navigate to the new file called, simple spread checker and drag it on the chart, click okay and when the next tick comes in you should see that we have some values here, I will make it a line chart, so it’s better to read, so the lowest spread value so far was sixteen, the current spread value is sixteen and the highest spread value is seventeen. Now we see a few price changes and now the current spread is changing and we see that the highest spread value now is seventeen, the lowest one is fourteen, so our expert advisor actually works as we expected but there is one more thing to mention, please click on view, strategy tester or press control and r, please pick the new file simple spread checker dot ex4, mark the visual mode and start a test. And this time you will see no changes at all and that’s because mql4 does not store the values for the spread for history data so if you press F2 you will see that we have a history center and everything inside is open, high, low, close and volume, there are no values for the spread and that’s why this expert advisor will not be useful inside of the strategy tester but it works on real-time charts and in this little video you have learned how to find out the current, the lowest and the highest spread for a certain currency pair and you have coded it yourself with a few lines of mql4 code.

In this video, we are going to create an expert advisor that is able to change a lot of colors for different objects right on the chart, so let’s find out how to do that with mql4. To get started please click on a little icon 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, continue, I will call this file simple color changer, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. In this simple example, we are going to change the color whenever the close price for the candle before is below the close price for the last candle and if that is true we want to start by setting the color for the bull candles so that’s the color of the body inside of the candles and we use chartsetinteger for the current chart, the property we want to select is chart underscore color underscore candle underscore bull and I would like to set it to green, we have a lot of different color options. All these colors here are available. Well, you might wonder why we use chartsetinteger instead of something like chartsetcolor. Actually, we could use a number like a twenty-two here and when we now compile the code this wouldn’t give us any errors but actually clr green is better to read, so this was the body color for the bull candles. Let’s repeat that for the bear candles. Everything is the same except for the property value here. I would also like to set the outer color and this is the property that is called chart color chart up, let’s also pick green here and we are going to repeat that for the bear candles, that is the property chart color chart down. One more of those, this one is called chart mode and now I can define what type of candle I would like to see on the chart because you can either pick candles, bars or a line chart. We are going to use chart underscore bars here and if you mark that and press F1 you will see that each of those candle types can be picked. Now we are going to set the grid to visible that is done by setting the chart show grid property to true. Let’s also change the foreground color with chart color foreground, we want to set it to yellow and of course, you can also set the background color that is chart color background and I’ll set it to black. Now let’s repeat the whole thing because when the close price for candle two is bigger than the close price for candle one I would like to see other colors. I just will copy and paste it here, basically, it’s all the same, of course, you can not only set properties you also can get properties. For example the max price and the min price on the chart that is done by using chart get double for the current chart, we are going to use chart price max and this last parameter here is for the current sub window, for example, if you would like to insert an oscillator here this would be sub window zero and this would be sub window one and we are going to chose the main window and get the max price and the min price, so let’s add a chart output here by using the comment statement. This will output the text and the values for those properties and that’s about it. Well if this was too fast for you or if you have no idea what all the code here does maybe you want to watch one of the other videos in the basic video series first or maybe even the premium course on our website might be interesting for you. This was a suggestion from a premium course member and if you are already a premium course member and have an idea for a video like this one please send me an email. For now, please click on the compile button or press F7 on your keyboard, you shouldn’t get any errors and if this is the case you can now click on the little button 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 color changer dot ex4,

In this video, we are going to create an expert advisor that is able to trade randomized signals on multiple currency pairs in one chart, so let’s drag the expert advisor onto a live chart, I need to allow auto trading, I click on okay and now we see that it is able to open multiple positions on one chart, so let’s find out how to do that with mql4. To get started please click on a little icon 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, continue, I will call this file simple multi-currency order, click on continue, continue and finish. Now you can delete everything above the ontick function and let’s also remove the two comment lines here. We start with a statement to calculate the ask price, this is very uncommon for mql4, it’s what we usually do in mql5 and to calculate an ask price you can use symbol info double, in this case for the current symbol on the chart, the second parameter here is symbol underscore ask – all in capital letters – and with normalize double and underscore digits I am going to cut the number of digits behind the dot to automatically fit the currency pair because we have currency pairs with three digits behind the dot and others with five digits behind the dot. And that’s what we do here. Well usually we just would take the ask price as it is but in this case we are going to open multiple positions for different currency pairs and therefore we need to find a way to calculate the ask prices for the other currency pairs, in this case for the US dollar against the Canadian dollar and this is how we can do it, in this case, I have exchanged the first parameter for the currency pair I want to trade. Please make sure that it matches what you see here in the market watch because some brokers use suffixes so let’s make sure that you get it right. Now let’s repeat that for the second currency pair. This is almost the same, except for the name of the variable and the currency pair string. Of course, we can do the same for the bid price, in this case, its symbol info double for the current symbol on the chart and we use symbol underscore bid and now we repeat that for the US dollar against the Canadian dollar. I have called this one USDCADBid and I have exchanged the first parameter and you might have guessed it we also do it here for the British pound against the US dollar. Now that we have all those bid prices and ask prices let’s create a string variable called signal. We are not going to assign a value here because this is going to be calculated now. In this case, we are going to use random entries, so I use mathsrand and gettickcount to create an initial value because gettickcount returns the number of milliseconds since we have started the system, this is a unique value and this whole expression will make sure that we do not get the same random values all the time and to calculate the actual random number we are going to use mathrand. This looks almost like the one we used before except the s in the middle and mathrand is used to calculate pseudorandom integer values between zero and thirty-two thousand seven hundred sixty-seven and this suffix here, percent two will give us two random numbers, so whenever the random number equals zero we would consider that to be a buy signal, so we assign the word buy to our signal. And if the random number equals one we consider that to be a sell signal so now we assign the word sell to our signal and whenever we have a buy signal and when orders total equals zero – that would mean we have a buy signal and no open orders – that’s when we are going to send three orders for the current symbol on the chart, the British pound against the US dollar and for the US dollar against the Canadian dollar. In the other case if the signal equals sell and we also have no open orders I would like to use order send to sell for the current symbol,

In this video, we are going to calculate this awesome oscillator here and we will also be able to define user signal values for the entries, so let’s find out how to do that with mql4. To get started please click on a little icon here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this file, simple awesome indicator, click on continue, continue and finish. Now you can delete everything above the ontick function and let’s also remove the two comment lines here. We start with an input variable, this input modifier will make it possible to let the user change the property in the expert advisor settings, it’s a floating type variable, we will call it signal value and this is the initial value and it can be changed by the user inside of the expert properties later on without recompilation. Let’s continue by creating a string variable called signal, we will not assign a value here because that needs to be calculated. And to get the current value for the awesome oscillator we use the included function iao, this one comes with mql4 and it has only three parameters. The first one is for the current symbol on the chart, the second parameter is for the period that we have selected on that chart, and the third parameter here is for the shift, we don’t use that so it is zero and that’s all to get the basic value here but to avoid confusion about rounding problems I would like to convert it to a better format so I will use a function called normalize double, the first parameter here is the value that we have calculated, the second parameter here is for the number of digits behind the dot so our awesome value here will be better to read in the comment statements. Now let’s define the entry conditions if the current awesome value is bigger than the signal value that we have defined here we would consider that to be a sell signal, so we assign the word sell to our signal. In the other case if the awesome value is below zero minus the defined signal value – that would be the negative value here – and if this expression is true we want to assign the word buy to our signal. So whenever we have a buy signal and the return value for orders total is zero – that would mean that we have no open orders and we want to buy – and that’s when we use order send to buy ten micro lot. In the other case if the signal equals sell and we have no open orders that’s when we want to sell and now we use order send to sell ten micro lot. Finally, we use the comment statement to make some outputs on the chart I would like to see the awesome value on the chart please notice that I used double to string to prevent that we see exponential expressions here and the last two things on the chart will be the signal value for buy and for sell trades so that’s about it. If this was too fast for you or if you have no idea what all the code here does maybe you want to watch one of the other videos in this basic video series or maybe even the premium course on our website might be interesting for you, for now, please click on the compile button here or press F7 on your keyboard, you shouldn’t get any errors and if that is the case you can click on a little button here or press F4 to go back to Metatrader. And in Metatrader please open an empty chart and click on insert, indicators, Bill Williams, awesome oscillator, we will go with the default values here, so let’s click on okay, here is the oscillator and now please right-click into the window and select template, save template and save it as tester dot tpl, you can override the current value just tester tpl used in the strategy tester, otherwise, we wouldn’t see anything, so now that we have done that please click on view, strategy tester or press control and r, select the new file, simple awesome indicator dot ex4, mark the option for the visual mode here and start a test. Here we are!
Empieza 7 días de prueba
$99.00 / mes después de la prueba.Cancela cuando quieras.
Podcasts exclusivos
Sin anuncios
Podcast gratuitos
Audiolibros
20 horas / mes