In this video we are going to find out how to create an automated sell order with MQL4 whenever the first bearish candle appears on the chart, so let’s find out how to do that.
Download “CODE - SIMPLE SELL ORDER”
To start please click on a 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: “SimpleSellOrder”, click on “ Continue”, “ Continue” and “Finish”.
We can delete everything that is above the “OnTick” function and let’s also remove the two comment lines.
Inside of the “OnTick” function we want to start by checking if we have no other open orders, that can be done by using “OrdersTotal” because “OrdersTotal” returns the number of market and pending orders in MQL4 and if this equals 0 (zero) that means that we have no open orders.
Afterwards we check if we have a bearish candle, that is the case if the close price for candle 1 is below the open price for candle 1, that would be the case for every white candle on the chart and if those two conditions are true we are actually going to sell, that is done by using the “OrderSend” command and this one takes a lot of parameters, so let’s find out what those parameters do.
The first one is used for the currency pair; “_Symbol” will automatically calculate that.
The second one; “OP_SELL” will tell Metatrader what to do; “OP_SELL” would be a sell operation while “OP_BUY” would buy.
The third parameter tells Metatrader how much we are going to sell.
Parameter four is the Bid price because we are going to sell for the Bid price.
Parameter five is the “slippage”, we use a low value of 3 here and the “slippage” is used to set boundaries because you don’t want to execute the order when the price has moved for 20 points or so, if you would set the value to 1 you would get lots of re-quotes.
Parameter six is the stop loss, if the price goes against us we want to be stopped out 300 points above the current Bid price.
The next one is for the take profit value; when the price falls for 150 points I want to take the profit.
This parameter could be used to pass a comment so you could use a string value to identify an Expert Advisor if you are going to use more than one Expert Advisor on the same chart. I rarely do it so I use the constant “NULL”, all capital letters here.
This one would be the magic number; that’s also to identify the Expert Advisor, you can think of the magic number like a license plate on your car.
This parameter is for the expiration; in our case its 0 (zero) because we are going to sell immediately but if we would use a sell stop or a sell limit we could set an expiration time and the last parameter here is a color, it’s green, and this is just the color for the arrow that is drawn on a chart whenever we sell something.
That’s it, so let’s click on the “Compile” button or press F7, you shouldn’t get any errors here; I have one warning because I didn’t check a return value but for this simple example we are done, so let’s click on the little button here or press F4 to go back to Metatrader.
In Metatrader you want to click on: “View/ Strategy Tester” or press CTRL and R, let’s pick the new file: “SimpleSellOrder.ex4”, mark the option for the visual mode here and start a test.
Here we are, this is the first bearish candle and now we see the first sell trade so our little Expert Advisor is working and in this little video you have learned how to automatically create a sell order with MQL4 as soon as the first bearish candle appears on your chart and you have coded it yourself with a few lines of MQL4 code.
Download “CODE - SIMPLE SELL ORDER”SimpleSellOrder.mq4 – Downloaded 157 times – 828.00 B