Exit after N-Daily Bars

 
Hi
I need to close orders after "n" days without count swap orders
in other words I want to close all losing orders after 5 or more days
Should I manage orders with Array ?
My EA is multi STOP orders type with Expiration entry after 5 days
so I don't know when trade is open
Let's to explain my solution :

-In global space :
-declare two counters
int LongCounter,ShortCounter;
-prepare a structure TradeStruct :
*datetime a_OpenDate;
*double a_OpenPrice;
*double a_UTradeIdNr;
-Declare two empty arrays :
TradeStruc LongArray[];
TradeStruc ShortArray[];

-At very begin of EA:
-Loop through Orders to find any NO swap orders
-Find opened/closed yesterday orders
-increase counter on Open and copy data (Date,OpenPrice,MagicNr) / descrease counter On Close
-Resize LongArray[LongCounter]/ShortArray[LongCounter] according to own type and counter
-Into EA :
  ...
  //Loop through opened Orders
  total=OrdersTotal();
  ...
    
        if(OrderType()==OP_BUY )
      {
          for(int i=0;i<LongCounter;i++)
         { 
            if(OrderMagicNumber()==LongArray[i].a_UTradeIdNr)
            {
                 double Profit=Ask-LongArray[i].a_OpenPrice;
                 double DeltaDays=SecondsToDays(CurTime()-LongArray[i].a_OpenDate);
                 if(DeltaDays >=5 && Profit < 0)
                 {
                     result=OrderClose(...);
                     break;
                 }
            }
         }
     }
 ...
  if(ConditionToBuy)
 {
    double UTradeMagic=CreateUniqueMagicNr();
    result=(OrderSend(Symbol(),OP_BUYSTOP,......,UTradeMagic,....);
 }
 



Any suggestion
Tks
B.-

 

Hmmm, 


There are some elements you have to define before going further:

 

1. Five days could include weekends. Do you mean tradeable days or calendar days? 

2. Will there be more than one trade for the same currency? If so, do you want the profits/losses accumulated for all the trades for the same currency?

3. Do you define a losing trade as a trade that is "in the red" (PL < 0.0) or  either "in the red" or when PL = 0.0?

Baiano :
Hi
I need to close orders after "n" days without count swap orders
in other words I want to close all losing orders after 5 or more days
Should I manage orders with Array ?
My EA is multi STOP orders type with Expiration entry after 5 days
so I don't know when trade is open
Let's to explain my solution :

-In global space :
-declare two counters
int LongCounter,ShortCounter;
-prepare a structure TradeStruct :
*datetime a_OpenDate;
*double a_OpenPrice;
*double a_UTradeIdNr;
-Declare two empty arrays :
TradeStruc LongArray[];
TradeStruc ShortArray[];

-At very begin of EA:
-Loop through Orders to find any NO swap orders
-Find opened/closed yesterday orders
-increase counter on Open and copy data (Date,OpenPrice,MagicNr) / descrease counter On Close
-Resize LongArray[LongCounter]/ShortArray[LongCounter] according to own type and counter
-Into EA :

  ...
  //Loop through opened Orders
  total=OrdersTotal();
  ...
    
        if(OrderType()==OP_BUY )
      {
          for(int i=0;i<LongCounter;i++)
         { 
            if(OrderMagicNumber()==LongArray[i].a_UTradeIdNr)
            {
                 double Profit=Ask-LongArray[i].a_OpenPrice;
                 double DeltaDays=SecondsToDays(CurTime()-LongArray[i].a_OpenDate);
                 if(DeltaDays >=5 && Profit < 0)
                 {
                     result=OrderClose(...);
                     break;
                 }
            }
         }
     }
 ...
  if(ConditionToBuy)
 {
    double UTradeMagic=CreateUniqueMagicNr();
    result=(OrderSend(Symbol(),OP_BUYSTOP,......,UTradeMagic,....);
 }
 



Any suggestion
Tks
B.-