MQL5 Tutorial

MQL5 TUTORIAL BASICS – 20 HOW TO CODE A SIMPLE CROSSOVER EXPERT ADVISOR


Listen Later

In this video we are going to create a standalone version for a crossover Expert Advisor that is producing buy and sell signals directly on a chart and it is actually going to trade those signals whenever crossover happens, we will get a signal here, so let’s find out how to do that with MQL5. First you need to 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 (template)” from template, “Continue”, I will call this file: “SimpleCrossoverEA”, click on “Continue”, “Continue” and “Finish”. Now you can delete everything that is above the “OnTick” function and let’s also delete the two comment lines here. We start by including the file: “Trade.mqh” that will help us to create an instance of the class: “CTrade” that will be called: “trade”. Now we want to create two input values, if you mark “input” and press F1 you will see that it defines an external variable, basically it will create a user input. We need two; one small Moving Average (SmallMovingAverage ) and one big Moving Average value (BigMovingAverage). Now let’s calculate the Ask price and the Bid price that is done by using “SymbolInfoDouble” for the current symbol on the chart, we use “SYMBOL_ASK” or “SYMBOL_BID”. “NormalizeDouble” and “_Digits” will give us the number of digits behind the dot. Let’s create an empty value for the signal (signal) that needs to be calculated. We start by creating an array for several prices; one for the small Moving Average (SmallMovingAverageArray) and one for the big Moving Average (BigMovingAverageArray), first we need to define the properties of the small Moving Average (SmallMovingAverageDefinition) – I need to reduce the font size here – we use the function “iMA” for the current symbol on the chart and the currently selected period on that chart, this is the number of candles that we want to use to calculate the small Moving Average – in our case it’s 20 candles – we don’t use a shift value and with “MODE_SMA” and “PRICE_CLOSE” we define that we want to calculate a Simple Moving Average that is based on the close price. Now let’s repeat that for the big Moving Average (BigMovingAverageDefinition), this is basically the same line – that is 50 by default – afterwards we use “CopyBuffer” to fill our small Moving Average array (SmallMovingAverageArray) according to the definition (SmallMovingAverageDefinition) that we have created here for the buffer 0 (zero) – that’s the only line the Moving Average has – we start with candle 0 (zero), copy the value for 3 candles and store them in a small Moving Average array (SmallMovingAverageArray), afterwards you might have guessed it, we do the same thing for the big Moving Average according to the definition (BigMovingAverageDefinition) we have created here, we store the values in our big Moving Average array (BigMovingAverageArray) and if the big Moving Average is greater than the small Moving Average for candle 1 and it was below the small Moving Average for candle 2 that would be a crossover, now we want to buy so we assign the word: “buy” to our signal. Otherwise if the big Moving Average is below the small Moving Average for candle 1 and if the big Moving Average was above the small Moving Average for candle 2 that’s a crossover in the other direction, now we would assign the word: “sell” to our signal and if our signal equals sell and if we have no open positions we use “trade.Sell” to sell 10 micro lot. Otherwise if the signal equals buy and we have no open positions we would use “trade.Buy” to buy 10 micro lot. Finally we want to use the “Comment” function, this will output “The signal is now:” followed by the signal that we have calculated. That’s about it, if this is too fast for you you might consider to become a member of the premium course or repeat the other basic videos in this series. When you’re done you can click on the “Compile” button or press F7 on your keyboard,
...more
View all episodesView all episodes
Download on the App Store

MQL5 TutorialBy Raimund Bauer