I want my EA to stop after hitting (Target_Profit_of_this_Session)

 

Hi Everyone,



Below is my code. I want my EA to stop once it will hit the target. 

I am not talking about any particular duration just it will stop the EA. 

For new execution/initialisation of the trade, Have to remove the EA from the chart and again drag and drop it from the Expert list.


So once the (Target_Profit_of_this_Session) hit the given value. My EA should stop. 



if (AccountProfit()>= Target_Profit_of_this_Session)
   {
    for(int i=OrdersTotal()-1;i>=0;i--)
       {
       OrderSelect(i, SELECT_BY_POS);
       int type   = OrderType();
               
       bool result = false;
              
       switch(type)
          {
          //Close opened long positions
          case OP_BUY  : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),10,Pink);
                         break;
               
          //Close opened short positions
          case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),10,Teal);
                          
          }
          
       if(result == false)
          {
            Print ("Account Profit Reached. All Open Trades Have Been Closed");
      
             return(0);
          }  
       }
      
  
   


  return(0);
}
 
Ujjawal Banerjee:

Hi Everyone,

Below is my code. I want my EA to stop once it will hit the target. 

I am not talking about any particular duration just it will stop the EA. 

For new execution/initialisation of the trade, Have to remove the EA from the chart and again drag and drop it from the Expert list.

So once the (Target_Profit_of_this_Session) hit the given value. My EA should stop.

What about ExpertRemove()?

Documentation on MQL5: Common Functions / ExpertRemove
Documentation on MQL5: Common Functions / ExpertRemove
  • www.mql5.com
ExpertRemove - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Carl Schreiber #:

What about ExpertRemove()?

Here EA will remove based on tick or no execution of trades. But i want my EA to get close after achieving that Target PROFIT. 

So is there any such easy way. Please help me out.

 
Ujjawal Banerjee #:

Here EA will remove based on tick or no execution of trades. But i want my EA to get close after achieving that Target PROFIT. 

So is there any such easy way. Please help me out.

And what shall the EA do after reaching Target Profit?

If you program:

bool ProfitReached = false;
...
void OnTick() {
    if (ProfitReached) return;
}
if (AccountProfit()>= Target_Profit_of_this_Session) {
    ...
    ProfitReached = true;
}

You have to stop and restart your EA anyway.

 
Carl Schreiber #:

And what shall the EA do after reaching Target Profit?

If you program:

You have to stop and restart your EA anyway.

My main intension is to stop EA once it reached the Target Profit. 

Suppose my todays target got it. And i am not infront of my system for a day or 2 days. So in that case EA should not start any trade in those time. 

When i will be available and infront of my system then i will restart manually my EA by just drag and drop. 

 
You could try this tactic:

Take a snapshot of the account equity as soon as the EA starts

Check current equity against the start equity every tick, if current equity is >= start equity, close all open positions and set a variable to disallow any further trading.

 Code snippet examples:

double StartEquity;
int ProfitTarget = 50;
bool TradingEnabled = true;
bool CheckedStartEquity = false;

//do this check once, at start of EA session
if (!CheckedStartEquity)
  {
  StartEquity = AccountEquity();
  CheckedStartEquity = true;
  }

//check equity after every tick
if (AccountEquity() >= StartEquity + ProfitTarget)
  {
     //Close all orders
     OrderClose(ticket,Lots,Bid,0,Red);
     //....
     //....
     
     //disable further trading
     TradingEnabled = false;
  }

//trading logic loop
if (TradingEnabled)
    {
     //TRADING LOGIC

    }

as Ujjawal said, you will need to reset the EA after every session, unless you add code to execute at a certain time to reset all these values.

 
Lachlan Meakin #:
You could try this tactic:

Take a snapshot of the account equity as soon as the EA starts

Check current equity against the start equity every tick, if current equity is >= start equity, close all open positions and set a variable to disallow any further trading.

 Code snippet examples:

as Ujjawal said, you will need to reset the EA after every session, unless you add code to execute at a certain time to reset all these values.

The shared code is not working. 

Reason: