Code to send email alerts when closed trades...

 

I was writing some code to send me email alerts after trades close, regardless of whether or not the expert closed the trade or the trade was closed via TP or SL. I have tested it and it works, however, I am having problems with the emails violating SMTP rules when use MT4's SendMail(). (see http://cr.yp.to/docs/smtplf.html)

Does anyone know how to get around or correct this problem, because this would be a VERY valuble tool, especially for those who need to focus time on other work and would like to keep up to speed on the account as trades are being executed. Personally, it would be great to have trades emailed to my BlackBerry to see the details after the trade is close. I appreciate any help!

Files:
 
cockeyedcowboy:
Nicholishen

Try to send emails to other address, like some friend that receives email on different sever then yours. I use a mailer to send messages to myself from MetaQuote and I get errors when sending to HotMail.com addresses, the emailer wont work. Thats the only email sever I can't send to. LF means LINE FEED. In your message it mentions MSN net work which includes hotmail.com. Also some email severs donot permit you to email to the address from which the mail originates from as well. Emailing to a friend will at least eliminate the above conditions from the list.

I have not used the mailer that comes with MQ. I use a third party software, as MQ didnot have that function back in v3.xx I have not rewriten my communication module to use the built-in mail yet, plan to do this in the near future.

The CockeyedCowboy

...looks like we posted at same time. Thanks for the info!

 

Nicholishen

Try to send emails to other address, like some friend that receives email on different sever then yours. I use a mailer to send messages to myself from MetaQuote and I get errors when sending to HotMail.com addresses, the emailer wont work. Thats the only email sever I can't send to. LF means LINE FEED. In your message it mentions MSN net work which includes hotmail.com. Also some email severs donot permit you to email to the address from which the mail originates from as well. Emailing to a friend will at least eliminate the above conditions from the list.

I have not used the mailer that comes with MQ. I use a third party software, as MQ didnot have that function back in v3.xx I have not rewriten my communication module to use the built-in mailer yet, plan to do this in the near future.

The CockeyedCowboy

 

fixed

...It seems that it did not like my "\n" statements.

To use the mail alert you must first setup MT preferences. If you haven't yet done it, you would go to: TOOLS>OPTIONS>EMAIL. Enable the email feature and set the preferences for SMTP.

Depending on how you intend to use it you can insert it into your code and change the subject to the name of the advisor. Make sure to add filters to the qualifying if statement. An example is inserted but commented out. Optionally you can just#include as a generic mailer.

The benefit of this is to have the system email you the details of closed trades; even when they have closed due to SL or TP.

I would appreciate feedback as to what details you would like to see in the email.

Files:
 

I used your script and beefed it up a bit. Can't get around /n but it works for me. I am not a coder, I just played a little.

I would like to change it to alert on every instance of a market order, opening or closing.

Can you look at the code and see how you can augment the EA to do that?

//+------------------------------------------------------------------+

//| MailAlert TEST.mq4 |

//| Nicholishen |

//| |

//+------------------------------------------------------------------+

//https://www.forex-tsd.com/metatrader-4/1125-code-send-email-alerts-when-closed-trades.html

#property copyright "Nicholishen"

#property link ""

int k;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init()

{

for(int i=0;i<1000;i++){

if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){

if(OrderSymbol()==Symbol() ){

k++;

}

}else{

break;

}

}

Comment("Init() Trades Count = ",k);

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start()

{

//----

int f =0;

for(int i=0;i<10000;i++){

if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){

if(OrderSymbol()==Symbol() /*&& OrderMagicNumber()==MAGICMA && OrderComment() ==Cmt(Period())*/ ){

f++;

}

}else{

break;

}

}

Comment(" Trades in History ",f," Init cnt ",k );

if(k < f){

string ordertyp;

OrderSelect(f,SELECT_BY_POS,MODE_HISTORY);

if(OrderType()==0)ordertyp="BUY";

if(OrderType()==1)ordertyp="SELL";

SendMail("prof: "+DoubleToStr(OrderProfit(),2)+", bal: "+DoubleToStr(AccountBalance(),2)+", eq: "+DoubleToStr(AccountEquity(),2)+"",

"Symbol: "+Symbol()+" "+ordertyp+" "+DoubleToStr(OrderMagicNumber(),0)+" \n"+

"Comment: "+DoubleToStr(OrderComment()

"Ticket: "+DoubleToStr(OrderTicket(),0)+" \n"+

"Size: "+DoubleToStr(OrderLots(),2)+" \n"+

"OpenTime: "+TimeToStr(OrderOpenTime())+" \n"+

"Close Time: "+TimeToStr(OrderCloseTime())+" \n"+

"Open: "+DoubleToStr(OrderOpenPrice(),4)+" \n"+

"Close: "+DoubleToStr(OrderClosePrice(),4)+" \n"+

"Profit: "+DoubleToStr(OrderProfit(),2)+" \n\n"+

"Balance: "+DoubleToStr(AccountBalance(),2)+" \n"+

"Used Margin: "+DoubleToStr(AccountMargin(),2)+" \n"+

"Free Margin: "+DoubleToStr(AccountFreeMargin(),2)+" \n"+

"Equity: "+DoubleToStr(AccountEquity(),2)+" \n"+

"Open Orders: "+DoubleToStr(OrdersTotal(),0)+"" );

k++;

}

return;

}

//----

//+------------------------------------------------------------------+

 

Just made final modifications to it.

It alerts upon the close of a trade on a pair to which the EA was attached. I use a commercial EA, and I open up an extra chart to which I attach the alerter.

I would love to have it augmented to alert on trade openings as well, but I am not that smart yet. If anyone can contribute to this code, that would be great!

This is what the email looks like:

Subject: CLOSE pr: 23.59, bal: 5152.57, eq: 5058.81

Symbol:

Comment:

Ticket#:

Size:

OpenTime:

Close Time:

Open:

Close:

Profit:

Pips:

Balance:

Used Margin:

Free Margin:

Equity:

Open Orders:

Broker:

Leverage:

 

Hmm

Looks like my EA counts wrong, gives me some random closed trade from history. Anyone can look at the code?

 

"Auto email" sent modified to add price in indicator, help !

Hello !

First, i would like to thank the many contributos of the forum ! Great place !!!

I have no skills in programming and would like to slightly modify an indicator i use.

It sends an email whena na alert occurs. All is mentionned except the price.

So, what to add to have the price inserted in the email ?

