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

 

First of all, it is not clear from your post whether variables are global or local.

Any loops that include closing trades should count down, not up. You should at least read every new post on this forum, then you would know that.

Your whole block of code hinges on this

  if(UseDailyTarget==true && my_equity>=overall_total && TimeCurrent()>=trade_hour) 

 So check that before executing the loop.

What is this meant to do?

TimeCurrent()>=trade_hour

 It will always be true

 

What happened to tomorrow_midnight? 

  recommence_trading=today_midnight+trade_hour*3600;

 

Once you have sorted this, you should then code to check whether the OrderClose is successful or not. If not, then retry.

 

I see you are telling return(0) or return(1). That means that you are using functions to make your program; in that case, what we are seeing here is a part of a single function? If so why are you declaring all those variables inside that function? Some of them would normally be in the global space. That been said, there are many ways to write your software, but you have to keep in mind that engineering your idea is propably the most time consuming part of all the work that has to be done in order to complete your program (see: figure out how to do things).

Regarding your last post, it looks like there are not enough informations for us to help you out as  the critical part should be what are you are doing with the value returned. I am assuming that return(0) will make something such as trade_disabled=true and return(1) will make a false. But you must work out - or post here some more code on how you are using trade_disabled

 best regards 

 

edit:  GumRai, my post may seem out of space regarding your last post  but i noticed the presense of it only after submitting mine - I was stuck in page 2 to be precise. Apologies for that :)

 

Hi,

 

Yet to read the answers. How about placing the order sending function in an "if" block checking for a flag. Then after closing all positions for the day, set the flag to false. Then, for every tick, check if the flag is false. If the flag is false, check what is current time. If the current time is the "next day pre-decided time", then the flag is set to true.

As I understand, once the advisor is activated, onTick function will be invoked until the advisor is deactivated.

Now, checking other replies from "Gurus". :)

Warm wishes

 

Amitabh. 

 
@William Roeder I really appreciated your effort here... I need a code that would close all trades (whether in profit or loss)on my ea one hour thirty minutes before the close of the day and and resume two hours after the opening of the market...Thanks in advance for your support
 

Hello Programmer.im stuck with this code. i want to stop trading for the day and continue the next trading day if the sum of close loss and open loss reaches certain amount like say -$100.




	          
 

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
//Looping through close loss position
double GetTodayCloseLoss(){

   int OrdLoss=0;
   double TotalLoss=0.0;
   int hist_count=OrdersHistoryTotal();
   // loop through all orders 
   for(int i=hist_count-1;i>-1;i--)
   {
      // each order need to be selected before refering to its data
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) break; 
      if(OrderType()==OP_BUY || OrderType()==OP_SELL)
      {
         if(OrderProfit()<0.0)
         { 
            OrdLoss ++ ;
            TotalLoss += OrderProfit()+OrderSwap()+OrderCommission();    
         }
         
      }      
   }  
   Print(" OrdLoss= ",OrdLoss," TotalLoss= ",DoubleToStr(TotalLoss,2));
   return( OrdLoss);
  
   }
//Looping through Openloss Position
 
 double getTodayOpenPl(){
 int OpenProf=0,OpenLoss=0;
double TotalOpenProf=0.0,TotalOpenLoss=0.0;
// loop through all orders 
for(int i=0;i<OrdersTotal();i++)
{
   // each order need to be selected before refering to its data
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; 
   if(OrderProfit()>0.0)
   { 
      OpenProf ++ ;
      TotalOpenProf += OrderProfit();      
   }
   else
   {
      OpenLoss ++ ;
      TotalOpenLoss +=  OrderProfit()+OrderSwap()+OrderCommission();
   }
}
  Print("OpenProf= ",OpenProf," TotalOpenProf= ",DoubleToStr(TotalOpenProf,2)," OpenLoss= ",OpenLoss," TotalOpenLoss= ",DoubleToStr(TotalOpenLoss,2));
  return(OpenProf && OpenLoss);
}

   
  }
//+------------------------------------------------------------------+
 
 
09036028683:

Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.

Reason: