MQL5 TUTORIAL BASICS – 19 HOW TO TRADE AN SMA

video
play-sharp-fill
  1. Introduction to Trading Small Accounts and Managing Risk (00:00 – 00:10)
    • Introduction to the concept of trading with small accounts and effective risk management strategies.
  2. Accessing Accounts in MetaTrader Navigator (00:10 – 00:24)
    • Demonstrating how to access the ‘Accounts’ section in the MetaTrader Navigator and explaining the account types.
  3. Opening a New Trading Account with a Broker (00:24 – 00:39)
    • Instructions on opening a new trading account, choosing between Metatrader 4 or 5, and selecting the ‘Pro-Cent’ account type.
  4. Setting Leverage and Passwords for the Account (00:39 – 00:56)
    • Choosing leverage options and setting up passwords for personal and investor access.
  5. Understanding the Cent Account System (00:56 – 01:22)
    • Explaining how the cent account works, with a focus on trading in US Dollar cents.
  6. Making Deposits and Internal Transfers (01:22 – 01:36)
    • Discussing deposit options and how to perform internal transfers between accounts.
  7. Real Money Trading with a Cent Account (01:36 – 02:13)
    • Highlighting the advantage of trading with real money in a cent account, using a smaller initial investment.
  8. Transitioning from Demo to Real Cent Account (02:13 – 02:30)
    • Advising on moving from a demo account to a real cent account and the scalability of trading with cents.
  9. Managing Trades and Margin in a Cent Account (02:30 – 02:53)
    • Demonstrating trade management and margin considerations in a cent account.
  10. Finding Brokers Offering Cent Accounts (02:53 – 03:05)
  • Tips on finding brokers that offer cent accounts suitable for your country.
  1. Real Profits and Fees with Cent Accounts (03:05 – 03:19)
  • Emphasizing that trading with cent accounts involves real profits and fees.
  1. Using Statistics Services and Account Performance (03:19 – 03:31)
  • Encouraging the use of statistics services like FX Blue to track account performance.
  1. Conclusion: Setting Up a Cent Account for Small Budget Trading (03:31 – 03:47)
  • Concluding the tutorial with a summary of how to set up a cent account for trading with a small budget.

In this video we are going to create a simple Expert Advisor that is going to trade this Moving Average. It’s a Moving Average for 20 candles, 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: “SimpleMovingAverageEA”, click on “Continue”, “Continue” and “Finish”.

Now you can remove everything above the “OnTick” function and the two command lines here.

We start by including the file: “Trade.mqh” that comes with MQL5 and it provides easy trade functions, so let’s create an instance of “CTrade” that will be called: “trade” and we are going to use it to open our positions later on.

As we want to trade in both directions we need to get the Ask price and the Bid price, that is done by using the function “SymbolInfoDouble” for the current symbol, for the Ask price we use “SYMBOL_ASK” – all in capital letters – and for the Bid price we use “SYMBOL_BID” also in capital letters, I like to use “NormalizeDouble” and “_Digits” to automatically calculate if we have a currency pair with 3 digits or 5 digits.

Now let’s continue, we create an array for the price data that will be called: “PriceInfo“ and we use the function: “MqlRates” to create it, because “MqlRates” stores the information about prices, volumes and spread.  

Now we want to fill the array (PriceInfo) with price data that is done by using “CopyRates” for the current symbol on the chart and the currently used period on that chart, actually this and this statement are equal, we could also use “_Symbol” and “_Period”, we start with the current candle 0 (zero) and we want to copy the price data for 3 candles and store it in our price info (PriceInfo) array.

Let’s create a string for the signal that will also be called: “signal” and we don’t assign any value here because we are going to calculate that later on. 

Now we need another array, this time for the Moving Average it will be called: “myMovingAverageArray”, so let’s define the properties for the Moving Average (movingAverageDefinition). We use the included “iMA” function of MQL5 for the current symbol on the chart and the currently selected period on that chart, I want to calculate the Moving Average for 20 candles in the past, this is a shift value that could be used to move the Moving Average to the right or to the left, I don’t want to use it so I select 0 (zero), we want to calculate a Simple Moving Average so we use “MODE_SMA” – all in capital letters – and “PRICE_CLOSE” also in capital letters is used to calculate the value based on the close price.

So let’s use “CopyBuffer” to fill the array (myMovingAveargeArray) with data for the buffer 0 (zero) – that’s the red line here, this Indicator only has one buffer – we start with candle 0 (zero) and we want to copy the price data for 3 candles and store it in the my Moving Average array (myMovingAverageArray) according to the Moving Average definition (movingAverageDefinition) that we have created here.

Let’s define the buy conditions, we want to check if the close price for candle 1 is bigger than the value for the Moving Average for candle 1 and if the close prize for candle 2 was below the Moving Average value for candle 2 that would be a cross over and now we want to buy so we assign the word: “buy” to our signal. 

In the other case if the close price for candle 1 is now below the Moving Average for candle 1 and the close price for candle 2 was above the Moving Average for candle 2 before that would be a sell signal and now we assign the word: “sell” to our signal, and if our signal equals sell and “PositionsTotal” is below 1 we want to open a sell trade for 10 micro lot, otherwise if the signal equals buy and we have no positions that would mean that we want to open a buy trade for 10 micro lot. If this is too fast for you I would suggest to go to the MQL5 basic videos that have been created before.

One thing is still missing, we need a chart output so we use the “Comment” function to output the text: “The signal is now:” and the calculated signal. 

That’s it. When you are done you can click on the “Compile” button here you should not get any errors and in that case you can click on a little button here or press F4 to go back to Metatrader and in Metatrader you want to click on: “View/ Strategy Tester” or press CTRL and R, please pick the new file: “SimpleMovingAverageEA.ex5”, mark the visualization option here and start your test. 

Now you should see the red line here that is the Moving Average, we have signals on our chart; buy and sell and here we actually have opened our first position, let’s zoom in, here it is and now you have learned how to create an Expert Advisor to trade a Simple Moving Average for 20 candles and you have coded it yourself with a few lines of MQL5 code.

Download “CODE - SIMPLE MOVING AVERAGE EA”

SimpleMovingAverageEA.mq5 – Downloaded 75 times – 1.85 KB