Take profit or loss at the end of the day

To add comments, please log in or register
GreggBazin
9
GreggBazin  

Hi there,

I'm a newbie and I have a problem with a part of my code.

I would like to close my trade at 21h45 every day, even if the position is positive or negative.


Here is my portion of code. I don't understand why it's not working:


 
  for(int cnt=0;cnt<OrdersTotal();cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber 
         )  
        {
         if(OrderType()==OP_BUY)  
           {
              if((Hour()<21)&&(Minute()<45)) //here is my close buy rule
              {
                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
              }
           }
         else 
           {
                if((Hour()<21)&&(Minute()>45)) // here is my close sell rule
                {
                   OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
                }
           }
        }
     }
   return(0);
}

int TotalOrdersCount()
{
  int result=0;
  for(int i=0;i<OrdersTotal();i++)
  {
     OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
     if (OrderMagicNumber()==MagicNumber) result++;

   }
  return (result);
}


Thanks in advance for your help and your time !

William Roeder
20208
William Roeder  
GreggBazin: I would like to close my trade at 21h45 every day,
  1. For buy orders, your code closes orders when the hour is between zero and 20 (inclusive) and the minute is less than 45.
  2. For sell orders, your code closes orders when the hour is between zero and 20 (inclusive) and the minute is more than 44.
  3. Move your time tests outside the for loop and test for what you say you want.
  4. No need to test the order type when you use OrderClosePrice.
  5. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol,) you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
              Loops and Closing or Deleting Orders - MQL4 programming forum
  6. You say you want to close "my trade" Do you mean all trades on the current chart — this is what your for loop attempts. It is not what your TotalOrdersCount returns.
GreggBazin
9
GreggBazin  

Thanks William !


I worked on my code and here is a new result, simplier:

input int    MagicNumber=0101;
input double Lots=1;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert start fonction                                         |
//+------------------------------------------------------------------+
int start()
  {

   if((Hour()==09)&&(Minute()==00))
   {
       OrderSend(Symbol(), OP_BUY, Lots, Ask, 2, Ask - 100, Ask + 200, "test achat", MagicNumber, 0, Green);
   }


   if((Hour()==12)&&(Minute()==00))
   {
       OrderSend(Symbol(), OP_SELL, Lots, Bid, 2, Bid + 100, Bid - 200, "test vente", MagicNumber, 0, Red);
   }


for(int cnt=1;cnt<OrdersTotal();cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   
         OrderSymbol()==Symbol() &&
         OrderMagicNumber()==MagicNumber 
         )  
        {
         if(OrderType()==OP_BUY)  
           {
              if((Hour()==20)&&(Minute()==59)) //here is the close buy rule
              {
                   OrderClose(OrderTicket(),Lots,Bid,2,Red);
              }
           }
        }

 else 
           {
               if((Hour()==20)&&(Minute()==59)) // here is the close sell rule
               {
                   OrderClose(OrderTicket(),Lots,Ask,2,Red);
               }
           }
     }

    return(0);
  }



I still have 2 problems:


1/ We have the same position launch more than one tim. In ProRealTime, we have a function wich name is "DEFPARAM CumulateOrders = false" and "IF not longonmarket THEN..."
With these functions, if one trade is open, we can't open a second one, so no mutliple repetitions as I have with my code above...

2/ The position doesn't close at 20h59. I would like to avoid overnight and close the position at 20h59.

What do you think about that?

Any advice? Thanks again for your time

To add comments, please log in or register