Take profit and stop loss in percentage

302

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 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     AllowSell=true;
extern string   MagicNumbers="To be changed in case of conflict with other EAs";
extern int      SellMagicNumber =10002;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
//----
//----
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 sls=NormalizeDouble(Bid+SL*Point,Digits); //

// Calculate take profit
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()==SellMagicNumber) {int halt2=1;}
}
}
}
//-------------------------------------------------------------------+

{
}

}// halt1

if((halt2!=1)&&(AllowSell==true)){// halt2
RefreshRates();
// Sell criteria
{
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()==SellMagicNumber)&&(OrderTakeProfit()==0||OrderStopLoss()==0)) { OrderModify(OrderTicket(),0,sls,tps,0,CLR_NONE); }

}
}
}
//-------------------------------------------------------------------+
// Error processing
//-------------------------------------------------------------------+
int Error=GetLastError();

}

//-------------------------------------------------------------------
return(0);
}
//+-----------------------------------```
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...
20207

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.
302

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)

20207