MQL4TUTORIAL.COM

MQL4TUTORIAL.COM

By MQL4TUTORIAL.COMBusinessInvesting
Download on the App Store

MQL4TUTORIAL.COM episodes

  • MQL4 TUTORIAL BASICS – 28 HOW TO COUNT OPEN BUY POSITIONS
    In this video, we are going to create an expert advisor that is able to count the current number of open buy positions. This is a question that I have gotten from a customer. Right here the expert advisor says that we have six buy positions, so let’s click on results, we have buy and sell trades here, let’s order them by type and we have one, two, three, four, five, six buy positions, so let’s find out how to create a simple expert advisor that can count our positions. 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 from template, continue, I will call this file simple buy position counter, click on continue, continue and finish, now you can delete everything between the on tick function and the on init function, we also want to remove everything above the on init function and all the grey comment lines here. Inside of the on init function, I’m going to call a function called open test positions, you wouldn’t do that on real account but I need to open some positions first before I can count them. Inside of the on tick function, I would like to use the comment statement to create an output: number of buy positions and the number of open buy positions will be calculated in a function that we are going to create right now. The new function should return an integer value, the name is count buy positions and we start by creating a local variable, called number of buy positions, right now the value is zero and we are going to use a for loop. Inside of the for loop we use orders total, this will deliver the number of all the orders, we use a counter value to go through all the orders until there is no order left and for each order we want to get the currency pair for that particular order that is done by using the function order symbol, we use order select to select an open trade, the one for the current counter value, we want to use select by position and we use mode trades, if you mark that and press F1 you will learn that this is for opened and pending orders while mode history would be used when we want to calculate something about the closed and cancelled orders, now we use the function order symbol to find out the currency pair for the order and after we have done that we can check if the current symbol on the chart equals the currency pair of the order and we also want to know if the order type equals op buy, let’s look that up. Op buy is a buy operation, op sell would be a sell operation, the other four here are for pending orders but we just wanted to count buy positions so op buy is the one we need and if all those conditions are true we now want to increase the number of buy positions by one, here we close the for loop and finally we use the return statement to return the number of five positions to our main module. That’s about it. I will show you my open test positions function, I’m using a random generator to generate a random number of positions, I use order send to open buy and sell trades, so that we have something to count, you wouldn’t do that on a real account so when you’re done please click on the compare button here or press F7 on your keyboard, we have no errors here so now we can click the little button here or press F4 to go back to Met trader. In Metatrader please click on view, strategy tester or press ctrl and r, here we pick the new file, simple buy positions counter dot ex4, please enable the visual mode here and start a test. Here we are! We have one buy and one sell position and the number of buy positions is one, let’s look into the results tab and this is correct, now let’s stop the test and restart it, this time the expert advisor says that we have two buy positions, this is the first and this is the second position so our little expert adviser is working, if you have any similar questions that could become a video like this one just send me an email but for now you kn...
    7 min
  • MQL4 TUTORIAL BASICS – 27 HOW TO CODE A SIMPLE SELL TRAILING STOP
    In this video, we want to create a trailing stop for sell trades, you can see here the red line is the railing stop and when the price falls the trailing stop will follow, so let’s find out how to code something like this in MQL4. To do that please click on the little button here or press F4 in your Metatrader, 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: “SimpleSellTrailingStop”, click on “Continue”, “Continue” and “Finish”. Now we can delete everything that is above the “OnTick” function and let’s also remove the two comment lines here. Inside of the “OnTick” function we first want to check if we have no open orders that is done by checking if the function “OrdersTotal” delivers a value below 1, and if this is the case we want to open a demo sell trade, so let’s actually call that “sellticket” because if we don’t have open positions we will not find out if the trailing stop works. Obviously, you wouldn’t do that on a real account without any entry signal, so let’s use “OrderSend” to open a demo trade (OP_SELL) for 10 micro lot. Now we want to go through all the open orders, we use “OrdersTotal” to give us the current number of orders and we use a “for” loop to count down from the number of orders we have. Inside of the “for” loop we use “OrderSelect”, the number of the current order, we want to select by position (SELECT_BY_POS) and we use “MODE_TRADES” as we want to find out everything about the open orders. In our case we want to check if the order belongs to the current chart, that is done by comparing the return value for the function “OrderSymbol” with the return value of the function “Symbol()” and I would also like to find out if the current order type equals “OP_SELL” because this is what we need to find the sell orders. In the next step we need to find out if the order stop loss (OrderStopLoss) equals 0 (zero) or if the order stop loss (OrderStopLoss) is too high and is bigger than the current Bid price plus 150 points, and if this is the case we use “OrderModify” for the current ticket number (OrderTicket) that has the order open price (OrderOpenPrice), we now want to set the current stop loss to 150 points above the Bid price, the order take profit value (OrderTakeProfit) will not be changed, we don’t need any expiration date (0) and we don’t need a color (CLR_NONE). Please don’t forget to close all the braces here and that’s about it. If this is too complicated for you right now you maybe want to watch the other videos in this basic series or if you are interested you could also become a member of the premium course. For now please click on the “Compile” button here or press F7, we get one warning that we should check the “OrderModify” return value but for this simple example we don’t need to do that so if you don’t have any errors here you can now click on the little button here or press F4 to go back to Metatrader. In Metatrader we just pick a clean chart, right-click into the chart, select “Template/ Save Template” and we save it as “tester.tpl” as this is the template that is going to be used in the Strategy Tester. You can override the current one and when you are done you can click on “View/ Strategy Tester” or press CTRL and R, please pick the new file: “SimpleSellTrailingStop.ex4”, mark the option for the visual mode here and start a test. And here we are, this is our sell trade, and we should see the trailing stop coming down. There it is! And in this little video, you have learned how to create an automated trailing stop for sell trades and you have coded it yourself with a few lines of MQL4 code. MQL4 Tutorial searches:mql4 trailing stop code
    6 min
  • MQL4 TUTORIAL BASICS – 26 HOW TO CODE A SIMPLE BUY TRAILING STOP
    In this video, we are going to create a trailing stop that will rise whenever the price rises, so let’s find out how to calculate something like this 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”, I will call this file: “SimpleBuyTrailingStop”, click on “Continue”, “Continue” and “Finish”. Now we can delete everything above the “OnTick” function and the two comment lines here. First, we want to check if “OrdersTotal” is below 1, if that is the case we have no open orders and for this example, we are going to open a demo buy trade because we need something for the trailing stop so we use “OrderSend” to open a 10 micro lot trade here. You wouldn’t do that on a real account without any entry signal and because it is possible to have more than one position we now want to count all the orders and we use “OrdersTotal” and a “for” loop to go through each and every order that we have. First we want to check if we can select an order for the current counter in the loop, so we use “OrderSelect”, the counter number, we want to select by position (SELECT_BY_POS), we use “MODE_TRADES”, that’s for currently open positions and for orders, if you would like to see closed orders and positions you could also use “MODE_HISTORY”. Now we need to find out if the symbol for the order (OrderSymbol) equals the current symbol (Symbol()) on the chart, afterwards we use “OrderType” to find out if it’s a buy position (OP_BUY) and if all these here is true we check if the current order stop loss (OrderStopLoss) is below 150 points and if this is true we use “OrderModify” for the current order (OrderTicket) with the order open price (OrderOpenPrice) and we set the stop loss to 150 points below the Ask price, we leave the order take profit value (OrderTakeProfit) as it is, we don’t need an expiration date (0) and we don’t need to use any color (CLR_NONE). That’s about it. Please don’t forget to close the braces here. Here we close the “for” loop. Well, that might be a little bit complicated and if it was too fast for you or if you don’t understand what all the code here does maybe you want to take part in the premium course or maybe you want to watch the other videos in this basic video series. For now please click on the “Compile” button, we have one warning here but no errors and if that is the case please click on the little button here or press F4 to go back to Metatrader. In Metatrader we use an empty chart, right click, select “Template/ Save Template” and save it as “tester.tpl”. You can replace the current one and we do it to have a clean chart because we don’t need any Indicators for this Trailing Stop Expert Advisor. If you don’t see the Strategy Tester here, please click on “View/ Strategy Tester” or CTRL and R, pick the new file: “SimpleBuyTrailingStop.ex4”, mark the visual mode here and start a test. And here is our first position, this is the trailing stop and as the price rises the trailing stop will follow and in this little video you have learned how to automate a trailing stop for a buy position and you have coded it yourself with a few lines of MQL4 code. MQL4 Tutorial searches:trailing stop code mql4
    5 min
  • MQL4 TUTORIAL BASICS – 25 SIMPLE SHIFTED MOVING AVERAGE EXPERT ADVISOR
    In this video, we are going to create an Indicator that is acting like this one. It’s actually the same Simple Moving Average that is used to create buy and sell signals. We have a real value and a shifted value, so let’s find out how to do that with MQL4. To get started please click on a little icon here or press F4 on your keyboard, now you should see the Metaeditor and here you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleShiftedSMAEA”, click on “Continue”, “Continue” and “Finish”. Now we want to delete everything above the “OnTick” function and let’s remove the two comment lines here. We start by creating a string for the signal but we don’t assign any value because we are going to find out that value later on. First we want to create a Simple Moving Average; let’s call this one: “MyMovingAverage1”, we use the “iMA” function for the current symbol (_Symbol), we use “PERIOD_M1” or if you would prefer to automatically calculate the period on the chart you could also use “_Period”, the third parameter is for 20 candles, parameter 4 can be used for one of the two shift values, in this example we leave it as 0 (zero). In the next parameter we say that we want to use a Simple Moving Average so we use “MODE_SMA” – all in capital letters – and the result should be calculated based on the close price (PRICE_CLOSE) in this case for candle 1. Now we do the same for my Moving Average 2 (MyMovingAverage2) but this time we use a shift value of 20. So that’s basically everything, the name of the Moving Average is different and the shift value is 20 instead of 0 (zero). And if my Moving Average 1 (myMovingAverage1) is bigger than my Moving Average 2 (myMovingAverage2) I would consider that to be a buy signal so I assign the word: “buy” to the signal. Otherwise if my Moving Average 1 (myMovingAverage1) is below my Moving Average 2 (myMovingAverage2) I would consider that to be a sell signal and now I assign “sell” to our signal. And if the signal equals buy and “OrdersTotal” equals 0 (zero) – that’s when we have no open orders and a buy signal – we use the “OrderSend” commend to buy 10 micro lot. Otherwise, if the signal equals sell and if “OrdersTotal” equals 0 (zero) we want to use “OrderSend” to sell 10 micro lot. Finally we use “Comment” to output the text: “The current signal is:” followed by the calculated signal. If this was too fast for you or if you don’t know what all the code here does maybe you want to watch the other basic videos in this video series or maybe even the premium course is interesting for you, for now, we click on “Compile” and we don’t get any errors here and in that case we can click on the little button here or press F4 to go back to Metatrader. And in Metatrader, we now click on: “Insert/ Indicators/ Trend/Moving Average”, here we pick a period of 20 candles, a shift value of 0 (zero), the MA method should be “Simple” for Simple Moving Average, apply to close price, I will pick the colour red for this one and click on “OK”. Here is our first Moving Average, now let’s repeat that, click on: “Insert/ Indicators/ Trend/Moving Average”, and this time we will change the shift value to 20 and the colour to green and when I click on “OK” I will see a second Moving Average. Now let’s right click into the chart, select “Template/ Save Template” and save it as “tester.tpl”, you can replace the old one because “tester.tpl” is what is going to be used in the next strategy test. So let’s actually do that by clicking on: “View/ Strategy Tester”, please click on: “SimpleShiftedSMAEA.ex4”, mark the visual mode here and start your test. And here we are! We have a red and a green line, our current signal is sell so we have opened a sell trade and now we have a crossover and the current signal is buy so our little Expert Advisor works, and you have learned how to create a standalone version of a Simple Moving Average and a shifted Simple Moving Average and y...
    7 min
  • MQL4 TUTORIAL BASICS – 24 HOW TO CODE THE AVERAGE TRUE RANGE OSCILLATOR
    In this video we are going to create an Expert Advisor that is going to trade this Indicator here, this is an Oscillator, it’s called the Average True Range. Usually, it’s used as a filter but we are going to automate it with MQL4 and it is going to execute buy and sell trades. 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”, I will call this file: “SimpleAverageTrueRangeEA”, click on “Continue”, “Continue” and “Finish”. We start by removing everything above the “OnTick” function and let’s also delete the two comment lines here. The first thing we need is a string for the signal, by now it does not contain any value because we are going to calculate that later on and now we use the ”iATR” function that comes with MQL4, it will calculate an Average True Range value for us, we pass the “_Symbol” parameter to make it calculate the current symbol on the chart and “_Period” will give us the current period on the chart. So what is this value here; 14? Let’s find out! When you click on: “Insert/ Indicators/Oscillators/ Average True Range”, you will see that the 14 is the period that is used to calculate the value or in other words we are going to calculate it based on 14 candles, so let’s click on “OK”, here is the Oscillator and now you want to right click, select “Template/ Save Template” and save it as “tester.tpl” because this is the template that is going to be used in the backtest. I am going to replace the current version, this last value here is a shift value, we haven’t used it before but we are going to use it now because this shift function allows us to calculate the value for a bar or a candle a few periods ago, so what does that mean? Well, actually we are using the value 5 here to calculate an old Average True Range value for candle number 5. When you put your mouse over the Oscillator here you will see that we have different candle values for each candle on the chart and we will now invent an entry that is going to use the values for candle 5 and for the current one. Let’s say if the Average True Range goes up and the current Average True Range value (AverageTrueRangeValue) is bigger than the old Average True Range value (OldAverageTrueRangeValue) we consider that to be a buy signal so we assign the word: “buy” to our signal. Otherwise, if the current Average True Range value (AverageTrueRangeValue) is below the old one (OldAverageTrueRangeValue), we consider that to be a sell signal so we assign the word: “sell” to our signal. We are going to buy whenever the signal equals buy and when we have no open orders that’s when we use “OrderSend” to buy 10 micro lot. Otherwise, if the signal equals sell and we have no open orders we use “OrderSend” and sell 10 micro lot. Finally let’s create an output by using the “Comment” function that will output the text: “The signal is:” followed by the calculated signal. Well if you don’t understand what all the code here does or if this was too fast for you maybe you want to watch the other videos in this basic video series or maybe even the premium course is interesting for you, for now, let’s click on the “Compile” button… I don’t know how I do that but I have all the time this special character in my code?! Now let’s recompile the code this time we have no errors and that’s when we click on the little button here or press F4 to go back to Metatrader, please don’t forget to save the template as “tester.tpl”, afterwards you can click on: “View/ Strategy Tester” or press CTRL and R, here is the Strategy Tester, so please mark the visual mode here and start a test. …here we are! The Expert Advisor is actually trading and here we actually made the first profit, usually an Oscillator is only used as a filter but it’s also possible to generate buy and sell signals and in this little video you have learned how to automate...
    6 min
  • MQL4 TUTORIAL BASICS – 23 HOW TO CODE A PARABOLIC SAR EXPERT ADVISOR
    In this video we are going to automate this Indicator, it’s the Parabolic SAR Indicator. You see the little dots here, they are creating buy and sell signals depending if the dots are above or below the candles, so let’s find out how to automate that with MQL4. To do that please click on a 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: “SimpleParabolicSAR”, click on “Continue”, “Continue” and “Finish”. Now you can delete everything above the “OnTick” function and the two comment lines here. We start by creating a string variable for the signal that will also be called: “signal”, we don’t assign any value because we need to calculate that. This is done by using the “iSAR” function that comes with MQL4. The first parameter is the current symbol, the second parameter is for the current period on the chart, so let’s find out about those values, pick any chart, click on: “Insert/ Indicators/ Trend/ Parabolic SAR” and you will see a step value of 0.02 and a max value of 0.2, now we click on “OK” and here are the little dots and if you want to see the Indicator values in the backtest, you now need to right-click on the chart, select “Templates/ Save Template” and save it as “tester.tpl” because this is the template that is going to be used in the Strategy Tester. We can replace the current one, so now we know this is the step value, this is the max value and the last parameter here would be a shift value, we don’t want to use any shift values so this is 0 (zero) and if the SAR value (SARValue) is below the Bid price we have a buy signal so we assign the word: “buy” to our signal. Otherwise if the SAR value (SARValue) is bigger than the Ask price that would be a sell signal and that’s when we assign the word: “sell” to our signal and whenever the signal equals buy and “OrdersTotal” equals 0 (zero) that would mean that we have a buy signal and no open positions and that’s when we use “OrderSend” to buy 10 micro lot. Otherwise, if the signal equalled sell and we also have no open positions that’s when we use “OrderSend” to sell 10 micro lot. Finally, we create a chart output using the “Comment” statement to output the text: “The signal is:” and the calculated signal. That’s all, if you don’t know what all the code does or if this was too fast for you maybe you want to watch the other basic videos in this video series or maybe you are even interested in the premium course. Otherwise when you’re done you can click on the “Compile” button here or press F7, now you shouldn’t get any errors and in that case, you can click on the little button here or you can press F4 to go back to Metatrader. In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R. Please pick the new file: “SimpleParabolicSAR.ex4”, mark the visual mode option here and start the test. …and this is our little Expert Advisor at work, we already have bought, so it works and now you know how to create an automated Expert Advisor for the Parabolic SAR Indicator and you have coded it yourself with a few lines of MQL4 code. MQL4 Tutorial searches:mt4 parabolic sar eamql cannot load parabolic indicator
    5 min
  • MQL4 TUTORIAL BASICS – 21 HOW TO CODE A MACD EXPERT ADVISOR
    In this video, we are going to code an Expert Advisor that is able to calculate the MACD Oscillator, it does not only generate signals it actually trades them, so let’s find out how to do that with MQL4. To get started please click on the little symbol here or press F4, 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: “SimpleMacDEA”, click on “Continue”, “Continue” and “Finish”. Now you can remove everything above the “OnTick” function and let’s also delete the two comment lines here. We start by creating a signal that will be a string variable, right now we don’t assign any value here because we need to calculate that later on and to do that we use the included “iMACD” function that comes with MQL4, it uses a few parameters so let’s go through the parameters one by one. The first one is for the current symbol; “NULL” is a placeholder, it will actually calculate the current symbol, you could also use “_Symbol“ and for the time frame we could use “_Period”, I would like to calculate the values based on the close price, “MODE_MAIN” is used to calculate the base Indicator line, the last value here is a shift value that would be used to calculate the value for a different candle but in our case we use 0 (zero) because we want to calculate the value for the current candle. So what’s left are these 3 values here; let’s open a new chart window, click on: “Insert/ Indicators/ Oscillators/ MACD”, now you should see the values: fast EMA 12, slow EMA 26, MACD SMA 9, apply to close, that’s exactly what we are using here, so let’s click on “OK” and the MACD should appear in the lower part of your chart. Let’s right-click select “Templates/ Save Template” and save it as “tester.tpl” because this is the template that is going to be used in the backtest but before we can do that we need to finish our Expert Advisor here. If the current value for the MACD is greater than 0 (zero) – zero is this line here – so if the signal line is above the zero line we would expect it to return to the zero line, that would be a sell signal so we assign the word: “sell” to our signal. Otherwise if the MACD is below the zero line that would be a buy signal so we assign the word: “buy” to our signal, and if the signal equals buy and “OrdersTotal” equals zero that would mean we have no open orders we use the “OrderSend” command to buy 10 micro lot. Otherwise, if the signal equals sell and we have no open orders we use “OrderSend” to sell 10 micro lot. Finally, we use “Comment” to output the text: “The current signal is:” and the signal that we have calculated on the chart. Let’s click on the “Compile” button here or press F7, you shouldn’t get any errors here but if it was too fast for you you might want to watch the other videos in this basic series first or maybe even the premium course is interesting for you. If everything is okay you can click on the 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: “SimpleMacDEA.ex4”, enable the visual mode here and start a test. Here we are! Our Expert Advisor is actually working we also have open positions and in this little video you have learned how to automate the MACD using MQL4 to open buy and sell trades on your chart and you have coded it yourself with a few lines of MQL4 code. MQL4 Tutorial searches:how to code macdmacd mql4mql 4 video tutorialmt4 cod
    6 min
  • MQL4 TUTORIAL BASICS – 20 HOW TO CODE A SIMPLE CROSSOVER EXPERT ADVISOR
    In this video we are going to create a crossover Expert Advisor, we have two Moving Averages, one for 50 candles and one for 20 candles, and when we see a crossover of these two Moving Averages it will create a signal that should happen in a few seconds and then we will see, yes, here is the first trade that has been automatically opened, so let’s find out how to do that with MQL4. To start please click on the 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/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleCrossoverEA”, click on “Continue”, “Continue” and “Finish”. Now you can remove everything above the “OnTick” function and let’s also delete the two comment lines here. First we need to declare two variables; one is for the small Moving Average (SmallSMA) that will be for 20 candles, the other one is for the big Moving Average (BigSMA), that’s for 50 candles. You see this modifier: “input” here, it creates an external variable, an external variable can be changed without recompilation. Inside of the “OnTick” function we start by creating a variable for the signal (signal), that’s a string variable and we don’t assign any value right now because we need to calculate it later on, but first please let me calculate a few Moving Average values. The first one will be called small Moving Average one (SmallMovingAverage1), we use the “iMA” function that comes with MQL4 for the current symbol on the chart and the currently selected period on that chart. The value for the candles will be this one, the small SMA (SmallSMA) for 20 candles, we don’t need a shift value, we use “MODE_SMA” to calculate a Simple Moving Average and the calculation should be done based on the close price (PRICE_CLOSE) for candle 1. Now we do the same thing for the big Moving Average (BigSMA) for candle 1, this line is almost identical, we have a different variable name here and a different value because big SMA (BigSMA) equals 50 candles, and to calculate if we have a crossover we also need to do that for candle 2. So basically we copy both lines, change the last variable here for candle 2 and please do not forget to also change the name of the variable here. So now we have four Moving Average values and if the big Moving Average (BigMoingAverage1) is greater than the small Moving Average for candle 1 (SmallMovingAverage1) and the big Moving Average for candle two (BigMoingAverage2) was below the small Moving Average for candle two (SmallMovingAverage2) that would be a sell signal so now we assign the word: “sell” to our signal. Otherwise if the big Moving Average (BigMoingAverage1) now is smaller than the small Moving Average for candle 1 (SmallMovingAverage1) and the big Moving Average for candle two (BigMoingAverage2) was bigger than the small Moving Average for candle two (SmallMovingAverage2) that would be a crossover in the other direction and now we have a buy signal, so we assign the word: “buy” to our signal, and if our signal equals buy and “OrdersTotal” is 0 (zero) that means that we have no open orders we will use the “OrderSend” function to buy 10 micro lot. Otherwise if the signal equals sell and we also have no open position or open order we use the “OrderSend” command to sell 10 micro lot. Finally we want to create a chart output, so we use the “Comment” function to output the words: “The current signal is:” and the signal we have calculated. If this was too fast for you maybe you want to watch the other MQL4 starter videos or maybe even take the premium course. When you are done you can click on the “Compile” button or press F7 on your keyboard, that should work without any errors and in that case you can click on the little button here or press F4 to go back to Metatrader. In Metatrader we use a little trick, please click on: “Insert/ Indicators/ Trend/ Moving Average”, select 20 for 20 candles in the period,
    8 min
  • MQL4 TUTORIAL BASICS – 19 HOW TO TRADE A SIMPLE MOVING AVERAGE
    In this video we will create this simple Expert Advisor that is actually going to trade. It crates buy or sell signals based on the Simple Moving Average that you see here. When you hover the red line you will see that’s Moving average for 20 candles, so let’s find out how to create such a simple system. To do that first 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”, I will call this file: “SimpleMovingAverageEA”, click on “Continue”, “Continue” and “Finish”. Let’s remove everything that is above the “OnTick” function and delete the two command lines here. We start by creating a string variable for the signal that will be called: “signal” and it doesn’t contain any value so far because we are going to assign that later on. Now we want to calculate the Moving Average for 20 candles. MQL4 comes with a function that is called: “iMA”, we pass a few parameters here; The first one will get the current symbol on the chart, The second one will find out what the time period on that chart is, The third parameter here is for the number of candles that we are going to use. This value here and the last one are shift values; the first one could be used to move the drawing of the red line here to the right or to the left, The last one here is used to tell Metatrader for which candle we want the value because all those candles here have an on value for the Simple Moving Average and all the values are connected to draw the line. This parameter here (MODE_SMA) is to define that we want to use the Simple Moving Average or SMA and it should be calculated based on the close price (PRICE_CLOSE). And if the Moving Average is below the current price we want to assign the value: “buy” to our signal. Otherwise if the Moving Average is above the current price we want to assign the value: “sell” to our signal. Now we need to check if the signal equals buy and the other condition we want to check if is “OrdersTotal” equals 0 (zero) so we have no open order. You see this operator here (&&)? It is used to check if both conditions are true, so only if the left condition is true and the right condition is true we use “OrderSend” to buy 10 micro lot. Otherwise if the signal equals sell and if “OrdersTotal” is 0 (zero) we use “OrderSend” to sell 10 micro lot. If you have problems understanding this line please go back to the former basic videos. For this simple version we will just need to add one more thing, we use the “Comment” line to output the sentence: “The current signal is:” followed by the signal. That’s about it. Let’s click on the “Compile” button, we don’t have any errors so now we can go back to Metatrader. Now if we start our Expert Advisor we don’t see the Simple Moving Average here, to change that we use a little trick; we just click on: “Insert/ Indicators/ Trend/ Moving Average”, here we pick the 20 candles, the shift value is 0 (zero), we want the Simple Moving Average and that should be calculated based on the close price, so lets click on “OK”. Here is our Indicator, now we right click into the chart, select: “Templates/ Save Template” and we save it as: “tester.tpl” because this is the template that is going to be used in our Strategy Test. So let’s click on: “View/ Strategy Tester” or press CTRL and R, pick the new file: “SimpleMovingAverageEA.ex4”, mark the visual mode here and start a test. And here we are! This is the Simple Moving Average that we have calculated, we get buy or sell signals directly on our chart, this is our first sell position here so the little EA is actually trading and in this little video you have learned how to create a working Expert Advisor that actually trades buy and sell signals for a Simple Moving Average and you have coded it yourself with a few lines of MQL4 code.
    6 min
  • MQL4 TUTORIAL BASICS – 18 HOW TO TRADE SMALL ACCOUNTS WITHOUT BEING STOPPED OUT
    In this video we want to talk about a way to trade small accounts without being stopped out, this one is a real account, you see it’s colored yellow, demo accounts are marked in a green color usually and this one says it has a balance of 29,160 Dollars and 15 cents, and it’s a so-called “ProCent” account, you can see that here. This is the website from RoboForex, one of the brokers I use, when I decide to open a new trading account I can pick either Metatrader 4 or Metatrader 5, in this case I will use Metatrader 4 and when I select an account type you will see that there is something that is called “MT 4 Pro-Cent”, so let’s pick that one. I can choose if I want to use US Dollar or Euros. For the leverage I can go up to 1 to 2,000, usually I use 1 to 500, let’s insert a password and I also want to provide an investor password because this is what we are going to use later on in our statistics. I don’t need an affiliate code so now I could click on “Confirm” and the next step would be to make a deposit. Here are several ways to transfer money, I have already done that, this is my account, it says: “US Dollar cents”, leverage is 1 to 500 and the balance here is 29,160.15 US Dollar cents and that’s actually what we see here. So in a cent account you can do something like this, I have made a deposit of 300 Dollars for an MQL5 account and that would become 30,000 US Dollar cents in your cent account. This broker also makes it possible to do internal transfers, I did that for two of my accounts, split at the equity to open two new accounts, make sure that you read the “terms and conditions” when you open a new account because if you take any bonuses that can become a problem when you do a withdrawal later on, so this is my statistics on FX Blue, here is the current balance: 29,160.15 US Dollars, the account was opened on April the 9th 2019, this is how the account performed so far and now you have all these statistics here, the gross profit is 989 Dollars today, so with a cent account you will pay real fees, you will make real profits. I found a few websites like this one that lists brokers who provide cent accounts, this is the one that I have used in this example and you might be able to find a solution for your country because in this little video you have learned how to trade small accounts without being stopped out by using a cent account and now you know how to open your own cent account and trade dozens of positions on all these charts here and once everything works as desired it is no problem to scale up your account size.
    5 min

About MQL4TUTORIAL.COM

From the publisher's feed

MQL4 Tutorial for Metatrader 4 or MT4 about automated trading, trading robots, MQL 4 and MQL programming