MQL4 TUTORIAL BASICS – 15 HOW TO SET A BUY STOP

video
play-sharp-fill

In this video we are going to set a buy stop. A buy stop is a pending order, you don’t see it right now but if you zoom into the chart you will see that we have placed an order far above the current Ask price and in this video we are going to find out how to do that with MQL4.
To get started please click on the 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: “SimpleBuyStop”, click on: “Continue“, “Continue“ and “Finish”, and now we can remove everything above the “OnTick” function and let’s also delete the two comment lines here.
To get started we will check a simple condition; we want to know if we have any open positions or orders, so we use “OrdersTotal” and if that equals 0 (zero) we want to use the “OrderSend” command to send a pending buy order and to do that for our buy stop we need to pass several parameters here.
Let’s go through the parameters one by one:
The first one is used to find out the current currency pair on the chart, in my case it’s the Australian against the Canadian Dollar.
The second parameter is what we are actually going to do, we are using “OP_BUYSTOP” here, so let’s mark that and press F1 and we learn that the “OP_BUYSTOP” parameter will pass a value of 4 and that is equal to a pending order for a buy stop.
The next parameter here is the lot size, in our case its 10 micro lot, we want to buy if the price rises 200 points above the current Ask price.
The next parameter is for the slippage, if you set this one to 0 (zero) you will get a lot of re-quotes because before your orders executed the price might move. You also don’t want to set it too high because you don’t want to buy if the price moved too far away from the current level. 3 is a good value here.
The next parameter here is set to 0 (zero) because I don’t use a stop-loss. You could use one, in that case you could set it to 100 points above the Ask price or something like that, but I have defined a take profit value; I would like to make 200 points of profit because if our order is executed 200 points above the current Ask price and we close the position 400 points above the current Ask price that would give us 200 points profit.
This parameter is set to “NULL” – all capital letters – we could use it as a comment, I don’t need one.
The next one here would be the magic number, that’s kind of a license plate if you use several EA’s on one chart that might be helpful, I also don’t need that one.
The next parameter could be used to set an expiration date but we don’t need an expiration date in this simple example and the last one is a color; it is green and that is just the color of the arrow that is drawn on the chart and that’s about it.
If you are ready you can click on the “Compile” button here or press F7 on your keyboard, you should get no errors here and if that is the case you can trust the little button here or press F4 on your keyboard to go back to Metatrader.
And in Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, please pick the new file: “SimpleBuyStop.ex4”, enable the visual mode here and start a test.
…and here we go. We don’t see anything but when we zoom into the chart we see that our buy stop is here, so our little Expert Advisor works and in this little video you have learned how to place a buy stop above the current Ask price and you have coded it yourself with a few lines of MQL4 code.

Download “CODE - SIMPLE BUYSTOP”

SimpleBuyStop.mq4 – Downloaded 345 times – 787.00 B