Download MetaTrader 5

How do I get my ea to stop trading for the rest of the day after...

To add comments, please log in or register
Write articles and earn money. We pay 200 USD for them!
Opengates
165
Opengates 2015.04.21 21:30 

Hello,

Every Guru in the house,

Please help me concerning this code, I want this code to close all orders when the profit has reached certain level and stop trading for the rest of the day. The code below truly closes all the order as supposed but it continues opening more others, pls enlighten me on this. Can I use "break;" "Sleep(5000000);" or what code do i use to stop it for the rest of the day?

Any useful help would be appreciated.

Thank you

 double Ppt=15;
 //close both buy and sell in profits
 for(int i=OrdersTotal()-1; i>=0; i--) {
 if(!OrderSelect(i,SELECT_BY_POS)) continue;
 if(OrderType()==OP_BUY || OrderType()==OP_SELL) {
 if(CloseOnProfits==true && OrderProfit()>=Ppt) {
 OrdCls=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage);
 if(OrdCls==true) {
 Comment("Block: CloseOnProfits. Order #", OrderTicket()," closed in profit."); 
 return(0);
  } 
    }
      }
     } 
Keith Watford
Moderator
9599
Keith Watford 2015.04.22 02:39  

It depends on what you want the EA to do.

You say that you want it to stop trading for the rest of the day, do you want the EA to resume trading the next day with no input from you?

I've never used it myself, but you could use ExpertRemove() to close the EA and remove it from the chart.

or just create a boolean flag

  static bool trade_allowed=true;
  if(!trade_allowed)
     return;
  
  if(condition_to_stop_trading)
     {
     //Close trades
     trade_allowed=false;
     }

 

 Note that you will need to restart the EA to reset the bool or include another condition to reset it to true

whroeder1
14470
whroeder1 2015.04.22 13:20  
Opengates: stop trading for the rest of the day.
static int HR2400 = PERIOD_D1 * 60; // 86400 = 24 * 3600
int      TimeOfDay(datetime when=0){
   return (when == 0 ? TimeCurrent() : when) % HR2400 );            }
datetime DateOfDay(datetime when=0){
   return (when == 0 ? TimeCurrent() : when) - TimeOfDay(when) );   }
datetime Tomorrow( datetime when=0){
   return DateOfDay(when == 0 ? TimeCurrent() : when) + HR2400);    }
datetime Yesterday(datetime when=0){   DownloadHistory(PERIOD_D1);
   datetime today = DateOfDay(when == 0 ? TimeCurrent() : when);
   int      iD1   = iBarShift(NULL, PERIOD_D1,  today - 1);
   return iTime(NULL, PERIOD_D1, iD1); }
/////////////////////////////////////////////////////////////////////
void OnTick(){
   static datetime tradingAllowed=0;
   if(TimeCurrent() < tradingAllowed) return;
   :
   if( OrderSend(...) ) tradingAllowed = Tomorrow();
Opengates
165
Opengates 2015.04.22 20:13  
WHRoeder:

s

Thank you for your reply. The code you gave above is beyond my scope. This is what I want to achieve: After I have made profit to a certain level, I want the ea to close all opened order and stop trading for the rest of the day, and start again on the dot of 9am the second day. The code I posted above closes all the opened orders but still continues to trade in spite of my using "return(0);". Please help me.

While waiting.

Thanks once again

Keith Watford
Moderator
9599
Keith Watford 2015.04.22 21:50  

You will need to look up the  Ternary Operator to understand WHRoeder's code.

 

You have to be precise when writing your code.

From your posts it is unclear what your exact requirements are. Your posted code closes a single trade when that trade's profit reaches a certain level. What about any other open trades?

It seems that you may want to close all orders when the aggregate profit reaches a certain level, but this is not clear. It is just my assumption. Your program cannot make assumptions, it can only follow instructions.

Write down exactly what you want the EA to do and if necessary, break it into sections that you can work on 

Keith Watford
Moderator
9599
Keith Watford 2015.04.23 06:41  

You may find it easier to understand this bit of code for the time. I have used WHRoeders method of using a datetime instead of a bool.

   int trade_hour=9;
   static datetime recommence_trading=0;

   if(condition_to_stop_trading)
     {
      datetime today_midnight=TimeCurrent()-(TimeCurrent()%(PERIOD_D1*60));
      datetime tomorrow_midnight=today_midnight+PERIOD_D1*60;
      recommence_trading=tomorrow_midnight+trade_hour*3600;
     }

   if(TimeCurrent()<recommence_trading)
      return;

 

Then you have to think about the weekend as any time on Monday will be later than 9am on the Saturday

   int trade_hour=9;
   static datetime recommence_trading=0;
   if(condition_to_stop_trading)
     {
      datetime today_midnight=TimeCurrent()-(TimeCurrent()%(PERIOD_D1*60));
      datetime tomorrow_midnight;
      if(TimeDayOfWeek(TimeCurrent())==5)
         tomorrow_midnight=today_midnight+PERIOD_D1*60*3;
      else
         tomorrow_midnight=today_midnight+PERIOD_D1*60;
      recommence_trading=tomorrow_midnight+trade_hour*3600;
     }

   if(TimeCurrent()<recommence_trading)
      return;

 

You may also need to consider whether it is possible for the condition to stop trading may occur between Midnight and 9am  and you want trading to recommence at 9am the same day

   if(condition_to_stop_trading)
     {
      datetime today_midnight=TimeCurrent()-(TimeCurrent()%(PERIOD_D1*60));
      datetime tomorrow_midnight;
      if(Hour()<trade_hour)
         recommence_trading=today_midnight+trade_hour*3600;
      else
        {
         if(TimeDayOfWeek(TimeCurrent())==5)
            tomorrow_midnight=today_midnight+PERIOD_D1*60*3;
         else
            tomorrow_midnight=today_midnight+PERIOD_D1*60;
         recommence_trading=tomorrow_midnight+trade_hour*3600;
        }
     }

   if(TimeCurrent()<recommence_trading)
      return;

 Not tested

Keith Watford
Moderator
9599
Keith Watford 2015.04.23 06:45  

I realised that you don't really need to check this every tick

so maybe use the if condition

   if(condition_to_stop_trading && TimeCurrent()>=recommence_trading )
Opengates
165
Opengates 2015.04.24 22:21  

Thanks WHRoeders and GumRai,

This is what I want to achieve: 

* The normal daily start-trading-time I set for my ea is 9am and stop-trading-time is 20pm.

*After the profit has reached certain level i.e Acct Equity greater than Acct. balance to my desired amount,

*The ea would close all opened orders and no more open any order until when the start-trading-time comes the next day and it will resume trading normal activities.

I am yet to correlate all the above codes with what I want to achieve. Need a simple codes that will work exactly.

Thanks for your willingness to help a fellow while waiting...

whroeder1
14470
whroeder1 2015.04.25 00:40  
Opengates: Need a simple codes that will work exactly
learn to code it, or pay someone. We're not going to code it FOR you. We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem.
Opengates
165
Opengates 2015.04.26 16:57  

WHRoeder,

I have already posted the one I wrote above, it's just that it didn't work exactly the way I want it to work, that's why I posted it but it seems you don't really understand me.

Thanks anyway. I'm grateful for your time.

whroeder1
14470
whroeder1 2015.04.26 17:44  
I understood you completely. You have not posted any code since my previous post. You just rejected my post and asked for simple codes again. Since you haven't learned the mql4 language, we have nothing to communicate with.
123
To add comments, please log in or register