Need help on CloseOnTime EA

[Deleted]  

Hi - i am looking for an ea that closes all orders at a specific time.

Can anybody help me out with this ea? - it does not do anything on my charts.

Thank you very much. Maurice

//---- input parameters
extern int CloseHour=23;
extern int CloseMinute=55;

int start() {
int cnt, ticket, total;
if (Hour() == CloseHour && Minute() >= CloseMinute) {
total=OrdersTotal();
for (cnt=0; cnt < total ;cnt++) {
if (OrderType() == OP_BUY) {
OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);
return(0); // exit
}
if (OrderType() == OP_SELL) {
OrderClose(OrderTicket(), OrderLots(), Ask, 3, Yellow);
return(0); // exit
}
}
}
return(0);
}

Alexander
2441
Alexander  

Very close. Just replace:

for (cnt=0; cnt < total ;cnt++) {
if (OrderType() == OP_BUY) {
OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);
return(0); // exit
}
if (OrderType() == OP_SELL) {
OrderClose(OrderTicket(), OrderLots(), Ask, 3, Yellow);
return(0); // exit
}

to

for (cnt=total-1; cnt >=0 ;cnt--) {
RefreshRates();
if (OrderType() == OP_BUY) {
OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow
}
if (OrderType() == OP_SELL) {
OrderClose(OrderTicket(), OrderLots(), Ask, 3, Yellow);

}

[Deleted]  

Sorry Roger, that won't work.

- You forgot the OrderSelect().

- And you need to fix the missing right parenthesis and semi-colon after the first "Yellow" in your response.

- I'd also use TimeHour(TimeCurrent()) and TimeMinute(TimeCurrent()).


Maurice2trade, try this instead:


//---- input parameters
extern int CloseHour=23;
extern int CloseMinute=55;

//----

int start()

{

int cnt, total;
if ((TimeHour(TimeCurrent()) == CloseHour) && (TimeMinute(TimeCurrent()) >= CloseMinute))

{
total=OrdersTotal()-1;
for (cnt=total; cnt>=0; cnt--)

{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

if (OrderType() == OP_BUY)

{

OrderClose(OrderTicket(), OrderLots(), Bid, 3, Yellow);
return(0);

}

if (OrderType() == OP_SELL)

OrderClose(OrderTicket(), OrderLots(), Ask, 3, Yellow);
}
}
return(0);
}

[Deleted]  

Cloudbreaker, it works perfect now! Thank you very much. Roger, your help is much appreciated!

Maurice

[Deleted]  

Guys, i have an additional question - do you know of an ea that can be used as a timer. Same as above but now, close last opened position after x minutes.

That would be the ultimate tool - since i have a scalping robot that works very good, but the chance of a succestrade diminishes very fast is the trade does not attain it's take profit target in 90 minutes or so.

greetings from a sunny Amsterdam - Maurice

Nikolay Panev
1253
Nikolay Panev  
maurice2trade:

Guys, i have an additional question - do you know of an ea that can be used as a timer. Same as above but now, close last opened position after x minutes.

That would be the ultimate tool - since i have a scalping robot that works very good, but the chance of a succestrade diminishes very fast is the trade does not attain it's take profit target in 90 minutes or so.

greetings from a sunny Amsterdam - Maurice

Save your entry time when opening a trade:


OrderSend(Symbol(),....);

datetime EntryTime = TimeCurrent();


Then check if X minutes have passed to close the trade this way:


if(TimeCurrent()>EntryTime+XMins*60)

{

OrderClose(....);

}


It's not that precise because sometimes there are no new ticks for a period of time.


Regards

[Deleted]  

Hi Robofx,

could you put these lines into full ea code for me, since i am not yet skilled in programming ea. Also in another tread you answered jmskates, who posted a similar question some time ago. He posted his code, is getting errors. Maybe you can fix his code and at the same time help me?

Thanks, Maurice

Nikolay Panev
1253
Nikolay Panev  
maurice2trade:

Hi Robofx,

could you put these lines into full ea code for me, since i am not yet skilled in programming ea. Also in another tread you answered jmskates, who posted a similar question some time ago. He posted his code, is getting errors. Maybe you can fix his code and at the same time help me?

Thanks, Maurice

This is the code that will close the last opened order after x Minutes. You can add this code to other EA or use it as a separate expert. Just put the code into start() function.


   //---- Some variables
   int      xMinutes=15;
   datetime LastOrderTime=0;
   int      LastOrderTicket;
   int      SlipPagePips = 2;
   
   
   // Find the last opened trade
   for(int k=OrdersTotal()-1;k>=0;k--)
   {
      if(OrderSelect(k,SELECT_BY_POS,MODE_TRADES))
      {
         if(OrderOpenTime()>LastOrderTime) 
         {
            LastOrderTime=OrderOpenTime();
            LastOrderTicket=OrderTicket();
         }
      }
   }
   
   // Check if x mintues have passed since last order was opened
   if(TimeCurrent()>(LastOrderTime+xMinutes*60))
   {
      //Select the last trade
      if(OrderSelect(LastOrderTicket,SELECT_BY_TICKET,MODE_TRADES))
      {
         if(OrderType()==OP_BUY)
         {
            OrderClose(LastOrderTicket,OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),SlipPagePips,Yellow);
         }
         if(OrderType()==OP_SELL)
         {
            OrderClose(LastOrderTicket,OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),SlipPagePips,Yellow);
         }
       }
    }
[Deleted]  
Works perfect ! Yhanks very much!