DEAL_REASON_SL no longer recognized?

 

I have a function for finding the price and reason for the last deal to have taken place. I made a change, it stopped working, so I changed it back...and it still wasn't working. I reverted to an older version and it STILL IS NOT WORKING. How is this possible? Oh yeah and backtesting on history data keeps freezing now, even on previous, untouched stable versions. 

New version (works for everything except dreason==DEAL_REASON_SL is never triggered):

   ulong             dticket;
   ulong             last_ticket;
   string            dsymbol;
   long              dmagic;
   double            dcom;
   double            dprice;
   long              dtype;
   double            dvolume;
   datetime          dtime;
void OnTrade()                        
 {
    if(started) TradeProcessor(); 
    
 }
void TradeProcessor()
  {
   ResetLastError();
//--- download trading history from the specified interval to the program cache
   int dreason=0;
   long entry;
   bool select_pos=pos_info.Select(m_Symbol);
   if(select_pos)
     {
      bool selected=HistorySelect(pos_info.Time(),TimeCurrent());
      if(!selected)
        {
         PrintFormat("%s. Failed to load history from %s to %s to cache. Error code: %d",
                     __FUNCTION__,TimeToString(pos_info.Time()),TimeToString(TimeCurrent()),GetLastError());
         return;
        }
      //--- get the current values

      int deals=HistoryDealsTotal();
      string name;
        
      //--- for all deals
      for(int i=deals-1; i>=0 ; i++)
        {
         //--- try to get deals ticket
         if((dticket=HistoryDealGetTicket(i))>0)
           {
            //--- get deals properties
            dtime= (datetime)HistoryDealGetInteger(dticket,DEAL_TIME);
            dsymbol= HistoryDealGetString(dticket,DEAL_SYMBOL);
            dreason= (int)HistoryDealGetInteger(dticket,DEAL_REASON);
            dmagic=  HistoryDealGetInteger(dticket,DEAL_MAGIC);
            dcom=    HistoryDealGetDouble(dticket,DEAL_COMMISSION);
            dprice=  HistoryDealGetDouble(dticket,DEAL_PRICE);
            dtype=   HistoryDealGetInteger(dticket,DEAL_TYPE);
            entry= HistoryDealGetInteger(dticket,DEAL_ENTRY);
            dvolume= HistoryDealGetDouble(dticket,DEAL_VOLUME);
            //--- only for current symbol

            if(dticket!=last_ticket)
             {
              if(dreason==(DEAL_REASON_EXPERT) && dsymbol==m_Symbol)    ///<<-----This works!
               {
                name="Ticket #_"+string(dticket);
                Print("Expert deal: ",pos_info.Comment(),"\n",name," Magic: ",dmagic," Commission: $",DoubleToString(dcom,2),
                "Ordercount: ",Ordercounter);
                last_ticket=dticket;
                totalcommission+=dcom;
                break;
                }
              
            if(dreason==(DEAL_REASON_CLIENT) && dsymbol==Symbol())    //<<---This works!
              {
               rebootresume(dcom,dticket,dprice,dtime,1);
               break;
               }
              }
            if(dreason==(DEAL_REASON_SL) && dsymbol==Symbol())   //<<--This doesn't work, dreason==(DEAL_REASON_SL) never returns true.
              {
               PlaySound("alert2.wav");                          //it never gets inside to playsound
               last_ticket=dticket;
               break;
               }
             
             

           }
        }
      return;
     }
  }
Testing trading strategies on real ticks
Testing trading strategies on real ticks
  • www.mql5.com
The article provides the results of testing a simple trading strategy in three modes: "1 minute OHLC", "Every tick" and "Every tick based on real ticks" using actual historical data.
 
Try removing the break statements.

You should find another way of limiting your loop.