SendMailOnce("Signal " + Symbol(),Symbol()+ ":" + PeriodToText() + " - Up Signal" + #price# ,1);

I will have to specify the correct code for price as well as a conversion from real to string.

I know this sounds too easy for most of you, but it will help me a lot !

Thanks in advance,

CilO

 

I modified a script to send an email when a trade is opened or closed.

It just send some basic information about the trades. It can easily modified to fit your needs.

Files:
 

Email Alerts

You can are also use the Email alert to send text messages to your cell phone, see my other post on this, Google Email to Text AT&T for an example.

 

email alert

Hi

I try to change indictor"DDFX 2 Neuro Trade v3_MTF_alert" to email alert. but does not work.

Please help to verify what I wrote wrong.

Thank you very much!

Original sentence:

if(TrendUp[1] > TrendDown[1] && TrendUp != EMPTY_VALUE && SoundAlert == true && Bars>alertBar) {Alert("MTF_DDFX going Down on the " + Period() + " " + Symbol() + " minute chart");alertBar = Bars;} if(TrendUp[1] alertBar) {Alert("MTF_DDFX going Up on the " + Period() + " " + Symbol() + " minute chart");alertBar = Bars;}

I just change to

if(TrendUp[1] > TrendDown[1] && TrendUp != EMPTY_VALUE && SoundAlert == true && Bars>alertBar) {SendMail(StringConcatenate(Symbol(),"Down "),Down);alertBar = Bars;} if(TrendUp[1] alertBar) {SendMail(StringConcatenate(Symbol(),"Up "),Up);alertBar = Bars;}

Original indictor

//+------------------------------------------------------------------+

//| MTF_DXFO.mq4 |

//|------------------------------------------------------------------+

#property indicator_chart_window

#property indicator_buffers 6

#property indicator_color1 CLR_NONE

#property indicator_color2 CLR_NONE

#property indicator_color3 DarkSlateBlue

#property indicator_color4 FireBrick

#property indicator_color5 NavajoWhite

#property indicator_color6 Orange

#property indicator_width3 1

#property indicator_width4 1

#property indicator_width5 4

#property indicator_width6 4

//---- input parameters

/*************************************************************************

PERIOD_M1 1

PERIOD_M5 5

PERIOD_M15 15

PERIOD_M30 30

PERIOD_H1 60

PERIOD_H4 240

PERIOD_D1 1440

PERIOD_W1 10080

PERIOD_MN1 43200

You must use the numeric value of the timeframe that you want to use

when you set the TimeFrame' value with the indicator inputs.

---------------------------------------*/

extern int TimeFrame = 0;

//---- indicator buffers

double Up[];

double Down[];

double CrossUp[];

double CrossDown[];

double TrendUp[];

double TrendDown[];

double alertBar;

extern bool SoundAlert = true;

//+------------------------------------------------------------------+

//| Custom indicator initialization function |

//+------------------------------------------------------------------+

int init()

{

//---- indicator line

SetIndexStyle(0, DRAW_HISTOGRAM, STYLE_DOT);

SetIndexBuffer(0, Up);

SetIndexStyle(1, DRAW_NONE, STYLE_DOT);

SetIndexBuffer(1, Down);

SetIndexStyle(2, DRAW_HISTOGRAM, STYLE_DOT);

SetIndexBuffer(2, CrossUp);

SetIndexStyle(3, DRAW_HISTOGRAM, STYLE_DOT);

SetIndexBuffer(3, CrossDown);

SetIndexBuffer(4, TrendUp);

SetIndexStyle(4, DRAW_ARROW);

SetIndexArrow(4, 221);

SetIndexBuffer(5, TrendDown);

SetIndexStyle(5, DRAW_ARROW);

SetIndexArrow(5, 222);

//---- name for DataWindow and indicator subwindow label

switch(TimeFrame)

{

case 1 : string TimeFrameStr="Period_M1"; break;

case 5 : TimeFrameStr="Period_M5"; break;

case 15 : TimeFrameStr="Period_M15"; break;

case 30 : TimeFrameStr="Period_M30"; break;

case 60 : TimeFrameStr="Period_H1"; break;

case 240 : TimeFrameStr="Period_H4"; break;

case 1440 : TimeFrameStr="Period_D1"; break;

case 10080 : TimeFrameStr="Period_W1"; break;

case 43200 : TimeFrameStr="Period_MN1"; break;

default : TimeFrameStr="Current Timeframe";

}

IndicatorShortName("MTF_DFXO("+TimeFrameStr+")");

}

//----

return(0);

//+------------------------------------------------------------------+

//| |

//+------------------------------------------------------------------+

int start()

{

datetime TimeArray[];

int i,limit,y=0,counted_bars=IndicatorCounted();

// Plot defined time frame on to current time frame

ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame);

limit= Bars-1;

for(i=0,y=0;i<limit;i++)

{

if (Time<TimeArray[y]) y++;

/***********************************************************

Add your main indicator loop below. You can reference an existing

indicator with its iName or iCustom.

Rule 1: Add extern inputs above for all neccesary values

Rule 2: Use 'TimeFrame' for the indicator time frame

Rule 3: Use 'y' for your indicator's shift value

**********************************************************/

Up=iCustom(Symbol(),TimeFrame,"DDFX 2 Neuro Trade v3",0,y);

Down=iCustom(Symbol(),TimeFrame,"DDFX 2 Neuro Trade v3",1,y);

CrossUp=iCustom(Symbol(),TimeFrame,"DDFX 2 Neuro Trade v3",0,y);

CrossDown=iCustom(Symbol(),TimeFrame,"DDFX 2 Neuro Trade v3",1,y);

if (Time==TimeArray[y]) TrendUp=iCustom(Symbol(),TimeFrame,"DDFX 2 Neuro Trade v3",4,y);

if (Time==TimeArray[y]) TrendDown=iCustom(Symbol(),TimeFrame,"DDFX 2 Neuro Trade v3",5,y);

if(TrendUp[1] > TrendDown[1] && TrendUp != EMPTY_VALUE && SoundAlert == true && Bars>alertBar) {Alert("MTF_DDFX going Down on the " + Period() + " " + Symbol() + " minute chart");alertBar = Bars;}

if(TrendUp[1] alertBar) {Alert("MTF_DDFX going Up on the " + Period() + " " + Symbol() + " minute chart");alertBar = Bars;}

}

return(0);

}

//+------------------------------------------------------------------+

Reason: