In this video we are going to talk about a standalone Expert Advisor that is going to actually trade. Whenever the price is below this yellow line here – this is a Moving Average – we have already made a sell trade and in this video we are going to find out how to do a standalone Expert Advisor that actually trades 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 (template)” from template, “Continue” (Next), I will call this version: “SimpleStandaloneSMA”, click on “Continue” (Next), “Continue” (Next) and “Finish”.
Now you can remove everything above the “OnTick” function and let’s also delete the two comment lines here.
Okay. Let’s start by creating a string variable for the signal, that variable will be also called: “signal” and we don’t assign a value here because we are going to calculate it later on.
To calculate it we use the built-in “iMA” function that comes with MQL 4 and it will calculate the Moving Average for the current symbol on the chart and the currently selected period on that chart – for example I’m on the 1-minute chart right now – we want to calculate the Moving Average for 20 candles, I don’t want to use a shift value, I could move the Moving Average drawing to the right or to the left but we are going to use a zero (0) shift value here.
“MODE_SMA” will calculate the Simple Moving Average, “PRICE_CLOSE” we’ll use the close price for the calculation and the last value here is candle 0 (zero) – that’s the current candle – I could also calculate the Moving Average based on one of the other candles here but in this simple version we are going to use the current candle.
And if the Moving Average is below the current price – so if my Moving Average (MyMovingAverage) is below the close price for candle 0 (zero)…
…and please notice that close for candle 0 (zero) actually doesn’t exist because candle 0 (zero) is always the candle that is just in development and close price for candle 0 (zero) gives you the current price here and when that price is bigger than the Moving Average value we want to set the signal variable to buy so we assign the word: “buy” to our signal.
And guess what? If the Moving Average is above the current price, so if my Moving Average (MyMovingAverage) is bigger than the current price we want to set the signal variable to sell so we assign the word: ”sell” to our signal.
Now let’s actually buy something!
When the signal equals buy and “OrdersTotal” equals 0 (zero) – and that is true when we have no open orders – we use the “OrderSend” command to buy 10 micro lot and the take profit value is 150 points above the Ask price.
If you are interested in the “OrderSend” command just mark it here and press F1 and you will see how it works, but let’s continue with the selling part!
To sell 10 micro lot we check if the signal equals “sell” and if we have no open orders and in that case we use the “OrderSend” statement to sell 10 micro lot and this time the take profit value will be 150 points below the Bid price. Please notice that we use “OP_BUY” to buy something and “OP_SELL” to sell something.
That’s almost it.
In the last step we use the “Comment” function to output the words: “The current signal is:” and the calculated signal on our chart.
Okay, so far so good, please click on the “Compile” button or press F7 on your keyboard, you should see no errors and we have 2 warnings because we could use the return value for “OrderSend” but for this simple example everything is okay, we don’t use that so now please click here or press F4 to go back to Metatrader.
In Metatrader we click on “View/ Strategy Tester” or press CTRL and R, here in the Strategy Tester please select the “SimpleStandaloneSMA.ex4” file, mark the option for the visual mode and start your test.
…and here is our little Expert Advisor at work, we have already made a sell trade because the price is below the Moving Average line here and now you know how to create a standalone Expert Advisor to automatically trade a Moving Average and you have coded it yourself with a few lines of MQL4 code.
Download “MQL4 TUTORIAL - SIMPLE STANDALONE SMA EXPERT ADVISOR”SimpleStandaloneSMA.txt – Downloaded 3583 times – 996.00 B