Help! multiple trade on one candlestick in the same direction

 
I have this EA am coding in mt5.  still learning Mt5 though. 

The EA takes multiple trade on one candlestick in the same direction, I don't want it so. 

I want only one trade once I have a buy signal and the take profit or stop loss is  hit, no more trade untill the opposite sell signal becomes true. 
And same too for sell signal,  once I have one trade running, once take profit or stop-loss is hit,  I don't want any trade for sell again until buy becomes true.

Need someone to help me out please. 
 
Zadicus5 :
I have this EA am coding in mt5.  still learning Mt5 though. 

The EA takes multiple trade on one candlestick in the same direction, I don't want it so. 

I want only one trade once I have a buy signal and the take profit or stop loss is  hit, no more trade untill the opposite sell signal becomes true. 
And same too for sell signal,  once I have one trade running, once take profit or stop-loss is hit,  I don't want any trade for sell again until buy becomes true.

Need someone to help me out please. 

In the OnTradeTransaction () procedure, you catch the moment when the "DEAL_IN" transaction was recorded in the trading history. At the same time, the opening time of the current bar is remembered.

Now, if there is a signal, you just need to compare the saved time and the opening time of the current bar.

 
Zadicus5:
I have this EA am coding in mt5.  still learning Mt5 though. 

The EA takes multiple trade on one candlestick in the same direction, I don't want it so. 

I want only one trade once I have a buy signal and the take profit or stop loss is  hit, no more trade untill the opposite sell signal becomes true. 
And same too for sell signal,  once I have one trade running, once take profit or stop-loss is hit,  I don't want any trade for sell again until buy becomes true.

Need someone to help me out please. 

Add this code above both buy and sell 'if' statements.

if (PositionsTotal()==0)
           {	//Your code

	}

Kindly note, your EA will scan all trades in that account because of the PositionsTotal() function. That means your EA will not trade if you manually open another trade or if the same EA on a different chart has an open trade. This is because PositionsTotal will then no longer be equal to zero. The workaround is a bit challenging but possible. Use this code to eliminate the immediate problem then get back for more questions later. All the best

 
Note. Close all previous signal positions before the above code to make PositionsTotal()==0
 
Vladimir Karputov :

In the OnTradeTransaction () procedure, you catch the moment when the "DEAL_IN" transaction was recorded in the trading history. At the same time, the opening time of the current bar is remembered.

Now, if there is a signal, you just need to compare the saved time and the opening time of the current bar.

Example:

the variable m_last_deal_time stores the time of the bar at which the position is open.

//+------------------------------------------------------------------+
//|                                             One deal per bar.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
//---
CPositionInfo  m_position;                   // object of CPositionInfo class
CTrade         m_trade;                      // object of CTrade class
CSymbolInfo    m_symbol;                     // object of CSymbolInfo class
//--- input parameters
input ulong    InpDeviation         = 10;          // Deviation, in points (1.00045-1.00055=10 points)
input bool     InpPrintLog          = false;       // Print log
input ulong    InpMagic             = 300;         // Magic number
//---
datetime       m_last_deal_time;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if(!m_symbol.Name(Symbol())) // sets symbol name
     {
      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");
      return(INIT_FAILED);
     }
   RefreshRates();
//---
   m_trade.SetExpertMagicNumber(InpMagic);
   m_trade.SetMarginMode();
   m_trade.SetTypeFillingBySymbol(m_symbol.Name());
   m_trade.SetDeviationInPoints(InpDeviation);
//---
   iTime(m_symbol.Name(),Period(),0);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   datetime time=iTime(m_symbol.Name(),Period(),0);
   if(time!=m_last_deal_time)
      m_trade.Buy(m_symbol.LotsMin(),m_symbol.Name());
  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//--- get transaction type as enumeration value
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
      long     deal_ticket       =0;
      long     deal_order        =0;
      long     deal_time         =0;
      long     deal_time_msc     =0;
      long     deal_type         =-1;
      long     deal_entry        =-1;
      long     deal_magic        =0;
      long     deal_reason       =-1;
      long     deal_position_id  =0;
      double   deal_volume       =0.0;
      double   deal_price        =0.0;
      double   deal_commission   =0.0;
      double   deal_swap         =0.0;
      double   deal_profit       =0.0;
      string   deal_symbol       ="";
      string   deal_comment      ="";
      string   deal_external_id  ="";
      if(HistoryDealSelect(trans.deal))
        {
         deal_ticket       =HistoryDealGetInteger(trans.deal,DEAL_TICKET);
         deal_order        =HistoryDealGetInteger(trans.deal,DEAL_ORDER);
         deal_time         =HistoryDealGetInteger(trans.deal,DEAL_TIME);
         deal_time_msc     =HistoryDealGetInteger(trans.deal,DEAL_TIME_MSC);
         deal_type         =HistoryDealGetInteger(trans.deal,DEAL_TYPE);
         deal_entry        =HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
         deal_magic        =HistoryDealGetInteger(trans.deal,DEAL_MAGIC);
         deal_reason       =HistoryDealGetInteger(trans.deal,DEAL_REASON);
         deal_position_id  =HistoryDealGetInteger(trans.deal,DEAL_POSITION_ID);

         deal_volume       =HistoryDealGetDouble(trans.deal,DEAL_VOLUME);
         deal_price        =HistoryDealGetDouble(trans.deal,DEAL_PRICE);
         deal_commission   =HistoryDealGetDouble(trans.deal,DEAL_COMMISSION);
         deal_swap         =HistoryDealGetDouble(trans.deal,DEAL_SWAP);
         deal_profit       =HistoryDealGetDouble(trans.deal,DEAL_PROFIT);

         deal_symbol       =HistoryDealGetString(trans.deal,DEAL_SYMBOL);
         deal_comment      =HistoryDealGetString(trans.deal,DEAL_COMMENT);
         deal_external_id  =HistoryDealGetString(trans.deal,DEAL_EXTERNAL_ID);
        }
      else
         return;
      ENUM_DEAL_ENTRY enum_deal_entry=(ENUM_DEAL_ENTRY)deal_entry;
      if(deal_symbol==m_symbol.Name() && deal_magic==InpMagic)
         if(deal_type==DEAL_TYPE_BUY || deal_type==DEAL_TYPE_SELL)
            if(deal_entry==DEAL_ENTRY_IN)
               m_last_deal_time=iTime(m_symbol.Name(),Period(),0);
     }
  }
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data                                 |
//+------------------------------------------------------------------+
bool RefreshRates()
  {
//--- refresh rates
   if(!m_symbol.RefreshRates())
     {
      if(InpPrintLog)
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","RefreshRates error");
      return(false);
     }
//--- protection against the return value of "zero"
   if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
     {
      if(InpPrintLog)
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","Ask == 0.0 OR Bid == 0.0");
      return(false);
     }
//---
   return(true);
  }
//+------------------------------------------------------------------+
Files:
 
It is very simple.

You just have to define a small function that determines if a certain candle has already been traded and use the result of this function together with the entry condition of your EA.

Here is the function.

bool funbTradedCandle() {
   
   datetime dtCandleInitDate= iTime(_Symbol, PERIOD_CURRENT, 0);
      
   bool baux = HistorySelect(dtCandleInitDate, TimeCurrent()); //baux is an auxiliary variable, with no meanning
   
   baux = HistoryDealsTotal() > 0;
   
   return baux;
}

To evaluate if the current candle has alrready been traded just call the function like this:


if(!funbTradedCandle() && <Your entry condition>) //"!" operator is very important
{
        <Your trade statement>
}

funbTradedCandle can be improved to evaluate any candle. The version above is the simpliest

Reason: