EA keeps on opening trades as conditions are met.

 
Hi, my expert advisor is built using the TD sequential indicator. Whenever a candle hits a '9' a buy or sell order is made. The problem is this condition can occur when still in a trade on the same pair. I would like to only have one trade open per pair. Any help will be appreciated.
#property copyright "Copyright 2021 - Benjamini04"
#property version   "1.00"
#property description "TD Sequential EA opens orders only in levels 9 of TD Sequential."
#property description "Please take in consideration that take profit and stop loss must be in points."
#property description "Breakeven was added in the parameters."
#property description "When the profit has 50% the EA will close partial positicion , 50% of lots."
#property description "This EA uses the TD indicator, it must be in the 'Indicators' folder, with the same name."

#include <Trade/Trade.mqh>
CTrade trade;
input group "TD Sequential EA Settings"
input double Lotsize     = 0.2;
input int    Magicnumber = 8888;
input double TakeProfit  = 6000; //Take Profit Points
input double StopLoss    = 3000;  //Stop Loss Points
//******************************************************************
double tpoint, tradenow, ask, bid,Open[],Low[],High[],Close[];
int    td_handle; 
double td_buffer[];
int    Slippage    = 10;
double level_td_green = 9;
double level_td_red   = 9;
string external_indicator   = "TD" ;
int df = 0;

//+------------------------------------------------------------------+
//| Trade Count                                                      |
//+------------------------------------------------------------------+
int TradeCount(ENUM_TIMEFRAMES TimeFrame)
  {
//---
    

   int      Cnt;
   ulong    Ticket;
   datetime DT[1];

   Cnt = 0;

   if(CopyTime(_Symbol, TimeFrame, 0, 1, DT) <= 0)
   {
      Cnt = -1;
   }
   else
   {
      HistorySelect(DT[0], TimeCurrent());

      for(int i = HistoryDealsTotal() - 1; i >= 0; i--)
      {
         Ticket = HistoryDealGetTicket(i);

         if(HistoryDealGetString(Ticket, DEAL_SYMBOL) == _Symbol)
         {
            if(HistoryDealGetInteger(Ticket, DEAL_ENTRY) == DEAL_ENTRY_IN)
            {
               Cnt++;
            }
         }
      }
   }

   if (PositionsTotal() > 0){
  return(INIT_FAILED);
 }
   return(Cnt);
  }

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{

   
   
//--- trade.SetExpertMagicNumber(Magicnumber)
  
    trade.SetDeviationInPoints(Slippage);
   trade.SetAsyncMode(false);   
   td_handle=iCustom(_Symbol,PERIOD_CURRENT,external_indicator);
   tpoint = Point();
   
   if (PositionsTotal() > 0){
  return(INIT_FAILED);
 }
  

   return(INIT_SUCCEEDED);
   
   
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
   
   
   ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
   bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
   
   CopyBuffer(td_handle,2,0,100,td_buffer);
   ArraySetAsSeries(td_buffer,true);
   
   double td01=NormalizeDouble(td_buffer[1],2);
   
  
   
      
   if(IsNewCandle()) tradenow=1;
   
   if(td01 == level_td_green && tradenow==1)
   {
      trade.Buy(Lotsize,Symbol(),ask,0,0,NULL);
      tradenow=0;
   }
   if(td01 == -level_td_red && tradenow==1)
   {
      trade.Sell(Lotsize,Symbol(),bid,0,0,NULL);
      tradenow=0;
   }
 
   
   setAll();
   
  }
//+-------------------------------------------------------------------------+
//+Void set all fuction: Objective modify trades with tp and sl parameters--+
//+-------------------------------------------------------------------------+
void setAll()
{
   
   int pt = PositionsTotal();
  
   
   for(int i=pt;i>=0;i--)
   {
      ulong tick = PositionGetTicket(i);
      if (PositionSelectByTicket(tick))
      {
         if (PositionGetInteger(POSITION_TYPE)==0 && PositionGetDouble(POSITION_SL)==0)
         {
         double btp=PositionGetDouble(POSITION_PRICE_OPEN)+(TakeProfit*tpoint);
         double bsl=PositionGetDouble(POSITION_PRICE_OPEN)-(StopLoss*tpoint);
         trade.PositionModify(tick,bsl,btp);
         }
         if (PositionGetInteger(POSITION_TYPE)==1 && PositionGetDouble(POSITION_SL)==0)
         {
         double stp=PositionGetDouble(POSITION_PRICE_OPEN)-(TakeProfit*tpoint);
         double ssl=PositionGetDouble(POSITION_PRICE_OPEN)+(StopLoss*tpoint);
         trade.PositionModify(tick,ssl,stp);
         }
      }
   }
  
}
//+-----------------------------------------------------------------------------------------------------+
bool IsNewCandle()
{ 
static int BarsOnChart=0; 
if (Bars(_Symbol,PERIOD_CURRENT) == BarsOnChart) return (false); 
BarsOnChart = Bars(_Symbol,PERIOD_CURRENT); return(true); 
}
 

1. Remove these lines from OnInit:

   if (PositionsTotal() > 0){
  return(INIT_FAILED);

2. Study an example, Calculate Positions and Pending Orders

How to start with MQL5
How to start with MQL5
  • 2020.12.19
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...