MQL5 Tutorial

MQL5 Tutorial

By Raimund BauerBusinessTechnologyInvesting
Download on the App Store

All MQL5 Tutorial episodes

  • MQL5 TUTORIAL BASICS – 2 WHAT ARE FUNCTIONS
    In this video we want to talk about included functions. Whenever something is automated with MQL5 there are functions involved and we have a few functions that are already included in our Expert Advisor template here; the first one is the “OnInit” function, it’s the expert initialization function and it will be called when you drop your Expert Advisor on to a chart, so this one will only run once, it does have a return value here, basically you could check if the “OnInit” function was successful. It will return one of these values here and that would tell us if the “init” process was successful or if it failed. We also have another function here that is called: “OnDeinit”, it is use to clean up before the Expert Advisor is closed, it doesn’t return anything, that’s what “void” is saying – “void” is used whenever something doesn’t return a value – in this case the reason is very simple, “OnDeinit” is the last thing that will run in our program and there is nothing that we can return to anything to because its closed afterwards. This is the “OnTick” function, this function runs whenever the price changes on the chart, most of the coding logic would be triggered by the “OnTick” function and whenever you see a price change on one of your charts everything between the two brackets here will be executed. We can now remove the rest of the template here because for our simple example we just need the “OnTick” function and we will use it to output the local time and that’s done by calling another function. Whenever we want to see an output on our chart we use the “Comment” function, this one will output a user value on the left corner of the chart, takes so called parameters in round brackets, that could be a text, like “Hello” and this is already a complete Expert Advisor. We now could compile it, click on the little button here to go back to Metatrader and when I take the “SimpleExpertAdvisor” and drag it on the chart and confirm it with the “OK” button it will output the text: “Hello” that we have defined here. But the “Comment” function can do more, remember we wanted to output the local time, so let’s use the words: “The local time is ” and with a comma we can pass another parameter, in our case it’s “TimeLocal”, so let’s recompile the code again and now the Expert Advisor says: “The local time is” and we also get the output of the local time right away on our chart. In this video you have learned about built-in functions, like the “OnTick” function that is called whenever the price changes and you have also learned how to use the “Comment” function to output a text followed by a calculated value to make Metatrader 5 print out the text: “The local time is” followed by the calculated time directly on your chart and you have done it yourself with a few lines of MQL5 code.
    5 min
  • MQL5 TUTORIAL BASICS – 1 HOW TO CREATE A SIMPLE EXPERT ADVISOR
    What we see here is a so-called Expert Advisor, that’s an automated program that can run inside of Metatrader and it is able to open and close positions and in this video we are going to find out how to create a very basic simple version of an Expert Advisor. To do that please click on the little button here or press 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 version: “SimpleExpertAdvisor”, click on “Continue”, “Continue” and “Finish”, and here we are! The Metaeditor has created this template for us and we can already compile it by clicking on the button here or pressing F7, this version is readable for humans and when we compile it we create a readable version for the Metatrader, it’s already available here so let’s open a new chart window we can drag our new file on the chart, so far it doesn’t contain any logic and that’s what we are going to add in the next few videos.
    2 min
  • MQL5 TUTORIAL – HOW TO SETUP A TRADING SERVER WITH AMAZON WEB SERVICES
    In this video we are going to set up a trading computer on Amazon Web Services, they have a service that is a good idea for people who want to have a machine running in the cloud 24/7. I’m doing a lot of mass testing, this is a visual test, it takes some time, I disable the visualization here and start another test, this is much faster. In my case I’m trading the newest version of the Robot Trading System, using a random entry but I really like to do mass testing in a very big data like way, let’s stop that, change the optimization settings to “Fast generic based algorithm”. Here I can select one parameter that I want to mass test, in my case it’s a value that should start at 1 and end at 100, so I will have 100 mass tests and this is one of those mass test results, all those dots here would represent the end result. Everything above this line here is the profit; everything below this line is a loss. These are all my results and to speed up the whole process I would like to use a cloud-based system from Amazon Web Services, so let’s find out how to set up such a system. To do that we go to aws.amazon.com, I have already credentials so I can click on the “Sign In” button and here you will find a lot of services, in my case I’m looking for WorkSpaces, a desktop that is hosted in the cloud, so let’s get started, I would like to choose the quick setup, let’s pick the Windows 10 machine with 4 gigabytes of RAM, I fill in my credentials and click on “Launch WorkSpaces” and that’s it. It will take a few minutes, this is the part where you can manage your WorkSpaces instances and it may take up to 20 minutes to become available, this instance will run in North Virginia. Depending on the area you pick you might pay different prices. Let’s go to Google, and Google for pricing and I learn that I have monthly or hourly billing options and here are all the prices for North Virginia, I could pick one of those options here, for example the 4 GiB machine could be hired for 35 Dollars a month, or I can pick the hourly pricing, in that case the monthly fee is much cheaper but I will pay additional 30 cents per hour. My workspace is being launched so I will continue when it becomes available. So here we are, the workspace is ready, it’s available now, I could now change the settings by clicking on: “Actions/ Modify WorkSpace”, here I could pick a different compute type, for example I could use the 16-GiB machine, this is more costly but it also will be much faster. Here are all the details, for example my user credentials and some other stuff, the “Running Mode Properties” will shut down the machine when it isn’t used for an hour, I could modify the settings to “AlwaysOn”, in that case I would be billed monthly, that’s the one to choose for a trading server, so let’s download the WorkSpaces client, I’m using Windows but you could also use any of the listed devices here, let’s install the machine, it’s connecting, Amazon did sent me an email, I need to click on this link here, here I can set a password, now I click on “Update User”. Oh, I need to include uppercase characters so let’s do that, click on “Update User” and now I will try to log in, I want to be remembered on my PC, now it’s launching a session and here we are! My WorkSpaces instance is up and running, let’s go to the “Control Panel” and it says right now I’m running a machine with 4 gigabyte of RAM, my Windows license already is activated, Firefox is also pre-installed so let’s go to metatrader5.com and from here I can start the installation process and set up everything on my trading server. Okay, so now you know how to set up an Amazon WorkSpaces client to run your own trading server in the cloud and you have installed it within a few minutes. After the installation is finished, you can do your back testing on the Amazon Workspaces server if you need any further information about the installation process for a Metatrader 4 or Metatrader 5 just go to one of my websites,
    7 min
  • MQL5 TUTORIAL – AUTOMATED STATISTICS WITH MYFXBOOK
    This is one of my longtime testing accounts, it’s for the English Framework version that we have created in the MQL5 Premium Course, sorry for my voice I was ill for about one week but I wanted to show you how to connect your account to advanced statistics. Right now it’s uploading a statement to Myfxbook, I’m running the Expert Advisor version 1.62 from Myfxbook on one of my charts here, so let’s open a browser, you are probably used to these statistics here, that’s what you can export when you use Metatrader. The Strategy Tester report will tell you about some of your results but it’s only good for strategy tests. This is the website: myfixbook.com and here you can see on my real-time results – in this case we are talking about the Forex Trading Framework in English – the statistics have been updated 12 minutes ago. This is the German version and it has been updated 1 minute ago. Now let’s open a new account, I would like a 100,000 Dollars account, let’s pick a password and an investor password, this one is the one you use to open trades and the investor password is the one to give away if somebody wants to check your statistics. Now let’s confirm that, here is the account number, right click on “Accounts/ Login to Trade Account”, paste the number and password here and click on “OK” and now we have a second connection here. Our balance is 100,000 hundred thousand US Dollars, you will see the login form, I choose a username, email address, a password and I also need to fill in the CAPTCHA, I agree, “Sign Me Up” and now it says: “The account was created successfully, please check your email for the activation link.” Here we have the activation link, let’s copy that and paste it here, and now it says: “Congratulations! You have successfully activated your account.” Now I can click on: “Portfolio/ Add Account”, currently I don’t have any accounts, so let’s click on the “Add Account” button, my platform is Metatrader 5 and it downloads the Expert Advisor installer, let’s double click that, it says: “Drag and drop the files, shortcuts below for the Metatrader 5 installation” that I have already on my PC, now I can drag the icon into the window, in my case it’s already installed. When I scroll down in the “Navigator” I see the Myfxbook Expert Advisor below the “Examples” folder, so let’s drag that onto the chart, click “Yes”, allow DLL imports and type in the user credentials for Myfxbook, confirm everything with the “OK” button, let’s change the timeframe here, you will also see the description when you click on the link: “How to add an account?”, this is the path that will appear in the window once you have dropped your icon here, Metatrader 5 also needs to be restarted and now we get a “System Added” notification and here is our new account. So far I haven’t made any trades, the statement was updated successfully, so let’s open a new trade here, my system also opened to automated positions right now, in the ”Experts” list I can also see the “Properties”, it may take some time but now you know how to add your own Metatrader 5 account to create advanced statistics on Myfxbook.com. I started to use it in 2014, this is the Brexit system, it’s so far made 192% since June 22nd 2016, that’s the evening before the Brexit Votum, it made 8577 trades and I can now see the statistics for each and every trade for the last 2 and a half years. Now I’m back in the Forex Trading Framework account, we see a balance of 429,643 Dollars and if you look at the Myfxbook site you will see 429,643 Dollars, the last update was 44 minutes ago and now you should see an update within the next few minutes. Let’s refresh the page, it now says: “Uploading statement”, “Statement uploaded successfully”, it’s about 20 minutes to 4 so let’s go back to Myfxbook, let’s refresh the page and now it says its 46 seconds ago. Okay, now you know how to set up your MQL5 account for Myfxbook and how to upload your statistics by using the Myfxbook Expert Advisor to automa...
    7 min
  • MQL5 TUTORIAL – SIMPLE ON BALANCE VOLUME
    In this video we are looking at this little Indicator, it’s the Onbalance Volume Indicator. Investopedia (www.investopedia.com) says that the Onbalance Volume should show us that whenever the Indicator increases without a significant price change the price should follow and now we are going to find out how to automate the Onbalance Volume Indicator in MQL5. To do that please click on a little button here or press F4 in your Metatrader, now you should see the Metaeditor window and here we click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleIOBValue”, click on “Continue”, “Continue” and “Finish”. Now you can delete everything above the “OnTick” function and let’s also remove the two comment lines here. This is a very simple Indicator, we start by creating an array for the Expert Advisor (EAArray), this one will be a double array so it can hold floating type values, now we use “ArraySetAsSeries” to sort the array (EAArray) from the current candle downwards and to define the Onbalance Volume we use the included function that is called: “iOBV”, so let’s mark that and press F1, here we see that it uses the current symbol on the chart, the currently selected period on the chart and the third parameter is called: applied volume so let’s click on this link and we see that we could use the tick volume or the real volume. In our case we use “VOLUME_TICK”. Now we use “CopyBuffer” to fill our Expert Advisor array (EAArray) according to the IOBV definition (IOBVDefinition) that we have created here for the first buffer… well, a buffer is in our case this line here, some Indicators have more than one buffer for example if I would “Insert/ Indicators/ Trend/ Bollinger Bands”, this one has three different lines, so it uses 3 buffers and the first buffers is always called buffer 0 (zero), the second buffer would be number one and so on. That’s because arrays are zero based in MQL5 so the first item in an array is always called by calling the index zero. Let’s copy the data for 3 candles and the result will be stored in our EA array (EAArray). To calculate the current Onbalance Value (IOBVVALUE) we now need to look at the value for candle 0 (zero) in our EA array (EAArray), remember this one is the current candle so this would be candle 0 (zero), candle 1, candle 2, and so on. Finally we want to create a chart output that’s done with the “Comment” function and it will output the words: “The IOB Value is:” followed by the calculated value, so let’s compile that and this worked without any errors, we only have a minor warning – you see the little triangle here – I have used an int value, let’s try double recompile the code and now the warning is gone, so now that everything is finished 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”, I select a new file: “SimpleIOBValue”, mark the visualization option here and start a test. …and here we are, this is the value and it’s also drawn on the chart here as you can see, it has one digit behind the dot and this digit is always zero so if I use an integer here recompile the code I will get the warning again but when I restart the test you will see that the digit and the dot are gone, I prefer it this way. …and now you know how to calculate the Onbalance Volume and you have coded it yourself with a few lines of MQL5 code.
    6 min
  • MQL5 TUTORIAL – ADVANCED IFORCE ENTRY
    In this video we are going to trade this Indicator; it’s the Force Index Oscillator, I’m just now using the Robot Trading System and I would like to show you how to trade the Force Index for 10 years. This is an entry module that I’m going to use in the Robot Trading System you could also use it in your own system, so let’s find out how to code this in MQL5! To do that please click on the little button here or press F4 in your Metatrader, now you should see the Metaeditor, in my case I have loaded the Robot Trading System, you see I have all these entries here and this one is the one we are going to create right now. To do that please click on: “File/ New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “CheckEntry_IForce”, click on “Continue”, “Continue” and “Finish”. Now you should see something like this, here we can delete everything above the “OnTick” function and the two comment lines and to create this entry module (CheckEntry_IForce) we first need to create a string variable for the signal, that will also be called: “signal” but we don’t assign a value here. The next thing we need to do is to create an array, we use “MqlRates” to create a price array (PriceArray) because “MqlRates” stores the information about price, volumes and spread. Afterwards we sort the price array (PriceArray) from the current candle downwards by using “ArraySetAsSeries” for our price array (PriceArray). Now we fill the price array (PriceArray) with data for 3 candles by using “CopyRates” for the current symbol and the currently selected period on the chart. We start with the current candle 0 (zero), copy the prices for 3 candles and store it in the price array (PriceArray). Now we need to create a second array, this one is for the Expert Advisor data (myPriceArray), let’s define the properties for the Force Index EA by using the built-in MQL5 function: “IForce”, this one also needs a few parameters: the first one is for the symbol, the second one is for the period, here we have 13 because if we click on: “Insert/ Indicators/ Oscillators/ Force Index”, you will see that it is calculated based on 13 candles, that’s what we use here. The last two parameters are: “MODE_SMA” and “VOLUME_TICK” because the calculation is done for a Simple Moving Average and we used the tick volume here. We also use “ArraySetAsSeries” for the second price array (myPriceArray) to sort it from the current candle downwards and with “CopyBuffer” we fill our price array (myPriceArray) with data according to the Force Index definition (ForceIndexDefinition) that we have created here. We do it for the first buffer – that’s buffer 0 (zero) – from the current candle 0 (zero) for 3 candles. Let’s get the value for the current candle of the Expert Advisor, we will assign it to the value: “ForceIndexValue” and to get that value we just need to look at candle 0 (zero) in our price array (myPriceArray). We use “NormalizeDouble” and a 6 because the Force Index has six digits behind the dot. Now that we have calculated the value we just look if the Force Index value (ForceIndexValue) is below zero, in that case we would assign a “buy” signal to our signal variable, otherwise if the Force Index value (ForceIndexValue) is above zero we assign the word: “sell” to our signal. In the last step we are going to return the signal to our main module, in my case it’s the Robot Trading System, it can trade all these entry modules here. If you don’t have a system you can go to: robottradingsystem.com or you could create your own version. Now let’s compile the system. I’ve got a warning here that: “it’s not compatible with MQL5 market” but that’s not important for me and because we had no errors I can now click on the button here or press F4 to go back to Metatrader. In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, here we pick the main module, in my case it’s the Robot Trading System, let’s enable the visualization mode here and start a test....
    8 min
  • MQL5 TUTORIAL – SIMPLE SHIFTED SMA EXPERT ADVISOR
    In this video we are going to create an Expert Advisor that is going to output buy or sell signals based on the SMA (Simple Moving Average) here, it’s a Moving Average signal that is created for 50 candles, this one is the current signal and this one is the signal predicted into the future and whenever those two lines cross we either get a buy or a sell signal. When the future SMA is below the other one, we have a buy signal and when the current SMA crosses the future SMA and is below it – like here – we have a sell signal. To create the Expert Advisor 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, please click on “Continue”, I will call this one: “SimpleShiftedSMA”, click on “Continue”, “Continue” and “Finish”. Now we can delete everything above the “OnTick” function and the two comment lines here. We start by creating a string for the signal, this one will also be called: “signal” and we don’t assign a value right now because this one needs to be calculated later on. Let’s create two arrays; one for the current Moving Average and one for the shifted Moving Average, afterwards we define the properties for the Moving Average by using the built in “iMA” function that comes with MQL5 for the current symbol on the chart and the currently selected period, the calculation will be based on 50 candles, here we use a shift value of 0 (zero), the calculation will be for an SMA so we use “MODE_SMA”, that stands for Simple Moving Average and the result will be based on the close price. The second calculation looks almost like the first one, we have two differences here; the first difference is that this one is for the shifted average definition (ShiftedAverageDefinition) and the second one is that we use a shift value of 20 here, this will move our SMA 20 candles into the future, so when we pass this you will see that this is the current candle, this is the current value and this one is the predicted value that is shifted into the future. For both of our arrays we use “ArraySetAsSeries” to sort the current array from the current candle downwards here for the Moving Average array (myMovingAverageArray) and here for the shifted average array (myShiftedAverageAttay) and with “CopyBuffer” we fill our array according to the definition that we have created here for the current buffer 0 (zero) – that’s the first line for our Indicator – from the current candle 0 (zero) for 3 candles and we store the result in our Moving Average array (myMovingAverageArray). For the shifted average it’s the same, the only difference is that the value will be stored in the shifted average array (myShiftedAverageAttay). To get both values we look at candle 0 (zero) either in the Moving Average array (myMovingAverageArray) or in the shifted Average array (myShiftedAverageAttay) and if the average array value (myMovingAverageValue) is above the shifted average array value (myShiftedAverageValue) we would assign the word: “buy” to our signal. In the other case if the average array value (myMovingAverageValue) is below the shifted average value (myShiftedAverageValue) that would be a sell signal so now we assign the word: “sell” to our signal. Last but not least we use the “Comment” function to output the words: “The current signal is:” followed by our current “signal” directly on the chart. Okay, when you’re done you can click on the “Compile” button or press F7, that should work without any errors and if that is the case you can click here or press F4 to go back to Metatrader and in Metatrader we click on: “View/ Strategy Tester”, here we pick the file: “SimpleShiftedSMA.ex5”, mark the visualization option and start our test. …and here we are, currently we have an uptrend, the shifted value is below the current value so that’s a buy signal and as soon as we see a crossover here – like right now – we get a sell signa...
    6 min
  • MQL5 Tutorial – Simple Sell Stop Expert Advisor
    A sell stop is an order that has been placed into the market and that will be triggered whenever the price is falling below the price level that we have defined, this one has already been triggered, so let’s see if we can create another one. Here you see that we have placed a sell stop order and as soon as the price reaches the price level that we have defined a sell position is opened. Now how can we create an Expert Advisor that is able to place such a sell stop? 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: “SimpleSellStop”, click on “Continue”, “Continue” and “Finish”. Now we can delete everything above the “OnTick” function and the two comment lines here. We are going to start by using the “include” statement to include the file “Trade.mqh”, this one comes with MQL5 and we use the included “CTrade” class to create an instance that will be called: “trade” and we are going to use it to open our sell stop later on. Inside of the “OnTick” function we get the Bid price, that is done by using “SymbolInfoDouble” for the current symbol on the chart, we use “SYMBOL_BID” to get the Bid price and with “NormalizeDouble” and “_Digits” we make sure that we calculate the right amount of digits behind the dot, that could be 3 digits like for this currency pair or 5 digits like for this one. Let’s get the account balance by using “AccountInfoDouble”, “ACCOUNT_BALANCE” and we also want to calculate the equity by using “AccountInfoDouble” ,“ACCOUNT_EQUITY”, so if you mark “AccountInfoDouble” and press F1 you will see examples and here are all the values that you could use. This is all account related information.  Now If we have no open positions and no open orders, so if “PositionsTotal” equals 0 (zero) and “OrdersTotal” also equals 0 (zero) we want to open a new order for a sell stop for 10 micro lot. The difference between a position and the order is that a position is something that has already been triggered, so this is a sell position but when the position is closed we place an order below the current price level and as soon our order is triggered it becomes a position. To do that we use “trade.SellStop” to open an order for 10 micro lot, 100 points below the current price level, we don’t use a stop loss here and the take profit should be 300 points below the current Bid price, the last parameters here are used to set an expiration or a comment but we don’t need that. Instead we use the “Comment” function to output the current balance and the current equity directly on our chart. Okay, that’s it. Please click on the “Compile” button or press F7, and if you don’t have any errors here please click here or press F4 to go back to Metatrader. Now in Metatrader we click on; “View/ Strategy Tester” or we press CTRL and R, let’s pick the new file here: “SimplesellStop.ex5”, mark the visualization option and start your test. …and here we are! This is our sell stop and it will be triggered as soon as the price level drops below this line here, that has happened now and here is our first open position. Now you know how to code an Expert Advisor that is going to create automated sell stops and you have done it yourself with a few lines of MQL5 code.
    6 min
  • MQL5 TUTORIAL – SIMPLE TENKANSEN STANDALONE EXPERT ADVISOR
    In this video we are going to create buy and sell signals and code a standalone version that is able to calculate this one line from the Ichimoku Kinko Hyo, it’s called a Tenkan-sen, that’s the red line here. Now we want to find out how to do that in MQL5? To do that please click on the little icon here or press F4 on your keyboard now you should see the Metaeditor window and here you want to click on: ”File/ New/ Expert Advisor (template)” from template, “ Continue”, I will call this file: “SimpleTenkansenStandaloneEA”, click on “Continue”, “Continue” and “Finish”. Now you can delete everything above the “OnTick” function and the two comment lines here. We start with the “include” statement to include the file “Trade.mqh”, that comes with MQL5 and we need it to create an instance of “CTrade” that will be called: “trade” and we will use it to open and close positions later on. We start by calculating the Ask price and the Bid price, this is done by using “SymbolInfoDouble” for the current symbol on the chart, we use “SYMBOL_ASK” to calculate the Ask price and “SYMBOL_BID” to calculate the Bid price. “NormalizeDouble” and “_Digits” will automatically calculate the right number of digits behind the dot. Next we need to create a signal variable for the signal that also will be called: “signal” and we don’t assign a value here. We use “MqlRates” to create a price array called: “PriceInformation”, that one will contain the prices of the candles, we use “ArraySetAsSeries” to sort the array (PriceInformation) from the current candle downwards and with “CopyRates” we fill the price array (PriceInformation) with data for the current symbol on the chart and the currently selected period on the chart. We start from the candle 0 (zero) and copy the price information for 3 candles into our price array (PriceInformation). For the Tenkan-sen we need to create another array and we use the MQL5 function: “iIchimoku”, this one can calculate the Tenkan-sen, the Kijun-sen, the Senkou Span depending on the index buffer that we use, buffer 0 (zero) is for the Tenkan-sen, buffer 1 is for the Kijun-sen and so on. For the parameters we use the current symbol, the currently selected period on the chart, here we have three more values; 9, 26 and 52 and when we click on: “Insert/ Indicators/ Trend/ Ichimoku Kinko Hyo” we will see the same values here so that’s how we use them. 9 is for the Tenkan-sen, 26 is for the Kijun-sen and 52 is for the Senkou Span B. In our example we only calculate one of them; that’s the Tenkan-sen, so let’s use “ArraySetAsSeries” to sort our Tenkan-sen array (TenkansenArray) from the current candle downwards and with “CopyBuffer” we fill our Tenkan-sen array (TenkansenArray) according to the Ichimoku definition (IchimokuDefinition) that we have created here for buffer 0 (zero), we start with the current candle 0 (zero) and we use the data for 3 candles. Now we can calculate the Tenkan-sen value (TenkansenValue) by looking at candle 0 (zero) in our “TenkansenArray”. For our simplified example we would say that whenever the value is above, so the “ThenkansenValue” would be greater than the close price for candle 1 in our “PriceInformation” array, we would consider that to be a sell signal and so we assign the word: “sell” to or signal. In the other case if the “TenkansenValue” is below the “PriceInformation” close price for candle 1 we would consider that to be a buy signal and now we would assign the word: “buy” to our signal. And when the signal equals sell and we have no open positions that’s when we would use “trade.Sell” to sell 10 micro lot, otherwise if the signal equals buy and we have no open positions we would use: “trade.Buy” and buy 10 micro lot. We finish by creating a chart output using the “Comment” function to output the text: “The current signal is:” followed by the calculated “signal”. Now this is only one of several values that you could calculate for this Indicator,
    7 min
  • MQL5 TUTORIAL – SIMPLE BEARS POWER STANDALONE EXPERT ADVISOR
    In this video we are going to talk about this Indicator, it’s the Bears Power Indicator and we want to create an Expert Advisor that is able to actually produce signals that will be seen right here on the chart and in this case we use the Bears Power Indicator tool trade automated. Usually this Indicator is used as a filter and you would use another signal and only when the signal and the filter are pointing in the right direction you would open a new position. To code that please click on the little button here or press F4 on your keyboard, 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 one: “SimpleBearsPowerStandaloneEA”, click on “Continue”, “Continue” and “Finish”, now you can delete everything above the “OnTick” function and the two comment lines here. We start by including the file: “Trade.mqh”, this one comes with MQL5 and it contains some trading functions. We need to create an instance from the class: “CTrade” that will be called: ”trade” and we are going to use it to open positions later on. Inside of the “OnTick” function we calculate the Ask price and the Bid price, that’s done by using “SymbolInfoDouble” for the current symbol on the chart, “SYMBOL_ASK” will give us the Ask price while “SYMBOL_BID” will give us the Bid price and with “NormalizeDouble” and “_Digits” we calculate the right number of digits after the decimal point. We also need to create a string for the signal, that string variable will also be called: ”signal” and so far we don’t have any value because that is going to be calculated later. Let’s create an array for the price data that will be called: “PriceInformation” and we use “MqlRates” to create it because “MqlRates” is the structure that stores the information about prices, volumes and spread. We sort the array downwards from the current candle by using “ArraySetAsSeries” and with “CopyRates” we fill the array (PriceInformation) with price data for the current symbol on the chart and the currently selected period. We start with the current candle 0 (zero) and we need price data for 3 candles – that’s enough – and store the result in our price information array (PriceInformation). Now let’s create another array called: “myPriceArray” this one is for the Expert Advisor data, the other one was for the candle prices. The second array also needs to be sorted downwards by using “ArraySetAsSeries”. Now we use the “iBearsPower” function that comes with MQL5 for the current symbol and the currently selected period on the chart, the last parameter 13, is the number of candles that you also will see when you drag the Bears Power Oscillator on your chart. Now we use “CopyBuffer” for the bears power definition (BearsPowerDefinition) that we have created here to calculate the values for buffer 0 (zero) – that’s the first buffer – from the current candle 0 (zero) for 3 candles and store the result in our price array (myPriceArray) that makes it possible to calculate the current array value by looking at candle 0 (zero) in our price array (myPriceArray) and we store a result in the bears power value (BearsPowerValue) and if the “BearsPowerValue” is above 0 we would consider that to be a buy signal so we assign the word: “buy” to our signal. Otherwise if the “BearsPowerValue” is below 0 we consider that to be a sell signal so now we assign the word: “sell” to our signal and if the signal equals sell and we have no open positions we use “trade.Sell” to sell 10 micro lot. Otherwise if the signal equals buy and we have no open positions we would use “trade.Buy” and buy 10 micro lot. Finally we create a chart output that is done by using the “Comment” function and we want to output the words: “The current signal is:” followed by the calculated “signal”. Now let’s compile the code, we don’t have any errors and in that case we can click here or press F4 to go back to Metatrader.
    7 min