MT4 SendMail – An Easy Guide To Sending FX Notifications

mt4 sendmail Hello dear friends! In the previous article we discussed the process of setting up MetaTrader 4 for sending email notifications. Also, as a proof-of-concept we sent our very first MT4 email alert. Today we will delve deeper into the topic of MQL4 email notifications and we will learn how to send combine useful information in the emails our FX Robots send to us!

The MT4 SendMail function

As you recall, the MQL4 function responsible for sending emails is the SendMail. This function takes two parameters:
bool SendMail(
string subject, // header
string some_text // email text
);

The subject parameter will be displayed as the subject of your email, and some_text is the email message. We will practice using this SendMail today, however you can always find additional information on the MetaQuotes’ official website.

Email header

What would you put in the subject? Some ideas include:

  • Order sent successfully
  • Order closed at TakeProfit (StopLoss)
  • Account balance is low!
  • Profit for the day report
  • Price is above level X
  • MACD Crossing Alert
  • etc.

As you can see, your subject line should represent what the message will be about, and it should attract attention. When you first set up your expert advisors to send email notifications, it will be an exciting novelty. However, like with everything, with time you will get used to it, and this in turn will create a risk of you missing something important. That’s why your email header should be short, sharp, and to the point.

Email body

There are several tricks which will help you create a concise yet informative email text to then feed into the MT4 SendMail function. Today I will show you these tricks.

Trick 1 – concatenation with +

Use the + (plus) operator to quickly concatenate strings. For example, the following code will send an email saying “Hello Peter”:
string Greeting = "Hello";
string Name = "Peter";
SendMail("Test email", Greeting + " " + Name);

Trick 2 – conversion to type string

If you have  an integer or a double, then using string() is the easiest way to convert it to a string to avoid compiler warnings. Example:
SendMail("Test email", "Current Ask price is: " + string(Ask));
Trick 3 – “\n” to create new lines

If you are wondering if it’s possible to create new lines in your email, then the answer is YES!

Yes it is possible to create new lines in your email. To do this, use the “\n” escape sequence. Here’s an example:
SendMail("Test email", "Hello there!\nThe current Ask price is: " + string(Ask));

“Hello there” will be on the first line, and “The current Ask price is: x.xxxx” will be on the second line.

Practice time!

Let’s create a practice script to put all of this knowledge together and better learn MT4 SendMail.

IMPORTANT: Make sure that you are using a demo account for this exercise. This script will execute an order, so do not use it on a real trading account.

Create an empty script and copy-paste the following code into it:
//+------------------------------------------------------------------+
//| TestSendMail.mq4 |
//| Copyright, Kirill |
//| http://www.ForexBoat.com |
//+------------------------------------------------------------------+
#property copyright "Copyright, Kirill"
#property link "http://www.ForexBoat.com"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
int ticket = OrderSend(Symbol(), OP_BUYa, 0.1, Ask, 10, Bid - 20*Point, Bid + 20*Point);

if(ticket > 0)
{
//Select the order
OrderSelect(ticket, SELECT_BY_TICKET);

string direction;
if(OrderType() == 0)
{
direction = "Buy";
}
else
{
direction = "Sell";
}

//MT4 SendMail function
bool res = SendMail("MT4: OrderSent", "Order #" + string(ticket) + " Opened Succesfully\n"
+"Instrument: " + OrderSymbol() + "\n"
+"Direction: " + direction + "\n"
+"Open Price: " + string(OrderOpenPrice()) + "\n"
+"StopLoss: " + string(OrderStopLoss()) + "\n"
+"TakeProfit: " + string(OrderTakeProfit()) + "\n"
);

if(res == false)
{
Alert("Error sending email");
}

OrderClose(ticket, 0.1, OrderClosePrice(), 10);
}

else
{
bool res = SendMail("MT4: OrderSend Error", "There was an error sending your order! Error #" + string(GetLastError()));

if(res == false)
{
Alert("Error sending email");
}
}

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

Now go ahead and launch the script on your DEMO account. You should see a BUY order get opened then immediately closed. However, prior to closing the order the script will send you an information-rich email about that order.

If everything has been done correctly, the email will look like this:

mt4-sendmail

 

All done! Now we get a proper, informative email whenever this script opens an order.

Next steps

This is just one example for using MT4 SendMail. You can experiment with different trading scenarios and the types of notifications you want to use to accommodate for those scenarios.

Also, there is additional information that you can extract from a market order. For more information on how to use the OrderSelect function visit the Free MQL4 tutorial over here: https://www.forexboat.com/orderselect/

 

I trust you found this article useful. If so, leave me some feedback in the comments section below!

Remember to subscribe to the ForexBoat newsletter to get notified of any more goodies. But for now, happy trading!

Kind regards,

Kirill

What are you waiting for?

START LEARNING FOREX TODAY!

share This:
Muhammad Awais

4 Responses to “MT4 SendMail – An Easy Guide To Sending FX Notifications”

August 29, 2014 at 6:02 am, Julian said:

Hey Kirill,

Added the code and after making a few changes run it. I see you wanted to test our knowledge :).
Keep on the good articles.

Reply

August 31, 2014 at 4:10 pm, Kirill said:

Hi Julian,

Thanks for the support! More articles are on their way.

Kirill

Reply

December 01, 2015 at 6:38 am, Mr. Indranx said:

It’s shows error on value of ‘OrderSelect’ should be checked and ‘OrderClose’

Reply

April 02, 2017 at 3:46 am, joefrank1982 said:

Line 15 of the code says Buya in the order…it should only be Buy.

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: