Closed the trade based on Time duration

 

Hello,

I made the code, which close old trade after X time from Order Open Price.

My code :

void StopOrderSL(int trade_close_magic)
  {
   for(int pos_0 = OrdersTotal() - 1; pos_0 >= 0; pos_0--)
     {
      OrderSelect(pos_0, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() != Symbol() || OrderMagicNumber() != trade_close_magic)
         continue;
      ResetLastError();
      if(OrderType() == OP_BUY)
        {
         datetime BuyStop_orderOpenTime = OrderOpenTime();
         int BuyStop_hoursDifference = (TimeCurrent() - BuyStop_orderOpenTime) / 3600;
         if(BuyStop_hoursDifference >= Stop_hours && Bid <= OrderOpenPrice() - Stop_SL * Point)
           {
            ObjectDelete("B"+OrderTicket());
            if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
               Print(__FUNCTION__ " => Buy Order failed to close, error code: ", GetLastError());
           }
        }
      if(OrderType() == OP_SELL)
        {
         datetime SellStop_orderOpenTime = OrderOpenTime();
         int Sell_hoursDifference = (TimeCurrent() - SellStop_orderOpenTime) / 3600;
         if(Sell_hoursDifference >= Stop_hours && Ask >= OrderOpenPrice() + Stop_SL * Point)
           {
            ObjectDelete("S"+OrderTicket());
            if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
               Print(__FUNCTION__ " => Sell Order failed to close, error code: ", GetLastError());
           }
        }
     }
  }

I asked ChatGTP to check for errors and it given me the code like this :


void StopOrderSL(int trade_close_magic)
  {
   for(int pos_0 = OrdersTotal() - 1; pos_0 >= 0; pos_0--)
     {
      OrderSelect(pos_0, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol() != Symbol() || OrderMagicNumber() != trade_close_magic)
         continue;
      datetime orderOpenTime = OrderOpenTime();
      int hoursDifference = (TimeCurrent() - BuyStop_orderOpenTime) / 3600;
      ResetLastError();
      if(OrderType() == OP_BUY)
        {
         if(hoursDifference >= Stop_hours && Bid <= OrderOpenPrice() - Stop_SL * Point)
           {
            ObjectDelete("B"+OrderTicket());
            if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
               Print(__FUNCTION__ " => Buy Order failed to close, error code: ", GetLastError());
           }
        }
      if(OrderType() == OP_SELL)
        {
         if(hoursDifference >= Stop_hours && Ask >= OrderOpenPrice() + Stop_SL * Point)
           {
            ObjectDelete("S"+OrderTicket());
            if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE))
               Print(__FUNCTION__ " => Sell Order failed to close, error code: ", GetLastError());
           }
        }
     }
  }


I have two question :

1. which one is more reliable and faster? mine version or ChatGTP?.

2. Can i use OrderOpenPrice directly like this int hoursDifference = (TimeCurrent() - OrderOpenTime()) / 3600;

 
I got your point. But can you please tell me is my code is correct or it requires any changes?
 
anuj71:

Hello,

I made the code, which close old trade after X time from Order Open Price.

My code :

I asked ChatGTP to check for errors and it given me the code like this :



I have two question :

1. which one is more reliable and faster? mine version or ChatGTP?.

2. Can i use OrderOpenPrice directly like this int hoursDifference = (TimeCurrent() - OrderOpenTime()) / 3600;

just add the seconds of stop hours to your order entry time and compare current time against the elapsed order time
 

Hi

IMO your code looks better – AI code has a mistake in syntax and names (for a start).

Yours seems to be OK. You can use OrderOpenTime directly in the hour difference formula. Though if you want to make sure that the trade would be closed. I would use some bigger slippage in OrderClose.

Best Regards

Reason: