If last order is a loss => Sleep() function - page 2

Keith Watford
Moderator
26186
Keith Watford  
8tango:

This function find if the last close order is a profit or loss:

Did you read my post?

Keith Watford:

Again, your code does not necessarily find the last trade.

Orders History is not necessarily in chronological order. William has a link so maybe he will post it when he sees this.

8tango
285
8tango  
Keith Watford:

Did you read my post?

Yes I readed it but who is William and how contact him? Can he help me with my function?

Keith Watford
Moderator
26186
Keith Watford  
8tango:

Yes I readed it but who is William and how contact him? Can he help me with my function?

I doubt that he will help you other than telling you the same as I have already. The only thing is that he has a list of links.

Somewhere in the documentation it does state that you should not assume that the history is in chronological order.

string LastCloseOrder(int magic)
{
   datetime lastOrderTime=0;
   
   string last;
   for(int i=(OrdersHistoryTotal()-1);i>=0;i--)
   {
      if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY))
      {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic)
         {
            //RefreshRates();   //There is no need for RefreshRates here.
            //Check OrderCloseTime() and if it is later than lastOrderTime, update lastOrderTime and last else continue
            if(OrderProfit()>0){last="profit";}
            if(OrderProfit()<0){last="loss";}
            break; 
         }
      }
   }
   return(last);
}
8tango
285
8tango  
Keith Watford:

I doubt that he will help you other than telling you the same as I have already. The only thing is that he has a list of links.

Somewhere in the documentation it does state that you should not assume that the history is in chronological order.

If it can help someone, this funcion work fine:

extern int DelayMinOpenAfterLoss = 1; // Stop trading after a loss. DelayMinOpenAfterLoss in minutes

void OnTick()
{
  if(LastCloseOrder(MagicNumber)==false){}
  else{OpenNewOrder();}
}

bool LastCloseOrder(int magic)
{
   int maxDuration = 60*DelayMinOpenAfterLoss; // 60*1 = 1 minute
   for(int i=(OrdersHistoryTotal()-1);i>=0;i--)
   {
      if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY))
      {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic)
         {
            datetime CloseTime = OrderCloseTime();
            int NewOpenTime = CloseTime + maxDuration;                     
            if(TimeCurrent() < NewOpenTime && OrderProfit()<0){return(false);}
            else{return(true);}             
         }
      }
   }
   return(true);
}