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.

Download “MQL4 TUTORIAL - SIMPLE POSITION - MULTIPLIER” SimplePositionMultiplier.txt – Downloaded 31 times – 607 B

MQL4 Tutorial searches: