MQL5 Tutorial

MQL5 Tutorial

By Raimund BauerBusinessTechnologyInvesting
Download on the App Store

All MQL5 Tutorial episodes

  • 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
  • MQL5 TUTORIAL BASICS – 31 HOW TO CLOSE ALL SELL POSITIONS
    In this video we want to to find out how to close all the sell positions at the same time, this little expert advisor is going to open ten sell positions and as soon as we have exactly ten positions it is going to close them, 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, 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 and let’s also remove the two comment lines. We start by using the include statement here to include the file trade dot mqh, it comes with mql5 and it makes it possible to create an instance of ctrade called trade in this case and we are going to use this instance to open our positions later on. First, we need to get the bid price that is done by using symbol info double for the current symbol on the chart and we are using symbol underscore bid, all in capital letters. I also like to use normalize double and underscore digits to automatically calculate the right number of digits behind the dot, that depends on the currency pair… If we have less than ten positions, so if positions total returns a value below ten we use trade dot sell to open a test position, the lot size will be ten micro lot and we continue to do that until positions total will deliver a return value of exactly ten, so now we have ten open positions and in that case we want to close all open sell positions with a function called: close all sell positions that doesn’t exist right now so we need to code it. The return type of the function will be void, the name is close all sell positions, we will use a for loop to go through all the open positions until there are no positions left. First, we need to use the function position get ticket to get the ticket number for the current position number, so i in this case is just the counter for this for loop and the ticket number is a longer number that will identify the position. We also need to find out the position direction so we use position get integer, the parameter here is position underscore type – all in capital letters – and now we can check if the position direction equals position type sell – all in capital letters –, if this is the case we have a sell position and now we use trade dot position close for the current position ticket that we have calculated here to close the position. Finally, we need to close the for loop and the function and that’s about it. If you don’t understand what all the code here does or if this was too fast for you, you might want to watch the other videos in this basic video series or maybe even the premium course is interesting, for now, please click on the compile button or press F7, you shouldn’t get any errors here and in that case you can click on the little button here or press F4 to go back to Metatrader. Since the last update there are some strange settings here, first I need to re-enable the standard toolbar, now we can click on view, strategy tester or press control and r, and here we now have a list of choices that has been implemented by Metaquotes, I want to select “Run single test of an Expert Advisor”, please pick the new file: simple sell position closer dot ex5, enable the visualization option here and start a test. Here we are! The expert advisor is working, it is able to open positions here and as soon as we have ten positions on the chart it will call the new function to close them all at the same time, so in this little video you have learned how to code that with mql5 and you have done it yourself with a few lines of code.
    6 min
  • MQL5 TUTORIAL BASICS – 30 HOW TO CLOSE ALL BUY POSITIONS
    This test expert advisor is going to open ten test positions and as soon as we have ten open positions we call a function called close all buy positions, so let’s find out how to do that with mql5. To get started please press F4 or click on the little symbol here, 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 buy position closer as we are going only to close buy positions. Now let’s click on continue, continue and finish, and now we can delete everything above the on tick function and the two comment lines here. First, we need to include the file trade dot mqh, this will help us to create an instance of the class ctrade that will be called trade and we are using it to open our test positions. Inside of the on tick function we start by getting the ask price, that is done with symbol info double for the current symbol and we use symbol underscore ask – all in capital letters – I also use normalize double and underscore digits to automatically calculate the number of digits behind the dot. First we want to know if the number of positions total is below ten, that is the case if we have less than open positions and if that is the case we use trade dot buy to buy ten micro lot for the ask price that we have calculated here and as soon as we have exactly ten positions we want to call a function called close all buy positions to close all the open buy positions at the same time. This function does not exist so far, so let’s create it, it has no return type and it will be called close all buy positions, we use a for loop to go through all the open positions and for each position we use position ticket to get the ticket number, we also want to calculate the position direction that is done by using position get integer, position underscore type – all in capital letters – and if the position direction equals position underscore type underscore buy, this position is a buy position and now we use trade dot position close for the current ticket number to close the current position. Finally, we need to close the for loop and the function, that’s about it, and if you don’t know what all the code here does maybe you want to start with the other videos in this basic series or maybe even the premium course is interesting for you. For now please click on the compile button here or press F7, that should work without any errors and if that is the case you can click on the 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 closer dot ex5, enable the visualization mode here and start a test. And here we! The expert advisor is opening test positions and as soon as we have ten open positions it calls our custom function to close all open buy positions. This video was an idea of a premium course member and if you have any ideas that should be explained in such a video here just send me an email because in this little video you have learned how to create an expert advisor to close all the open buy positions at once and you have coded it yourself with a few lines of mql5 code.
    5 min
  • MQL5 TUTORIAL BASICS – 29 HOW TO CALCULATE THE NUMBER OF SELL POSITIONS
    In this video we are going to find out how we can calculate the number of sell positions, the expert advisor currently counts seven positions here, that’s one, two, three, four, five, six, seven, so let’s find out how to do that. To get started please click on the little button here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, I will call this file simple sell position count, click on continue, continue and finish. Now you can delete everything between the on tick function and the on init function here. Let’s also remove everything above the on init function and the grey comment lines. We are going to start with an include statement to include the file trade dot mqh, this file contains trading functions so now we can create an instance of the class c trade with the name trade. Inside of the on init function, I’m going to call a custom function open test positions to open some test positions. Obviously, you wouldn’t do that on a real account but I need some positions so I have something to count. Inside of the on tick function we want to use the comment statement to output the text: number of sell positions and we call another custom function called: count sell positions that we need to create now. This new function should return an integer value and the first thing we need to do is to create an integer variable called: number of sell positions that has a value of zero and now we use a for loop to go through all the open positions. The number of positions is delivered by the function orders total? (positions total) and as long as we have more positions we will look at all the positions and check a few things. The first thing is that we want to get the position symbol for the current position, we also want to get the position type that is done by using the function position get integer and the parameter here is position underscore type – all in capital letters – and we are looking for the position type sell but before we do that we want to know if the current symbol on the chart equals the symbol of the currency pair for the position and if that is also true we check if the position direction equals position type sell. That is the case we have a sell position and that is when we increase the current number of sell positions by one and when we are done we want to close the for loop and finally we use the return statement to return the number of sell positions that we have calculated right now. That’s about it. I want to show you the function to open the test positions, I’m using a random generator to calculate a random number and this random number is used to buy and sell a random number of test positions that we can count. You wouldn’t do that on your real money account so when you’re done please click on the compile button here or press F7, that should work without any errors, if you have some errors or if you don’t understand what all the code does maybe you want to watch the other videos in this basic video series or maybe even the premium course is interesting for you but if you don’t have any errors here please 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, here we pick the new file simple position count dot ex5, please enable the visualization mode here and start a test. Here we are, the expert advisor says that we have nine sell positions, so let’s sort by type, these are buy positions, here are the sell positions and this is one, two, three, four, five, six, seven, eight, nine positions, the next one is a buy position so the EA is working. If you have any questions or suggestions that should become a video like this one just write me an email but for now you know how to create an expert advisor to calculate the current number of sell positions and you have coded it yourself with a few lines of mql5 code.
    6 min
  • MQL5 TUTORIAL BASICS – 28 HOW TO CALCULATE THE NUMBER OF OPEN BUY POSITIONS
    In this video, we are going to find out how many buy positions we have. This is a question that came from one of my members of the premium course. We have several buy and sell positions here, let’s order them by type and we have 1, 2, 3, 4, 5, 6, 7, 8 buy positions, that’s what the text is saying here so let’s find out how to calculate that with MQL5. To do that please click on the little button 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 (template)” from template, “Continue”, I will call this file: “SimpleBuyPositionCount”, click on “Continue”, “Continue” and “Finish”. Now you can delete everything between the “OnTick” function and the “OnInit” function, we also remove the comment lines here, that’s everything that’s grey. We start by importing a “Trade.mqh” file – that’s a library – it includes several trading functions and it enables us to create an instance of the class “CTrade” that will be called: “trade”, inside of the “OnInit” function I will call a custom function that is called: open test positions (OpenTestPositions). You wouldn’t do that on a real account but I need to open some test positions to have something to count. Inside of the “OnTick” function, we are going to do one thing and that is to use the “Comment” statement to output the number of positions that we will get from calling a custom function called buy positions and we are going to code that now. Actually, let’s make that a little bit more user-friendly and create the custom function called count buy positions (CountBuyPositions), this one will return a whole number so it’s an integer value. We will start by creating a local variable called number of buy positions (NumberOfBuyPositions), the value is 0 (zero) right now because we want to calculate that number of buy positions and to do that we use a “for” loop, inside of the “for” loop we will use the function “PositionsTotal” and as long as our counter value is above 0 (zero) we will go through all the open positions and here is what we are going to do; first we want to create a string for the currency pair and assign the value for “PositionGetSymbol” of the current counter value that will identify the currency pair, afterwards we want to get the position type that is done by using “PositionGetInteger” and the parameter we use is “POSITION_TYPE” – all in capital letters – now we check if the current symbol on the chart equals the currency pair of our position and one more thing we want to know is if the position direction equals position type buy (POSITION_TYPE_BUY) – also in capital letters. And if that is the case we want to take the current value for number of buy positions and increase it by one. We need to end the “for” loop here and now we want to return the number of buy positions to the main function that is done by using the “return” statement and we will return the value for the calculated number of buy positions (NumberOfBuyPositions) here. One more bracket is missing to close the whole function, that’s about it. I will show you my function to open test positions (OpenTestPositions), don’t use it on a real account, I use a random generator to calculate a random number so we will have up to 10 positions and afterwards I will open buy and sell trades just for testing purposes, don’t do that on a real account. So let’s compile the code by clicking on the little button here or you can also press F7. I have an error here, let’s make that a “void” function because I don’t need to check the return value, let’s recompile the code and this time we didn’t get any errors so now we can click on a little button here or press F4 to go back to Metatrader. In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, we pick the “SimpleBuyPositionCount.ex5” file, please mark the visualization option here and start a test. Here we are our! Our Expert Advisor tells us that we have 8 buy positions,
    7 min
  • MQL5 TUTORIAL BASICS – 27 HOW TO CODE A SIMPLE SELL TRAILING STOP
    In this video we are going to create a trailing stop for sell trades, you can see when the price falls that the trailing stop is adjusted automatically and now we want to find out how to do that in mql5. 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 file, expert advisor from template, continue, I will call this file: simple sell trailing stop, click on continue, continue and finish. Now you can delete everything above the on tick function and the two comment lines here. First, we include the file trade dot mqh that one will give us simplified trading functions, and it includes a class called c trade, we will create an instance of that class called trade that is what we are going to use later on to open a new trade. To do that we first need to find out the bid price that is done by using symbol info double, for the current symbol on the chart and we use symbol underscore bid – all in capital letters. I also like to use normalize double and underscore digits because that will automatically calculate the number of digits behind the dot depending on the currency pair that could be 3 or 5 digits. So let’s first find out if we have any open positions, and if positions total equals zero that would mean that we don’t have any positions. So on a demo account, we would now use trade dot sell, to sell 10 micro lot because we need a position otherwise, we cannot find out if the trailing stop works and afterwards we want to call the function check the trailing stop and pass the bid price. Well, this function does not exist, so we now need to create it. I use void because I don’t need to return any values, I just want to check and adjust the trailing stop. Our function will take the bid price as a parameter and I would like to have the stop loss 150 points above the bid price. So let’s go through all the positions that is done with a for loop, positions total will give us the number of open positions and we want to count down until we have no open positions left. First we need to get the position symbol for the current position because we only want to continue if the current symbol on the chart and the position symbol are equal and we also want to check if the position type is order type sell – all in capital letters – because if you mark that and press F1 you will see that this kind of order type is for a market sell order and that’s what we need. If this is the case we want to get the position ticket – that’s kind of a license plate that will identify one particular position – and we get it by using position get integer. Please use position underscore ticket – once again all in capital letters – afterwards we want to calculate the current stop loss that is done by using position get double and position underscore sl – also in capital letters – and if the current stop loss is above the stop loss that we want to have we use trade dot position modify for the current position ticket to adjust the current stop loss by 10 points. Finally, we need to close the loops and that’s about it. Once again if this is too fast for you or if you don’t understand what the code does you maybe want to watch the other videos in this basic series or maybe even you want to take part in the premium course. But for now please click on the compile button, you should not get any errors here and if this is the case please click on the little symbol here or press F4 to go back to Metatrader. Inside of Metatrader I take an empty chart, right-click, select templates, save template and I save it as tester dot tpl, replace the current one because this is what we want to see in the backtest, we don’t need any Indicators. When you are done please click on view, strategy tester or press ctrl and r, here we want to pick the new file simple sell trailing stop ex5, please mark the option for the visualization here and start your test.
    6 min
  • MQL5 TUTORIAL BASICS – 26 HOW TO CODE A SIMPLE BUY TRAILING STOP
    In this video we want to create a trailing stop loss like this one, the stop loss is trailing whenever the price is going up so let’s find out how to do that. 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/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleBuyTrailingStop”, click on “Continue”, “Continue” and “Finish”. Now you can delete everything above the “OnTick” function and please also remove the two comment lines here. We start by including “Trade.mqh”, this file will give us simplified trading functions, and we start by creating an instance of “CTrade” that will be called: “trade” and we are using it to open a test function in our case. To do that we need to get the Ask price, that is done by using “SymbolInfoDouble”, for the current symbol (_Symbol), we use “SYMBOL_ASK” – all in capital letters – and with “NormalizeDouble” and “_Digits” we will get either 3 or 5 digits behind the dot depending on the currency pair we have on the chart. So let’s find out if we have no open positions, that would be true when “PositionsTotal” equals 0 (zero) and in that case, we are going to open a buy position by using “trade.Buy”, we will buy 10 micro lot. This is just a test position for this case because we need a position to find out if the trailing stop really works, you wouldn’t do that on a real account without any entry signal and finally, we want to check the trailing stop for our position. In this case we are using the Ask price as a parameter and now we need to create the check trailing stop function (CheckTrailingStop), this one will take the Ask price and now we use it to calculate the stop loss, we want it to be 150 points below the current Ask price and as it is possible to have more than one position on the chart we use “PositionsTotal” and a “for” loop to go through all the open positions. First we want to find out if the position belongs to the right symbol, so we use “PositionGetSymbol” for the current position number to get the symbol and if the symbol of the current currency pair (_Symbol) equals the symbol of the position (symbol), we have a position that needs to be modified. And now we get the position ticket (PositionTicket) by using “PositionGetInteger”. A position ticket will identify our position; it’s like a license plate. We also want to calculate the current stop loss for the position that is done by using “PositionGetDouble”, “POSITION_SL” will give us the stop loss, if you mark that and press F1 you will see that it’s also possible to get the position volume (POSITION_VOLUME) or the take profit value (POSITION_TP) and other position properties but we just want to check if the current stop loss is below the desired stop loss and if this is the case we use “trade.PositionModify” for the position ticket (PositionTicket) that we have calculated here and now we rise the current stop loss by 10 points. Finally we want to close the “if” loop, the “for” loop and the function and if you don’t understand what all the code does or if this was too fast for you maybe you want to watch the other videos in this basic series or maybe you want to even become a customer for the premium course. Otherwise please click on the “Compile” button now, you shouldn’t get any errors here and in that case you can click on a little button here or press F4 to go back to Metatrader. In Metatrader we use an empty chart without any Indicators, we click on: “Templates/ Save Template” and save it as: “tester.tpl” because this is the template that is going to be used in the backtest, so let’s replace the current one, click on: “View/ Strategy Tester” or press CTRL and R, please pick the new file: “SimpleBuyTrailingStop.ex5” and start a test. Here we are! Our trailing stop is actually working and in this little video you have learned how to code a buy trailing stop and you have done it yours...
    6 min
  • MQL5 TUTORIAL BASICS – 25 HOW TO CODE A SHIFTED SMA EXPERT ADVISOR
    In this video, we are going to create an Expert Advisor for this Indicator here; actually, it’s a Simple Moving Average and a shifted Simple Moving Average. The Indicator produces buy and sell signals that can be automatically traded in Metatrader so let’s find out how to do that with MQL5. First please click on the little button here or press F4 to open the Metaeditor and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleShiftedSMAEA”, click on “Continue”, “Continue” and “Finish”. Now you can remove everything above the “OnTick” function and the two comment lines here. We start with an include statement to include the file: “Trade.mqh”, this one will give us some simple trade functions. First we create an instance of “CTrade” that will be called: “trade” and we are going to use it later on to open buy and sell positions and to be able to do that we first need to find out the Ask and the Bid price that is done with “SymbolInfoDouble”, “_Symbol”, we use “SYMBOL_ASK” to calculate the Ask price and “SYMBOL_BID” to calculate the Bid price. I also use “NormalizeDouble” and “_Digits” because that will automatically calculate the number of digits behind the dot. Now let’s create a string variable for the signal. So far it has no value; we are going to calculate that later on. First, we create a Moving Average array (myMovingAverageArray) and we also want to create a shifted Moving Average array (myShiftedAverageArray). To calculate the value for the Moving Average array (myMovingAverageArray) we use the integrated “iMA” function. The first parameter is for the current symbol on the chart (_Symbol), The second one will give us the current period (_Period), Parameter 3 is 20 for 20 candles, Parameter 4 is 0 (zero) because we don’t use a shift value for this Moving Average, The next parameter is “MODE_SMA” – that stands for Simple Moving Average – And the last parameter is “PRICE_CLOSE” – in capital letters – will give us the calculation based on the close price. Now let’s repeat the whole thing, this time for the shifted Moving Average. Everything is similar except for the shift value here. This value will move the current Simple Moving Average – that is the red one – 20 candles into the future, that’s the green line. Now we use “ArraySetAsSeries” to sort both Moving Averages from the current candle downwards and with “CopyBuffer” we fill the Moving Average array (myMovingAverageArray) and the shifted Moving Average array (myShiftedAverageArray) according to the Moving Average definition (movingAverageDefinition) that we have created here or the shifted Average definition (shiftedAverageDefinition) that we have created here and fill both arrays for buffer 0 (zero) from the current candle 0 (zero) for 3 candles. Now we are able to get the current value for the Moving Average array (myMovingAverageArray) by looking into candle 0 (zero) of our Moving Average array (myMovingAverageArray) and we get the shifted Moving Average value by looking into candle 0 (zero) in our shifted Moving Average array (myShiftedAverageArray). And if the Moving Average value (myMovingAverageValue) is bigger than the shifted Moving Average value (myShiftedAverageValue) that would be a buy signal so we assign the word: “buy” to our signal. In the other case if the Moving Average value (myMovingAverageValue) is below the shifted Moving Average value (myShiftedAverageValue) we would consider that to be a sell signal and now we assign the word: “sell” to our signal. And whenever the signal equals sell and “PositionsTotal” is below 1 – or in other words, we don’t have any open positions – we use “trade.Sell” and sell 10 micro lot. Otherwise, if the signal equals buy and we have no open position we use “trade.Buy” and buy 10 micro lot. So that’s almost it. We also want to use the “Comment” statement to output the text: “The current signal is now:” followed by the calculated signal.
    8 min
  • MQL5 TUTORIAL BASICS – 24 HOW TO CODE THE AVERAGE TRUE RANGE OSCILLATOR
    In this video, we are going to calculate the Average True Range Oscillator here, we are actually going to use it as an entry signal that is able to create buy and sell signals and open trades, so let’s find out how to do that with MQL5. We start by clicking on the little button here or you can also press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleAverageTrueRangeEA”, click on “Continue”, “Continue” and “Finish”. Now we can delete everything that is above the “OnTick” function and let’s also delete the two comment lines here. We start with an include statement to import the “Trade.mqh” file; this is used to create an instance of the class “CTrade” that will be called: “trade”. We use it to open new positions later on. Inside of the “OnTick” function we first need to create a variable for the signal that is also called “signal”, it has an empty value because we want to calculate the value. As we are going to buy and sell we need to calculate the Ask price and the Bid price, we do it by using “SymbolInfoDouble” for the current symbol on the chart, “SYMBOL_ASK” – in capital letters – will give us the Ask price and “SYMBOL_BID” will give us the Bid price. By using “NormalizeDouble” and “_Digits” it will automatically calculate if we have 3 digits behind the dot or 5 digits behind the dot, which depends on the currency pair. Let’s create a price array (PriceArray) for the data and the definition for the Average True Range can be done with the included function “iATR” that comes with MQL5. It takes 3 simple parameters; The first one is for the current symbol on the chart (_Symbol), The second one is for the period that is used on that chart (_Period), The third one here is 14, so let’s mark the function and press F1 and we see that the last parameter is used for the value of the averaging period for the Indicator calculation, so what does that mean? Well, let’s open any chart, click on: “Insert/ Indicators/Oscillators/ Average True Range”, and you will also see the 14 here, it’s just a period of 14 candles that is used to calculate the value, so let’s click on “OK”, here is the Oscillator we need, so let’s right click in the chart, select “Templates/ Save Template” and save it as “tester.tpl” because this is the template that is going to be used in the backtest. You can override the current one. Now let’s continue. We sort the prices from the current candle downwards for our price array (PriceArray) by using “ArraySetAsSeries”. With “CopyBuffer” we fill our price array (PriceArray) with data according to the Average True Range definition (AverageTrueRangeDefinition) that we have created here. We do it for buffer 0 (zero) – that’s this Indicator line – and we start with candle 0 (zero) and copy price data for 3 candles. To calculate the current value for the candle we just have to look at the value of candle 0 (zero) in our price array (PriceArray). I use “NormalizeDouble” and comma 5 to get the output like the Indicator is usually presented, this one also has 5 digits behind the dot. Usually an Oscillator is used as a filter and not as an entry signal but I want to compare an old value and the current one so let’s create a static variable for an old value (OldValue), it is used to keep the value available inside of a function even when we leave the function and when we run the Expert Advisor for the first time the old value would be 0 (zero) and in that case we assign the value for the Average True Range (AverageTrueRangeValue) to the old one. When the current Average True Range (AverageTrueRangeValue) is greater than the old value (OldValue) we consider that to be a buy signal so we assign the word: “buy” to our signal, otherwise if the current value (AverageTrueRangeValue) is below the old value (OldValue) we would consider that to be a sell signal so we assign “sell” to our signal.
    8 min
  • MQL5 TUTORIAL BASICS – 23 HOW TO CODE A SIMPLE PARABOLIC SAR EXPERT ADVISOR
    In this video, we are going to create this interesting Expert Advisor for the Parabolic SAR Indicator. You see the little green points here, this is the signal, whenever the points are above the price that’s a sell signal and when they are below the price that’s a buy signal until the direction changes. So let’s find out how to automate the Parabolic SAR with 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 (template)” from template, “Continue”, I will call this file: “SimpleParabolicSAREA”, click on “Continue”, “Continue” and “Finish”. Now you can remove everything above the “OnTick” function and the two comment lines here. We start with an include statement to include the file: “Trade.mqh”, this one is included in MQL5 and it comes with trading functions that we are going to use to open our positions, so let’s create an instance of “CTrade” that will be called: “trade” and we are going to use it later. Inside of the “OnTick” function we start by calculating the Ask price and the Bid price that is done by using “SymbolInfoDouble” for the current symbol on the chart, with “SYMBOL_ASK” – all in capital letters – we will get the Ask price and with “SYMBOL_BID” we would get the Bid price. “NormalizeDouble” and “_Digits” is used to calculate the number of digits behind the dot because “_Digits” stores the number of digits after the decimal point and depending on the currency pair that can be 3 digits or 5 digits. Let’s create a variable for the signal we will also call it “signal” and we don’t assign a value, that is going to be calculated later on. First we need to create a price array for the prices on the chart that is done with “MqlRates”, we use “ArraySetAsSeries” for our price array (PriceArray) to sort it from the current candle downwards and now we need to fill the array (PriceArray) with price data hat is done by using “CopyRates” for the current symbol on the chart and the currently selected period. By the way “_Symbol” and this symbol (Symbol()) is the same so we also could use “_Symbol” and the same is true for “_Period”, we start with the current candle 0 (zero) and we want to get the price data for 3 candles and store it in our price array (PriceArray). Let’s create another array (mySARArray) for the Parabolic SAR Indicator, let’s define (SARDefinition) what we are going to do, we use the “iSAR” function that comes with MQL5 for the current symbol on the chart and the currently selected period. So let’s find out about these two values here. Pick any chart, click on: “Insert/ Indicators/ Trend/ Parabolic SAR” and here we see we have a step value of 0.02 and a max value of 0.2, so let’s click on “OK”, now you see the Indicator and we want to right-click on the chart, select “Templates/ Save Template” and save it as “tester.tpl”, yes I want to exchange the current one because if I don’t do that I might not see the Indicator in the backtest, but back to the code here are the two values; the step value and the max value, lets also sort this array (mySARArray) by using “ArraySetAsSeries” and this time we use “CopyBuffer” to fill our price array (mySARArray) according to the SAR definition (SARDefinition) that we have created here for the 0 (zero) buffer – that’s the only buffer this Indicator has – from candle 0 (zero) – that’s the current candle – for 3 candles and store the values in the SAR array (mySARArray). Now we can get the last SAR value (LastSARValue) by looking into candle 1 from the SAR array (mySARArray). I use “NormalizeDouble” and 5 digits because this is what you see when you point your mouse over one of those Indicator dots, we have 5 digits behind the dot. So let’s find out if we have a buy signal and that would be if the last SAR value (LastSARValue) is below the law of candle 1 in our price array (PriceArray) or in other words if the dot is below the candle we ass...
    8 min