Help with function

 

Hi guys, I have a function(), but am struggling to see what's wrong. The function is supposed to close a trade after not stop lossing or taking profit in 10 days, but it's not running correctly since I have a trade running for more than 30 days in my EA when looking at the holding time scatter plot (Attached)

The code I found and edited is attached here,

void CloseAllPositions()
{
for(int i=PositionsTotal()-1;i>0;i--)
{//go through all positions
int Ticket=PositionGetTicket(i);     //Get ticket # for current position
int PositionDirection = PositionGetInteger(POSITION_TYPE);
if(PositionDirection == POSITION_TYPE_BUY||PositionDirection == POSITION_TYPE_SELL)
{
trade.PositionClose(Ticket);
}
}
}

void OnTick()
  {
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

   datetime from = TimeToString(0,TIME_DATE);
   HistorySelect(from,TimeCurrent()); 
   int Deals=HistoryDealsTotal(); 
   datetime LastDealTime = HistoryDealGetInteger(Deals,DEAL_TIME);                     ///Time of Last Deal
   datetime LastDealTimeStringPlusOneHour = StringToTime(LastDealTime)+1*60*60;//1 hour
   datetime LastDealTimeStringPlus10Days = StringToTime(LastDealTime)+10*24*60*60;//10*24 hours
   bool HourAfter = (TimeCurrent()>LastDealTimeStringPlusOneHour); // delay after trade
  
   if(PositionsTotal()>0&&TimeCurrent()>LastDealTimeStringPlus10Days)
   {
   CloseAllPositions();
   }
}

Basic Principles - Trading Operations - MetaTrader 5 Help
Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels. is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is performed at the...
Files:
SS_1.PNG  34 kb
 
  1.    int Deals=HistoryDealsTotal(); 
       datetime LastDealTime = HistoryDealGetInteger(Deals,DEAL_TIME);                     ///Time of Last Deal
    
    Perhaps you should read the manual. When is a count a ticket?
    long  HistoryDealGetInteger(
       ulong                       ticket_number,     // Ticket
       ENUM_DEAL_PROPERTY_INTEGER  property_id        // Property identifier
       );
              Trade Functions / HistoryDealGetInteger - Reference on algorithmic/automated trading language for MetaTrader 5

  2. datetime LastDealTime = HistoryDealGetInteger(Deals,DEAL_TIME);                     ///Time of Last Deal
    datetime LastDealTimeStringPlusOneHour = StringToTime(LastDealTime)+1*60*60;//1 hour
    What is the purpose of converting a datetime to a string to a datetime?
 

At least this does not look right:

for(int i=PositionsTotal()-1;i>0;i--)
{//go through all positions

When you loop back to front, make sure to catch element at position 0. Your condition needs to be i>=0.

Just my opinion from looking at your code, get help from a freelancer if your project is aiming at real money.
Reason: