EA close all open position at $

 

Hi I have trying to close my EA for all open positions as soons as profit reaches to lets say $30.

I created input variable and function but it actually not working. Can someone guide what's missing there.

input double StopProfit = 30.0;

void OnTick()
{
   double profit = AccountInfoDouble(ACCOUNT_PROFIT);
   if (profit >= StopProfit)
      ExpertRemove();
}
 
harry.jh:

Hi I have trying to close my EA for all open positions as soons as profit reaches to lets say $30.

I created input variable and function but it actually not working. Can someone guide what's missing there.

This is interesting topic.. i would like to learn, maybe somebody here can explain more
 
harry.jh:

Hi I have trying to close my EA for all open positions as soons as profit reaches to lets say $30.

I created input variable and function but it actually not working. Can someone guide what's missing there.

input double StopProfit = 30.0;

void OnTick()
{
   double profit = AccountInfoDouble(ACCOUNT_EQUITY)-AccountInfoDouble(ACCOUNT_BALANCE);
   if (profit >= StopProfit)
      ExpertRemove();
}
 
harry.jh:Hi I have trying to close my EA for all open positions as soons as profit reaches to lets say $30.

I created input variable and function but it actually not working.

  1. Do you really want to close the EA? The trade will still be active and the EA not. Perhaps you mean you want to close the trade.

  2. Do you really expect an answer? There are no mind readers here and our crystal balls are cracked.

    “Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
         How To Ask Questions The Smart Way. (2004)
              When asking about code
              Be precise and informative about your problem

 
Yashar Seyyedin #:

Nope, I also treid it. Not working.

I also written another code. Compiling fine. But again it's also not working. Not sure what's the challenge 

harry.jh:

Hi I have trying to close my EA for all open positions as soons as profit reaches to lets say $30.

I created input variable and function but it actually not working. Can someone guide what's missing there.



// Variables
double starting_balance;
double current_profit;

// Initialization function
int OnInit()
{
   starting_balance = AccountInfoDouble(ACCOUNT_BALANCE);
   return(INIT_SUCCEEDED);
}

// Start function
void OnTick()
{
   current_profit = AccountInfoDouble(ACCOUNT_PROFIT);
   
   // Close EA if profit reaches the target
   if(current_profit - starting_balance >= profit_target)
   {
      ExpertRemove();
   }
}
Help needed:
 
William Roeder #:
  1. Do you really want to close the EA? The trade will still be active and the EA not. Perhaps you mean you want to close the trade.

  2. Do you really expect an answer? There are no mind readers here and our crystal balls are cracked.

    “Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
         How To Ask Questions The Smart Way. (2004)
              When asking about code
              Be precise and informative about your problem

No, I would like to close my EA and all open trade as soon desired profit booked.

 

The ExpertRemove(); built-in function should only be called when a for loop functions checks there are no orders running after the target profit is reached.

Another good pratice to do is setting a "offset profit" just in order to "protect" the balance from slippages or even the delay the Terminal takes to close each order individually during the "close_all_orders" process.

//---- The piece of code below:

// Variables
double starting_balance;
double profit_target;
double offset_profit;

// Initialization function
int OnInit()
{
   starting_balance = AccountInfoDouble(ACCOUNT_BALANCE);
   return(INIT_SUCCEEDED);
}

// Start function
void OnTick()
{
        check_profit();

}

//---    Function triggered when the profit target is reached

void check_profit()
{
        double current_equity=AccountEquity();
        if(current_equity>=(starting_balance+profit_target+offset_profit))check_close();

}

//---    Function to manage the order closing process

void check_close()
{

   close_all_orders();

   if(check_open_orders()==false) // only if there are no orders open the ExpertRemove() function will be called
   {
   Print("Profit target reached. The EA will be removed for safety.");
   ExpertRemove();
   }
   }

}


//---           Function to close all orders

void  close_all_orders()
{
   for(int i=OrdersTotal()-1; i >= 0; i--)
 {
   bool res=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
   if (OrderType()<=1 && OrderMagicNumber()==MAGICMA)
   {
   
   if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,NULL))Print("Order n° "+OrderTicket()+" failed to be closed.");
   
   }
 }
}


//---  Function to check if there are orders open


bool check_open_orders()
{ 


        for( int i = 0 ; i < OrdersTotal() ; i++ ) { 
                
                bool res = OrderSelect( i, SELECT_BY_POS, MODE_TRADES ); 
                
                if (OrderSymbol() == Symbol() && OrderMagicNumber()==MAGICMA) return(true); 
        } 
        
        return(false); 
}
 
I tried soft and hardstop buffer even: not working
{
   double equity = AccountInfoDouble(ACCOUNT_EQUITY);
   double balance = AccountInfoDouble(ACCOUNT_BALANCE);
   double profit = equity - balance;

   if (!SoftStopLossReached && profit >= (StopProfit + SoftStopLossBuffer))
   {
      SoftStopLossReached = true;
   }

   if (SoftStopLossReached || profit >= HardStopLossLevel)
   {
      ExpertRemove();
   }
}
 
Francisco Rayol #:

The ExpertRemove(); built-in function should only be called when a for loop functions checks there are no orders running after the target profit is reached.

Another good pratice to do is setting a "offset profit" just in order to "protect" the balance from slippages or even the delay the Terminal takes to close each order individually during the "close_all_orders" process.

//---- The piece of code below:

Not working

 
Hi anyone can help??
 
harry.jh # :
Hi anyone can help??

There's no reason why the suggested ones shouldn't work. In my opinion, using the freelance section, seek help from an expert coder for a fee.