Here is a closing bracket to much, it should be below the last if statement, if I am right.
( m_Symbol are unknown)

               }
              }
            if(dreason==(DEAL_REASON_SL) && dsymbol==Symbol())   //<<--This doesn't work, dreason==(DEAL_REASON_SL) never returns true.
              {
               

 

m_Symbol==Symbol(). I changed them to Symbol() for clarity but forgot to change all of them.

I tried removing the break statements but no change.

It recognized the expert deals and the client deals, but still not when a stop loss is triggered.
 
Code?
 

I figured it out. It was selecting the position before searching for a deal. There is no position when a stop is triggered.

Here's the working code with the change. Thanks for your help!

void TradeProcessor()
  {
   ResetLastError();
//--- download trading history from the specified interval to the program cache
   bool select_pos=pos_info.Select(m_Symbol);               ///////removed if(select_pos)<<<--
   bool selected=HistorySelect(0,TimeCurrent());           //////changed HistorySelect start time to 0 from pos_info.Time()
      if(!selected)
        {
         PrintFormat("%s. Failed to load history from %s to %s to cache. Error code: %d",
                     __FUNCTION__,TimeToString(pos_info.Time()),TimeToString(TimeCurrent()),GetLastError());
         return;
         }
      //--- get the current values

      int deals=HistoryDealsTotal();
      string name;
       
      //--- for all deals
      for(int i=deals-1; i>=0 ; i++)
        {
         //--- try to get deals ticket
         if((dticket=HistoryDealGetTicket(i))>0 && dticket!=last_ticket)
           {
            //--- get deals properties
            dtime= (datetime)HistoryDealGetInteger(dticket,DEAL_TIME);
            dsymbol= HistoryDealGetString(dticket,DEAL_SYMBOL);
            dreason= (int)HistoryDealGetInteger(dticket,DEAL_REASON);
            dmagic=  HistoryDealGetInteger(dticket,DEAL_MAGIC);
            dcom=    HistoryDealGetDouble(dticket,DEAL_COMMISSION);
            dprice=  HistoryDealGetDouble(dticket,DEAL_PRICE);
            dvolume= HistoryDealGetDouble(dticket,DEAL_VOLUME);
            //--- only for current symbol

            if(dreason==(DEAL_REASON_EXPERT) && dsymbol==m_Symbol)
               {
                name="Ticket #_"+string(dticket);
                Print("Expert deal: ",pos_info.Comment(),"\n",name,");
                }
              
            if(dreason==(DEAL_REASON_CLIENT) && dsymbol==m_Symbol)
              {
               rebootresume(dcom,dticket,dprice,dtime,1);
               }
              
            if(dreason==(DEAL_REASON_SL) && dsymbol==m_Symbol)
              {
               PlaySound("alert2.wav");
               }
            }
        }
      return;
     
  }
 
Good.

You need to limit your for loop. Currently it is passing all history, every time.

Use datetime to limit your History select.

Remember in a static datetime your last run, check for new deals since then.


 
Dominik Christian Egert #:
Good.

You need to limit your for loop. Currently it is passing all history, every time.

Use datetime to limit your History select.

Remember in a static datetime your last run, check for new deals since then.


datetime day[1];
   day[0]=-1;
   CopyTime(Symbol(),PERIOD_D1,1,1,day);
   ResetLastError();
//--- download trading history from the specified interval to the program cache
   bool select_pos=pos_info.Select(m_Symbol);
   bool selected=HistorySelect(day[0],TimeCurrent());
int deals=HistoryDealsTotal();
      string name;
      datetime dtime;
      string dsymbol;
      int dreason;
      double dprofit;
      //--- for all deals
      for(int i=deals-1; i<=deals ; i++)

But is the loop even necessary if I just want the most recent deal info?

Either way, this dramatically sped up the program. I went back over other for loops in my code and realized this is a recurring problem of loops being limited by break statements but still sometimes searching through history unnecessarily. I thought maybe this is what was causing my debugger to freeze and sure enough, that is working again too. Thanks for the help. Improving my coding skills everyday!

 
Welcome. NP

A tip on loops

Write them like this:


for(int cnt = NULL; (cnt < max) && !_StopFlag; cnt++)

while( (x < y)
&& (!_StopFlag) )

 

And your history, you remember the first/newest entry you process.

And you stop when you reach it, starting by the current newest, and compare the date with your saved date.

You'll get the idea...
static datetime last_processed = NULL;

int cnt = NULL;
while( (last_processed < orders[cnt].open_time)
&& (!_StopFlag))

{
cnt++;
}

last_processed = orders[0].open_time;


 
skylarwalker:

I have a function for finding the price and reason for the last deal to have taken place. I made a change, it stopped working, so I changed it back...and it still wasn't working. I reverted to an older version and it STILL IS NOT WORKING. How is this possible? Oh yeah and backtesting on history data keeps freezing now, even on previous, untouched stable versions. 

New version (works for everything except dreason==DEAL_REASON_SL is never triggered):

for(int i=deals-1; i>=0 ; i++)
shouldn't it be i-- ?! it's going up instead of going down