In this video we want to find out of how to set a buy limit. A buy limit is not a market order but a pending order. We don’t see it right now but when we zoom in to the chart we see that we have a buy limit that is far below the current price and in this video we are going to find out how to do that with MQL4.
Download “CODE - SIMPLE BUY LIMIT”
To start 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: “SimpleBuyLimit”, click on “Continue”, “Continue” and “Finish“.
Now we can remove everything that is above the “OnTick” function, and I will also delete the two comment lines here.
In this very simple version we are going just to check if we have no open positions or orders. That is done by using “OrdersTotal”, because “OrdersTotal” will return the number of market and pending orders and if that returns the value of 0 (zero) we have no open positions and no open orders and I that case we want to use “OrderSend” to set our buy limit.
To do that we need to pass a few parameters, we will use round braces here, if you mark one you will see the other one that’s something that might be very helpful for beginners because when you just start out you will have often errors because the number of braces do not mach and this is how you can check this out.
Okay, our first parameter is to find out which symbol is currently used on the chart, we use “Symbol” and two round braces (Symbol()) but if we mark “Symbol” and press F1 we will see there is also underscore symbol (_Symbol) and actually “_Symbol” does the same. The Reference says that we could also use the “Symbol” function and that will deliver the current currency pair that we are using on our chart.
In parameter 2 we say that we want to use a buy limit, that is done by using “OP_BUYLIMIT” – all in capital letters –, now we use buy limit and you could also set a buy stop, we use it to open a position of 10 micro lot, that’s parameter 3 and now we define the price level for our buy limit that should be the Ask price minus 20 points. The “_Point” function will deliver the point value for the current symbol.
The next parameter is for the slippage, 3 is a good value here, if you set it too low you will get a lot’s of requites and if you set it too high you might get a price that you don’t want.
Let’s continue with the next parameter, that’s for the stop loss, I could define it in a way like here but in our case we have no stop loss but we will have a value for the take profit and that should be 10 points above the current Ask price, so actually from minus 20 to plus 10 we would have the goal of 30 points for profit.
The next parameter could be used to set a comment, I use “NULL”, all in capital letters because I don’t use it here, you could also use a magic number – here called ID number –, the comment and the magic number could be used to identify an Expert Advisor if you are going to use more than one on your chart, I never do that.
The next parameter is for the expiration date, we could use it to set the time and date when the buy limit will no longer be valid. For this simple example I don’t have an expiration date so I set it to 0 (zero).
And the last parameter is “green” it’s a color and we use it to actually set the color of the arrow that is drawn when we open a position.
So far so good, we need to end our statement with a semicolon – actually all these here could be used in a single line but I guess it’s more readable in this way – and if you are done you can click on the “Compile” button here or press F7. That should work without any errors and if this is the case you can click on the little button here or press 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: “SimpleBuyLimit.ex4”, mark the visual mode here and start a test.
Here we are… Lets zoom in to the chart, here is our buy limit, lets speed up the process, to speed it up I will set this one to 10 points below the Ask price, recompile the code, let’s start another test and this time it should go much faster. This is still below the spread but now we have a buy order and in this little video you have learned how to create a buy limit and you have coded it yourself with a few lines of MQL4 code.
Download “CODE - SIMPLE BUY LIMIT”SimpleBuyLimit.mq4 – Downloaded 468 times – 749.00 B