Close position after X hours - page 2

 
but this forum is for helping people or not?...i'm learning
 

Exactly - this forum is for helping.

Not doing your thinking, coding and testing.

 
Il_Messia:
but this forum is for helping people or not?...i'm learning
I've been trying to help. You haven't posted your code, so there is no more that I can do.
 

ok the code is the following

 

void IfOrderDoesNotExist31()
  {
   bool exists=false;
   for(int i=OrdersTotal()-1; i>=0; i--)
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderType()==OP_BUYSTOP && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
           {
            exists=true;
           }
        }
   else
     {
      Print("OrderSelect() error - ",ErrorDescription(GetLastError()));
     }

   if(exists==false)
     {
      IfTradeContextNotBusy47();
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void IfTradeContextNotBusy47()
  {
   if(!IsTradeContextBusy())
     {
      BuyPendingOrder48();
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void BuyPendingOrder48()
  {
   int expire=(int)TimeCurrent()+60*Expiration;
   double price=NormalizeDouble(Ask,NDigits)+PriceGap*PipValue*Point;
   double SL=price-StopLoss*PipValue*Point;
   if(StopLoss==0) SL=0;
   double TP=price+TakeProfit*PipValue*Point;
   if(TakeProfit == 0) TP = 0;
   if(Expiration == 0) expire = 0;
   int ticket= OrderSend(Symbol(),OP_BUYSTOP,Lots,price,4,SL,TP,"NewsHourTrade",MagicNumber,expire,Blue);
   if(ticket == -1)
     {
      Print("OrderSend() error - ",ErrorDescription(GetLastError()));
     }
   CustomCode10();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CustomCode10()
  {
   newsday=TimeDay(TimeCurrent());
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void exitlong()
   { int OraCorr=(int)TimeCurrent();
     int HourX= TimeHour(OraCorr);
     int MinX= TimeMinute(OraCorr);
     Print("SUPERATO TEMPO MAX SENZA GAIN");
      for (int i = OrdersTotal() - 1; i >= 0; i--)
         {if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         {
             if(OrderType()==OP_BUYSTOP && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
             {
                 if((OrderOpenTime()<=TimeCurrent()-MaxHour*60*60) && (OrderProfit()<0))
                 {
                   bool ret3=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),4,clrRed);
                     if (ret3==false)
                    Print("OrderClose error - ", GetLastError());
                    }
                 }
           }
       }
   }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+              
void CustomIf22()
  {
   if(SellTrade==true)
     {
      Sleep40();
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Sleep40()
  {
   Sleep(DelaySeconds*1000);
   IfOrderDoesNotExist23();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void IfOrderDoesNotExist23()
  {
   bool exists=false;
   for(int i=OrdersTotal()-1; i>=0; i--)
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderType()==OP_SELLSTOP && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
           {
            exists=true;
           }
        }
   else
     {
      Print("OrderSelect() error - ",ErrorDescription(GetLastError()));
     }
//---
   if(exists==false)
     {
      IfTradeContextNotBusy46();
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void IfTradeContextNotBusy46()
  {
   if(!IsTradeContextBusy())
     {
      SellPendingOrder41();
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SellPendingOrder41()
  {
   int expire=(int)TimeCurrent()+60*Expiration;
   double price=NormalizeDouble(Bid,NDigits)-PriceGap*PipValue*Point;
   double SL=price+StopLoss*PipValue*Point;
   if(StopLoss==0) SL=0;
   double TP=price-TakeProfit*PipValue*Point;
   if(TakeProfit == 0) TP = 0;
   if(Expiration == 0) expire = 0;
   int ticket= OrderSend(Symbol(),OP_SELLSTOP,Lots,price,4,SL,TP,"NewsHourTrade",MagicNumber,expire,Red);
   if(ticket == -1)
     {
      Print("OrderSend() error - ",ErrorDescription(GetLastError()));
     }
   CustomCode10();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void exitshort()
   { int OraCorr=(int)TimeCurrent();
     int HourX= TimeHour(OraCorr);
     int MinX= TimeMinute(OraCorr);
       Print("SUPERATO TEMPO MAX SENZA GAIN");
      for (int i = OrdersTotal() - 1; i >= 0; i--)
         {if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         {
             if(OrderType()==OP_SELLSTOP && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
             {
                 if((OrderOpenTime()<=TimeCurrent()-MaxHour*60*60) && (OrderProfit()<0))
                 {
                   bool ret3=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),4,clrRed);
                     if (ret3==false)
                    Print("OrderClose error - ", GetLastError());
                    }
                 }
           }
       }
   }
 
Il_Messia: ok the code is the following
             if(OrderType()==OP_SELLSTOP && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
             {
                 if((OrderOpenTime()<=TimeCurrent()-MaxHour*60*60) && (OrderProfit()<0))
                 {
                   bool ret3=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),4,clrRed);
  1. If the order is pending, you can not close it, you must delete it. If the order is open, it is a sell, not a sell stop.
  2. void IfOrderDoesNotExist23(){
       :
             if(OrderType()==OP_SELLSTOP && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber){
                exists=true;
    What about the case where the order has opened?
  3. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
 

The code that you posted in your first post does not appear in the above code at all

             if(OrderType()==OP_SELLSTOP && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
             {
                 if((OrderOpenTime()<=TimeCurrent()-MaxHour*60*60) && (OrderProfit()<0))

If an order is a OP_SELLSTOP, profit will be zero so profit cannot be less than zero

 
GumRai:

The code that you posted in your first post does not appear in the above code at all

If an order is a OP_SELLSTOP, profit will be zero so profit cannot be less than zero

yeah guys just saw and edited before your comment it, but not workiing yet..
 
WHRoeder:
  1. If the order is pending, you can not close it, you must delete it. If the order is open, it is a sell, not a sell stop.
  2. What about the case where the order has opened?
  3. Check your return codes What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

 1. just editeted in OP_SELL and OP_BUY but doesn't work

2. yes EA must close open trade. It put pending orders on market opening, but delete well the one not executed

3. i'll check it but my english it's not good as you see. I'm about 3 days working on it and no exit.. 

 
Ok just solved with some changes to code....but now backtesting it close trades if not in gain immediately...i'm working