EmailStatus

Price Data Components
Series array that contains open time of each bar
Orders Execution
Checks for the total of open ordersChecks for the total of closed orders
Miscellaneous
It sends emails
0 Views
0 Downloads
0 Favorites
EmailStatus
//+------------------------------------------------------------------+
//|                                                  EmailStatus.mq4 |
//|                                               Paul Hampton-Smith |
//|                                                                  |
//+------------------------------------------------------------------+

#include <TimeGMT.mqh>

#property indicator_chart_window
//---- input parameters
extern int       EmailHourStart=9;
extern int       EmailHourEnd=18;

datetime dtLastReport = 0;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
	// set last report time as 3 days ago
	dtLastReport = TimeLocal()-3*24*3600;	
	start();
   return(0);
}


//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
	static int nLastHourLocal = -1;

	int nHourLocal = TimeHour(TimeLocal());
	if (nHourLocal != nLastHourLocal &&
		nHourLocal >= EmailHourStart && 
		nHourLocal <= EmailHourEnd) 
	{
		EmailStatus();
		nLastHourLocal	= nHourLocal;
	}
	return(0);
}
//+------------------------------------------------------------------+

void EmailStatus()
{
	double dblOpenProfit = 0.0;
	for (int i = 0 ; i < OrdersTotal() ; i++)
	{
		OrderSelect(i, SELECT_BY_POS);
		dblOpenProfit += OrderProfit() + OrderSwap();
	}
	string strBody = StringConcatenate(
		  "Balance     ",RightJustifyDblToStr(AccountBalance(),6,0),
		"\nEquity      ",RightJustifyDblToStr(AccountEquity(),6,0),
		"\nMargin      ",RightJustifyDblToStr(AccountMargin(),6,0),
		"\nFree margin ",RightJustifyDblToStr(AccountFreeMargin(),6,0),
		"\nOpen profit ",RightJustifyDblToStr(dblOpenProfit,6,0),"\n\n");

	if (OrdersTotal() == 0)
	{
		strBody = strBody + "No open or pending orders";
	}
	else
	{
		strBody = strBody + OrderReport(false); // open orders
		strBody = strBody + "\n\n" + OrderReport(true); // pending orders
	}

	strBody = strBody + "\n\n" + ClosedOrdersSinceLastReport();
	
	SendMail("Progress", strBody);
}	

string OrderReport(bool bPending)
{
	string strReport;
	if (bPending) 
		strReport = "Pending orders\n\n";
	else
		strReport = "Open orders\n\n";

	strReport = strReport + "  Open time local         Type  Lots Symbol  Price   S/L    T/P    Now Profit Comment\n";
	strReport = strReport + "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
	
	for (int i = 0 ; i < OrdersTotal() ; i++)
	{
		OrderSelect(i, SELECT_BY_POS);
		
		if (bPending && (OrderType() <= OP_SELL)) continue;
		if (!bPending && (OrderType() > OP_SELL)) continue;
		double dblDigits = MarketInfo(OrderSymbol(),MODE_DIGITS);
		strReport = StringConcatenate(strReport,"\n",
			TimeToStr(ServerToLocal(OrderOpenTime()),TIME_DATE|TIME_SECONDS)," ",
			CmdToStrFixed(OrderType())," ",
			RightJustifyDblToStr(OrderLots(),5,2)," ",
			OrderSymbol()," ", 
			DoubleToStr(OrderOpenPrice(),dblDigits)," ",
			RightJustifyDblToStr(OrderStopLoss(),6,dblDigits)," ",
			RightJustifyDblToStr(OrderTakeProfit(),6,dblDigits)," ", 
			DoubleToStr(MarketInfo(OrderSymbol(),MODE_BID),dblDigits)," ", 
			RightJustifyDblToStr(OrderProfit(),5,0)," ",
			OrderComment());
	}
	return(strReport);
}			

string ClosedOrdersSinceLastReport()
{
	string strReport = "";
	
	for (int i = OrdersHistoryTotal()-1 ; i >= 0 ; i--)
	{
		OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
		// only orders since last report
		if (ServerToLocal(OrderCloseTime()) < dtLastReport ) continue;

		double dblDigits = MarketInfo(OrderSymbol(),MODE_DIGITS);
		strReport = StringConcatenate(strReport,"\n",
			TimeToStr(ServerToLocal(OrderOpenTime()),TIME_DATE|TIME_SECONDS)," ",
			CmdToStrFixed(OrderType())," ",
			RightJustifyDblToStr(OrderLots(),5,2)," ",
			OrderSymbol()," ", 
			DoubleToStr(OrderOpenPrice(),dblDigits)," ",
			RightJustifyDblToStr(OrderStopLoss(),6,dblDigits)," ",
			RightJustifyDblToStr(OrderTakeProfit(),6,dblDigits)," ", 
			TimeToStr(ServerToLocal(OrderCloseTime()),TIME_DATE|TIME_SECONDS)," ",
			DoubleToStr(OrderClosePrice(),dblDigits)," ", 
			RightJustifyDblToStr(OrderProfit(),5,0)," ",
			OrderComment());
	}
	
	if (strReport == "")
	{
		return("No closed orders since local time " + TimeToStr(dtLastReport,TIME_DATE|TIME_SECONDS));
	}
	else
	{
		strReport = StringConcatenate(
			"Orders closed since local time ",TimeToStr(dtLastReport,TIME_DATE|TIME_SECONDS),
			"\n\n",
			"  Open time local         Type  Lots Symbol  Price   S/L    T/P   Close time local   Close Profit Comment\n",
			"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~",
			strReport);
		dtLastReport = TimeLocal();
		return(strReport);
	}
}			


string CmdToStrFixed(int cmd)
{
   switch(cmd)
   {
   case OP_BUY:       return("       Buy");
   case OP_SELL:      return("      Sell");
   case OP_BUYSTOP:   return("  Buy stop");
   case OP_SELLSTOP:  return(" Sell stop");
   case OP_BUYLIMIT:  return(" Buy limit");
   case OP_SELLLIMIT: return("Sell limit");
   default: return("OP_UNKNOWN");
   }
}      
	
string RightJustifyDblToStr(double dbl, int nWidth, int nDecimals) 
{ 
	string strRet = DoubleToStr(dbl,nDecimals); 
   int nLen = StringLen(strRet); 
   for (int i = nLen ; i < nWidth ; i++) strRet = " " + strRet; 
   return(strRet); 
} 
        
datetime ServerToLocal(datetime dtServer)
{
	return(dtServer+(-TimeZoneServer()+TimeZoneLocal())*3600);
}

Comments