In this video, we want to create an Expert Advisor to place a sell limit order. This sell limit order is a pending order and it will be triggered when the price rises and crosses this line and as soon as that happens we will have a sell trade.
It might be a little hard to understand the sense, especially in the beginning. Why should anybody wait for the price to rise and sell afterwards, but in charting there are actually strategies where this makes sense.
For now, we want to answer the question: how can we create an Expert Advisor in MQL4 that is able to place such a pending sell limit order automatically?
To do that please click on the little button here or press F4 in your Metatrader. Now you should see the Metaeditor and here you want to click on File/ New/ Expert Advisor from template. Continue. I will call this one: Simple Sell Limit EA. Continue. Continue and finish.
Now everything above the OnTick function can be deleted and please also delete the two comment lines here.
The first thing we do is to check if no open order exists and that would be the case if the OrdersTotal function returns zero. In that case we want to send a sell limit order and we do that by using the OrderSend command, but actually we have to pass a few parameters.
So, let’s start the parameter list with a round bracket. The first parameter is the current currency pair on the chart. That is delivered with the MQL4 function: Symbol.
In the second parameter, we say that we want to trade a sell limit. That is done by using the constant: OP_SELLLIMIT. If you mark this constant and hit F1 you will see that there is also a buy limit, a buy stop or a sell stop. These four constants here are used for pending orders, while OP_BUY or OP_SELL would cause a direct order.
Now we have to tell MQL4 how much we want to order. In our case the position size will be 10 micro lot, the pending order will be placed 20 points above the current Ask price and if you click on one of the currency pairs here you will see that you have a red line and a blue line.
The red line is for the Ask price. That is the price we have to pay if we buy something. The blue line here is the Bid price, that is what we would get if we sell something.
We have to tell MQL4 how much movement and deviation is allowed. We only want to allow 3 pips, so if the price movement… so whenever the volatility is too high here our OrderSend command will not be triggered.
In this simple example, we don’t use a Stop Loss, so the next parameter is a zero. Our Take Profit will be 10 points below the Bid price. We don’t need a comment so we use the constant: NULL, we also use zero for the id number because we don’t need one.
The same is for the expiration date because we don’t want our Expert Advisor order to expire and last but not least, we pass the color green, to show a green arrow on the chart. If you mark that and press the F1 key you will see a lot of other colors that you could use.
Okay. We started our parameter list with a round bracket, so let’s add a closing round bracket and a semicolon. Please don’t forget the closing curly bracket and if you are finished you can click on the compile button or press F7.
You should have no errors and no warnings here and in that case, you can click on the little button here or press F4 to go back to Metatrader.
In Metatrader please click on: “View/ Strategy Tester” or press CTRL and R and now please pick the file: SimpleSellLimitEA.ex4, mark the visual mode option here and start your test.
Here is our pending order, you can see that Metatrader has placed a sell limit above the current Ask price and now you know how to create an EA to automatically trade sell limits in MQL4 and you have coded it yourself with a few lines of MQL4 code.
Download “MQL4 Tutorial - Simple Sell Limit EA”SimpleSellLimitEA.txt – Downloaded 2967 times – 679.00 B