TradeEventPositionOpened() never executed!

 

Hi,

I'm new in mql5 and I'm trying to following the docs to listen the position opened "event".

The functions like TradeEventPositionOpened() and TradeEventPositionClosed() are never executed.

But the functions like TradeEventOrderTriggered() and TradeEventPositionStopTake() are executed.

For now, I'm overriding the TradeEventOrderTriggered() and checking if there is some position opened.

I'm missing something? 

Just for test, I have the follow code:

// EACustom.mq5 
... 
CExpertCustom expert; 
... 
int OnInit() {
... 
  if(!expert.Init(Symbol(), Period(), Expert_EveryTick, Expert_MagicNumber)) {
    printf(__FUNCTION__ + ": error initializing expert");
    expert.Deinit();
    return(INIT_FAILED);
  }

  expert.OnTradeProcess(true);
...
}

void OnTrade() {
  expert.OnTrade();
}


// ExpertCustom.mqh
class CExpertCustom : public CExpert {
private:
  bool _triggered;
public:
  CExpertCustom();
  ~CExpertCustom();
  bool Init(string symbol, ENUM_TIMEFRAMES period, bool every_tick, ulong magic=0);
  void Deinit(const int reason);
  virtual void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam);
  virtual bool TradeEventOrderTriggered(void); 
  virtual bool TradeEventPositionStopTake(void);
  virtual bool TradeEventPositionOpened(void);
  virtual bool TradeEventPositionClosed(void);
};

...

bool CExpertCustom::Init(string symbol, ENUM_TIMEFRAMES period, bool every_tick, ulong magic=0) {
  if(!CExpert::Init(symbol, period, every_tick, magic)) {
    return(false);
  }
... 
  return(true);
} 

bool CExpertCustom::TradeEventOrderTriggered(void) {
  if((PositionSelect(_Symbol) == true) && !_triggered) { 
    ... 
  }
  return(true);
} 

bool CExpertCustom::TradeEventPositionOpened(void) { 
  Print("Position Opened!"); 
  _triggered = true;
  return(true); 
} 

 

When I debug, the execution never go to this part of CExpert::CheckTradeState() function: 

...
//--- check position open
if(pos_tot==m_pos_tot+1)
  {
    //--- position open
    if(IS_WAITING_POSITION_OPENED)
      {
        res=TradeEventPositionOpened();
        NoWaitEvent(TRADE_EVENT_POSITION_OPEN);
      }
    //--- establishment of the checkpoint history of the trade
    HistoryPoint(true);
    return(res);
  }
...
 
If you want help you should post code that can be compiled and tested.
 

Hi Alain,

You can reproduce my situation by creating a simple EA (generate) from wizard and define that it will  listen for trade events, setting the m_on_trade_process to true.

In OnInit() function, after initialize the expert, place the follow line:

ExtExpert.OnTradeProcess(true);

 After, place a breakpoint in the line 185 on CExpert.mqh 

virtual bool      TradeEventPositionOpened(void)         { return(true); }

You can see that the breakpoint is never reach.

You will see that the CExpert::CheckTradeState(void) is executed, but TradeEventPositionOpened and similar functions are never executed.

I'm missing something? 

 
Someone?