In this video, we are going to find out how to trade pure price data. This is a so-called line chart and we are using the shifted price data to make, buy and sell decisions. 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 shifted price EA, click on Continue, continue and finish. Now you can delete everything above the ontick function. We also want to remove the Comment lines here. Let’s start with an include statement to include the file Trade.mqh.
It contains the class Ctrade. And we create an instance and call it trade. Afterwards, we need to calculate the Ask price and the Bid price, that can be done by using Symbolinfodouble for the current symbol on the chart.
We use either symbol underscore ask or symbol underscore bid and with normalize double and underscore digits, we make sure that we automatically calculate the right number of digits behind the dot. Let’s continue and create a string variable called signal, but we are not going to assign a value right now.
With MQLRates we create an array for the price information. Let’s use arraysetasseries to sort our price information array from the current candle downwards.
And now we can use CopyRates to fill the array with data for the current symbol on the chart and the currently selected period on that chart. We start with the current Candle zero and copy the price data for three candles to store it in the price information array. The next step is to create an array for the moving average values. We use the integrated function IMA for the current symbol on the chart and the currently selected period on the chart. We want to calculate the simple moving average for one candle and we want the shifted moving average to be drawn twenty candles into the future.
We use ModeSMA for simple moving average and the result should be calculated based on the close price.
With array set as series we also sort the moving average array from the current candle downwards. Now we can use copybuffer according to the definition that we have created here. We are going to copy data for buffer zero from current Candle zero. We copy price data for three candles. To calculate the current value for the moving average we just need to look into Candle zero of our moving average array and if the moving average value is below the close price for the current Candle zero, that would be a Buy signal and that’s when we assign the word buy to our signal.
Otherwise, if the moving average value is above the close price for Candle zero, that would be a Sell signal. And now we would assign the word sell to our signal. And when our signal equals sell and PositionsTotal is below one, we use trade.Sell to sell 10 Microlot. Otherwise, if the signal equals buy and we have no open positions, we use trade.Buy and buy 10 Microlot. Finally, I would like to add a comment statement that will output “The current signal is now”, followed by the calculated signal and 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. Or maybe even the Premium Course on our website might be interesting for you. If you need source codes for videos like this one, you can find a shop link on our website. But for now, please click on the compile button here.
Or press F7 on your keyboard. You shouldn’t get any errors. And if that is the case, you can click on a little button here or press F4 to go back to Metatrader. And in Metatrader, please pick an empty chart. Change the view to line chart here. Right, click into the chart and select template, save template. Please save it as tester.tpl because this is the view that we are going to see in the strategy tester, you can override the current one.
Now please click on View, Strategy tester or Control and R. Please pick the new file simple shifted price ea.ex5, mark the option for the visual mode here and start your test. Here we are. We already have the first sell position and as soon as the current price crosses the shifted price data here, this should switch to a buy signal, which happened right now. So in this little video, you have learned how to create an Expert Advisor that can use shifted price data for a line chart.
And you have coded it yourself with a few lines of MQL5 code.