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 parametersexternstring time_trader_v2="IoTime Trader";
externdouble Lots=0.1;
externint TakeProfit=60;
externint StopLoss=150;
externint Slip=5;
externstring TradeSettings="Mt4 time(min-max): hours 0-23, minutes 0-59, seconds 0-59";
externbool AllowBuy=true;
externbool AllowSell=true;
externint TradeHour=0;
externint TradeMinutes=0;
externstring MagicNumbers="To be changed in case of conflict with other EAs";
externint BuyMagicNumber =10001;
externint 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 lossdouble slb=NormalizeDouble(Ask-SL*Point,Digits); //(Ask-(95*Ask/(100*Lots)),Digits)double sls=NormalizeDouble(Bid+SL*Point,Digits); // // Calculate take profitdouble 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 criteriaif ((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;}
}
}// halt1if((halt2!=1)&&(AllowSell==true)){// halt2RefreshRates();
// Sell criteriaif ((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 ordersif (OrderSelect(i-1,SELECT_BY_POS)==true){ // If the next is availableif((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);
}
//+-----------------------------------
Good evening 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.