MQL4TUTORIAL.COM

MQL4TUTORIAL.COM

By MQL4TUTORIAL.COMBusinessInvesting
Download on the App Store

MQL4TUTORIAL.COM episodes

  • MQL4 TUTORIAL BASICS – 17 HOW TO PLACE A SELL STOP
    In this video we are going to set a sell stop, you can’t see it in the chart but when we zoom in you will see that we have a pending order; a sell stop that is far below the current price, so let’s find out how to do that with MQL4. First you need to 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: “SimpleSellStop”, click on: “Continue“, “Continue“ and “Finish”, now you can delete everything above the “OnTick” function and lets also remove the two comment lines here. Inside of the “OnTick” function we want to check if we have no open orders and positions, so we use the function; “OrdersTotal” that will give us the number market and pending orders and if the return value is 0 (zero) we know that we have neither open orders nor positions and in that case we want to use “OrderSend” to create a sell ticket, we need to pass a few parameters, so let’s go through the parameters one by one: The first one will deliver the currency pair that is used on our current chart, you can use “Symbol()” actually there are two versions; the first one is “_Symbol”, the second one is Symbol with two round braces (Symbol()) and that will return the currency pair – in my case it’s the Australian against the Canadian Dollar. The next parameter is the action that we want to perform, in our case we want to place a sell stop (OP_SELLSTOP). Parameter 3 stands for the lot size, we want to use 10 micro lot and this defines the entry price, so when the price moves to 100 points below the current Bid price we want to enter our sell trade. This is the slippage – that’s the tolerance – don’t set this value to 0 (zero) because you will get a lot of requites from your broker. This 0 (zero) here the next parameter is the stop loss, I didn’t use one here but I did use a take profit value, so we enter when the price is 100 points below the current Bid price and we would take our profit when the price moves to 200 points below the Bid price and because this is a sell trade it would give us 100 points profit because the difference between the take profit and the entry price is 100 points. This constant: “NULL” all in capital letters – is used because we don’t have a comment. If we want to set a comment we could do that by passing a string value here. The next one is called: “magic number” that’s kind of an or ID number like a license plate, you would use it if you want to identify your Expert Advisor among other Expert Advisors on the same chart, not needed in this case. This parameter could be used to set an expiration date, if you set it to 0 (zero) the order will stay in the market until it is filled or until you delete it and the last parameter here “Green” is the color for the arrow that is drawn on the chart as soon as we open the sell trade and that’s about it. So let’s click on the “Compile” button or press F7, you shouldn’t get any errors or warnings here and if that is the case you can click on the little symbol here or press a F4 to go back to Metatrader. And in Metatrader you want to click on: “View/ Strategy Tester” or press CTRL and R, here we pick the new file: “SimpleSellStop.ex4”, please mark the visual mode here and start a test. …here we are, the Expert Advisor is running, in the results you can see that we have a sell stop for 10 micro lot and when we zoom into the chart we can see that we have automatically placed a sell stop far below the current price and in this little video you have learned how to do that with MQL4 and you have coded it yourself with a few lines of MQL4 code.
    6 min
  • MQL4 TUTORIAL BASICS – 16 HOW TO PLACE A SELL LIMIT
    In this video we are going to set a pending order, a so called sell limit. That’s a pending order that is placed above the current price level; you can see it when we zoom into the chart and as soon as the price crosses the green line here that would open a sell trade, so let’s find out how to do that with MQL4. First we need to 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: “SimpleSellLimit”, click on: “Continue“, “Continue“ and “Finish”, now we can delete everything above the “OnTick” function and the two comment lines here. Before we open our pending order we want to check if we have no other open positions or orders, when we use “OrdersTotal” we can get the number of open orders and positions and if that equals 0 (zero) that would mean that no other open positions or orders exist. An order is something you place into the market that can become a position and that’s also the case for our sell limit. To place our order we will use “OrderSend”, that will require some parameters, so let’s go through the parameters one by one: The first one is for the currency pair on the chart, I have used symbol and two round braces here (Symbol()) but according to the “Reference” we could also use the “_Symbol” function, both functions will return the current currency pair on the chart – in my case it’s the Australian against the Canadian Dollar. Parameter 2 is for what we actually want to do; we want to open a sell limit so we use “OP_SELLLIMIT” – all in capital letters. Parameter 3 is for the lot size, in our case its 10 micro lot, so we pass 0.10. Parameter 4 is the price level that we are going to use to place our sell ticket, the current price is the Bid price and when the price rises 200 points above that level we want to open our sell position. In the next parameter we define a slippage – I use a value of 3 here – if the price moves more than 3 pips away from the current price I don’t want to place the order. If you use a value that is too low you might get a lot of requites from your broker and if you set the value too high you might get a price level that you don’t want. The next parameter is used to place a stop loss, I don’t use one here so I set it to 0 (zero), if you want to use one you need to use something that is above Bid plus 200 points. Next is the value for the take profit, we want to sell at 200 points above the Bid price and when the price drops for 100 points we will take the profit. In the next parameter we use “NULL” with all capital letters, otherwise we could pass a string value here, and this is followed by the so-called magic number or ID number, that’s kind of a license plate that you could use if you want to identify an Expert Advisor on the chart. I rarely use more than one Expert Advisor, so I don’t use it. This is followed by the expiration date, I have set the value to 0 (zero) because I don’t want my sell ticket to expire and the last parameter is used to set the color of the arrow that is going to be drawn when you open the sell ticket and that’s about it. When you’re done you can click on the “Compile” button here or press F7 on your keyboard, this worked without any errors and any warnings and if that is the case for you, you can click on a little button here or press a F4 to go back to Metatrader. And in Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, please pick the new file: “SimpleSellLimit.ex4”, mark the visual mode here and start your test. …here we are, we don’t see anything but when we zoom into the chart you will see the sell limit has been placed. In this little video you have learned how to automatically do that and you have coded it yourself with a few lines of MQL4 code.
    6 min
  • MQL4 TUTORIAL BASICS – 15 HOW TO SET A BUY STOP
    In this video we are going to set a buy stop. A buy stop is a pending order, you don’t see it right now but if you zoom into the chart you will see that we have placed an order far above the current Ask price and in this video we are going to find out how to do that with MQL4. To get started 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: “SimpleBuyStop”, click on: “Continue“, “Continue“ and “Finish”, and now we can remove everything above the “OnTick” function and let’s also delete the two comment lines here. To get started we will check a simple condition; we want to know if we have any open positions or orders, so we use “OrdersTotal” and if that equals 0 (zero) we want to use the “OrderSend” command to send a pending buy order and to do that for our buy stop we need to pass several parameters here. Let’s go through the parameters one by one: The first one is used to find out the current currency pair on the chart, in my case it’s the Australian against the Canadian Dollar. The second parameter is what we are actually going to do, we are using “OP_BUYSTOP” here, so let’s mark that and press F1 and we learn that the “OP_BUYSTOP” parameter will pass a value of 4 and that is equal to a pending order for a buy stop. The next parameter here is the lot size, in our case its 10 micro lot, we want to buy if the price rises 200 points above the current Ask price. The next parameter is for the slippage, if you set this one to 0 (zero) you will get a lot of re-quotes because before your orders executed the price might move. You also don’t want to set it too high because you don’t want to buy if the price moved too far away from the current level. 3 is a good value here. The next parameter here is set to 0 (zero) because I don’t use a stop-loss. You could use one, in that case you could set it to 100 points above the Ask price or something like that, but I have defined a take profit value; I would like to make 200 points of profit because if our order is executed 200 points above the current Ask price and we close the position 400 points above the current Ask price that would give us 200 points profit. This parameter is set to “NULL” – all capital letters – we could use it as a comment, I don’t need one. The next one here would be the magic number, that’s kind of a license plate if you use several EA’s on one chart that might be helpful, I also don’t need that one. The next parameter could be used to set an expiration date but we don’t need an expiration date in this simple example and the last one is a color; it is green and that is just the color of the arrow that is drawn on the chart and that’s about it. If you are ready you can click on the “Compile” button here or press F7 on your keyboard, you should get no errors here and if that is the case you can trust the little button here or press F4 on your keyboard to go back to Metatrader. And in Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, please pick the new file: “SimpleBuyStop.ex4”, enable the visual mode here and start a test. …and here we go. We don’t see anything but when we zoom into the chart we see that our buy stop is here, so our little Expert Advisor works and in this little video you have learned how to place a buy stop above the current Ask price and you have coded it yourself with a few lines of MQL4 code.
    6 min
  • MQL4 TUTORIAL BASICS – 14 HOW TO SET A BUY LIMIT
    In this video we want to find out of how to set a buy limit. A buy limit is not a market order but a pending order. We don’t see it right now but when we zoom in to the chart we see that we have a buy limit that is far below the current price and in this video we are going to find out how to do that with MQL4. To start 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: “SimpleBuyLimit”, click on “Continue”, “Continue” and “Finish“. Now we can remove everything that is above the “OnTick” function, and I will also delete the two comment lines here. In this very simple version we are going just to check if we have no open positions or orders. That is done by using “OrdersTotal”, because “OrdersTotal” will return the number of market and pending orders and if that returns the value of 0 (zero) we have no open positions and no open orders and I that case we want to use “OrderSend” to set our buy limit. To do that we need to pass a few parameters, we will use round braces here, if you mark one you will see the other one that’s something that might be very helpful for beginners because when you just start out you will have often errors because the number of braces do not mach and this is how you can check this out. Okay, our first parameter is to find out which symbol is currently used on the chart, we use “Symbol” and two round braces (Symbol()) but if we mark “Symbol” and press F1 we will see there is also underscore symbol (_Symbol) and actually “_Symbol” does the same. The Reference says that we could also use the “Symbol” function and that will deliver the current currency pair that we are using on our chart. In parameter 2 we say that we want to use a buy limit, that is done by using “OP_BUYLIMIT” – all in capital letters –, now we use buy limit and you could also set a buy stop, we use it to open a position of 10 micro lot, that’s parameter 3 and now we define the price level for our buy limit that should be the Ask price minus 20 points. The “_Point” function will deliver the point value for the current symbol. The next parameter is for the slippage, 3 is a good value here, if you set it too low you will get a lot’s of requites and if you set it too high you might get a price that you don’t want. Let’s continue with the next parameter, that’s for the stop loss, I could define it in a way like here but in our case we have no stop loss but we will have a value for the take profit and that should be 10 points above the current Ask price, so actually from minus 20 to plus 10 we would have the goal of 30 points for profit. The next parameter could be used to set a comment, I use “NULL”, all in capital letters because I don’t use it here, you could also use a magic number – here called ID number –, the comment and the magic number could be used to identify an Expert Advisor if you are going to use more than one on your chart, I never do that. The next parameter is for the expiration date, we could use it to set the time and date when the buy limit will no longer be valid. For this simple example I don’t have an expiration date so I set it to 0 (zero). And the last parameter is “green” it’s a color and we use it to actually set the color of the arrow that is drawn when we open a position. So far so good, we need to end our statement with a semicolon – actually all these here could be used in a single line but I guess it’s more readable in this way – and if you are done you can click on the “Compile” button here or press F7. That should work without any errors and if this is the case you can click on the little button here or press F4 to go back to Metatrader. And in Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, please pick the new file: “SimpleBuyLimit.ex4”, mark the visual mode here and start a test.
    7 min
  • MQL4 TUTORIAL BASICS – 13 HOW TO OPEN A SIMPLE SELL TRADE
    In this video we are going to find out how to create an automated sell order with MQL4 whenever the first bearish candle appears on the chart, so let’s find out how to do that. To start 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: “SimpleSellOrder”, click on “ Continue”, “ Continue” and “Finish”. We can delete everything that is above the “OnTick” function and let’s also remove the two comment lines. Inside of the “OnTick” function we want to start by checking if we have no other open orders, that can be done by using “OrdersTotal” because “OrdersTotal” returns the number of market and pending orders in MQL4 and if this equals 0 (zero) that means that we have no open orders. Afterwards we check if we have a bearish candle, that is the case if the close price for candle 1 is below the open price for candle 1, that would be the case for every white candle on the chart and if those two conditions are true we are actually going to sell, that is done by using the “OrderSend” command and this one takes a lot of parameters, so let’s find out what those parameters do. The first one is used for the currency pair; “_Symbol” will automatically calculate that. The second one; “OP_SELL” will tell Metatrader what to do; “OP_SELL” would be a sell operation while “OP_BUY” would buy. The third parameter tells Metatrader how much we are going to sell. Parameter four is the Bid price because we are going to sell for the Bid price. Parameter five is the “slippage”, we use a low value of 3 here and the “slippage” is used to set boundaries because you don’t want to execute the order when the price has moved for 20 points or so, if you would set the value to 1 you would get lots of re-quotes. Parameter six is the stop loss, if the price goes against us we want to be stopped out 300 points above the current Bid price. The next one is for the take profit value; when the price falls for 150 points I want to take the profit. This parameter could be used to pass a comment so you could use a string value to identify an Expert Advisor if you are going to use more than one Expert Advisor on the same chart. I rarely do it so I use the constant “NULL”, all capital letters here. This one would be the magic number; that’s also to identify the Expert Advisor, you can think of the magic number like a license plate on your car. This parameter is for the expiration; in our case its 0 (zero) because we are going to sell immediately but if we would use a sell stop or a sell limit we could set an expiration time and the last parameter here is a color, it’s green, and this is just the color for the arrow that is drawn on a chart whenever we sell something. That’s it, so let’s click on the “Compile” button or press F7, you shouldn’t get any errors here; I have one warning because I didn’t check a return value but for this simple example we are done, so let’s click on the little button here or press F4 to go back to Metatrader. In Metatrader you want to click on: “View/ Strategy Tester” or press CTRL and R, let’s pick the new file: “SimpleSellOrder.ex4”, mark the option for the visual mode here and start a test. Here we are, this is the first bearish candle and now we see the first sell trade so our little Expert Advisor is working and in this little video you have learned how to automatically create a sell order with MQL4 as soon as the first bearish candle appears on your chart and you have coded it yourself with a few lines of MQL4 code.
    6 min
  • MQL4 TUTORIAL BASICS – 12 HOW TO OPEN A BUY ORDER
    In this video we are going to find out how to open a simple buy position whenever we have a candle that is bullish and 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 one: “SimpleBuyOrder”, click on “Continue”, “Continue” and “Finish”. Now we can remove everything above the “OnTick” function and let’s also delete the two comment lines here. Okay. Inside the “OnTick” function we first want to check if we have no open orders that can be done by using the function “OrdersTotal”, it returns the number of market and pending orders and we want to find out if it is 0 (zero) so that would mean that we have no open positions and we don’t have any orders that might be triggered later. In this simple example we want to check for one other condition and that is if the close price for candle 1 was above the open price for candle 1. Candle 1 is the last finished candle – that’s this one in our case – right now it’s early in the morning so there is not much movement here. When we mark the “close” statement and press F1 we learn that it is actually an array that contains close prices for each bar on the current chart, so we just have to put the candle number inside of the square braces here and if the close price for the last candle 1 is above the open price for the last candle 1 we want to buy. Opening a position is a little bit complicated if you have never seen it before, in our case it looks like this. We use the “OrderSend” function and that requires a few parameters; we could have used everything in one line but to explain it I separated each parameter with a new line here. The first one is for the current symbol – that’s the currency pair on the chart. We are suing Australian Dollar against US Dollar here. The second parameter is what we want to do – in our case we want to open a buy order, you could also do something like “OP_SELL” to open a sell order and there are pending orders like: buy limit, sell limit, buy stop and sell stop. “OP_BUY” is going to buy immediately, this parameter is for how much we want to buy – in my case I’m going to buy 10 micro lot or 0.10 lot and I am going to buy it for the current Ask price. If you double click one of the currency pairs here you will always see two price lines; a red one and a blue one. If you want to buy you will get the lower price and if you want to sell you must pay the higher price, the difference between those two prices is the spread, so we want to buy at the Ask price. This is the so called slippage, we only want to buy if we don’t have a big price movement and in our case we are just going to buy if the price didn’t move more than 3 points. The next parameter is the stop loss and as you might have guessed the one that comes after the stop loss is the take profit – that’s when our position will be closed either when we made a loss or when we made a profit. We could also use a comment, I don’t do that, and otherwise you could use a “string” value here. If you are willing to use different kinds of Expert Advisors on one and the same chart that would be helpful. The parameter after the comment is called a magic number. This one is not magic at all, it’s like a license plate on a car, you just give it a unique number to identify it. This parameter would be the expiration; for a pending order you could set the order and set a deadline, so let’s say you don’t want to buy anything after a week has past and the last parameter “green” is used for the arrow that is drawn on the chart as soon as you open a new position. So let’s compile the code. This worked without any errors so we can click on the little button here or press F4 to go back to Metatrader. And in Metatrader we want to click on: “View/ Strategy Tester” or press CTRL and R, now we need to pick the new file: “SimpleBuyOrder.
    7 min
  • MQL4 TUTORIAL BASICS – 11 HOW TO CALCULATE A SIMPLE MOVING AVERAGE
    In this video we are going to calculate the values for a Simple Moving Average and we use the “Comment” function to output the current value on the chart. To do that please click on 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 file: ”SimpleMovingAverage”, click on “Continue”, “Continue” and “Finish” and now we can delete everything above the “OnTick” function and the two comment lines here. We start by calculating a Simple Moving Average for 20 candles and we use the “iMA” function to do that. This one comes with MQL 4 and when you mark it and press F1 you will see that it takes several parameters; the first parameter here is for the current symbol and when we use “_Symbol” it will automatically find out what kind of symbol we have on our chart – in my case the current symbol here is the currency pair: British Pound against Canadian Dollar – the second parameter here is the period and “_Period” will automatically find out what kind of time frame you use on your current chart – in my case I’m using the M1 chart for one minute candles – the third parameter here is for the number of candles that we are going to calculate, we have chosen 20 candles and when I pause the “Strategy Tester” here and put my mouse over the Moving Average you will see that it says: it’s a “Moving Average (20)” for the last 20 candles and that means that the current value here is the value that you get when you take the last 20 candles and calculate the average value. We have a shift parameter here and actually we have another one here, if you change this parameter here the drawing for the Moving Average will be moved to the right or to the left, if we would change the last value for the shift here that would change on which candle the current value should be calculated. For now just keep in mind that you use a 0 (zero) for both values. I actually almost never use any shifting values for any parameter. In parameter five we use “MODE_SMA” because we want to calculate a Simple Moving Average and for parameter six we use “PRICE_CLOSE” because the calculation should be based on the close price. So let’s insert a “Comment” function that will output the text: “MyMovingAverage20” and the calculated value and afterwards we click on “Compile”, that should work without any errors here and if that is the case please click on the little button here or press F4 to go back to Metatrader. In Metatrader we want to click on: “View/ Strategy Tester” or press CTRL and R, now please pick the new file:”SimpleMovingAverage.ex4”, mark the visual option here and start a test. This is how it looks like, you don’t see a Moving Average here, we only have calculated values but when you click on stop, suddenly the red line appears and now we use a little trick; please right-click on the chart, select “Template/ Save Template” and save it as “tester.tpl” because this is the template that is going to be used in your next test. So let’s start the test again and this time we see the red line for the Moving Average, here is our output, let’s zoom into the chart and when you put the mouse over the Moving Average of the last bar you will see that we have a value of: 1.6897 and that’s exactly what we have calculated here. Now you could use the value for the Moving Average and for the price to create a simple system that is going to buy whenever the price is above the Moving Average and that is going to sell whenever the price is below the Moving Average and in this little video you have learned how to calculate the value for a Simple Moving Average of 20 candles and you have coded it yourself with a few lines of MQL4 code. MQL4 Tutorial searches:simple moving average executable mql4 codesimple moving average code mql4moving average mql4mql4 moving average back bars
    6 min
  • MQL4 TUTORIAL BASICS – 10 HOW TO CODE A CUSTOM FUNCTION
    In this video we are going to create a custom function, in our case the function will double a value each time when the price changes so let’s find out how to do that. First we want to click on a little icon here or press a F4 in Metatrader 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 file: “SimpleFunction”, click on “Continue”, “Continue” and “Finish” and now you can delete everything above the “OnTick” function and the two comment lines here. First we want to create a static variable for the counter, it will be called: “counter”, it’s an integer (int) variable and the modifier: “static” is used to declare a local variable. A local variable is only available inside of one function, when you highlight the word: “static” and press F1 you will see that the “MQL4 Reference” says that the storage class of “static” defines a static variable, the static modifier is indicated before the data type and it exists throughout the function lifetime. Now we want to call an external function to double the value, the function we call will be named: “DoubleTheValue” (double the value) and we will pass the “counter” from the static variable here. Let’s create an output for the current value by using the “Comment” function, it will output the words: “Counter Value” and the current value of the “counter” on our screen but if we compile the code right now we will get an error that says that the double the value (DoubleTheValue) function is not defined, so let’s create that function. The first thing is always to define what kind of value will be returned by the function, in our case we are going to double whole numbers, so we will return an integer value (int), this is the name of the function, I have called it: double the value (DoubleTheValue) but you could use any other name and inside the round braces here we pass the value of the “counter” to the new function, here is where we pass it when we call the function: double the value (DoubleTheValue) and here is where we get the value inside of the function and you can see that we use another name here, you don’t need to call it “counter” because this is just the value of the “counter” that has been passed here. Let’s add two curly braces so this would be the declaration of the custom function but when we compile now we will see that “not all control paths return a value” and that is because we didn’t do anything inside of the function and we have not returned a result, so let’s take the counter value (CounterValue), multiply it by 2 and store the result in a new variable called: double value (DoubleValue) and afterwards we are going to return the result by using the “return” command, actually it’s a “return operator”, it terminates the current function execution and returns the control to the calling program and that’s about it. So let’s compile the code one more time, now you should not get any errors and no warnings and if that is the case you can click on the little button here or press F4 to go back to Metatrader. And inside of Metatrader we want to click on: “View/ Strategy Tester” or press CTRL and R, please pick the new file: “SimpleFunction.ex4”, enable the visual mode here and start a test and now you should see the counter value and a doubled value that comes up every time the price changes, this is a very simple example we only do one thing inside of the custom function and we only pass one parameter, you could perform much more complicated stuff but in this short video you have learned how to create a custom function with MQL4 and you have done it yourself with a few lines of MQL4 code.
    6 min
  • MQL4 TUTORIAL BASICS – 9 HOW TO USE A FOR LOOP
    In this video we are going to talk about the “for” loop, this “counter” is increased within a “for” loop, basically a “for” loop is used to start at a certain value and count until another value is reached and in this video we are going to find out how to do that. So please click on the little icon here or press F4 on your keyboard and now you should see the Metaeditor window and here we want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleForLoop”, now I click on “Continue”, “Continue” and “Finish”. Let’s delete everything above the “OnTick” function and the two comment lines here. First we need to create a start value, I will call this one: “counter”, it’s an integer variable (int) and in the initialization we will assign the value: 1 here, let’s also create an end value (endvalue) – in our case we want the “counter” to count until the value: 10,000 is reached – and inside of the “OnTick” function we are going to use the “for” loop, it has three expressions. If you have never seen a “for” loop before it might look a little bit weird but it’s not so hard to understand. The first parameter here is the starting value, that’s our “counter”, in our case the “counter” is 1. The second parameter is the expression that should be checked, so if the condition: “counter is below endvalue” is true the loop will continue, and the third parameter here is what should happen after this condition has been checked. In our case we are going to increase the “counter” by 1. “counter++” means that we take the current value of the “counter” and add 1. We could also use this kind of expression (counter=counter+1) and within two curly braces we want to do something; in our case we use the “Print” statement to output the words: ”the counter equals” and the current value of the “counter”. So let’s compile the code and if you don’t see any errors here you can click on the little symbol here or press F4 to go back to Metatrader and in Metatrader we want to click on: “View/ Strategy Tester” or we could press CTRL and R, here we pick the new file: “SimpleForLoop.ex4”, please enable the visualization here and start a test. And now we see that our “counter” is working, it’s counting up until it reaches 9,999 and the “for” loop is also able to count down, so actually we could switch the values here, now the starting value of our “counter” is 10,000, the “endvalue” is 1 and this time we want to count down from the “counter” value 10,000 as long as the “counter is bigger than the endvalue” and we want to subtract 1 for each time we count. So let’s recompile the code, go back to Metatrader and start a new test and this time we have a countdown. The “counter” will be diminished until this condition is no longer true; our “endvalue” is 1 that’s why the last entry here says that: “Counter equals 2”. We could also use the expression: “is greater or equals endvalue” (counter>=endvaue), now recompile, start a new test and this time it should say: “the last counter equals 1”. What’s interesting is that we could also take other values here, for example we could diminish the “counter” by 10, let’s recompile that one and this time our “counter” will be diminished by the factor of 10 and now you know how to use a “for” loop, a “counter” and an “endvalue” to count upwards or downwards and you have coded it yourself with a few lines of MQL4 code.
    6 min
  • MQL4 TUTORIAL BASICS – 8 HOW TO USE A WHILE LOOP
    In this video we are going to find out how to use the “while” loop to execute a piece of code again and again. To do that please click on the little button here or press F4 in your Metatrader and 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: “SimpleWhileLoop”, I click on “Continue”, “Continue” and “Finish” and now you can delete everything that is above the “OnTick” function and let’s also remove the two comment lines here. So for this simple example we will create a variable called: delay counter (DelayCounter), this is an integer value and we have another value, this is also an integer, the minimal value that we want to count until we continue with our program and that will be 10 million (10,000,000). Now we want to use the “while” statement to check if our delay counter (DelayCounter) is below the minimum number (MinNumber) value and when that is true everything between these two curly brackets here will be executed and to show it we use the “Print” statement to output the text: “DelayCounter:” and the current value of the delay counter. Afterwards we increase the delay counter by 1 that’s done by using the statement: “DelayCounter” equals “DelayCounter” plus 1 and that’s about it. So let’s click on “Compile”, we didn’t get any errors here and we also have no warning so now we can click on the little button here or press F4 to go back to Metatrader. And in Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, here we pick the new file: “SimpleWhileLoop.ex4”, let’s make that a little bit larger and enable the visual mode here and now you can start a test. In the “Journal” tab we see that our counter is working, it’s executing the “while” loop and nothing else. After the value of the delay counter has reached the 10 million mark our test is started, while you might noticed that also the output in the “Journey” is a little bit delayed and that is because printing out something is very time consuming, so once you are going to trade your own system please make sure that you don’t use “Print” too often because it will slow down the whole system. That’s almost it, but there is a catch; if we take out this part and paste it below the “while” loop we can still compile the program, we still get no errors but when we start a new test our delay counter is never increased. We have just created an infinite loop, nothing is happening, we don’t see any syntactical errors here but as this line is never able to increase the counter this expression here will stay true forever. Infinite loops like this one can cause big confusion, let’s replace the “while” loop with an “if” statement that will be executed only once every tick and when I recompile the code, stop the current test and start a new one, our counter will continue to work but this time it will only be increased for each time when the price changes, it was much faster with the “while” loop, so let’s change that back but keep in mind that you need to put a piece of code inside of the “while” loop that is able to create a situation when this condition here becomes fails otherwise your “while” loop might run forever, so let’s recompile it one more time and this time it works as expected, we should approach 10 million in a few seconds, here we are and now you have learned how to create a simple “while” loop and you have coded it yourself with a few lines of MQL4 code.
    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