Take profit

 

Hi to all please how to change take profite form points to levele  like r2 or s2 or any levele, i have camarilla EA the tp in points i want to change it to levele thank you.


#property copyright "Copyright 2023, MetaQuotes Ltd."

#property version   "1.00"



#include  <Trade/Trade.mqh>



input bool Stops = true;

input bool Limits = true;



input double Lots = 0.01;

input double TpPercent = 0.5;

input double SlPercent = 1.0;



input string TimeStart = "00:30";

input string TimeEnd = "18:00";



input ENUM_TIMEFRAMES MaTimeframe = PERIOD_H1;

input int MaPeriods = 200;

input ENUM_MA_METHOD MaMethod = MODE_SMA;

input ENUM_APPLIED_PRICE MaAppPrice = PRICE_CLOSE;



CTrade trade;

int maTrend;

int handleMa;

int barsTotal;



int OnInit()

{



   handleMa = iMA(_Symbol,MaTimeframe,MaPeriods,0,MaMethod,MaAppPrice);

   

   barsTotal = iBars(_Symbol,PERIOD_D1);

   

   OnTick();



   return(INIT_SUCCEEDED);

}



void OnDeinit(const int reason){

   ObjectsDeleteAll(0,"DGT");

}



void OnTick(){



   datetime timeEnd = StringToTime(TimeEnd);

   if(TimeCurrent() >= timeEnd){

      if(closeAndDeleteEverything() > 0){

      

      }

   }



   double profit = 0;

   for(int i = PositionsTotal()-1; i >= 0; i--){

      ulong posTicket = PositionGetTicket(i);

      

      if(PositionSelectByTicket(posTicket)){

         profit += PositionGetDouble(POSITION_PROFIT);

      }      

   }



   double profitTarget = AccountInfoDouble(ACCOUNT_BALANCE) * TpPercent / 100;

   double maxLoss = AccountInfoDouble(ACCOUNT_BALANCE) * SlPercent / 100;

 

   if(profit > profitTarget){

      if(closeAndDeleteEverything() > 0){

         Print(__FUNCTION__," > Reached profit target...");

      }

   }else if(profit < -maxLoss){

      if(closeAndDeleteEverything() > 0){

         Print(__FUNCTION__," > Reached maximum loss...");

      }

   }         

 

   datetime timeStart = StringToTime(TimeStart);

   int bars = iBars(_Symbol,PERIOD_D1);

   if(barsTotal != bars && TimeCurrent() > timeStart){

      barsTotal = bars;

      

      maTrend = getMaTrend();

      

      double highD1 = iHigh(_Symbol,PERIOD_D1,1);

      double lowD1 = iLow(_Symbol,PERIOD_D1,1);

      double closeD1 = iClose(_Symbol,PERIOD_D1,1);

      

      double pp = (highD1 + lowD1 + closeD1) / 3;

      double s1 = closeD1-(highD1-lowD1)*1.1 /6;

      double s2 = closeD1-(highD1-lowD1)*1.1/4;

      double s3 = closeD1-(highD1-lowD1)*1.1 /2;

      double r1 = closeD1+(highD1-lowD1)*1.1 /6;

      double r2 = closeD1+(highD1-lowD1)*1.1 /4;

      double r3 = closeD1+(highD1-lowD1)*1.1 /2;

      

      addLevel("pp",pp);

      addLevel("s1",s1);

      addLevel("s2",s2);

      addLevel("s3",s3);

      addLevel("r1",r1);

      addLevel("r2",r2);

      addLevel("r3",r3);

      

      if(maTrend > 0){

         executeBuy(pp,"pp");

         executeBuy(s1,"S1");

         executeBuy(s2,"S2");

         executeBuy(s3,"S3");

          executeBuy(r1,"R1");

          executeBuy(r2,"R2");

          executeBuy(r3,"R3");

      }else if (maTrend < 0){

          executeSell(pp,"pp");

          executeSell(s1,"S1");

          executeSell(s2,"S2");

           executeSell(s3,"S3");

          executeSell(r1,"R1");

          executeSell(r2,"R2");

          executeSell(r3,"R3");      

       }

    }     

      

   Comment("MA Trend : ",maTrend,"\nProfit: ",DoubleToString(profit,2));

}



ulong executeBuy(double price, string comment){

   double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);

   

   price = NormalizeDouble(price,_Digits);

   

   datetime expiration = StringToTime(TimeEnd);

   

   if(ask > price){

      if(Limits) trade.BuyLimit(Lots,price,_Symbol,0,0,ORDER_TIME_SPECIFIED,expiration,comment);

   }else if(ask < price){

      if(Stops) trade.BuyStop(Lots,price,_Symbol,0,0,ORDER_TIME_SPECIFIED,expiration,comment);

   }

   

   return trade.ResultOrder();

}        



ulong executeSell(double price, string comment){

   double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);

   

   price = NormalizeDouble(price,_Digits);

   

   datetime expiration = StringToTime(TimeEnd);

   

   if(bid > price){

      if(Stops) trade.SellStop(Lots,price,_Symbol,0,0,ORDER_TIME_SPECIFIED,expiration,comment);

   }else if(bid < price){

      if(Limits) trade.SellLimit(Lots,price,_Symbol,0,0,ORDER_TIME_SPECIFIED,expiration,comment);

   }

   

   return trade.ResultOrder();

}      

      

int getMaTrend(){

   double ma[];

   CopyBuffer(handleMa,MAIN_LINE,0,1,ma);



   double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);

   

   if(bid > ma[0]){

      return 1;

   }else if(bid < ma[0]){

      return -1;

   }

   return 0;

}   

 

void addLevel(string name, double price){

   datetime time1 = iTime(_Symbol,PERIOD_D1,0);

   datetime time2 = time1 + PeriodSeconds(PERIOD_D1);

   

   string objName;

   StringConcatenate(objName,"DGT ",name," ",time1," ",price);

   ObjectCreate(0,objName,OBJ_TREND,0,time1,price,time2,price);

}



int closeAndDeleteEverything(){

   int counter = 0;

   

   for(int i = PositionsTotal()-1; i >= 0; i--){

      ulong posTicket = PositionGetTicket(i);

      

      if(trade.PositionClose(posTicket)){

         Print(__FUNCTION__," > Pos #",posTicket," was closed...");

         counter++;

      }

   }                        



   for(int i = OrdersTotal()-1; i >= 0; i--){

      ulong orderTicket = OrderGetTicket(i);

      

      if(trade.OrderDelete(orderTicket)){

         Print(__FUNCTION__," > Order #",orderTicket," was deleted...");

         counter++;

      }

   }    



   return counter;

}          
Reason: