[ Ссылка ]
With MQL4 for Metatrader4 we have created an Expert Advisor to output a buy signal or sell signal for a simple moving average directly on your forex chart. This EA can be coded in 5 minutes or less...
What you see here on the chart is a Simple Moving Average. It's the little red line here and you also see the text: the current signal is buy or sell – that is calculated by an automated Expert Advisor – and whenever the red line is above the current price that would be a sell signal and when the red line is below the current price that is a buy signal. Like here.
Now how can we create an Expert Advisor that is able to output the calculated buy or sell signal directly on your chart.
To do that please click the little button here or press F4 on your keyboard and 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 one: Simple SMA (SimpleSMA). Continue, continue and finish. Now you can delete everything above the “OnTick” function and the two comment lines here.
The first thing we need to do is to create a string variable for the signal – that will also be called signal – and we don't assign a value here because we are going to calculate that one later.
We also want to use a Simple Moving Average for 20 minutes, this is a 1-minute chart and you could click on “Insert /Indicators /Trend /Moving Average” and here you will see the so-called parameters.
This Moving Average is based on 20 candles the shift value is set to 0, the Moving Average method is set to simple for Simple Moving Average and the calculated prices are based on the close price, and this is how it looks in MQL4: double MyMovingAverage = (equals) – and here we are going to use the “iMA” function that is built into MQ4, and it needs a few parameters: the first parameter is the symbol you have selected on your chart, in our case it's the Canadian Dollar against the Swiss Franc, this is the selected period on our chart because we're using a 1-minute chart. We use PERIOD_M1.
Here we have 20 candles for 20 minutes, we use a shift value of zero and because we want to use a Simple Moving Average we select MODE_SMA, that's for the MA method: Simple here, and we use PRICE_CLOSE, that's for the “Apply to: Close” price. We don't want to move our indicator to the left or to the right so the last parameter for the shift is also zero.
Now we need to find out if the SMA is below the bid price.
If you click on one of these currency pairs you will see two price levels, the red one is the ask price – this is what you need to pay if you buy something, and the blue one is the bid price, this is what you get when you sell it.
So, if the Moving Average is below the bid price we want to set the signal to buy. In MQL4 this is: signal = (equals)“buy” and in the other case if the Simple Moving Average is above the ask price – so, if the Moving Average is greater than the ask price that would be a signal to sell. So now, signal= (equals)”sell”.
After the calculation we want to see the output of the signal on our chart, that is done by using the “Comment” function, and we want to output the text: “The current signal is: ” followed by the signal that we have calculated here.
Okay, that's about it.
When you are ready you can click on the compile button here or press the F7 key on your keyboard, and that should work without any errors or warnings here.
When you're ready you can click on the little button here or press F4 to go back to Metatrader. And here in Metatrader 4 we use a little trick: please click on “Insert /Indicators /Trend /Moving Average”, set it to period 20, shift 0, MA method simple, and apply to close then click on OK.
Now the red line is drawn on your chart and you want to right-click on the chart, select “Template /Save template” and save it as: tester.tpl because that is the template that the Metatrader 4 is going to use for your back test.
To bring up the strategy tester please click on “View/ Strategy Tester” or press CTRL and R and in the Strategy Tester panel here you want to select the “SimpleSMA.ex4” – I need to lower the spread here. Please make sure to enable the visual mode and start your test.
And this is our Expert Advisor at work, it says buy or sell based on the position of the indicator below or above the price, and we have an output on our chart and you have coded it yourself with a few lines of MQL4 code.
MQL4 TUTORIAL - SIMPLE SMA EXPERT ADVISOR
Теги
mql4mql4 tutorialmql4 lessonslearn mql4mql4 coursetrading automationmetatrader4programmingexpert advisorforextradingeaindicatorbilal haiderforex boatjimdandy1958mql4 programmingmetatrader automationBuy StopSell StopStop LossTake profitmql4 bookmqll4 referencemql4 forummql4 videojim dandy mql4 coursesmt4 indicatorcounted