Sound and Email Notification

 

Could someone please help me with my notification code. The code is suppose to email when a trade is entered. During backtest the Print statement always has OrderCnt_0 = OrderCnt_1? Thanks in advance for the help. ---Tom

***Please See Updated Code below this one*****

int      OrderCnt_1    = 0;

  
OrderCnt_1 = Notification(OrderCnt_1);  // Send email and play sound when in trade. Placed in "int start()"


// -------------------Notification on Trade Entry-----------------------------------------------

int Notification(int OrderCnt_1)
  {
    int OrderCnt_0 = 0;
  
    for( i = OrdersTotal()-1; i >= 0 ; i--)
      {
        OrderSelect(i,SELECT_BY_POS);
        if (  OrderSymbol() == Symbol() && OrderMagicNumber()  == MagicNumber  && (OrderType() == OP_BUY || OrderType() == OP_SELL)  ) OrderCnt_0++; 
      }
     
     Print( OrderCnt_0,"  ",OrderCnt_1);
     
     if (OrderCnt_0 > OrderCnt_1)
      {
        PlaySound("alert.wav");
        SendMail("WPR_EUR15", "Order Placed");
      }
      
     
     return(OrderCnt_0);
  
  }

 

You need variables which keeps their values.

I'll warn against using Cnt_1 as some form of Stamp because Orders could close and the Value of Cnt_0 would change.

Instead check if orders processed at the point of entry and send email at that time.

Here's the code, I haven't tested its a rough copy.

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int My_Magic=777;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int start(){
    int Ticket=OrderSend(...);
    Order__Notification(Ticket, My_Magic);
return(0);}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int Order__Notification(int OrderSend_Value, int Magic){
    static int Cnt1;  int Cnt0;
    //~~~~~~~~~~
    if(OrderSend_Value>0){
        for(int i=OrdersTotal()-1;i>=0;i--){
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true
            && OrderSymbol()==Symbol() 
            && OrderMagicNumber()==Magic
            && OrderType()<2
            ){
                Cnt0++;
            }
        }
        Print(Cnt0,"___",Cnt1);
        //~~~~~~~~~~
        if(Cnt0>Cnt1){
            PlaySound("alert.wav");
            SendMail("WPR_EUR15", "Order Placed");
            Cnt1=Cnt0;
        }
        //~~~~~~~~~~
    }
    //~~~~~~~~~~
return(Cnt0);}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
ubzen:

You need variables which keeps their values.

I'll warn against using Cnt_1 as some form of Stamp because Orders could close and the Value of Cnt_0 would change.

Instead check if orders processed at the point of entry and send email at that time.

Here's the code, I haven't tested its a rough copy.


Thank you for your reply. My EA places lots of limit buys and limit sells. Only 1/5 of them become actual trades and it could take 15 - 30 minutes to do so. I would like something that will notify me only when I am actually in a trade.

I did not know "OrderTypes" are numbered. I can use OrderType() < 2 to replace (OrderType() == OP_BUY || OrderType() == OP_SELL). I learned something, thank you. However, for the task at hand, I don't see how to modify your code to get what I need. I am not an experienced programer, so please bear with me. ---Tom

 

In that case use Cnt1 but just update it when orders close. The main thing here is using a variable for Cnt which saves the values.

Been a long trading night so I hope I got it right this time.

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int My_Magic=777, Cnt1; //--Global variables save their values and avaliable to functions
                        //outside the start()
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int start(){
    //~~~~~~~~~~
    OrderEvent_Tracking();
    //~~~~~~~~~~
return(0);}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int OrderEvent_Tracking(){ int Cnt0; //--No longer Static so it'll not keep going up by ++
    //~~~~~~~~~~
    for(int i=OrdersTotal()-1;i>=0;i--){
        if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true
            && OrderSymbol()==Symbol() 
            && OrderMagicNumber()==My_Magic
            && OrderType()<2
        ){
            Cnt0++;
        }
    }
    Print(Cnt0,"___",Cnt1);
    //~~~~~~~~~~
    if(Cnt0>Cnt1){
        PlaySound("alert.wav");
        SendMail("WPR_EUR15", "Order Placed");
        Cnt1=Cnt0;
    }
    //~~~~~~~~~~
    if(Cnt0<Cnt1){
        PlaySound("alert.wav");
        SendMail("WPR_EUR15", "Order Closed");
        Cnt1=Cnt0;
    }
    //~~~~~~~~~~
return(Cnt0);}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 

updated for clarity, added OrderType < 2, and results of Print statement. I think part of my issue is maybe I can't trouble shoot email in a backtest? Notice time stamp. However, code isn't working in real trading either.

Note: this EA splits the trade into 2 trades with different TP targets. Thus going from 0 trades to 2 trades all at once. Also, it loss the trade go from 2 open trades to 0 all at once.

Print statement results:

2011.03.21 17:45 WPR_EUR15exp EURUSD,M15: 0 0

2011.03.21 17:45 WPR_EUR15exp EURUSD,M15: 0 0

2011.03.17 11:41 WPR_EUR15exp EURUSD,M15: 2 2

2011.03.17 11:41 WPR_EUR15exp EURUSD,M15: 2 2

2011.03.17 11:41 WPR_EUR15exp EURUSD,M15: 2 2

2011.03.17 11:41 WPR_EUR15exp EURUSD,M15: 2 2

2011.03.15 22:35 WPR_EUR15exp EURUSD,M15: 0 0

2011.03.15 22:35 WPR_EUR15exp EURUSD,M15: 0 0

Expected Print statement results

2011.03.21 17:45 WPR_EUR15exp EURUSD,M15: 0 0

2011.03.21 17:45 WPR_EUR15exp EURUSD,M15: 0 0

2011.03.17 11:41 WPR_EUR15exp EURUSD,M15: 2 0

2011.03.17 11:41 WPR_EUR15exp EURUSD,M15: 2 2

2011.03.17 11:41 WPR_EUR15exp EURUSD,M15: 2 2

2011.03.17 11:41 WPR_EUR15exp EURUSD,M15: 0 2

2011.03.15 22:35 WPR_EUR15exp EURUSD,M15: 0 0

2011.03.15 22:35 WPR_EUR15exp EURUSD,M15: 0 0

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int      MagicNumber   = 12115;
int      TradeCnt      = 0;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int start()
{
 TradeCnt = Notification(TradeCnt);   // Send email and play sound when in trade 
 return(0);
}

// -------------------Notification on Trade Entry-----------------------------------------------

int Notification(int PrevTradeCnt )
  {
    int NewTradeCnt = 0;
  
    for( i = OrdersTotal()-1; i >= 0 ; i--)
      {
        OrderSelect(i,SELECT_BY_POS);
        if (  OrderSymbol() == Symbol() && OrderMagicNumber()  == MagicNumber  && OrderType() < 2  ) NewTradeCnt++;     // OrderType() < 2 = (OrderType() == OP_BUY || OrderType() == OP_SELL) 
      }
     
     Print( PrevTradeCnt,"  ",NewTradeCnt);
     
     if (NewTradeCnt > PrevTradeCnt)
      {
        PlaySound("alert.wav");
        SendMail("WPR_EUR15", "In Trade");
      }
      
     
     return(NewTradeCnt);  
  }  
 
My guess is that it's got something to do with print not showing everything. Investigate the log file in addition. I'll go ahead and test your code on my side.
 
ubzen:

In that case use Cnt1 but just update it when orders close. The main thing here is using a variable for Cnt which saves the values.

Been a long trading night so I hope I got it right this time.



ubzen, I will try this. It looks promising. I posted below before I saw your new code. I will let you know outcome. Thanks for all your time. ---Tom
 

Your code works. I attached my log. Print statement sometimes Skips in back-tester so it's not reliable. Looking at the log is best. Sure try my code out, and let me know :) ... edit* Hum didn't attach, I'll just paste.

16:41:48 Test started for testing
16:41:48 2010.01.03 23:00  Test EURUSD,M5: open #1 buy stop 1.00 EURUSD at 1.4341 ok
16:41:48 2010.01.03 23:00  Test EURUSD,M5: open #2 sell stop 1.00 EURUSD at 1.4315 ok
16:41:48 2010.01.03 23:00  Test EURUSD,M5: 0  0
blah... blah... blah... In-Between
16:42:03 2010.01.03 23:58  Tester: order #2, sell 1.00 EURUSD is opened at 1.4315
16:42:03 2010.01.03 23:58  Test EURUSD,M5: 0  1
16:42:03 2010.01.03 23:58  Test EURUSD,M5: 1  1
16:42:03 2010.01.03 23:59  Test EURUSD,M5: 1  1
16:42:03 2010.01.03 23:59  Test EURUSD,M5: 1  1
16:42:03 2010.01.03 23:59  Test EURUSD,M5: 1  1
16:42:03 2010.01.04 00:00  Test EURUSD,M5: 1  1
16:42:03 2010.01.04 00:00  Test EURUSD,M5: 1  1
16:42:03 2010.01.04 00:00  Test EURUSD,M5: 1  1
16:42:03 2010.01.04 00:00  Test EURUSD,M5: 1  1
16:42:03 2010.01.04 00:01  Test EURUSD,M5: 1  1
16:42:03 2010.01.04 00:01  Test EURUSD,M5: 1  1

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int      MagicNumber   = 12115;
int      TradeCnt      = 0;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int start()
{
 if(OrdersTotal()==0){
    OrderSend(Symbol(),OP_BUYSTOP,1,Ask+12*Point,2,0,0,0,MagicNumber,0,Blue);
    OrderSend(Symbol(),OP_SELLSTOP,1,Bid-12*Point,2,0,0,0,MagicNumber,0,Red);
 }
 TradeCnt = Notification(TradeCnt,MagicNumber);   // Send email and play sound when in trade 
 return(0);
}

// -------------------Notification on Trade Entry-----------------------------------------------

int Notification(int PrevTradeCnt, int MagicNumber)
  {
    int NewTradeCnt = 0;
  
    for( int i = OrdersTotal()-1; i >= 0 ; i--)
      {
        OrderSelect(i,SELECT_BY_POS);
        if (  OrderSymbol() == Symbol() && OrderMagicNumber()  == MagicNumber  && OrderType() < 2  ) NewTradeCnt++;     // OrderType() < 2 = (OrderType() == OP_BUY || OrderType() == OP_SELL) 
      }
     
     Print( PrevTradeCnt,"  ",NewTradeCnt);
     
     if (NewTradeCnt > PrevTradeCnt)
      {
        PlaySound("alert.wav");
        SendMail("WPR_EUR15", "In Trade");
      }
      
     
     return(NewTradeCnt);  
  }  

Reason: