I cant fix the EA help

 
this is my mt5 EA it has a recovery function.it trades one trade per day and i want to make is if it loses the first trade the recovery trade enters at the opposite direction with more lots so far i made it but i need the EA also to not taka a recover trade if the first trade is profitable but i cant make it

 
help pls 

#include <Trade/Trade.mqh>
#include <Object.mqh>  // 

input double StopLoss = 3000;//Stop Loss in points
input double TakeProfit = 6000;//Take Profit in points
input double RiskPerTrade = 0.1; //Lot size for original trade
input bool UseRecoveryTrade = true; // Enable or disable recovery trade
input double RecoveryLotMultiplier = 6; //Lot size multiplier for recovery trade
input double RecoveryStopLoss = 3000; // Points for recovery trade Stop Loss
input double RecoveryTakeProfit = 6000; // Points for recovery trade Take Profit
input int StartHour = 12; // Start hour (UTC+6)
input int StartMinute = 10; // Start minute (UTC+6)
input bool UseTrailingStop = true; // Enable or disable trailing stop
input int TslTriggerPoints = 5000;
input int TslPoints = 10000;
input bool UseTrailingStop2 = true; // Enable or disable trailing stop 2
input int TslTriggerPoints2 = 6000; // A seccond TslTriggerPoints for better results 
input int TslPoints2 = 12000; // A seccond TslPoints for better results 


double LastTradePrice;
double LastTradeLot;
bool LastTradeIsBuy;
bool IsTradeExecutedToday = false;
bool IsRecoveryTradeExecutedToday = false; // Flag to track if a recovery trade executed today
bool IsRecoveryTradeOpen = false; // Flag to track if a recovery trade is already open

// New variables
datetime LastTradeDay;

int OnInit() {
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason) {
}

void OnTick() {

   if(UseTrailingStop)
     {
      
     
   for(int i = PositionsTotal() - 1; i >= 0; i--)
   {
      ulong posTicket = PositionGetTicket(i);

      if(PositionSelectByTicket(posTicket))
      {
         if(PositionGetString(POSITION_SYMBOL) == _Symbol)
         {
            double posOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
            double posSl = PositionGetDouble(POSITION_SL);
            double posTp = PositionGetDouble(POSITION_TP);
            
            double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
            double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

            if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
            {
               if(bid > posOpenPrice + TslTriggerPoints * _Point)
               {
                  double sl = bid - TslPoints * _Point;

                  if(sl > posSl)
                  {
                     CTrade trade;

                     if(trade.PositionModify(posTicket, sl, posTp))
                     {
                        Print(__FUNCTION__, " > Pos #", posTicket, " was modified...");
                     }
                  }
               }
            }
            else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
            {
               if(ask < posOpenPrice - TslTriggerPoints * _Point)
               {
                  double sl = ask - TslPoints * _Point;

                  if(sl < posSl || posSl == 0)
                  {
                     CTrade trade;

                     if(trade.PositionModify(posTicket, sl, posTp))
                     {
                        Print(__FUNCTION__, " > Pos #", posTicket, " was modified...");
                     }
                  }
               }
            }
         }
      }
   }
   }
   
   
   
   
  ////
  if(UseTrailingStop)
     {
  for(int i = PositionsTotal() - 1; i >= 0; i--)
{
   ulong posTicket = PositionGetTicket(i);

   if(PositionSelectByTicket(posTicket))
   {
      if(PositionGetString(POSITION_SYMBOL) == _Symbol)
      {
         double posOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
         double posSl = PositionGetDouble(POSITION_SL);
         double posTp = PositionGetDouble(POSITION_TP);
         
         double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
         double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

         if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
         {
            if(bid > posOpenPrice + TslTriggerPoints2 * _Point) // Use the new trigger points
            {
               double sl = bid - TslPoints2 * _Point; // Use the new points

               if(sl > posSl)
               {
                  CTrade trade;

                  if(trade.PositionModify(posTicket, sl, posTp))
                  {
                     Print(__FUNCTION__, " > Pos #", posTicket, " was modified...");
                  }
               }
            }
         }
         else if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
         {
            if(ask < posOpenPrice - TslTriggerPoints2 * _Point) // Use the new trigger points
            {
               double sl = ask - TslPoints2 * _Point; // Use the new points

               if(sl < posSl || posSl == 0)
               {
                  CTrade trade;

                  if(trade.PositionModify(posTicket, sl, posTp))
                  {
                     Print(__FUNCTION__, " > Pos #", posTicket, " was modified...");
                  }
               }
            }
         }
      }
   }
}
}   
   
   
   
   datetime currDate = TimeCurrent();
   MqlDateTime mql;
   TimeToStruct(currDate, mql);

 
   if (LastTradeDay != 0 && LastTradeDay == mql.day) {
      IsTradeExecutedToday = true;
   } else {
      IsTradeExecutedToday = false;
      IsRecoveryTradeExecutedToday = false; 
   }

   double sma = iMA(Symbol(), PERIOD_H4, 20, 0, MODE_SMA, PRICE_CLOSE);
   double distance = MathAbs(SymbolInfoDouble(Symbol(), SYMBOL_BID) - sma) / _Point;

  
   double lot = RiskPerTrade;

 
   int gmtOffset = TimeGMTOffset();
   
   // Calculate the UK time
   int ukHour = mql.hour - gmtOffset;
   int ukMinute = mql.min;

 
   if (!PositionSelect(Symbol())) {
    
      if (LastTradePrice != 0 && ((LastTradeIsBuy && SymbolInfoDouble(Symbol(), SYMBOL_BID) <= LastTradePrice - StopLoss * _Point) ||
    (!LastTradeIsBuy && SymbolInfoDouble(Symbol(), SYMBOL_ASK) >= LastTradePrice + StopLoss * _Point))) {
   
    if (UseRecoveryTrade && !IsRecoveryTradeExecutedToday && !IsRecoveryTradeOpen) {
       
        if (LastTradeIsBuy) {
            Sell(LastTradeLot * RecoveryLotMultiplier, RecoveryStopLoss, RecoveryTakeProfit);
        } else {
            Buy(LastTradeLot * RecoveryLotMultiplier, RecoveryStopLoss, RecoveryTakeProfit);
        }

       
        IsRecoveryTradeExecutedToday = true;
        IsRecoveryTradeOpen = true;
    }


      } else if (!IsTradeExecutedToday && distance >= 50) {
         
         if (ukHour >= StartHour && ukMinute >= StartMinute) {
            if (SymbolInfoDouble(Symbol(), SYMBOL_BID) > sma) {
               Buy(lot, StopLoss, TakeProfit); 
               LastTradeIsBuy = true; 
            } else {
               Sell(lot, StopLoss, TakeProfit); 
               LastTradeIsBuy = false; 
            }

            
            LastTradeDay = mql.day;
            IsTradeExecutedToday = true;
         }
      }
   }
}

void Buy(double lot, double sl, double tp) {
   if (lot < 0.1) {
      lot = 0.1;
   }

   MqlTradeRequest request;
   MqlTradeResult result;

   ZeroMemory(request);

   request.action = TRADE_ACTION_DEAL;
   request.symbol = Symbol();
   request.volume = lot;
   request.type = ORDER_TYPE_BUY;

   request.price = SymbolInfoDouble(Symbol(), SYMBOL_ASK);

   request.sl = NormalizeDouble(request.price - sl * _Point, _Digits);
   request.tp = NormalizeDouble(request.price + tp * _Point, _Digits);

   if (!OrderSend(request, result)) {}
   else {
      LastTradePrice = request.price;
      LastTradeLot = lot; 
      LastTradeIsBuy = true; 
      IsRecoveryTradeExecutedToday = false; 
      IsRecoveryTradeOpen = false; 
   }
}

void Sell(double lot, double sl, double tp) {
   if (lot < 0.1) {
      lot = 0.1;
   }

   MqlTradeRequest request;
   MqlTradeResult result;

   ZeroMemory(request);

   request.action = TRADE_ACTION_DEAL;
   request.symbol = Symbol();
   request.volume = lot;
   request.type = ORDER_TYPE_SELL;

   request.price = SymbolInfoDouble(Symbol(), SYMBOL_BID);

   request.sl = NormalizeDouble(request.price + sl * _Point, _Digits);
   request.tp = NormalizeDouble(request.price - tp * _Point, _Digits);

   if (!OrderSend(request, result)) {}
   else {
      LastTradePrice = request.price;
      LastTradeLot = lot; 
      LastTradeIsBuy = false; 
      IsRecoveryTradeExecutedToday = false; 
      IsRecoveryTradeOpen = false; 
   }
}

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Янко Караиванов:
this is my mt5 EA it has a recovery function.it trades one trade per day and i want to make is if it loses the first trade the recovery trade enters at the opposite direction with more lots so far i made it but i need the EA also to not taka a recover trade if the first trade is profitable but i cant make it

 
help pls 

You have to start by checking the history for the most recently closed position.
Then check if it is a profit or loss 
If loss, apply your recovery 
If profit, then do nothing 
 
Chioma Obunadike #:
You have to start by checking the history for the most recently closed position.
Then check if it is a profit or loss 
If loss, apply your recovery 
If profit, then do nothing 

thank you bro thats what i thought but how to make is into the code

 

In order to know if the position was profitable, you could compare the positions profit with the account equity and then set a flag to true if the position was profitable (before the position closes) 


//global variables
bool sufficient_gain = false;
double current_equity = 0;
      

void OnTick() {   
        
current_equity = AccountInfoDouble(ACCOUNT_EQUITY);    
        
        for(int i = PositionsTotal() - 1; i >= 0; i--)
        {
              ulong posTicket = PositionGetTicket(i);
              double pos_profit = PositionGetDouble(POSITION_PROFIT);
                
               if(pos_profit > (2/100)*current_equity){  // if your position is profitable by more than 2% of account equity (for example)
               
                  sufficient_gain = true;
               }        
        }

}


then you will make conditions with this new flag

    if (!sufficient_gain && !IsRecoveryTradeExecutedToday && !IsRecoveryTradeOpen) {
       
        if (LastTradeIsBuy) {
            Sell(LastTradeLot * RecoveryLotMultiplier, RecoveryStopLoss, RecoveryTakeProfit);
        } else {
            Buy(LastTradeLot * RecoveryLotMultiplier, RecoveryStopLoss, RecoveryTakeProfit);
        }

       
        IsRecoveryTradeExecutedToday = true;
        IsRecoveryTradeOpen = true;
    }
    else if(sufficient_gain){
          //the position was profitable

          if(sufficient_gain){
                // do something for the case that the position was profitable

                  sufficient_gain = false; //reset the flag again at the end of this condition
          }
    }


I haven't tested your code or anything, but I have used this POSITION_PROFIT in my own EA and it worked like this

 
Conor Mcnamara #:

In order to know if the position was profitable, you could compare the positions profit with the account equity and then set a flag to true if the position was profitable (before the position closes) 



then you will make conditions with this new flag


I haven't tested your code or anything, but I have used this POSITION_PROFIT in my own EA and it worked like this


thanks bro I love the mql5 community I will test it out when I come home

Reason: