In this video we are going to set a pending order, a so called sell limit. That’s a pending order that is placed above the current price level; you can see it when we zoom into the chart and as soon as the price crosses the green line here that would open a sell trade, so let’s find out how to do that with MQL4.
Download “CODE - SIMPLE SELL LIMIT”
First we need to 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: “SimpleSellLimit”, click on: “Continue“, “Continue“ and “Finish”, now we can delete everything above the “OnTick” function and the two comment lines here.
Before we open our pending order we want to check if we have no other open positions or orders, when we use “OrdersTotal” we can get the number of open orders and positions and if that equals 0 (zero) that would mean that no other open positions or orders exist.
An order is something you place into the market that can become a position and that’s also the case for our sell limit. To place our order we will use “OrderSend”, that will require some parameters, so let’s go through the parameters one by one:
The first one is for the currency pair on the chart, I have used symbol and two round braces here (Symbol()) but according to the “Reference” we could also use the “_Symbol” function, both functions will return the current currency pair on the chart – in my case it’s the Australian against the Canadian Dollar.
Parameter 2 is for what we actually want to do; we want to open a sell limit so we use “OP_SELLLIMIT” – all in capital letters.
Parameter 3 is for the lot size, in our case its 10 micro lot, so we pass 0.10.
Parameter 4 is the price level that we are going to use to place our sell ticket, the current price is the Bid price and when the price rises 200 points above that level we want to open our sell position.
In the next parameter we define a slippage – I use a value of 3 here – if the price moves more than 3 pips away from the current price I don’t want to place the order. If you use a value that is too low you might get a lot of requites from your broker and if you set the value too high you might get a price level that you don’t want.
The next parameter is used to place a stop loss, I don’t use one here so I set it to 0 (zero), if you want to use one you need to use something that is above Bid plus 200 points. Next is the value for the take profit, we want to sell at 200 points above the Bid price and when the price drops for 100 points we will take the profit.
In the next parameter we use “NULL” with all capital letters, otherwise we could pass a string value here, and this is followed by the so-called magic number or ID number, that’s kind of a license plate that you could use if you want to identify an Expert Advisor on the chart. I rarely use more than one Expert Advisor, so I don’t use it.
This is followed by the expiration date, I have set the value to 0 (zero) because I don’t want my sell ticket to expire and the last parameter is used to set the color of the arrow that is going to be drawn when you open the sell ticket and that’s about it.
When you’re done you can click on the “Compile” button here or press F7 on your keyboard, this worked without any errors and any warnings and if that is the case for you, you can click on a little button here or press a F4 to go back to Metatrader.
And in Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, please pick the new file: “SimpleSellLimit.ex4”, mark the visual mode here and start your test.
…here we are, we don’t see anything but when we zoom into the chart you will see the sell limit has been placed. In this little video you have learned how to automatically do that and you have coded it yourself with a few lines of MQL4 code.
Download “CODE - SIMPLE SELL LIMIT”SimpleSellLimit.mq4 – Downloaded 146 times – 783 B