Navigation:Home > Content >

mn_Show_Profit_Alerts.mq4

Time: 2018-07-19 | Download file:mn_Show_Profit_Alerts.mq4

//+------------------------------------------------------------------+
//|                                        Mn Show Profit Alerts.mq4 |
//+------------------------------------------------------------------+
//download source: http://www.forexfactory.com/showthread.php?p=7403504#post7403504
//mod. fxdaytrader (thousands separator, zerocolor, calcswapcommissionalso, profitpips, alerts ...
#property copyright "Mn // mod. fxdaytrader"
#property link "http://ForexBaron.net"

#property indicator_chart_window

extern bool FilterBySymbol      = FALSE;
extern bool FilterByMagicNumber = FALSE;
extern int  MagicNumber         = 0;
//
extern bool   CalcSwapCommissionAlso = true;//fxdaytrader
extern string ThousandsSeparator = " ";
extern string DecimalSeparator   = ",";
extern int    mCorn = 3, mtextSize = 18, mXDist = 1, mYDist = 1;
extern color  ProfitColor = Blue, LossColor = Red, ZeroColor=SteelBlue;
extern string mFont = "Arial";
extern bool   ShowProfitPips       = TRUE;
extern color  PipsColor            = White;
extern int    AddTomYDist          = 20;
extern string PipsFont     = "Arial";
extern int    PipsTextSize = 15;
double profitpips;
extern string ahis = "******* ALERT SETTINGS:";
extern bool   AlertAtProfitOf        = true;
extern double AlertAtProfitAmount    = 1000.00;
extern bool   AlertAtLossOf          = true;
extern double AlertAtLossAmount      = 1000.00;
extern int    AlertCandle            = 1;
extern bool   PopupAlerts            = true;
extern bool   EmailAlerts            = false;
extern bool   PushNotificationAlerts = false;
extern bool   SoundAlerts            = false;
extern string SoundFileProfit        = "alert.wav";
extern string SoundFileLoss          = "alert2.wav";
string SoundFileName;
int lastAlert=3;
int Multiplier;
double pips2dbl;
//+------------------------------------------------------------------+
   
