Take profit and stop loss in percentage

 
Good afternoon you all,

I have this Expert Advisor in which I can set take profit and stop loss in points. I want to change this parameter with the percentage of the investiment. For example, when the profit is at 25% of the amount invested or when the loss is at -70% of the amount invested the investiment will close. I hope someone will help me. Have a good day.


//---- input parameters
extern string   time_trader_v2="Nirvish Time Trader";
extern double   Lots=0.1;
extern int      TakeProfit=6;
extern int      StopLoss=100;
extern int      Slip=5;
extern string   TradeSettings="Mt4 time(min-max): hours 0-23, minutes 0-59, seconds 0-59";
extern bool     AllowBuy=true;
extern bool     AllowSell=true;
extern int      TradeHour=0;
extern int      TradeMinutes=0;
extern string   MagicNumbers="To be changed in case of conflict with other EAs";
extern int      BuyMagicNumber =10001;
extern int      SellMagicNumber =10002;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   Alert("Nirvish Time Trader");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert starts                                  |
//+------------------------------------------------------------------+

int start()
  {
//----
int StopMultd,Sleeper=1;



int digits=MarketInfo("EURUSD",MODE_DIGITS);
StopMultd=10;
double TP=NormalizeDouble(TakeProfit*StopMultd,Digits);
double SL=NormalizeDouble(StopLoss*StopMultd,Digits);
int Slippage=Slip*StopMultd;

// Calculate stop loss
double slb=NormalizeDouble(Ask-SL*Point,Digits); //(Ask-(95*Ask/(100*Lots)),Digits)
double sls=NormalizeDouble(Bid+SL*Point,Digits); // 

// Calculate take profit
double tpb=NormalizeDouble(Ask+TP*Point,Digits);
double tps=NormalizeDouble(Bid-TP*Point,Digits);

//-------------------------------------------------------------------+
//Check open orders
//-------------------------------------------------------------------+
if(OrdersTotal()>0){
  for(int i=1; i<=OrdersTotal(); i++)          // Cycle searching in orders
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
        {
          if(OrderMagicNumber()==BuyMagicNumber) {int halt1=1;}
          if(OrderMagicNumber()==SellMagicNumber) {int halt2=1;}
        }
     }
}
//-------------------------------------------------------------------+


if((halt1!=1)&&(AllowBuy==true)){// halt1

// Buy criteria
if ((TradeHour==Hour())&&(TradeMinutes==Minute())) //Signal Buy
 {
   int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"time trader buy order ",BuyMagicNumber,0,Blue);
   if(openbuy<1){int buyfail=1;}
 }
 
}// halt1
 
if((halt2!=1)&&(AllowSell==true)){// halt2
RefreshRates();
 // Sell criteria
 if ((TradeHour==Hour())&&(TradeMinutes==Minute())) //Signal Sell
 {
   int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"time trader sell order ",SellMagicNumber,0,Green);
   if(opensell<1){int sellfail=1;}
 }
 
}// halt2

//-----------------------------------------------------------------------------------------------------
if(OrdersTotal()>0){
  for(i=1; i<=OrdersTotal(); i++){          // Cycle searching in orders
  
      if (OrderSelect(i-1,SELECT_BY_POS)==true){ // If the next is available
        
          if((OrderMagicNumber()==BuyMagicNumber)&&(OrderTakeProfit()==0||OrderStopLoss()==0)) { OrderModify(OrderTicket(),0,slb,tpb,0,CLR_NONE); }
          if((OrderMagicNumber()==SellMagicNumber)&&(OrderTakeProfit()==0||OrderStopLoss()==0)) { OrderModify(OrderTicket(),0,sls,tps,0,CLR_NONE); }

        }
     }
}
//-------------------------------------------------------------------+
// Error processing
//-------------------------------------------------------------------+
if(buyfail==1||sellfail==1){
int Error=GetLastError();
  if(Error==130){Alert("Wrong stops. Retrying."); RefreshRates();}
  if(Error==133){Alert("Trading prohibited.");}
  if(Error==2){Alert("Common error.");}
  if(Error==146){Alert("Trading subsystem is busy. Retrying."); Sleep(500); RefreshRates();}

}


//-------------------------------------------------------------------
   return(0);
  }
//+-----------------------------------
Basic Principles - Trading Operations - MetaTrader 5
Basic Principles - Trading Operations - MetaTrader 5
  • www.metatrader5.com
is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels. is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is performed at the...
 
Trader1529: I have this Expert Advisor in which I can set take profit and stop loss in points. I want to change this parameter with the percentage of the investiment. For example, when the profit is at 25% of the amount invested or when the loss is at -70% of the amount invested the investiment will close.
  1. No, you have a EA that sets a TP/SL price.
  2. There is no "amount invested."

  3. For MT4: In code: Risk depends on your initial stop loss, lot size, and the value of the pair.
    1. You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    2. Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers.
    4. You must normalize lots properly and check against min and max.
    5. You must also check FreeMargin to avoid stop out
    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5=0.1 Lots maximum.
 
whroeder1:
  1. No, you have a EA that sets a TP/SL price.
  2. There is no "amount invested."

  3. For MT4: In code: Risk depends on your initial stop loss, lot size, and the value of the pair.
    1. You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    2. Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers.
    4. You must normalize lots properly and check against min and max.
    5. You must also check FreeMargin to avoid stop out
    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5=0.1 Lots maximum.

Ohh I see, thank you for your exhaustive answer.

I have one question. I want an EA in which I can set:

  1. The percentage of the initial capital to invest
  2. The take profit and the loss in percentage
  3. The position to open (long or short)
but I don't know how to write an EA. Can you please help me?

 
Trader1529: I want but I don't know how to write an EA. Can you please help me?

Help you with what? You haven't stated a problem, you stated a want.
You have only four choices:
  1. Search for it,
  2. Beg at Will code your trading systems for free - Free Expert Advisors - Trading Systems - MQL5 programming forum, or Coding help - MQL4 and MetaTrader 4 - MQL4 programming forum, or Need help with coding - General - MQL5 programming forum, or Free MQL4 To MQL5 Converter - General - MQL5 programming forum, or Requests & Ideas (MQL5 only!).
  3. learn to code it. If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.
  4. or pay (Freelance) someone to code it.
We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
          No free help
          urgent help.
Reason: