A guide to OrderSend – MQL4 for Complete Beginners Tutorial Part 15

Ordersend

Hello friends!

In today’s tutorial we will learn how to use the OrderSend function. OrderSend is an MQL4 trade function which is used to place Buy and Sell orders on the Forex Market. Once an order is placed you will be able to see it in Meta Trader 4 just like you can view any other position. 

OrderSend Parameters

In this tutorial we will focus on the StopLoss and TakeProfit parameters. In the previous tutorial we learned how to use extern variables to allow input of parameters by the trader. The external values were used to calculate the StopLoss and TakeProfit levels, which we will use in the OrderSend function today.

Another important parameter is cmd variable, which is used to specify the direction of your intended trade. In the video below I will show you how to use the OP_BUY keyword to execute a Buy transaction. Similarly, OP_SELL can be used to execute Sell orders.

Here is a list of other parameters that the OrderSend function allows you to input:
int OrderSend(
string symbol, // symbol
int cmd, // operation
double volume, // volume
double price, // price
int slippage, // slippage
double stoploss, // stop loss
double takeprofit, // take profit
string comment=NULL, // comment
int magic=0, // magic number
datetime expiration=0, // pending order expiration
color arrow_color=clrNONE // color
);

We will discuss most of  these parameters in today’s tutorial. However, more information can always be found on the MetaQuotes’ official website: OrderSend Reference

Video Tutorial

Alright! Let’s go ahead and send some orders with OrderSend. The video below is usually part of a paid course. Here you have the chance to see it for free. Simply enter your details and click “Play”

Remember – the code for this tutorial is located beneath the video.

This video is part of our Algorithmic Trading course. If you like it, you can check out by clicking the button below:

Algorithmic Trading Course

Code for 4-digit brokers

//+------------------------------------------------------------------+
//| Tutorial15.mq4 |
//| Copyright 2014, ForexBoat |
//| http://www.forexboat.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, ForexBoat"
#property link "http://www.forexboat.com"
#property version "1.00"
#property strict
#property script_show_inputs
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
extern int TakeProfit = 10;
extern int StopLoss = 10;

void OnStart()
{
double TakeProfitLevel;
double StopLossLevel;

TakeProfitLevel = Bid + TakeProfit*Point; //0.0001
StopLossLevel = Bid - StopLoss*Point;

Alert("TakeProfitLevel = ", TakeProfitLevel);
Alert("StopLossLevel = ", StopLossLevel);

OrderSend("EURUSD", OP_BUY, 1.0, Ask, 10, StopLossLevel, TakeProfitLevel, "My 1st Order!");

}
//+------------------------------------------------------------------+

 

Code for 5-digit brokers

//+------------------------------------------------------------------+
//| Tutorial15.mq4 |
//| Copyright 2014, ForexBoat |
//| http://www.forexboat.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, ForexBoat"
#property link "http://www.forexboat.com"
#property version "1.00"
#property strict
#property script_show_inputs
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
extern int TakeProfit = 10;
extern int StopLoss = 10;

void OnStart()
{
double TakeProfitLevel;
double StopLossLevel;

//here we are assuming that the TakeProfit and StopLoss are entered in Pips
TakeProfitLevel = Bid + TakeProfit*Point*10; //0.00001 * 10 = 0.0001
StopLossLevel = Bid - StopLoss*Point*10;

Alert("TakeProfitLevel = ", TakeProfitLevel);
Alert("StopLossLevel = ", StopLossLevel);

OrderSend("EURUSD", OP_BUY, 1.0, Ask, 10*10, StopLossLevel, TakeProfitLevel, "My 1st Order!"); //notice that slippage also has to be multiplied by 10

}
//+------------------------------------------------------------------+

What are you waiting for?

START LEARNING FOREX TODAY!

Tags:
share This:
Muhammad Awais

17 Responses to “A guide to OrderSend – MQL4 for Complete Beginners Tutorial Part 15”

September 15, 2014 at 11:37 pm, tablet8 said:

Hi
I tried a lot on this code but it didn’t work, I even copied it from your site but it didn’t work,
I don’t know what’s wrong with me !
I test it on these brokers: hotforex, exness, fibogroup,
all are with 5 decimals.
I changed the tp and sl to 100 but it still do not work.
the problem is with ordersend part, it doesn’t execute this part. it only show tp and sl but don’t open the trade.

Reply

February 05, 2015 at 2:39 am, Troy Matzkow said:

Hi tablet8,
I was having similar problems, I would first work out what Point your brokers uses you can do this by creating an Alert(Point); This will result with either 0.0001 (four points) or 0.00001 (five points), then copy paste the relevant code above.
2nd test that you can buy/sell manually on your demo account
3rd before dragging tutorial 15 onto a live chart make sure the Auto Trading button has been pressed (Which is located mid top left near New Order button, under Help). If this button is not pressed NO Automated trading can happen, ie the code we are trying to test.

Reply

September 15, 2014 at 11:58 pm, tablet8 said:

I found what was the problem but I don’t know how to solve it.
please make your codes based on 5 decimals brokers also.
or explain the required modification for 5 decimals brokers.