int init()
 {  
  if (ShowProfitPips) {
   ObjectCreate("mShowProf1", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSet("mShowProf1", OBJPROP_CORNER, mCorn);
   ObjectSet("mShowProf1", OBJPROP_XDISTANCE, mXDist);
   ObjectSet("mShowProf1", OBJPROP_YDISTANCE, mYDist);
   ObjectSetText("mShowProf1", " ", mtextSize, mFont, CLR_NONE);
  }
  
  if (ShowProfitPips) mYDist+=AddTomYDist;
  ObjectCreate("mShowProf", OBJ_LABEL, 0, 0, 0, 0, 0);
  ObjectSet("mShowProf", OBJPROP_CORNER, mCorn);
  ObjectSet("mShowProf", OBJPROP_XDISTANCE, mXDist);
  ObjectSet("mShowProf", OBJPROP_YDISTANCE, mYDist);
  ObjectSetText("mShowProf", " ", mtextSize, mFont, CLR_NONE);
  
  AlertAtLossAmount = MathAbs(AlertAtLossAmount)*(-1);
  return(0); 
 }
 
//-----------------------------------------------+ 
int start()
 {
   color mTextCol;
   double mProf = 0;
   profitpips=0.0;
   for(int i = 0; i < OrdersTotal(); i++)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) break;
      //if(OrderSymbol() == Symbol() && OrderType() < 2)
      if(FilterBySymbol&&OrderSymbol()!=Symbol()) continue;
      if(FilterByMagicNumber&&OrderMagicNumber()!=MagicNumber) continue;
      if (OrderType() < 2)
        {
          if (!CalcSwapCommissionAlso) mProf = mProf + OrderProfit();
           else if (CalcSwapCommissionAlso) mProf = mProf + OrderProfit() + OrderSwap() + OrderCommission();
           
          if (ShowProfitPips) {
           BrokerDigitAdjust(OrderSymbol());
           if (OrderType()==OP_BUY) profitpips=profitpips+((MarketInfo(OrderSymbol(),MODE_BID)-OrderOpenPrice())/pips2dbl);
           if (OrderType()==OP_SELL) profitpips=profitpips+((OrderOpenPrice()-MarketInfo(OrderSymbol(),MODE_ASK))/pips2dbl);
          }//if (ShowPips) {
        }
     }

   //mProf = -159651.62;//testing
   if(mProf < 0.00) { mTextCol = LossColor; SoundFileName = SoundFileLoss; }
    else if (mProf > 0.00) { mTextCol = ProfitColor; SoundFileName = SoundFileProfit; }
     else mTextCol = ZeroColor;
   //fxdaytrader
    string smProf = DoubleToStr(mProf,2);
    smProf = stringReplace(smProf,".",DecimalSeparator);
    if (mProf>0.00) {
      if (lastAlert!=1) if (AlertAtProfitOf&&mProf>=AlertAtProfitAmount) { lastAlert=1; doAlerts("Profit >= "+DoubleToStr(AlertAtProfitAmount,2)+" (current profit: "+DoubleToStr(mProf,2)+") "+AccountCurrency(),SoundFileName); }
     if (mProf>=1000.00&&mProf<10000.00) smProf = StringInsert(smProf,1,ThousandsSeparator);
     if (mProf>=10000.00&&mProf<100000.00) smProf = StringInsert(smProf,2,ThousandsSeparator);
     if (mProf>=100000.00&&mProf<1000000.00) smProf = StringInsert(smProf,3,ThousandsSeparator);
     if (mProf>=1000000.00&&mProf<10000000.00) { smProf = StringInsert(smProf,1,ThousandsSeparator); smProf = StringInsert(smProf,5,ThousandsSeparator); }
     if (mProf>=10000000.00) { smProf = StringInsert(smProf,2,ThousandsSeparator); smProf = StringInsert(smProf,6,ThousandsSeparator); }
    }//if (mProf>0.00) {
    else if (mProf<0.00) {
      if (lastAlert!=2) if (AlertAtLossOf&&mProf<=AlertAtLossAmount) { lastAlert=2; doAlerts("Profit <= "+DoubleToStr(AlertAtLossAmount,2)+" (current loss: "+DoubleToStr(mProf,2)+") "+AccountCurrency(),SoundFileName); }
     if (MathAbs(mProf)>=1000.00&&MathAbs(mProf)<10000.00) smProf = StringInsert(smProf,2,ThousandsSeparator);
     if (MathAbs(mProf)>=10000.00&&MathAbs(mProf)<100000.00) smProf = StringInsert(smProf,3,ThousandsSeparator);
     if (MathAbs(mProf)>=100000.00&&MathAbs(mProf)<1000000.00) smProf = StringInsert(smProf,4,ThousandsSeparator);
     if (MathAbs(mProf)>=1000000.00&&MathAbs(mProf)<10000000.00) { smProf = StringInsert(smProf,2,ThousandsSeparator); smProf = StringInsert(smProf,6,ThousandsSeparator); }
     if (MathAbs(mProf)>=10000000.00) { smProf = StringInsert(smProf,3,ThousandsSeparator); smProf = StringInsert(smProf,7,ThousandsSeparator); }
    }//else if (mProf<0.00) {
   //end fxdaytrader

   //ObjectSetText("mShowProf", DoubleToStr(mProf, 2), mtextSize, mFont, mTextCol);
   ObjectSetText("mShowProf", smProf+" "+AccountCurrency(), mtextSize, mFont, mTextCol);//fxdaytrader
   if(mProf == 0) 
     //ObjectSetText("mShowProf", " ", mtextSize, mFont, CLR_NONE);
     ObjectSetText("mShowProf", "0" +" "+AccountCurrency(), mtextSize, mFont, mTextCol);//fxdaytrader
     
   if (ShowProfitPips) ObjectSetText("mShowProf1","(Profit Pips: "+DoubleToStr(profitpips,2)+")", PipsTextSize, PipsFont, PipsColor);//fxdaytrader

  return(0);
 }
 
//-----------------------------------------------+
int deinit()
 {
  ObjectDelete("mShowProf");
  if (ShowProfitPips) ObjectDelete("mShowProf1");
  
  return(0);
 }

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

//String functions by Hanover, http://www.forexfactory.com/showthread.php?t=165557
//+------------------------------------------------------------------+
string StringInsert(string str1, int pos, string str2)   {
//+------------------------------------------------------------------+
// Creates a result where str2 is inserted into str1, at pos in str1, e.g.
// StringInsert("0123456789",5,"XY") returns "01234XY56789"

  string outstr = StringSubstr(str1,0,pos) + str2 + StringSubstr(str1,pos);
  return(outstr);
} 

//+------------------------------------------------------------------+
string stringReplace(string str, string str1, string str2)  {
//+------------------------------------------------------------------+
// Usage: replaces every occurrence of str1 with str2 in str
  string outstr = "";
  for (int i=0; i        

Recommend