
MQL5 Tutorial
Podcast von Raimund Bauer
MQL5 Expert Advisor Algo Trading for Metatrader5
Nimm diesen Podcast mit
Alle Folgen
128 Folgen
In this video we are going to create an expat advisor for a simple moving average sell trailing stop, so let’s find out how to do that with mql5. To get started please click on a little icon here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this file simple sma sell trailing stop, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. First, we want to include the file trade dot mqh, this one comes with mql5 and it provides trading functions, it also contains the class ctrade, so we are going to create an instance of that class called trade. We start by creating a static variable for the last stop moving average value, now we want to calculate the ask price and the bid price that is done by using symbol info double for the current symbol on the chart, we use either symbol ask or symbol bid and with normalize double and underscore digits we make sure that we calculate the right number of digits behind the dot. To use a stop loss we need to open a test position, we are going to use trade dot sell to do that and the condition is whenever positions total is below one – that means we have no open positions – and that’s when we want to sell ten micro lot. We also want to set the last stop moving average value to a very high value, actually, you could also set it to zero but you would need to add another condition because usually, we are going to check if the current value is lower than the last stop moving average value and that one will never be below zero so let’s go with the high value for now. In the next step, we are going to create an array for the moving average. With array set as series, we are going to sort the array from the current candle downwards and now we can use the integrated ima function for the current symbol on the chart and the currently selected period on that chart to create a simple moving average for five hundred candles, we don’t use a shift value here, we are going to use mode underscore sma as it stands for simple moving average and we want to calculate the results based on the close price. Let’s continue with copy buffer, this one is used to fill the array according to the moving average definition that we have created here, we will grab the values for buffer zero that’s the moving average signal line here, we will start with candle zero – that’s the current candle – and we will copy the values for three candles and store them in the array. And now that we have done that we can calculate the stop moving average value by looking into candle one of our moving average array. The first condition we want to check to move a sell stop loss is if the current stop moving average array is above the ask price otherwise we would not be able to change it. We also want to know if the current stop moving average value is below the last stop moving average value, and if this is true we are going to use a function called check sma sell trailing stop and pass the bid price and the stop moving average value as a parameter. This function doesn’t exist so far, so we need to create it now and please don’t forget to set the last stop moving average value and assign the current stop moving average value because the current one will be the last one the next time we call this function. Our trailing stop function will use void because we don’t need to return anything, the name is check sma sell trailing stop and we pass the bid price and the stop moving average value as parameters here. Now we use a for loop to go through all the open positions, we will count down until there are no other positions left, afterwards we use position get symbol for the current counter of the for loop to get the currency pair symbol for the current position and only if the symbol on the chart matches the currency pair symbol we are going to continue.

In this video, we are going to create an expert advisor that is able to create a a simple buy trailing stop that is based on a simple moving average value, so let’s find out how to do that with mql5. To get started please click on a little icon here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this file simple sma buy trailing stop, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. First, we want to include the file trade dot mqh, this one comes with mql5 and provides simplified trading functions afterwards we create an instance of the class ctrade and call it trade. Inside of the ontick function we start with a static double variable called last stop moving average value, we continue by calculating the ask price and the bid price that can be done by using normalize double, symbol info double for the current symbol on the chart and the second parameter is symbol underscore ask or underscore bid, with normalize double and underscore digits we make sure to calculate the right number of digits behind the dot. To have something that we can use for the trailing stop we need to open a test position, so whenever positions total is smaller than one or in other words when we have no open position we use trade dot buy to buy ten micro lot. That will open a new position and that’s when we set the last stop moving average value to zero because for each position we need a new value. Let’s go on and create an array for the moving average, now we want to sort the array from the current candle downwards that is done by using array set as series for our moving average array that we have created here. Now let’s define the properties, we want to use the ima function for the current symbol on the chart and the currently selected period on that chart, we want to use it for five hundred candles starting from the current candle zero, we don’t use a shift value and we use mode underscore sma because this stands for simple moving average and the calculation should be done based on the close price. Afterwards, we use copy buffer to fill our moving average array according to the definition that we have created here for buffer zero, we start with candle zero – that’s the current candle – and we copy the values for three candles and store them in the array. And to get the value that we need we simply have a look at the value for candle one in our moving average array and we assign the value to stop moving average value. Now that we have done that we can check if the stop moving average value is below the current bid price and the second condition will be if the stop moving average value is bigger than the last stop moving average value and if that is the case we want to call a function that is called check sma buy trailing stop, we will pass the ask price and the stop moving average array value as parameters here and afterwards we want to assign the current value for the next function call, so we store the value for stop moving average value in the variable called last stop moving average value. Please remember this was a static variable so it will keep the values inside of this function, well this function doesn’t exist so far, so we need to create it now, the name of the function will be check sma buy trailing stop, we use void because we don’t use a return value here and we pass the ask price and the stop moving average value as parameters. Now let’s use a for loop to go through all the open positions, we use position get symbol to get the symbol for the current position because we need to check if the current symbol on the chart is equal to the position symbol and if this is true we get the position ticket by using the function position get integer position underscore ticket. To get the current stop loss we use position get double and the parameter i...

In this video, we are going to create an expert advisor that can calculate the highest and the lowest spread for a certain number of candles in a currency pair, so let’s find out how to do that with mql5. To get started please click on the 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 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 with an input value for the candles to count, in my case I want to use five candles and find the highest and the lowest spread for those five candles, this input modifier here will allow us to change the value without recompilation by changing the expert properties. Inside of the ontick function we start with a double variable called lowest spread value and in this case, I’m going to set the initial value to one hundred thousand, this is definitely not the lowest spread value that you will see but we are going to calculate that later. Let’s create another variable, this time for the highest spread value, I will set the initial value to zero and this is also not the highest spread value that you will see. Afterwards, we use mql rates to create an array for price information. Mql rates stores the information about prices, volumes and spread and spread is what is interesting for us. Afterwards, we use array set as series to sort the array from the current candle downwards and now we fill the price array with data by using copy rates, in my case for the current symbol on the chart and the currently selected period on that chart, I’m going to start with the current candle zero and I will copy the data for two hundred candles and store it in the price information array. Now we can use a for loop to go through all the candles that are interesting, the current candle number will be the number of candles to count that we have defined here, in our case five and for as long as the current candle number is bigger or equal to zero we will go through the for loop and count down the number of candles until this condition is no longer true, and to get the current candle spread we look inside of the price information array for the current candle number and we want to know the spread, so we use dot spread. To make it easy to check if the values are correct we want to add a print statement that will output the current value for the candle, the candle number we are processing right now and the current candle spread in our journal and if the current candle spread is lower than the lowest spread value, we want to save it as the lowest spread value. In the other case if the current candle spread is higher than the highest spread value that’s when we want to save the current candle spread as highest spread value. Finally, we want to create a chart output by using the comment statement, this will show us the lowest and the highest spread on the chart and finally, we want to end the for loop and close the ontick function. 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, this was another question 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. So far so good! Please click on the compile button or press F7 on your keyboard, I don’t have any errors and if this is the case you can click on a little button here or press F4 to go back to Metatrader. And in Metatrader we want to click on view, strategy tester or press ctrl and r, please pick the new file, simple spread checker dot ex5, mark the option for the visual mode and start your test. And here we are!

In this video, we are going to create an expert advisor that is able to change the colors for several objects on the chart, so let’s find out how to do that with mql5. To get started please click on the 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. We start by creating an array, we will use mql rates to get the price information because mql rates is a structure that stores the information about price, volumes and spread. Afterwards, we use array set as series to sort our price information array from the current candle downwards and with copy data, we can fill our price information array with data for the current symbol on the chart, we are going to use the currently selected period on that chart and we will copy the data starting from candle zero for three candles and store it in our price information array. Now that we have done this we are able to calculate the close price for candle one that is done by looking into the value for candle one in the price information array. We are interested in the close price, so if you cut that out and press the period button on your keyboard you will see that you can get close, high, low or open prices, we will go with the close price. Afterwards, we do the same for the close price of candle two. Everything else is similar but this time we are going to look for the close price of candle two in our price information array and if the close price for candle two is below the close price of candle one we want to change the colors on the chart that is done by using chartsetinteger. You probably would expect something like chartsetcolor, so this is a little strange. We will do that for the current chart and the property we are going to use change first is chart color candle bull, I will set it to green, so I use clr green here and if you mark that and press F1 you will see that we can use a lot of different predefined colors here and this will change the body color for the bull candles. Let’s do the same thing here for the bear candles. Everything is pretty much the same except for the property that we are going to change, this time is chart color candle bear and now we use chartsetinteger to change the property for chart color chart up. This is for the outer color of the bull candles and I’ll also change this one to green and repeat the whole thing and do it for the bear candles by using chart color chart down. The next one is for the chart mode and here we can pick one of three values. Chart mode will change the type, we can have candlesticks, bars or line chart, we can pick one of these values and I decided to go with a bar chart. I want to have a grid on the chart, so I use chartsetinteger to set the property for chart show grid to true. Let’s change the foreground color for the data by using chart color foreground and I will set it to yellow here and finally, I would like to set the background color, so I use chart color background and set it to black. In the other case when the close price for candle two is above the close price for candle one I would like to see other colors. This is basically the same stuff so I will use copy and paste here, this time we will see the color red for the candles, that’s also what I use for the chart mode, I would like to see candles on the chart, I don’t want to see the grid, so I set the property to false. The chart color foreground should be magenta and the chart color background should be white. But you can not only set values for the chart properties it’s also possible to read values. In our case we will read the max price and the min price, that is done by using chart get double for the current chart and the properties are chart price max and chart ...

In this video, we are going to create an expert advisor that is able to open positions for multiple currency pairs in the back-test, so let’s find out how to do that with mql5. To get started please click on the 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 multiple currency order, click on continue, continue and finish. Now you can delete everything above the ontick function and the two comment lines here. We will start with an include statement to include the file trade dot mqh, it contains the class ctrade and we create an instance called trade that we are going to use to open positions later on. Inside of the on tick function, we start by calculating the ask price that is done by using symbol info double for the current symbol on the chart we use symbol underscore ask and with normalize double and underscore digits we make sure that we calculate the right number of digits behind the dot. Well as we are going to trade several currencies we also need ask prices for the other currencies, so let’s calculate the ask price for the GBPUSD. We also use symbol info double but this time the first parameter is not underscore symbol but the name of the currency pair. You might have different names, so please make sure that you spell that correctly. Now let’s repeat the whole thing, this time I would like to calculate the ask price for the currency pair USDCAD, so that’s what we use here and of course, we need to repeat the whole thing for the bid prices that is done by using symbol info double underscore symbol and symbol bid for the current currency pair and please change it here for the currency pairs you want to trade. Now let’s create a string variable called signal, so far we don’t assign any value here because we are going to calculate that now. For this simple example I am going to use a random entry signal, so let’s use mathsrand to create an initial value for the random calculation. We use gettickcount as a parameter because this function returns the number of milliseconds since we have started the system, so this will create a base value that we use to create our random values. And this is done by using mathrand we just need two values, so we use percent two and if the random number that we have calculated equals zero we would assume that this is a buy signal, so we assign the word buy to our signal. Otherwise, if the random number equals one that would be a sell signal and now we assign the word sell to our signal and whenever we have a sell signal and positions total returns a value that is below ten we would use trade dot sell to sell ten micro lot for each of our currency pairs here. This expression here stands for the current currency pair, you could also use underscore symbol like we did here, basically, it stands for no value. Let’s repeat the whole thing for the buy trades, so if our signal equals buy and positions total is below ten we use trade dot buy to buy ten micro lot either on the current chart or for the two other charts that we have defined. And that’s about it. If you have no idea what all the code here does or if this was too fast for you maybe you want to watch one of the other videos in the basic video series first or maybe even the premium course on our website might be interesting for you, for now, please click on the compile button or press F7, I didn’t get any errors here and if this is the case you 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 click on the file, simple multiple currency order dot ex5, mark the option for the visual mode and start a test. Here is our expert advisor! We already have lots of orders and as you can see we have orders in different currency pairs in the strategy tester,

App downloaden & deine Podcasts überall hören