Open Trade maxTrades failure

 

Good day everyone


I please need someones advise on the following. I have the attached EA that I have made. Everything is working fine but there are one problem that I cant seem to resolve and I have really tried my best. I have a maxTrade function so that when my "sellcondition" is met the EA needs to open the maxTrades that has been selected. Once the max Trades were opened then the EA needs to wait untill the market has droped below my sell condition first then wait for the sellcondition to be reached again and open the maxTrades again even if the other trades are still open. Im trying to create a crash500 EA and lets say the sell condition is met I need it to open 4 (maxTrades) sell trades. If the market then drops but does not close my open max trades yet but goes up again to the sellcondition it should again open the 4 (maxTrades) even if the previous ones are open. I manage to get the maxTrades to work but it open the maxTrades in total and not each time the sellcondition is met. I have been struggeling now for weeks with this. Please please is there anyone who can assist .

void OnTick() {

//+----------------USER INFO DISPLAY LIVE INFO------------------------------+


// get the info needed for the chart output
{
  string AccountServer=AccountInfoString(ACCOUNT_SERVER);
  string AccountName=AccountInfoString(ACCOUNT_NAME);
  long AccountLogin=AccountInfoInteger(ACCOUNT_LOGIN);
  string AccountCompany=AccountInfoString(ACCOUNT_COMPANY);
  double AccountMarginFree=AccountInfoDouble(ACCOUNT_MARGIN_FREE);
  double AccountProfit=AccountInfoDouble(ACCOUNT_PROFIT);
    
 
// create a chart output

  Comment(
             "Account Name: ",              AccountName,"\n",          
             "Account Login:",              AccountLogin,"\n",
             "Account Server:",             AccountServer,"\n",
             "Trial Version: ",             ExpirationTime ? "true" : "false", "\n",
             "Expiration Time: ",           ExpirationTime ? TimeToString(ExpirationTime) : "Never","\n",
             "Account Margin Free:",        AccountMarginFree,"\n",
             "Account Profit:",             AccountProfit,"\n"                      
          );
             
  }
 

 
// Updating the trailing stop on tick here
   if (InpApplyTrailingStop)
    {
     UpdateTrailingStopSimple(TrailingStop);
    }

// Create an array for several RSI values
   double RSIArray[];
   ArraySetAsSeries(RSIArray, true);

// Define the properties of the RSI
   int RSIDefinition = iRSI(_Symbol,_Period, 15, PRICE_WEIGHTED);

// Copy the RSI values into the array
    CopyBuffer(RSIDefinition, 0, 0, 3, RSIArray);

// Get the RSI value for the current candle
    double RSIValue = RSIArray[0];

// Get the fast and slow MA values for bar 1 and bar 2
    if (!FillBuffers())
        return;
   SellCondition = (RSIValue >= 83 && (BufferFastMA[1] < BufferSlowMA[1]) && (BufferFastMA[2] < BufferSlowMA[2]));
   
    
    if (SellCondition)
    {
        TradeOpen(ORDER_TYPE_SELL, StopLoss, TakeProfit);
    }

    // Check if slow MA is below the fast MA and close all positions
    
    if (BufferSlowMA[1] < BufferFastMA[1] && CloseSellPositions)
    {
        CloseAllPositions();
    }
   
    }
void TradeOpen(ENUM_ORDER_TYPE type, double stopLoss = 0, double takeProfit = 0) {

// Check if its time to create trade
   if (TimeCurrent() >= ExpirationTime){
   ExpirationTime = TimeCurrent()+ InpNextCandleOpen *60;
 
 

   
// Maximum allowed trades
 
  const int maxTrades =4;
  int openTrades = 0;
  for (int i = 0; i < PositionsTotal(); i++)
    {
       if (SelectPosition(i))
        openTrades++;
    }
// Chck if the maximum trades limit has been reached
   if (openTrades >= maxTrades)
   return; 

   double price;
   double sl = 0;
   double tp = 0;
 
   if (type == ORDER_TYPE_SELL) {
   price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
   if (stopLoss > 0) sl = price + stopLoss;
   if (takeProfit > 0) tp = price - takeProfit;
   
   
   }
   
 price = NormalizeDouble(price, Digits());
 sl    = NormalizeDouble(sl, Digits());
 tp    = NormalizeDouble(tp, Digits());
 
 if (!Trade.PositionOpen(Symbol(), type, InpOrderSize, price, sl, tp, InpTradeComment)) 
   {
    Print("Open failed for %s, %s, price=%f, sl=%f, tp=%f", Symbol(), EnumToString(type), price, sl, tp);
   }
  }
  }
  void CloseAllPositions()
{
    for (int i = 0; i < PositionsTotal(); i++)
    {
        if (SelectPosition(i)&& PositionInfo.PositionType()==POSITION_TYPE_SELL)
        {
            Trade.PositionClose(PositionInfo.Ticket());
        }
    }
}

    // Trailing Stop Functions
 void UpdateTrailingStopSimple(double trailingStopGap) {

 double sellTSPrice = SymbolInfoDouble(Symbol(), SYMBOL_ASK) + trailingStopGap;
 UpdateTrailingStop( sellTSPrice);
  }

 void UpdateTrailingStop( double sellTSPrice)
 {
  for (int i = PositionsTotal() - 1; i >= 0; i--)
   {
    if (!SelectPosition(i)) continue;

 if (PositionInfo.PositionType() == POSITION_TYPE_SELL)
  {
   if ((PositionInfo.StopLoss() == 0 || sellTSPrice < PositionInfo.StopLoss()) && (!true || sellTSPrice <= PositionInfo.PriceOpen())) 
     {
       Trade.PositionModify(PositionInfo.Ticket(), sellTSPrice, PositionInfo.TakeProfit());
     }
   
    
    }
    
   }
   
  }
 
  
 bool SelectPosition(int index)
 {
  if (!PositionInfo.SelectByIndex(index)) return false;
  return (PositionInfo.Symbol() == Symbol() && PositionInfo.Magic() == InpMagic);
  }

 bool FillBuffers()
 {
  int valuesRequired = 3;
  if (CopyBuffer(HandleFastMA, 0, 0, valuesRequired, BufferFastMA) < valuesRequired) 
   {
    Print("Insufficient results from fast MA");
    return false;
   }
  if (CopyBuffer(HandleSlowMA, 0, 0, valuesRequired, BufferSlowMA) < valuesRequired)
   {
    Print("Insufficient results from slow MA");
    return false;
   }
 
    return true;
   }
 double PointsToDouble(int points)
  {
   return points * Point(); // just number of points * size of a point
  }
 
   ArraySetAsSeries(RSIArray, true);
   int RSIDefinition = iRSI(_Symbol,_Period, 15, PRICE_WEIGHTED);
    CopyBuffer(RSIDefinition, 0, 0, 3, RSIArray);

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
          How to call indicators in MQL5 - MQL5 Articles (2010)

 

One suggestion :

Delete all statements that are not related to the questions and clarify the statements that are related to the questions  , making it easier to solve