In this video we are going set a sell limit.
A sell limit is a pending order far above the current Bid price, so let’s find out how to do that with MQL5.

In Metatrader first we need to click on a little symbol here or press F4. 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 that is above the “OnTick” function and let’s also remove the two comment line here.
We are stating with an import, we include the file: “Trade.mqh”, this one comes with MQL5 and Metatrader 5 and we use it to create an instance of “CTrade” that will be called: “trade” and we are going to use it later on to open our sell limit.

First we need to find out about the current Bid price, we do it by using “SymbolInfoDouble”; “_Symbol” is the first parameter, “SYMBOL_BID” – all in capital letters – is the second parameter, “NormalizeDouble” and “_Digits” that will automatically calculate the number of digits behind the dot, so for this currency pair it would return three digits behind the dot while other currency pairs would return five digits behind the dot.

Now that we have the Bid price we need to check another condition; I would like to know if we have any open orders or open positions, so if “OrdersTotal” equals 0 (zero) that would mean that we have no other pending order and if “PositionsTotal” equals 0 (zero) that means that we have no open positions and in that case we want to set our sell limit, so let’s add two curly braces here, we can use “trade.SellLimit” because we have created an instance called: “trade” and we need to pass a few parameters here;
The first parameter here is for the lot size, we are going to open a sell limit for 10 micro lot,
The second parameter is the price, our sell limit should be opened whenever the price rises 200 points above the current Bid price,
Parameter 3 will be used to automatically get the current symbol on the chart, in my case it’s the Euro-US Dollar,
The next parameter would be the stop loss, you could set it to over 200 points above the current Bid price but in this case I don’t use it so I use 0 (zero), what we do use is the take profit, we want to close the position when the price falls from 200 points above the Bid price to 100 points above the Bid price, “ORDER_TIME_GTC” stands for: “it is good until we cancel the order”, “ORDER_TIME_DAY” would be used if you would like to place an order that is just for the current trading day, this also is all in capital letters. We have no expiration date and so we have no expiration time, so this parameter is also 0 (zero).
And the last one could be used to set a comment, if you don’t pass a string variable here you can use “NULL” – this is also in capital letters – and we use it instead of a string value, that’s basically it, so when you are done you can click on the “Compile” button here or press F7, you shouldn’t get any errors and if that is the case you can click on a little button here or press F4 to go back to Metatrader. 

In Metatrader please click on: “View/ Strategy Tester” or press CTRL and R, here we want to pick the new file: “SimpleSellLimit.ex5”, please mark the visualization option here and start a test.

Now the Expert Advisor is running and we can see our sell limit here, it’s for 10 micro lots and when we zoom into the chart we see that it is far above the current Bid price and in this little video you have learned how to automatically set a sell limit and you have coded it yourself with a few lines of MQL5 code.



SimpleSellLimit.mq5 – Downloaded 67 times – 798.00 B