Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1577

 
Mihail Matkovskij:

OnTradeTransaction will be triggered when any stop order on the server is triggered.

But the function has to be prescribed?
 
Eugen8519:
How about through?

DEAL_REASON_SL
DEAL_REASON_TP

no

there is a very good example of OnTradeTransaction in the docs

 
MakarFX:
But the function has to be prescribed?

If you add a function specifically for this purpose, it would be handy. Why not...?

But there is no need to track stop crossings in it, because the server will do that and send a signal toOnTradeTransaction.

 
Mihail Matkovskij:

If you add a function specifically for this purpose, it would be handy. Why not...?

But it is not necessary to trace the crossing of stops in it, because it will be done by the server and it will send a signal toOnTradeTransaction.

Misha, I am not really good at mql5, I am just at the beginning,

but I think we should first write something like.

if(OnTradeTransaction(...)==....)  SendNotification ("текст");

If I'm wrong, I would be glad to correct it

 
MakarFX:

Misha, I'm not very good at mql5, I'm just starting to get into it,

but I think you should first write something like...

If I'm wrong, I'd be glad to correct you.

OnTradeTransaction is such an event handler. Therefore, all processing code should be in it. Or call the processing function if you want. See description in documentation at the link.
 
Mihail Matkovskij:
OnTradeTransaction is such an event handler. Therefore, all processing code should be in it. Or processing function if you want. See the description in the documentation at the link.

Are we talking about the same thing?

if(OnTradeTransaction(...)==....)  SendNotification ("текст");
 
How about this? I think I found one from mql4
void AlertOrder()

{

   string txt;

   double OCP;

   int i=OrdersHistoryTotal()-1;

   if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)

   {                                

      OCP=OrderClosePrice();

      if (OCP==OrderStopLoss()) txt="SL";

      if (OCP==OrderTakeProfit()) txt="TP";

      }

      SendNotification(Symbol() + " hit "+txt+"");

 }
 
Eugen8519:
How about this? I found one from mql4
OrdersHistoryTotal

not in mql5

 
MakarFX:

Are we talking about the same thing?

No. You are going to call the handler in the condition (if). But you don't need to call it anywhere. Simply write the processing code inOnTradeTransaction and that is all. Look at parameters that are passed toOnTradeTransaction when a stop is triggered.

Although, you must know what events and event handlers are, what events there are in MQL. Then you will begin to understand it all.

Here is a link to what Event Hand lers arehttps://www.mql5.com/ru/docs/basis/function/events#ontradetransaction.

They are often just called event handlers to avoid confusion. For example, as you have confusedOnTradeTransaction with an ordinary function that is called in if.

Документация по MQL5: Основы языка / Функции / Функции обработки событий
Документация по MQL5: Основы языка / Функции / Функции обработки событий
  • www.mql5.com
Функции обработки событий - Функции - Основы языка - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Eugen8519:
How about this? I found one from mql4

Try this (I haven't tested it)

datetime lastclouse; 
int OnInit()
  {
   lastclouse=TimeCurrent();
   .....
  }
void OnTick()
  {
   if(LastClouseProfit()!=EMPTY_VALUE) SendNotification (LastClouseProfit());
   .....   
  }  
double LastClouseProfit()
  {
   ulong ticket=0;
   double profit=EMPTY_VALUE;
   HistorySelect(lastclouse,TimeCurrent());
   ticket=HistoryDealGetTicket();
   profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);
   if(profit!=EMPTY_VALUE) lastclouse=TimeCurrent();
   return(profit);
  }