MQL4TUTORIAL.COM

MQL4TUTORIAL.COM

By MQL4TUTORIAL.COMBusinessInvesting
Download on the App Store

MQL4TUTORIAL.COM episodes

  • MQL4 TUTORIAL – 3 Percent Monthly Trading Profit a month? Is it possible?
    A curious novice MQL programmer asked me today if it was possible to get at least 3% profit each month in an email request. Even Warren Buffett, one of the richest men in the world, is only able to generate a 20% profit on his investments annually. According to what I’ve heard, an annual gain of roughly 36% from a 3% monthly profit would be significantly greater than Warren Buffett’s typical return. Does that imply that it is impossible? Actually, if you want or need to reach at least 3% per month, it is tough to find and run a lucrative solution over a longer period of time. Essentially, the goal is to minimize mistakes, especially in the beginning, and to lose as little money as possible rather than to instantly turn a particularly high profit. The majority of new traders lose the majority of their trading capital within the first few months. Therefore, the first step is to minimize losses while you are still learning and have enough capital rather than to maximize profits. This has a second issue in that every month is a little bit different. Profits from automated trading cannot be compared to a steady paycheck. There may be months when you make less money, but you might make a little bit more money the following month. There is also the issue of people constantly wanting to change and improve their own systems. If you can’t follow your own rules, you will, at least by the third or fourth exception, know that you made the incorrect decision at the incorrect time. Frustration from this can give rise to actions like trading in revenge. Humans are emotional, and as a result, awkward situations or reckless actions arise frequently. Despite having good intentions, it frequently has disastrous effects. Additionally, a lot of people mistake automatic trading for gambling or a form of fortune telling. However, the actual focus is more on statistical probabilities, which allows for some exceptions to the rule. You shouldn’t assume that you will make that much money, on average, every week or month you trade. Whether you like automatic trading or manual trading, you will essentially struggle if you can’t maintain your own trading technique over an extended length of time, create a sound strategy, abide by your own standards, and stay away from emotional choices. If you want to succeed, the only way to consistently make money trading over the long term is done. That way, you can invest your profits into your own system, and that makes it even better. However, one should also be realistic in their expectations and understand that trading is a discipline that takes time, patience, and ongoing learning, rather than being a quick route to riches. Long term success depends on continuous testing and adjusting to shifting market conditions.
    3 min
  • The Equity Stop – how to find the best stop loss percentage solution
    In the trading market, which is always changing, it is important to know how to protect your money. A stop-loss order is one of the most common ways to do this. A stop-loss order is an important tool for any trader, but what is the best stop loss rate to use? In this detailed guide, we’ll look at different stop-loss methods and help you figure out the best percentage for the way you trade. What does Stop-Loss Order mean? A stop-loss order is a type of trading order that limits the amount of money an investor can lose on a stock market or forex account. It works by closing a position immediately when it hits a certain price, called the stop-loss level. This helps traders keep their money safe and limit their losses when the market is moving a lot. Stop-loss orders (for stocks and foreign exchange) There are different kinds of stop-loss orders, such as standard, trailing, and stop-limit orders. Each type has its own pros and cons, but they all have the same goal: to keep your losses on the market to a minimum. When choosing the best stop loss percentage, there are a few things to think about. Several things should be taken into account when figuring out the best stop loss percentage for your trading plan. These things are: Volatility: The way prices change can have a big effect on how well your stop-loss order works. When the market is volatile, you may need a bigger stop loss percentage so that you don’t get stopped out too often. Levels of support and resistance: Find the key areas of support and resistance in the price movement of the stock. Putting your stop-loss order near these levels can help protect your account, since most people pay attention to changes in price near these levels. Trading Style: Day traders may use tighter stop-loss percentages to keep losses to a minimum, while buyers with a longer time horizon may prefer to have more wiggle room to ride out market changes. If you want to trade over a long period of time, you need to think about the swap cost. Risk tolerance: The best percentage for your stop loss relies on how much risk you are willing to take. A trader who is less risk-averse might choose a smaller percentage, while a trader who is more risk-taking might be willing to take on more risk. In the end, it comes down to how much you can take without cheating the system. Popular ways to define a stop loss setting Set your stop-loss order a certain amount below the current price or entry point of the stock. For example, if you bought at $100 and set a 5 percent stop-loss, your order would automatically close the trade if it fell to $95. Moving average method: Set your stop-loss order based on a moving average, like the 50-day or 200-day moving average. This plan can help you adjust to changes in the market and in trends. ATR method (Average True Range): The average range of price changes over a certain time frame is used to calculate the ATR, which is a measure of price volatility. By increasing the ATR by a certain number, you can use the stock’s volatility to figure out the right volatility stop loss level. Set your stop-loss order just below a level that has been a support in the past. This approach helps you keep your position safe while letting the market move as it should. How important it is to have a well-thought-out stop-loss order plan For trading to go well, you must have a clear plan for stop-loss orders. It lets you set your maximum loss ahead of time, handle risk well, and avoid making emotional decisions when the market is uncertain. By taking things like volatility, levels of support and resistance, and your own trading style into account, you can find a better stop loss percentage for your specific case. How to find the right setting for a stop loss order? When trading, it’s important to have a well-thought-out and personalized stop loss strategy to protect your money from possible losses. A stop loss should keep an investor from losing a lot of money,
    3 min
  • MQL4 TUTORIAL – SIMPLE BULLS POWER EXPERT ADVISOR
    In this video we are going to talk about the Bulls Power Indicator, it is drawn in a separate window below the candles. The Bulls Power Indicator basically can tell us if the bulls are stronger than the bears. Whenever those lines of the histogram are above the zero line we will put out a buy signal here and whenever the direction changes and the histogram is drawn below the zero line that would be a sell signal. Okay. To create the Indicator and the Expert Advisor that is going to trade it you need to 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 Simple Bulls Power (SimpleBullsPower.ex4) click on “Continue “, “Continue” and” Finish” and now you can remove everything above the “OnTick” function and the two comment lines here. Let’s create a string variable for the signal here – I will also call it signal – it doesn’t contain a value so far because that is what we are going to calculate in this simple Expert Advisor and to do that we can use the “iBullsPower” function that is included in MQL4, that will calculate the Bulls Power Indicator and return its value. Now what is meant by value? If we passed the execution here we will have a value. In this case it’s 0.0006 and that matches the value of the last bar here of the histogram and to calculate it we need to pass a few parameters. The first one is the current symbol on the chart, the second parameters for the selected period on your chart, in my case I’m trading a 1 minute chart, the period could also be something like 5 minutes 15 minutes and so on and “_Period” makes sure that we are calculating the correct period that is the period from the chart you are trading right now. The next parameter has the value 13 and when you click on “Insert/ Indicators/ Oscillators/ Bulls Power” you will see the period has a value of 13 and we calculate the value based on the close price and that’s exactly why the 13 is used here as the third parameter and “PRICE_CLOSE” is parameter number 4. Well this last parameter stands for the shift. What is a shift? Actually you can move the current indicator value to the right or to the left depending on the shift value. We don’t want to do that so we use 0 and if the Bulls Value is above 0 and the histogram is drawn above the line here we consider that to be a buy signal so we assign the word: “buy” to our signal variable. In the other case if the Bulls Value is below 0 or in MQL4 if BullsPowerValue is smaller than 0 we assign the word: “sell” to our signal. Last but not least we create a screen output by using the built in “Comment” function to output the words: “BullsPowerValue:” followed by the calculated value and in a new line we output: “Signal:” followed by the calculated signal. Okay. That’s it. Now you can click on the compile button here or press F7 to compile your code and that should work without any errors and in that case you can click on the button here or press F4 to go back to Metatrader. In Metatrader we will use a little trick, please click on “Insert/ Indicators/ Oscillators/ Bulls Power”, click “OK”. Now the Bulls Power Indicator will be drawn on your chart and now we right click in the chart, select “Template/ Save Template” and save it as: “tester.tpl” because that is the template that is going to be used for your next back-test. To start the Strategy Tester you can click on “View/ Strategy Tester” or press CTRL + R, please select the newly created “SimpleBullsPower.ex4” file and start your test. Now you should see the little Expert Advisor at work, it creates an output for a current Bulls Power Value and below that you see a buy or a sell signal. On a real account you would combine the signal of the Bulls Power Value to something like a trend following indicator and use it as a filter but for now you know how to create an Expert Advisor that is able t...
    7 min
  • MQL4 TUTORIAL – SIMPLE WPR EXPERT ADVISOR
    In this video we are going to create an Expert Advisor to output buy or sell signals on your chart based on the WPR Indicator. This is the Williams’ Percent Range Indicator, it’s based on the last 100 candles and whenever the blue line here crosses one of the dotted lines, that will produce a buy or a sell signal, when it’s below it’s a buy signal, and when it’s above the upper dotted line here, that would be a sell signal. Now how can we create an Expert Advisor that is able to output buy or sell signals for the Williams’ Percent Range Indicator directly on your Forex chart? To do that please click 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: Simple WPR (SimpleWPR) for Williams’ Percent Range. Click on continue, continue and finish. Now you can delete everything that is above the “OnTick” function here and please also delete the two comment lines. We will start with a variable for the signal – this is a string variable called signal – and so far, we will not assign any value because we are going to calculate the signal later. Now let’s calculate the WPR value, we are doing it by using the “iWPR” function that comes with MQL4. It needs the current symbol on your chart, the selected time frame like 1 minute or 60-minute charts, this is the period for the calculation, we will use 100 candles here because when you right-click on the indicator and select properties, you will see that 100 candles are the period for the calculation in the default value. And the last parameter is for a shift value, we could use that to move the Indicator to the left or to the right, but we don’t need to do that. We use “_Symbol” for the symbol on the chart, _Period for the selected period on the chart, this parameter is for our 100 candles and because we are not going to use any shift value the last parameter is zero. When the WPR value is below minus 80 – that would be the case when the blue line is below the dotted line here, that’s the minus 80 mark – we want to create a buy signal, and here is how that is done with MQL4: if WPR value is below minus 80 we want to create a buy signal, so we assign the word: “buy” to our string variable with the name signal. In the other case if the WPR value is above minus 20 – that’s whenever the blue line here crosses the upper dotted line – we want to create a sell signal. So, if WPR value is bigger than minus 20 we create a sell signal by assigning the word: “sell” to our signal variable. In the last step we are going to create a screen output by using the “comment” function and that will output the word WPR value followed by the calculated value. This will produce a new line and in the second line we want to have the text signal followed by our calculated signal. 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. If you don’t get any errors and no warnings here, you can click on a little button here or press F4 to go back to Metatrader. In Metatrader we are going to use a little trick, just click on “Insert /Indicators /Oscillators /Williams’ Percent Range” and click on OK. Now your chart should look like this, and you want to right-click, select “Template /Save template” and save it as tester.tpl because this is the template the strategy tester is going to use for your back test. If you are ready, please click on select “View/ Strategy Tester” or press CTRL and R. Now you should see the Strategy Tester panel here and you want to select the file: “SimpleWPR.ex4”, mark the visualization mode here and start your test. This is our Expert Advisor at work, it generates buy or sell signals on our chart and you have created it yourself with a few lines of MQL4 code.
    6 min
  • MQL4 TUTORIAL BASICS – SIMPLE ACCOUNT CHECK (translated with AI which is a bit scary)
    In this video we want to take a look at how you can limit the account number for the use of an Expert Advisor, so that only the account number can be used that you allow. So let’s take a look at how you can program something like this in MQL4. We are currently seeing an output on the chart that says the account number is wrong. And if you click on Journal here, we can also see that we have a license problem.   So let’s take a look at how you can program something like this in MQL4. Okay, this is now our simple example of an Expert Advisor that is able to check and limit an account number. We start here with the declaration for an integer variable called account number. Then comes the ontick function, which is always called up in the Expert Advisor when there is a change in the price. And here we use an if-question to call up the function account number and check whether it is correct or whether it is wrong. If it is correct, then we would get the output that the account number is correct. In the other case, if the return value is false, then we get the message that the account number is wrong. The actual function is a bool function, which means it can only return two values.   It has the name check account number and this addition here export, which would allow us to use the function in a library and import it into an Expert Advisor. We start by creating a bool variable with the name isLicensedAccount and giving the initial value here false. Then we get the account number with the function accountInfoInteger and transfer this to the parameter accountLogin. This returns the account number that is used and we now check here whether this account number matches the account number value that we transfer in our Expert Advisor. If that is the case, then we set our variable here to true and make an output that tells us that we are using the correct account number. In the other case, if our variable has the value wrong, then we use the print command to issue a license problem. And finally, we use the return function here to return the return value of our variables here to the onTick function, which then shows us the corresponding return value on the chart. By the way, this complete example would also work in mql5 and if you haven’t switched to MetaTrader 5 yet, it will be real time for that.   For the moment, let’s click on Compile or press the F7 key. This should work without errors and if that is the case with you, then you can click on this little button up here or press the F4 key to return to the MetaTrader. Okay, if everything worked well, then you should now have an Expert Advisor with which you can test it. I have the wrong account number here. We change that now, log in with the correct account and if we stop the test now and restart it again, then we should see the account number is correct, the entries in the journal also fit now and you learned in this short video how you can create an Expert Advisor with a few lines in mql4 that is able to ask for a certain account number to protect against the use of illegitimate account numbers.   And you programmed that yourself with these few lines here in mql4. That is how you can create an Expert Advisor with a few lines in mql4 that is able to ask for a certain account number to protect against the use of illegitimate account numbers. And you programmed that yourself with these few lines here in mql4. That is how you can create an Expert Advisor with a few lines in mql4. And you programmed that yourself with these few lines here in mql4.
    4 min
  • MQL4 TUTORIAL – SIMPLE IOSMA EXPERT ADVISOR
    In this video we want to calculate this oscillator here, it’s a Moving Average of Oscillator, that’s a strange name but actually it produces buy or sell signals whenever it crosses the 0 (zero) line here. So when it crosses from above this will create a sell signal and when it crosses from below 0 (zero) it creates a buy signal. Now how can we create an Expert Advisor that is able to output the buy or sell signals for our oscillator here? To do that please click on the little button here or press F4 on your keyboard and now you should see the Metaeditor window, until you want to click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleIOSMA” click on “Continue”, “Continue” and “Finish” and now you can delete everything above the “OnTick” function and the two comment lines here. First we need to create a string variable for the signal, this is called: “signal” and we don’t assign a value now, it’s a string variable because we are going to assign a text but before we can do that we need to calculate the IOSMA that is done by using the included function “iOsMA” that comes with MQL4. It takes a few parameters; the first one is for the current symbol – we use the constant “NULL” here –, the second is for the current time frame – that’s the number 0 –, when you click on the “ENUM_TIMEFRAMES” link here you will see that the 0 (zero) stands for the current period. We have values for 12, 26 and 9, that’s also what we see here and when you click on “Insert/ Indicators/ Oscillators/ Moving Average of Oscillator” you will see the fast EMA is 12, slow EMA is 26, and MACD SMA is 9, so that’s why we use this. The result will be calculated based on the close price and the 0 (zero) stands for the current candle but for a crossover we want to compare the current candle with the last candle. This line is exactly the same as the one above except of the last parameter because the candle here is candle 0 and the candle before is candle 1 and whenever we want to find a crossover we need to compare the two candles, so that’s why we calculate candle 1 here and here is how we use the values; if the last IOSMA value (IOSMAValue) was below 0 (zero) and if the current IOSMA value (IOSMAValue) is above 0 (zero) that would be a buy signal so we assign the word “buy” to our signal. In the other case if the last IOSMA value (IOSMAValue) was above 0 (zero) and if the current IOSMA value (IOSMAValue) is below 0 (zero) that would be a sell signal so we assign the word “sell” to our signal and in the last step we want to output the signal directly on our chart, so we use the built in “Comment” function to output the word: “Signal: “ followed by our “signal” that we have calculated. Okay, that’s it. Please click on the “Compile” button or press F7 on your keyboard; that should work without any errors here and in that case you can click on a little button here or press F4 to go back to Metatrader. In Metatrader we use a little trick; let’s click on “Insert/ Indicators/ Oscillators/ Moving Average of Oscillator”, go with the default values here and press “OK” and now the oscillator shows up, now let’s right click into the chart, select “Template/ Save Template” and save it as: “tester.tpl” because this is the template that our back-test is going to use now. If you don’t see the “Strategy Tester” please click on “View/ Strategy Tester” or press CTRL and R and here you want to pick the file: “SimpleIOSMA.ex4”, enable the visual mode here and start a test. …and here is our little Expert Advisor at work, it produces buy or sell signals directly on our chart and now you know how to code the Moving Average of Oscillator and you have done it with a few lines of MQL4 code.
    6 min
  • MQL4 TUTORIAL – 197 DAYS – REAL TRADING RESULTS WITH MQL5
    This video shows how 197 days of real trading turned out. Today is February 3, 2023, and I wanted to help my system work better. By accident, I just started a new trade here again. We’re going to end the trade now because I already reached my goal on February 1, 2023. The whole plan was to make a 10% profit and close the system. If you look at this, you can see that we just went over $11,000. Here in the order history, you can see that I put down $10,000 on July 18 (as far as I can remember), and on February 1, I made more money than I had planned. This is the corresponding statistic on my website. If you click here, you can go to the FX Blue website and see the exact statistics of all the data for this system. Here, the whole thing can be shown again as the total profit. Just now, on February 3, I closed the last trade. The first trade I made was on July 18, 2022. Overall, we’ve grown by 10%, and each month we’ve grown by 1.4%. Only 68% of all trades made money when the system was active, and on average, we made 12 trades per day for 199 days. If I hadn’t opened a position by accident when I started this video, it would only be 197 days. With the help of a calculator, if we divide 10% by 197 days and multiply by 365, we get an annual growth of 18.5%. What’s interesting to me is the equity issue, because you can see here that most of the time during the time we traded, the equity was very close to being in balance. That means the system didn’t have many drawdowns. You can see that we only traded in Euros and Dollars. That was 2408 orders, a gross profit of more than $2,000, and a gross loss of $1,112. With a profit factor of 1.9, the total profit was $1,001.28. Here are some more facts and figures. For example, the average number of hours it takes to trade. At 334.9 hours, this one stands out. But as a whole, you can say that most of them were less than 100 hours. fxblue’s numbers show that there is a 0% chance of losing everything. You can also look at 100 more facts and figures here. Those who want to can do it here on FX Blue or by calling the website PlatinSystem.com. This is currently being redirected, but you can look at the statistics here as well. And the link to the playlist is still somewhere down here. You have to turn on YouTube content here for GDPR, but once you do, you can look at the playlist. This is the very first video. Overall, I took a few notes on this, and I’ll add this video right now. I’m very happy with the results, and I am going to contunue to work on the system now.
    3 min
  • MQL4 TUTORIAL – SIMPLE MARKET TIME EXPERT ADVISOR
    In this little Expert Advisor we are going to find out if the market is closed or if it is open, like right now. The Expert Advisor contains some external variables where you can define when the market opening and closing time is for your country. In my case here in Germany the trading hours start at 9:00 am and they end at 5:30 pm – in German notation that is 17:30 so, when we see 9:00 this line here will change to “Market is open” and when it says 17:30 the “Market is closed”. Now how can we create an Expert Advisor that is able to read the local time and generate a signal for the market opening or closing time? Well, to do that please click on the little button here or press F4 on your keyboard. Now you should see the Metaeditor here and here you want to click on “File/ New/ Expert Advisor (template)” from template, continue — I will call this one: ”SimpleTime” — click on continue, continue and finish. Now you can delete everything above the “OnTick” function and the two comment lines here. Above the “OnTick” function we are going to define 2 string variables for the opening and closing time of the market. The modifier “extern” makes it possible that you will be able to modify the variable settings here in the Expert properties. Let’s also define two integer variables; one is for the index of the market opening time and the second one for the index of the market closing time. And I would also like to have a string variable for the market state. We will actually start with a closed market and in the “OnTick” function we need to find out the time with seconds and that can be done by using “TimeToStr” and we use “TimeLocal” we want to have the “TIME_DATE” and the “TIME_SECONDS”. Let’s press F1 and here you can see “TIME_DATE” will give you the year the month and the day and the output for “TIME_SECONDS” is hours minutes and seconds. That will give us the current time on our computer and now we want to find out if the opening time is in the time string, so we use “StringFind” to find the market opening time in the time with seconds that we have calculated here. The last parameter here is used for the starting position, we want to start to search from the first character in the string and we do exactly the same for the closing time. This time we use “StringFind” to find the market closing time in our time string and if the opening time was found the value for index market opening time will be greater than zero and in that case we want to set the market state to open, otherwise if the closing time was found the index market closing time is greater than zero and so we would set the market state to closed. Last but not least we use the “Comment” function to output the text “Time:” followed by the current time we have calculated and the text: “Market is” followed by the market state directly on our chart. So, let’s compile the code here by clicking on a little button here or pressing F7 and that worked without any errors and without any warnings so now we are finished and we can click on a little button here or press F4 to go back to Metatrader. In Metatrader you want to click on “View/ Strategy Tester” or press CTRL + R that should bring up the Strategy Tester panel and here you want to select the Expert Advisor called: “SimpleTime.ex4”, please make sure that you enable the visual mode here and start your test. Now you should see that the market is closed until the time reaches 9 am – that should be the case in a few seconds now and then it will change to market is open and it did. So, now you know how to find out the market open and closing time, maybe to use it for entry and exit signals and you have calculated it yourself with a few lines of MQL4 code.
    6 min
  • MQL4 TUTORIAL – SIMPLE CHART CLICK EVENT
    In this video I wanted to talk to you about something that is called an event. Maybe you know about events, they are pretty common in object-oriented programming. If you click on anything in your Metatrader, that’s actually an event. Pressing F2 on your keyboard will bring up the “History Center” and the reason is that; Metatrader is waiting for keystrokes, so if you press F2 it would bring up the “History Center” and if you press F4 you would see in the Metaeditor. Those events also exist on your chart, in this case I’m clicking on the chart here and here comes a message box that is telling me that the chart was clicked. It also tells me the X and the Y value. For example if I click in the upper left corner here you will see that the X value is 1 and Y value is -1. When I click the lower right corner here the value for X and Y is very high, it’s counting the number of pixels from the left upper corner. This is the X direction and this is the Y direction. So, if you know how to code an event you can use the information where somebody clicked to do something on your chart like placing a trade or setting a stop loss. Now how can we create an Expert Advisor that is looking for chart events? 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: Simple Chart Event (SimpleChartEvent). Click on continue. Continue and finish. Now you can remove everything that is above the “OnTick” function here and let’s remove the two comment lines. Usually we would start in the “OnTick” function here but this time we are not going to use it because “OnTick” is what happens whenever price changes. In our case we want to check for a chart event when a mouse is clicked. If you mark that and hit F1 you will see that the “OnChartEvent” function is the handler for a group of chart events. One event for example would be the key down event when you press a key. There are also events to change objects, and there even is an event to find out if you drag something on the chart. The “OnChartEvent” will take a few parameters here: the first parameter is the event ID and there are three more parameters called: “lparam”, “dparam” or “sparam” for long, double, or string. So let’s add these parameters here and like any function we need an opening and closing bracket and now we want to check if the “EventID” we have provided here equals the event “CHARTEVENT_CLICK”. Now let’s press F1, this is the event we are looking for. You could also check for other events like: mouse move and you could even create a custom event, but for this simple example we want to check if somebody clicked on the chart. If that is the case we want to output a message box and this message box will output the text: “Chart was clicked” followed by the X value – that’s the “lparam” – and the Y value – that’s the “dparam”. This one is the headline, so let’s replace that and we only need one button so with “MB_OK” we create just a single button. Let’s have a look at this one that’s the “MB_OK” button, you could create other buttons like: yes or no and based on what the button returns you could make another choice but one button and one option is enough for this simple example. So let’s compile the code by pressing the little button here or hitting F7 on your keyboard and we made a mistake, we shouldn’t use a hyphen here, so let’s replace this expression, recompile the code and now it says zero errors and no warnings! If that is the case for you please click on the little button here or press F4 to go back to Metatrader. In Metatrader you can select any chart you like, look for the simple chart event entry in the “Navigator” window, drag it on the chart, confirm by pressing “OK” and when you click on the chart now it should say that the chart was clicked and it should also tell you the X and the Y value for ...
    7 min
  • MQL4 Tutorial – Simple RSI Expert Advisor
    What you see here on the chart is the RSI indicator, it is a very popular indicator and whenever the blue line here crosses one of the dotted lines that will create a signal. Let’s speed it up a little bit! Now you see that the blue line is between the two dotted lines most of the time, but when it’s above the upper line that is considered to be a sell signal. In the other case if it’s below the lower dotted line that would be a buy signal and now we want to find out how we can create an Expert Advisor that is going to output buy or sell signals directly on our chart. To do that please click the little button here or press F4 on your keyboard! Now you should see the Metaeditor window here. Here you want to click on “File/ New/ Expert Advisor (template)” from template. Continue. I will call this one: Simple RSI (SimpleRSI). Continue, continue and finish. Now you can delete everything above the “OnTick” function and the two comment lines here. The first thing we need is to create a string variable for the signal – a string variable is for text – we will also call it signal, but we will not assign any text here, because that is what we are going to calculate later on. Now we want to calculate the RSI value that can be done by using the “iRSI” function that comes with MQL4. It needs some parameters. The first one is the symbol on the chart, the second one is the selected period on the chart; like one minute or one hour, the third parameter is the number of the candles that are used for the calculation. If you right click the indicator and select RSI properties you will see that the period is 14 candles in the default settings and the indicator is calculated based on the close price. So, that’s what we are going to use for the next parameter. The last parameter here is for a shift but we are not going to use it because we don’t want to move the indicator to the right or to the left. And now that we have calculated the RSI value we want to find out if the value is below 20 – that would be the case when the blue line here is below the lower dotted line. In MQL4 we can use the “if” statement to find out if the RSI value is below 20 and in that case, we would have a buy signal, so we assign the word “buy” to our signal. In the other case, if the RSI value is above 80; so, if RSI value is greater than 80 we consider that to be a sell signal and we assign the word “sell” to our signal. Let’s create a screen output! To do that we use the built-in comment function to output the text: RSI value (RSIValue) followed by the calculated value we have created here. This backslash n (“\n”) produces a new line and in that second line we output the text: “signal” followed by our signal. That’s actually it. Now click on “Compile” or press F7 and that should work without any errors or warnings, and in that case please click here on the little button or press F4 on your keyboard to go back to Metatrader. In Metatrader we use a little trick; please click on “Insert /Indicators /Oscillators /Relative Strength Index” and click on OK. Now the RSI window should appear on your chart and now you want to right click in the chart window, select “Template /Save Template” and save it as tester.tpl because that is the template that the strategy tester is going to use for your next back test. After you have done that please click on “View / Strategy Tester” or press CTRL + R and in the strategy tester you want to select the file: SimpleRSI.ex4, enable the visual mode here and start your test. Now you should see… Oh, something is going wrong here! The signal is always present. Obviously I have missed something, and yes, this is only a comment! Let’s add the “if” statement, so if RSI value is above 80 that would produce a sell signal. Let’s recompile the code! Go back to Metatrader. Stop the running test and start a new one. This looks much better! We don’t see a signal as long as the blue line is between the dotted lines here,
    7 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