Reply

September 16, 2014 at 5:18 pm, Kirill said:

Hi tablet8,

That is a great question. Indeed, you will need to adjust the code for 5-digit brokers. The easiest way to make sure you do this correctly is to replace Point with Point*10, and you also have to multiply the slippage value by 10.

I have followed your advice and added the adjust code for 5-digit brokers above. It should work fine.

Let me know if you still experience issues after you modify your code.

Regards,
Kirill

Reply

September 16, 2014 at 7:43 pm, tablet8 said:

Hello
thank you very very much for your help, I tried the new code but it still didn’t work .
I searched a lot in forums one guy had said that if the broker is market execution we can not make OrderSend like this one.
is that true? is there any solution?
I am going disppointed from learning mql4 🙁 I spent many hours dealing with this case but couldn’t solve it.

Reply

September 16, 2014 at 7:50 pm, tablet8 said:

Dear Kirill

please take a look at the dreaded error 130

I read it but I don’t know how to modify it for our code=

“A guide to OrderSend – MQL4 for Complete Beginners Tutorial Part 15”

you know I am a complete beginner , I don’t know what to put in the parentheses:

doubleGetPipSizePrecision()

Reply

September 16, 2014 at 8:11 pm, tablet8 said:

Wow, I am genius!!! I solved it. the problem was with the name of EURUSD in my brokers:
in my broker symbol eurusd is EURUSD.I,

I changed the EURUSD in the code to EURUSD.I and run the code again and it worked .
I am veeery happppy.
I did it myself with no help:)

Reply

September 17, 2014 at 8:59 pm, Kirill said:

Great work tablet8!

Reply

September 16, 2014 at 8:27 pm, tablet8 said:

I have some questions about this code:
1: it only works on EURUSD, How can we write it to work on all pairs?
2:this code works with EURUSD, how can we change it to work with both EURUSD and EURUSD.I
when I switch between the brokers I should change the code for each broker and it is not good.

Reply

September 17, 2014 at 9:00 pm, Kirill said:

Hi tablet8,

Use this function instead of specifying the name of your currency pair: Symbol()

This will make your code much more versatile.

Cheers,
Kirill

Reply

September 18, 2014 at 1:46 am, tablet8 said:

thank you.

Reply

September 19, 2014 at 5:55 pm, Ron said:

Hello Kirill, I have the same issue as table8.When I enter “GOLD” the EA works,When I enter other symbols It does not.Also, I enter the function Symbol() instead of the “GOLD” , the code compiled but It does not set the order just shows the alerts.Please advice.Thanks, Ron

Reply

September 19, 2014 at 6:44 pm, Kirill said:

Hi Ron,
Great question – GOLD and other non-currencies can be particularly nasty. Given that I don’t have visibility of FxPro’s terminal, my advice would be for you to use the Alert() function to show you what Symbol() is returning: Alert(Symbol()); That way you will see if the name differs from GOLD.

Having said that, it seems rather strange to me that using Symbol() instead of “GOLD” doesn’t work. You need to have an investigation and play around with it to see what’s wrong.

In case you’re still stuck – just upload some screenshots here and I will have a look.

Cheers,
Kirill

Reply

September 19, 2014 at 11:19 pm, Ron said:

Hi Kirill, I tried a lot of options but with no luck ( you can see from the picture that I get the symbol but after getting the alerts there is no order send..I even tried another broker.
Here is the code that I tried, please advise what can be done (perhaps you can hand me a broker that supports this code?):

#property copyright “”

#property link “”

#property version “1.00”

#property strict

#property script_show_inputs

//+——————————————————————+

//| Script program start function |

//+——————————————————————+

extern int TakeProfit = 10;

extern int StopLoss = 10;

void OnStart()

{

double TakeProfitLevel;

double StopLossLevel;

TakeProfitLevel = Bid + TakeProfit*Point*10; //0.00001 * 10 = 0.0001

StopLossLevel = Bid – StopLoss*Point*10;

Alert(“TakeProfitLevel = “, TakeProfitLevel);

Alert(“StopLossLevel = “, StopLossLevel);

Alert(Symbol());

OrderSend(Symbol(), OP_BUY, 1.0, Ask, 10*10, StopLossLevel, TakeProfitLevel, “My 1st Order!”); //notice that slippage also has to be multiplied by 10

}

September 22, 2014 at 7:04 pm, Kirill said:

Hi Ron,

I’m not really following. The first 0.01 in your example is the lotsize, and that is totally up to you what you put there. If your broker allows 0.01 lots then you can use that. However, the second 0.01 in your example is placed where the slippage should be. For a 5-digit broker slippage should be indicated as a # of points.

Have a look at this discussion on the MQL4 forum: http://forum.mql4.com/47335

Kirill

April 07, 2017 at 9:22 pm, pinar said:

my stoploss and takeprofit lines dont show

Reply

April 18, 2017 at 4:28 pm, Damyan Diamandiev said:

Hi Pinar!

Can you please give us more details about the problem you are having. In order to assist you we will need more details about the code you are writing.

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

What are you waiting for?

START LEARNING FOREX TODAY!

as seen on: