In this video we are going to set a sell stop, you can’t see it in the chart but when we zoom in you will see that we have a pending order; a sell stop that is far below the current price, so let’s find out how to do that with MQL4.
First you need to 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: “SimpleSellStop”, click on: “Continue“, “Continue“ and “Finish”, now you can delete everything above the “OnTick” function and lets also remove the two comment lines here.
Inside of the “OnTick” function we want to check if we have no open orders and positions, so we use the function; “OrdersTotal” that will give us the number market and pending orders and if the return value is 0 (zero) we know that we have neither open orders nor positions and in that case we want to use “OrderSend” to create a sell ticket, we need to pass a few parameters, so let’s go through the parameters one by one:
The first one will deliver the currency pair that is used on our current chart, you can use “Symbol()” actually there are two versions; the first one is “_Symbol”, the second one is Symbol with two round braces (Symbol()) and that will return the currency pair – in my case it’s the Australian against the Canadian Dollar.
The next parameter is the action that we want to perform, in our case we want to place a sell stop (OP_SELLSTOP).
Parameter 3 stands for the lot size, we want to use 10 micro lot and this defines the entry price, so when the price moves to 100 points below the current Bid price we want to enter our sell trade.
This is the slippage – that’s the tolerance – don’t set this value to 0 (zero) because you will get a lot of requites from your broker.
This 0 (zero) here the next parameter is the stop loss, I didn’t use one here but I did use a take profit value, so we enter when the price is 100 points below the current Bid price and we would take our profit when the price moves to 200 points below the Bid price and because this is a sell trade it would give us 100 points profit because the difference between the take profit and the entry price is 100 points.
This constant: “NULL” all in capital letters – is used because we don’t have a comment. If we want to set a comment we could do that by passing a string value here.
The next one is called: “magic number” that’s kind of an or ID number like a license plate, you would use it if you want to identify your Expert Advisor among other Expert Advisors on the same chart, not needed in this case.
This parameter could be used to set an expiration date, if you set it to 0 (zero) the order will stay in the market until it is filled or until you delete it and the last parameter here “Green” is the color for the arrow that is drawn on the chart as soon as we open the sell trade and that’s about it.
So let’s click on the “Compile” button or press F7, you shouldn’t get any errors or warnings here and if that is the case you can click on the little symbol here or press a F4 to go back to Metatrader.
And in Metatrader you want to click on: “View/ Strategy Tester” or press CTRL and R, here we pick the new file: “SimpleSellStop.ex4”, please mark the visual mode here and start a test.
…here we are, the Expert Advisor is running, in the results you can see that we have a sell stop for 10 micro lot and when we zoom into the chart we can see that we have automatically placed a sell stop far below the current price and in this little video you have learned how to do that with MQL4 and you have coded it yourself with a few lines of MQL4 code.


SimpleSellStop.mq4 – Downloaded 302 times – 1.26 KB