MQL4TUTORIAL.COM

MQL4TUTORIAL.COM

By MQL4TUTORIAL.COMBusinessInvesting
Download on the App Store

MQL4TUTORIAL.COM episodes

  • MQL4 TUTORIAL – ADVANCED ICCI EXPERT ADVISOR
    In this video we are going to create an entry module (CheckEntry) for this Indicator; this is the Commodity Channel Index Indicator, it’s an oscillator and it creates signals and whenever this green line here crosses one of the dotted lines it will create a signal. When the green line is above the upper dotted line that’s a sell signal because we expect the prices to fall, and when the green line is below the lower dotted line that would be a buy signal because now we expect the price to rise. Now how can we create an Expert Advisor entry module (CheckEntry) for the Commodity Channel Index that is able to create buy and sell signals and actually trade with a main module like the Forex Trading Framework? Well, to do that please click on the little button here or press F4 in your 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 “CheckEntry_ICCI” click on “Continue”, “Continue” and “Finish” and now you can delete everything above the “OnTick” function and the two comment lines here and we don’t need a “void OnTick” function but we will make it a string function called: “CheckEntry”. Within that function we are going to create a string for the signal that will also have the name: “signal” but no value so far because we are going to calculate that now and that is done by using the “iCCI” function that comes with MQL4. It takes a few parameters; the first one is the symbol for the current chart, the second one and the last one our shift values. For example you could move the drawing of the Indicator to the right or to the left. We will calculate it based on 14 candles and we will use a typical price. If you click on “Insert/ Indicators/ Oscillators/ Commodity Channel Index“ you will also see 14 candles and typical price and the default values here. Actually we want to buy if the value is above 100 – that is the case whenever this line here is crossed –, if we pause the Indicator here and click on “properties” you will see a tab called: “levels” the upper dotted line is on the plus 100 (+100) level and the lower one is on the minus 100 (-100) level so if the ICCI value (ICCIValue) is bigger than 100 we consider it to be overbought and so we assign the word: “sell” to our signal. In the other case if it is below 100; so if ICCI value (ICCIValue) is smaller than minus 100 that would be a buy signal so we assign the word: “buy” to our signal and in the last step we need to return our signal to the main module. So what’s the main module? In my case it’s the Forex Trading Framework, this is basically a collection of functions to open and manage trades. You can create and use your own main module or you could download the demo version of the Forex Trading Framework from my website, it uses the include statement to include the newly created file for the ICC here and basically you can trade almost every entry with it so when you are done with the entry module (CheckEntry) for the Commodity Channel Index just save the entry module (CheckEntry), import it in the main module, click on “Compile” and that worked without any errors here so now we can click on this little button or press F4 to go back to Metatrader. In Metatrader we use a little trick, let’s click on “Insert/ Indicators/ Oscillators/ Commodity Channel Index“, we will go with the default values and click on “OK”, here is our indicator and now we right-click into the chart, click on “Template/ Save Template” and save it as: “tester.tpl” because this is the template that Metatrader is going to use for your back-test and if you don’t see the Strategy Tester here you click on “View/ Strategy Tester” or press CTRL and R, select your main module – in my case it’s the framework –, enable the option for the visual mode here and start your test. Here is our little Expert Advisor at work it has already made a few trades and we have a little profit and now you know h...
    6 min
  • MQL4 TUTORIAL – SIMPLE WHILE LOOP
    In this video we are talking about the “while” loop in MQL4; we used it here to open an order. Especially I want to talk about the difference between a “while” loop and an “if” statement because those things seem to be the same but there is a difference. To create an Expert Advisor for our “while” loop 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: “SimpleWhileLoop”, click on ”Continue”, ”Continue” and “Finish” and now you can remove everything above the “OnTick” function and let’s also delete the two comment lines. Okay. That’s it. First we want to find out if we have any open orders. That is done by checking if “OrdersTotal” equals zero and in that case we want to call a function called: “OpenBuyOrder.” I’ll use a copy of the version that I have used for the Simple Buy Trade video so if you are interested in all the parameters you can check out that video. Right now it’s about “if” or “while” so let’s start with the “if” statement. If “OrdersTotal” is zero we don’t have open orders and now we want to buy. Let’s compile the code by clicking on the button here or pressing F7; that worked without any errors so now we can click here or press F4 to go back to Metatrader. In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, in the Strategy Tester please select the: “SimpleWhileLoop.ex4” file, I will disable the visual mode here to speed things up and start a test. This takes only a few seconds and now we made a loss of: 39 Dollars and 43 Cents by using the ”if” statement. Now we simply replace the “if” statement with a “while” statement here, recompile the code and start another test. A few seconds later here is our result; it’s exactly the same, we lost 39 Dollars and 43 Cents. Okay, so far so good. We don’t see any difference here. Now let’s see what happens when we change the statement. Now we want to find out if we have less than 10 orders. Let’s compile the code; we start the same test with the same parameters here, it takes a few more seconds this time but here is our result; we made a loss of: 394 Dollars and 29 Cents. Let’s see what happens if I use the “if” statement, once again I recompile the code, start exactly the same test and what result would you expect? It’s a total net profit of: 173 Dollars and 95 Cents. Now how can it be possible that the “if” statement and the “while” statement do something different when the only thing we changed is the number of the orders? To get the answer please mark the visual mode here, we will slow down the test and start a new one and you see that the orders are opened one after the other. Let’s stop the test, once again we use “while” here, recompile, now we test again and this time you only see one line for the buy orders and you can’t read the text here because all those orders have been opened at exactly the same time. And that’s the difference! The “while” statement will be executed 10 times until this condition is no longer true so it will open 10 buy orders at exactly the same time. The “if” statement will only be executed once each time we have a price change so it is executed only once until the next “OnTick” function is called when the price changes again. Over time this makes a big difference in the result and now you know the difference between the “while and the “if” statement and how to create an Expert Advisor that is using the “while” loop and you have coded it yourself with a few lines of MQL4 code. MQL4 Tutorial searches:mql4 coding a loopmql4 open order if zeromql4 whilewhat is a looped expert advisor
    6 min
  • MQL4 TUTORIAL – SIMPLE CUSTOM TIME FUNCTION
    In this video we are going to create a little custom function to calculate the local time. You see that we have an output for the current date that is traded here in the Strategy Tester and this is the current time and now we are going to create a custom function in MQL4 to calculate and output the time in this way. To do that please click 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: “SimpleCustomTimeFunction”, click on “Continue”, “Continue” and “Finish” and now you can delete everything above the “OnTick” function and let’s also delete the two comment lines here. We start with a string that will be called: “myTime” it’s a string variable for the custom time that we are going to calculate and we are going to create a custom function that will be called: “GetTime” and to calculate “myTime” I just try to call the function: “GetTime” here. If I click on the “Compile” button now you will see that “GetTime” function is not defined so this is definitely nothing that comes with MQL 4 and now we are going to create it. It should return a string value, the name is: “GetTime” and as soon as I add two curly brackets and recompile the code the error is gone. Now let’s add some logic to the “GetTime” function that we have created right now. I would like to output the time with seconds like we see it here: 17:55 means 5 minutes to 6:00 p.m. and to calculate the time we use a function that is called: “TimeLocal” and we want the output for the date; this is the first part here with the date (TIME_DATE), this is the pipe symbol and here is the second parameter for the time with seconds (TIME_SECONDS) – that’s the second part here – and we use time to string (TimeToStr) to convert the time from a datetime variable to a string. Let’s mark “TimeToStr” press F1 and we see; okay, it converts the value of the time and seconds that have passed since January the 1st in 1970 into a string that has this format here and now that the calculation is done we want to return the calculated time to the main function that is done by using “return” for our string variable that we have calculated here, so everything within the “GetTime” function will be calculated here and the result is assigned to the variable: “myTime” so if we add the “Comment” function here for “myTime” it should create the desired chart output for the time we have calculated. Now please click on the “Compile” button or press F7 that should work without any errors and if that is the case please click here or press F4 to go back to Metatrader and in Metatrader we click on: “View/ Strategy Tester” or press CTRL + R that will bring up the Strategy Tester panel here and here you want to select the new file: “SimpleCustomTimeFunction”, please enable the visual mode here and start your test. …and here we are. Our Expert Advisor is working and now you know how to create a custom time function for Metatrader 4 and you have coded it yourself with a few lines of MQL4 code. MQL4 Tutorial searches:mql4 parameter conversionbutton is pressed mql4mql4 convert openordertime to localmql4 customise menumql4 function CODE contém não contém
    5 min
  • MQL4 TUTORIAL – ADVANCED BOLLINGER BANDS EXPERT ADVISOR
    In this video we are going to trade the Bollinger Bands, I really like this Indicator – actually it was the first one that made me a profit a few years ago. This would be a sell signal; the last candle closed outside the upper Bollinger Band and here we have a reentry. In the other case; like here the close is below the lower Bollinger Band and the next candle reenters from below, that would be a buy signal. Now how can we create an Expert Advisor that is able to produce these entry signals and to work as an entry module (CheckEntry_Bollinger) for a main module that is actually going to trade Bollinger Bands? Well, to do that please click on the little button here or press F4 on your keyboard and now you should see the Metaeditor window here, please click on “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this one: “CheckEntry_Bollinger”, because this is what we are going to call from our main module. For now click on “Continue”, “Continue” and “Finish”, and let’s delete everything above the “OnTick” function and the two comment lines here. In our case we will not use the “OnTick” function but a “string CheckEntry” function, that’s the one that we are going to call from our main module to find out if we have a signal. So we start by creating an empty string variable for the signal, let’s call it: “signal” and don’t assign a value here. To trade the Bollinger Bands we are going to calculate the upper and the lower Bollinger Band, we use the built-in function called: “iBands” for the current symbol on the chart, and the current period on that chart. The signal will be calculated based on the last 20 candles, we have a deviation of 2 and the calculation will be based on the close price. The lower Bollinger Band uses a parameter called: “MODE_LOWER” and for the upper Bollinger Band we use: “MODE_UPPER” and this is the calculation for candle 1 – that’s always the candle before the one that is built right now, so this would be candle 0 and this would be candle 1 – and to find out if we have a crossover we calculate the candle before. This is exactly the same thing but this time we are using candle 2 and if the price is above the last close price – so if it’s going up – and if close 2 was below the lower band and there is a third condition; if close 1 is above the lower band now, so when it was below for candle 2 and if it is above for candle 1 that would mean that we have crossed the lower band from outside and if that is true we assign the word: “buy” to our signal. Let’s do a little formatting here, so this is the whole buy statement. Obviously we need the same for the sell signal and here we are. If the price is below the last close price – if close 2 was above the upper Bollinger Band and if close 1 is now below the upper Bollinger Band that would be a re-entry from here and now we want to assign the word:“sell” to our signal. In the last step we are going to return the signal to our main module by using the “return” function. In my case the main module is the Forex Trading Framework, this is the MQL4 version from April 2018. The framework is basically a collection of functions that can open and handle positions in MQL4. Here we include the “CheckEntry_Bollinger.mq4” file that we have created here, so please save the file we have created. Now I need to go to my own main module, you could use your version or download the demo version from my website. I have to click on “Compile” or press F7 and we don’t have any errors here so now I can click on the little button here or press F4 to go back to Metatrader. In Metatrader we use a little trick, we click on “Insert/ Indicators/ Trend/ Bollinger Bands“, and here you will see the 20 candles, the deviation value of 2 and as we said we calculate based on the close price, we don’t use a shift value so now we can click on “OK” and now you right-click into the chart, select “Template/ Save Template” and save it as: “tester.
    7 min
  • MQL4 TUTORIAL – SIMPLE EMA EXPERT ADVISOR
    In this video we want to talk about an Exponential Moving Average that is creating buy or sell signals, the signals are based on the direction. If the Moving Average is above or below the candles; whenever the candles are below the Moving Average this would be a sell signal and when the candles are above the Moving Average this creates a buy signal. Now how can we create an Expert Advisor in MQL4 that is able to output the buy and sell signals based on an Exponential Moving Average? 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 one: “SimpleEMA” for Exponential Moving Average, click on “Continue”, “Continue” and “Finish” and now you can delete everything that is above the “OnTick” function and let’s remove the two comment lines here. Okay, we start by creating a string variable for the signal, we call it: “signal” and we don’t assign a value because we are going to calculate it in the next step and this is how it’s done: we use the built-in “iMA” function that comes with MQL4. It needs to have a few parameters; the first one is the current symbol on the chart – in our case the symbol is the British Pound against the Australian Dollar (GBP/AUD) – the second parameter is the period that we have selected on our chart, we are using the 1-minute chart here and these two variables: “_Symbol” and “_Period” are able to find out what kind of currency pair you trade and which of the timeframes you use. The next parameter is for 20 candles, so the value here is calculated based on the last 20 candles here and this parameter is a shift value. If you mark the “iMA” function and press F1 you will see that we have a shift value here that is called: “ma_shift” and another one here that is also called: “shift”. Let’s open a new chart window: click on “Insert/ Indicators/ Moving Average”, let’s ad a Moving Average with a shift value of 0 and another one in yellow with a shift value of 20 and now you see what it does, it moves the drawing of the Moving Average into the future. I could also use -20 here and now it was moved to the past, this expression: “MODE_EMA” actually lets you select between a Simple Moving Average and Exponential Moving Average, a Smooth Moving Average and a Linear Weighted Moving Average. To find out what the differences are you might maybe want to look up the 4 types on investopedia or vikkipedia. The Exponential Moving Average basically is a simple Moving Average but the last few current candles play a bigger role in the calculation. You could play around with the 4 types here to see the differences on your chart, for now we go with the Exponential Moving Average and it should be calculated based on the close price, so we use “PRICE_CLOSE” here. This is the offset shift and this is the shift that we have, and this is the shift value we have changed in this example to move the drawing of the Moving Average to the right or to the left. It might take you some time to get used to it but for now both values are 0 and if the SMA is below the bid price – that would be the case if my Moving Average (MyMovingAverage) is smaller than “Bid” – that would create a buy signal so we assign the word: “buy” to our signal. Of course we also want to calculate the other case. If our SMA is above the ask price; so if my Moving Average (MyMovingAverage) is bigger than “Ask” that would be a sell signal so we assign the word: “sell” to our signal variable. In the last step we want to create an output on our chart so here we use the “Comment” function that will output the text: “The current signal is:” followed by our “signal” and when you’re done you can click on the “Compile” button here or press F7, that worked without any errors and now you can click here or press F4 to go back to Metatrader. In Metatrader we need to use a little trick; we click on “In...
    7 min
  • MQL4 TUTORIAL – SIMPLE COMMODITY CHANNEL INDEX
    In this video we are going to trade the Commodity Channel Index, that’s the Indicator right here; you see the two dotted lines and whenever the line here crosses the upper dotted line we consider that to be a sell signal and when it crosses the lower dotted line that would be a buy signal. In this video we are going to find out how to automatically calculate this signal with a few lines of MQL4 code. To do that please click on the little button here 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: “SimpleCommodityChannelIndex”, click on “Continue”, “Continue” and “Finish” and now you can remove everything above the “OnTick” function and the two comment lines here. Let’s start with a string variable for the signal, that variable will also be called: “signal” – we don’t assign a value because we are going to calculate that later on. Let’s define our indicator that is done by using the “iCCI” function that comes with MQL4 and if you mark it and hit the F1 key you will see it takes a few parameters. The first parameter is for the symbol – that’s actually the currency pair on your chart –, the second one is for the time frame, in our case we use the 1 minute time frame – another time frame could be 5 minutes or 15 minutes and so on –, the third parameter is for the averaging period, that’s actually the period we use to calculate the signal and if you click on “Insert Indicators Custom CCI” you will see that the CCI period is 14, so that’s what we are using here followed by a typical price. You could also use a close or an open price, the highest or the lowest price, and the last parameter here is for the shift. You could use a shift value to move the Indicator window to right or to the left. We are not going to use it so our last parameter will be 0. Now that we know the value we want to find out if it is above 100, so if the “ICCIValue” we have defined here is greater than 100 – that would be the case when it’s above the dotted line here, because this is the 100-plus line –, we consider our currency pair to be oversold so we assign the word: “sell” to our signal. In the other case if our value is below -100, so if our “ICCIValue” is below -100 – that would be the case when the line is below the dotted line here, because the lower line is the minus 100 line – in that case we consider it to be a buy signal so we assign the word: “buy” to our signal. In the last step we create a chart output that is done by using the “Comment” function and we want to output the word: “Signal:” followed by the calculated signal (signal) and in a new line we want to have the output: “ICCI Value:” followed by the current ICC I value (ICCIValue). Okay. Please don’t forget the closing bracket and when you are done you can click on the “Compile” button here or press F7 on your keyboard and now you should see an output like this. We have compiled it without any errors and without any warnings and in that case you can click on a little button here or press F4 to go back to Metatrader and in Metatrader we use a little trick; please click on “Insert Indicators Custom CCI” we will go with the default values here so please click on “OK”. Now you should see the little window with the Indicator below the candles and now we right-click in our chart and select: “Template Save Template” and save it as: “tester.tpl” because this is the template that our strategy tester is going to use for the next back test. If you don’t see the strategy tester panel you can click on “View Strategy Tester” or press CTRL and R, let’s select the simple “SimpleCommodityChannelIndex.ex4” file we have just created enable the visual mode here, pick a currency pair and start our test. So let’s speed up the test and now you can see buy and sell signals appearing on your chart so our little Expert Advisor works and now you know how to code the Commodity Channel Index Indicator in...
    6 min
  • MQL4 TUTORIAL – SIMPLE POSITION MULTIPLIER
    In this video we are going to create an Expert Advisor that contains a position multiplier (SimplePositionMultiplayer). In our case we are trading 5 positions at once, let’s stop the Expert Advisor, click on “Expert properties/ Inputs/ NumberOfPositionsToTrade” and increase the value to 10, confirm it with okay. I start another test and this time our Expert Advisor is trading 10 positions and now we are going to find out how to code an Expert Advisor in MQL4 that is able to offer a user adjustable value that can be changed without changing the source code. 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 one: “SimplePositionMultiplier”, click on “Continue”, “Continue” and “Finish” and now you can remove everything above the “OnTick” function and the two comment lines here. So now let’s create an external variable for the number of positions to trade that will be called: “NumberOfPositionsToTrade”; most coders like to use short names and a few weeks afterwards they ask themselves: “What the hell is this variable doing?” but I think if you invest a few seconds to type in a good name for the variable is always time that is good Invested. Okay. In the “OnTick” function we want to create a double variable that is called: “Candle1Close” and it contains the value for the close price of candle 1. Let’s do that same thing again but this time we call it: “Candle2Close” because it contains the close price for candle 2 and now we want to stack a few conditions to open a new trade. If “OrdersTotal” delivers a value that is below the “NumberOfPositionsToTrade” we have defined here and if that is the case we come to our second condition; the second condition is we want to test if the last close price was higher than the close price before and that would be the case if the “Candle1Close” price is bigger than the “Candle2Close” price. The current candle is always candle 0, this is candle 1 the candle before is candle 2 and so on, so if this expression is true our price is higher now and now we want to send a buy order that is done by using the “OrderSend” statement. To find out about all the different parameters here just watch the Simple Buy Order video; basically we buy 10 micro lots and our take profit will be 150 points above the “Ask” price and when you are done you can click on the little “Compile” button here or press F7 on your keyboard. That worked without any errors, so now we can click here or press F4 to go back to Metatrader. In Metatrader please click on “View/ Strategy Tester” or press “CTRL and R”, now select the “SimplePositionMultiplayer.ex4” file we just have created and enable the visual mode here, set the “Expert properties”, let’s say we want to have 3 trades, click on “OK” and start your test and this time we see 3 open trades on the chart and now you know how to create an Expert Advisor that is able to deliver a user-defined variable that can be adjusted by the user when he clicks on “Expert properties” and you have coded it yourself with a few lines of MQL4 code.
    5 min
  • MQL4-TUTORIAL-ADVANCED-IAD-EXPERT-ADVISOR
    In this little video we are going to trade this Indicator, it’s an Oscillator, it’s name the Accumulation Distribution Indicator and it will create buy or sell signals based on the value of the Indicator here. If we speed up the process you can see that it’s actually making a profit. Now we want to find out how we can create an Expert Advisor that is able to create buy or sell signals based on the Indicator window here. 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: “CheckEntry_IAD” because that is what I am going to call in my main module, now let’s click on “Continue”, “Continue” and “Finish” and now you can delete everything above the “OnTick” function here and lets also remove the two command lines. Usually we use the “OnTick” function because this function is called whenever a new tick is generated; a tick is any price change here on the chart. We need a string value and we will call the new function check entry (CheckEntry) because that is what I’m using in my main module to create a signal, this is the German version for the MQL4 Forex Trading Framework; the English version still isn’t finished by now but you could also use your own module or download the Forex Trading Framework from my website later, but for now we want to continue with the check entry (CheckEntry) function. One of the first things we need is to create a string variable for the signal that will also be called: “Signal” and we don’t assign a value because we are going to calculate that later. So let’s define the IAD value (IADValue); that can be done with the MQL4 function: “iAD”, it is included in MQL4 and if you mark it and hit F1 you will see that it calculates the Accumulation Distribution Indicator and returns its value. It has three parameters; the first one is the symbol – that’s actually our chart –, the second one is the time frame, we use a 1 minute chart here but another time frame would be 5 minutes or 15 minutes and the last parameter is for the shift. We could use the shift to move the Indicator to the right or to the left and for this simple example we assume that whenever the “IADValue” is greater than zero that will be a sell signal so we assign the word: “sell” to our signal. In the other case now the “IADValue” would be smaller than zero we would create a buy signal and assign the word: “buy” to our signal. In the last step we use the “return” function to return our signal to our main module; in my case it’s the framework. Here we would call the check entry (CheckEntry) function and that would return the value for the signal. Now when you are done you click on “Save” because the entry module just needs to be saved, the main module needs to be compiled by clicking on the little button here or pressing F7 and that worked without any errors and without any warnings so now I can click here or press F4 to go back to Metatrader. In Metatrader we use a little trick; please click on “Insert/ Indicators/ Volumes/ Accumulation Distribution”, we will go with the default values here and click on “OK” and now the Indicator window appears below the candles. Now let’s right click into template here, select “Template/ Save Template” and save it as: “tester.tpl” because that is the template that is going to be used in your next back test. Let’s click on save and to open the “Strategy Tester” we click on “View/ Strategy Tester” or we press CTRL and R, now I select the Forex Trading Framework and you would select your own main module, please enable the visual mode here and start a test. And here is our little Expert Advisor at work you can also speed it up and in this little video you have learned how to create an Expert Advisor that is able to trade the Accumulation Distribution Indicator and you have coded the entry yourself with a few lines of MQL4 code.
    6 min
  • MQL4 TUTORIAL – SIMPLE MODULAR EXPERT ADVISOR
    In this video we are going to create a simple modular system that can output the entry signal for different indicators. Right now we are treading the MACD entry signal, right now it says buy and when the line is crossed and the bars are drawn on the other side of the horizontal line it will say sell; now the signal has turned again and it wants to buy. Let’s stop this test and find out how we can create an Expert Advisor that can handle different entries without changing the source code of the main module. To do that please click on the little button here or press F4 in your Metatrader and now you should see the Metaeditor window and here we want to click on “File/ New/ Expert Advisor (template)” from template, “Continue”, this one will be called: “SimpleModule”, let’s click on “Continue”, “Continue” and “Finish” and now you can remove everything that is located above the “OnTick” function here and let’s also delete the two comment lines. The first thing we want to do is to include files. We are going to include external files that contain the function to find out if we have a buy or a sell signal. That can be done by using the include statement that is going to import the function within the “CheckEntry_IAC.mq4” file. Let’s open that file. This is it. It contains a function called: “CheckEntry” that will use the “iAC” function to create a sell signal or a buy signal and it returns the signal to our main module. In the “OnTick” function – remember this is the function that will be called whenever the price changes – we get a string signal from our external file so let’s create a string variable called: “signal” and the value will be calculated in a check entry function (CheckEntry). That function is located in our external file: “CheckEntry_IAC” and for this simple example we just want to create an output for the signal, so let’s use the “Comment” function to output the word: “signal:” followed by the calculated signal on our chart. Remember you will need to have the imported file “CheckEntry_IAC.mq4” in the same directory. All you need to tell MQL4 for is the correct path to that file. We have explained the IAC entry in another video. In our case we want to find out how to import an entry as an external module, so let’s click on the compile button here or press F7 and that worked without any errors and any warnings here so now I can click on a little button here or press F4 to go back to Metatrader. In Metatrader I want to use a little trick; let’s click on “Insert/ Indicators/ Bill Williams” and select the Accelerator Oscillator, go with the default values here, click on “OK” and here is the Accelerator template, so let’s right-click into the chart, select “Template/ Save Template” and save it as: “tester.tpl” because this is the template that Metatrader is going to use for the next back test. After you have done that please click on “View/ Strategy Tester” or press CTRL + R, now please select the “SimpleModule.ex4” file we have compiled, mark the option for the visual mode here and click on start. Now our Expert Advisor is producing buy or sell signals based on the Accelerator Oscillator, let’s stop the test here and now I go back into Metaeditor and I will exchange the entry file here and now I include the “CheckEntry_MACD.mq4” file. It basically does the same as the other entry but this time it uses the “iMACD” function to create a sell or a buy signal and return it to our main module. This entry is also explained in another video and now I need to compile the main module so let’s click on compile or press F7, that also work without any errors or warnings so I can click here or press F4 to go back to Metatrader. Let’s delete the Accelerator Indicator, click on “Insert/ Indicators/ Oscillators/ MACD”, go with the default values, click on “OK”. Here is our MACD Indicator so let’s right-click, save the template, again we use the “tester.tpl” file, click on “Save” and start another test with exactly the same Expert Advisor...
    7 min
  • MQL4 TUTORIAL – SIMPLE MOUSE CLICK EXPERT ADVISOR
    This time we are going to talk about chart events; a chart event will happen when you interact with your chart. For example in this simple Expert Advisor that is called: Simple Mouse Click . You can click anywhere in the chart and you will see the text: ”You pressed the mouse at the following coordinates:” and the x and y coordinates here. That will happen whenever you click on the chart. For example if you click on the left border here you will see that the x value is very low and the y value is high and if you click on the upper border here it’s the other way around; now the x value is high and y value is low. You will get the highest values here in the lower right corner and now we want to find out how we can create an Expert Advisor that is able to take the mouse coordinates and create the output on the chart. To do that please click on the little button here or press F4 in your Metatrader and now you should see the Metaeditor window here and here you want to click on “File/ New/ Expert Advisor (template)” from template, “Continue” – I will call this one: “SimpleMouseClick”, click on “Continue”, “Continue” and “Finish”. Let’s remove everything that is above the “OnTick” function and the two command lines here. Usually the “OnTick” function would be triggered whenever the price changes on your chart, but in our case we want to look for mouse clicks, so let’s change “OnTick” to “OnChartEvent”. Let’s mark that expression here and press F1 and you will see that the “OnChartEvent” is the handler of a group of chart event elements. For example if you press a key, move your mouse, create an object or change an object and whenever something like this happens it will create an event. In our case the event will be “CHARTEVENT_CLICK” this is the event handling function and it will take a few parameters here; the first parameter is the “ID”, that’s the event ID – in our case that will be “CHARTEVENT_CLICK” –, the second parameter is of the long type, so it’s called: “lparam” for long parameter, the third parameter is a double parameter, so it’s called: “dparam”, and the last parameter is called: “sparam” for string because it’s of the string type. Now we want to find out if the mouse button was clicked and that is the case whenever the id equals the chart event “CHARTEVENT_CLICK” and when that is true we want to use the “Comment” function to output the text: ”You pressed the mouse at the following coordinates:” followed by the x coordinate that is the value of the “lparam” and the y coordinate that is stored in the “dparam”, and that is pretty much it, so when you’re done you can click on the “Compile” button here and that did work without any errors and without any warnings and this code is exactly the same in MQL5 so if you ever wanted to switch from Metatrader 4 to Metatrader 5 the event handling is exactly the same in this case. Now you can click on the little button here or press F4 on your keyboard to go back to Metatrader. In Metatrader usually we would click on “View/ Strategy Tester” or press CTRL + R and you could select the “SimpleMouseClick.ex4” file here and start a new test but wherever you click you won’t get anything, so let’s stop the test here, remove the “Strategy Tester”. Now simply select the Simple Mouse Click Expert Advisor (SimpleMouseClick) here and drag and drop it on your chart. I will confirm that, click on “OK” and here it is! When I click on the chart I get the output: “You pressed the mouse at the following coordinates:”, x is: 97 and y is: 113 and when I click anywhere else the values will change because every time I click the chart event is triggered and now you know how to create an Expert Advisor that can get the coordinates of each and every mouse click on your chart and you have created it with a few lines of MQL4 code. MQL4 Tutorial searches:CHARTEVENT_CLICK indexmql g mousemql4 mouse click chart and draw an arrowmql4 right clicktrigger mouse left click in mql4
    6 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