MQL5 TUTORIAL – SIMPLE POSITION MULTIPLIER

video
play-sharp-fill

In this video we are going to create a Simple Position Multiplier, right now we are trading 5 positions and in this video we are going to find out how to code something like this in MQL5.
To do that please click on the little button hero 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: “SimplePositionMultiplier”, click on “Continue”, “Continue” and “Finish” and now you can remove everything above the “OnTick” function and the two comment lines here.
The first thing we need is to import the trade library; that’s the file: “Trade.mqh” and we are going to create an external variable that can be adjusted by the user.
To do that we use the input modifier, it’s an integer variable with the name: “NumberOfPositions” and the default value will be 1 position that is allowed.
In the “OnTick” function we first need to get the “Ask” price. The “Ask” price is the price that we have to pay if we want to buy something and we get it by using “SymbolInfoDouble” for the current symbol and “SYMBOL_ASK” will provide the current ask price while “SYMBOL_BID” would give us the “Bid” price.
The “Bid” price is the price you get when you want to sell something and with “NormalizeDouble” and “_Digits” we make sure that we have the right number of digits behind the dot. You see we have currency pairs with 5 digits behind the dot and other currency pairs with 3 digits behind the dot.
Let’s use “MqlRates” to create a price array (PriceArray), that will be used to store our prices.
With “ArraySetAsSeries” we sort our price array (PriceArray) from the current candle downwards and now we can fill it with price data; that is done by using “CopyRates” for the current symbol and the currently on the chart selected period to fill our price array (PriceArray) from the current candle 0 for 3 candles.
If the last close price is higher than the close price before – so that would be the case if the close price for candle 2 is lower than the close price for candle 1 – we check if the number of trades is below the number of positions. That is done by using the built-in function: “PositionsTotal”; that will give us the number of the open positions and if the result is below the number of positions that we have defined here we will open a simple buy trade for 10 micro lots and set the “take profit” value to 150 points, that’s about it. If you are done you can click on the “Compile” button here…
Oh, I missed something…
I need to create an instance for “CTrade”, so let’s do that and re-compile the code and this time it works.
If that is the case for you please click on the little button here or press F4 on your keyboard to go back to Metatrader.
In Metatrader click on “View/ Strategy Tester” or press “CTRL and R”, we select the “SimplePositionMultiplayer.ex5” file we just have created.
Now click on the “Inputs” tab and change the variable: “NumberOfPositions” to 10, and here we are, our Expert Advisor is trading lot of positions at the same time and now you know how to create an Expert Advisor where you can adjust the number of positions that should be traded and you have coded it yourself with a few lines of MQL5 code.

Download “MQL5 TUTORIAL - SIMPLE POSITION MULTIPLIER”

SimplePositionMultiplier.txt – Downloaded 465 times – 968.00 B