OnTakeProfit & OnStopLoss instead of OnTradeTransaction() - page 3

To add comments, please log in or register
Dr Matthias Hammelsbeck
584
Dr Matthias Hammelsbeck  

spottygegasus:

your question gives reason to reorganize the header Transaction_5.mqh and to make it semantically cleaner.

Decisions between buy and sell are shifted into the client callback functions.

#include <myTrade/DealInfo.mqh>
typedef void(*fct_TA)(const MqlTradeTransaction &trans);
class CTransaction {
private:
  fct_TA  m_fct_OnOpenPositionByExpert;
  fct_TA  m_fct_OnOpenPositionByClient;

  fct_TA  m_fct_OnSL;
  fct_TA  m_fct_OnTP;
  fct_TA  m_fct_OnClosePositionByExpert;
  fct_TA  m_fct_OnClosePositionByClient;

public:
  CTransaction() {
    m_fct_OnOpenPositionByExpert    =NULL;
    m_fct_OnOpenPositionByClient    =NULL;

    m_fct_OnSL                      =NULL; // hit SL :-(
    m_fct_OnTP                      =NULL; // hit TP :-)
    m_fct_OnClosePositionByExpert   =NULL;
    m_fct_OnClosePositionByClient   =NULL;
  }
  ~CTransaction() {
  }


  void Set_OnOpenPositionByExpert (fct_TA f) {
    m_fct_OnOpenPositionByExpert = f;
  }
  void Set_OnOpenPositionByClient (fct_TA f) {
    m_fct_OnOpenPositionByClient = f;
  }

  void Set_OnSL (fct_TA f) {
    m_fct_OnSL = f;
  }
  void Set_OnTP (fct_TA f) {
    m_fct_OnTP = f;
  }
  void Set_OnClosePositionByExpert (fct_TA f) {
    m_fct_OnClosePositionByExpert = f;
  }
  void Set_OnClosePositionByClient (fct_TA f) {
    m_fct_OnClosePositionByClient = f;
  }

  
  void OnTradeTransaction (const MqlTradeTransaction &trans) {
    myDealInfo di;
    switch (trans.type) {
    case TRADE_TRANSACTION_DEAL_ADD:
      if (di.SelectByTicket(trans.deal)) {
        switch (di.Entry()) {
        case DEAL_ENTRY_IN:
          switch (di.Reason()) {
          case DEAL_REASON_EXPERT:
            if (m_fct_OnOpenPositionByExpert!=NULL) m_fct_OnOpenPositionByExpert (trans);
            break;
          case DEAL_REASON_CLIENT:
            if (m_fct_OnOpenPositionByClient!=NULL) m_fct_OnOpenPositionByClient (trans);
            break;
          }
          break;
          
        case DEAL_ENTRY_OUT:
          switch (di.Reason()) {
          case DEAL_REASON_SL:
            if (m_fct_OnSL!=NULL)  m_fct_OnSL (trans);
            break;
          case DEAL_REASON_TP:
            if (m_fct_OnTP!=NULL)  m_fct_OnTP (trans);
            break;
          case DEAL_REASON_EXPERT:
            if (m_fct_OnClosePositionByExpert!=NULL) m_fct_OnClosePositionByExpert (trans);
            break;
          case DEAL_REASON_CLIENT:
            if (m_fct_OnClosePositionByClient!=NULL) m_fct_OnClosePositionByClient (trans);
            break;
          }
          break;
        }
      }
      break;
    }
  }
};
spottypegasus
14
spottypegasus  
Dr Matthias Hammelsbeck:

spottygegasus:

your question gives reason to reorganize the header Transaction_5.mqh and to make it semantically cleaner.

Decisions between buy and sell are shifted into the client callback functions.

Great, it looks more structured...

How would we catch OnModifyTPSL event?

What are the DEAL constants to watch out for?

Thanks & Regards...

Dr Matthias Hammelsbeck
584
Dr Matthias Hammelsbeck  

How would we catch OnModifyTPSL event?

SInce the modification of SL/TP happens actively either by calling CTrade.PositionModify in an expert advisor or manually in the terminal by shifting thew SL/TP line in the chart I see no need to catch this event.

But if you want to catch this event you have to extend the signature of the event functions typedef_ed in Transaction_5.mqh to

typedef void(*fct_TA)(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result);

Then in CTransaction.OnTradeTransaction look for   

trans.type == TRADE_TRANSACTION_REQUEST

   and  

request.action == TRADE_ACTION_SLTP


What are the DEAL constants to watch out for?

For the deal constants please see the documentation MQL5 Reference/Constants, Enumerations and Structures/Trade Constants/Deal Properties


Sincerely

Matthias

spottypegasus
14
spottypegasus  
spottypegasus:

Great, it looks more structured...

How would we catch OnModifyTPSL event?

What are the DEAL constants to watch out for?

Thanks & Regards...

I have found theTRADE_TRANSACTION_POSITION constant for catching the OnModifyTPSL event...

It works fine...

Regards...

123
To add comments, please log in or register