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

 
MakarFX:

Versuchen Sie es (ich habe es nicht getestet)

Thank you, I will check later.

 
Eugen8519:



Please help me to position the function correctly

...

But how and where to insert

To send a message when a stop loss ortake profit is reached?


MakarFX:

Are we talking about the same thing?

I found an example that tracks the SL exit withOnTradeTransaction:https://www.mql5.com/ru/code/21147.

Candle shadows v1
Candle shadows v1
  • www.mql5.com
Анализ размера свечи и тени свечи. В OnTradeTransaction отлавливаем открытие (DEAL_ENTRY_IN) и закрытие позиций (DEAL_ENTRY_OUT), а также закрытие по Stop loss (DEAL_REASON_SL).
 

The same EA on two different instruments

Hello! Could you please help me out?

I have an EA which works on the chart on which it has been dragged (the Symbol() value is taken as the name of the symbol).

But this EA also has a ticket_exist variable which contains a ticket of an placed order and the EA understands that if the ticket_exist is not equal to zero,the order is placed here.

Question: If I open such an EA on two different instruments and the ticket_exist variable is not equal to zero on one instrument, will the variable with the same name remain equal to zero on the other instrument?

SU.

 
Mihail Nefedov:

The same EA on two different instruments

Hello! Could you please help me out?

I have an EA which works on the chart on which it has been dragged (the Symbol() value is taken as the name of the symbol).

But this EA also has a ticket_exist variable which contains a ticket of the set order and the EA understands that if the ticket_exist is not equal to zero,the order is set here.

Question: If I open such an EA on two different instruments and the ticket_exist variable is not equal to zero on one instrument, will the variable with the same name remain equal to zero on the other instrument?

SU.

The value of the variable in one EA is not available for another EA. The main thing is that either the symbols, or magiks, or both are different.

 

Can you tell me what time it is in ticks? Server time or UTC ?

I remember reading somewhere, but can't remember where...

 
Alexey Viktorov:

The value of a variable in one EA is not available to another EA. The main thing is that either the symbols, or magics, or both of them should be different.

Thank you! If I understood correctly, then in order to get a conflict of EAs (two absolutely identical ones) working with different symbols, their variables should be set as global (for the whole terminal) or, for example, an order should be accessed by its sequence number or simply by the fact of its existence?

 
MakarFX:

Try this (I haven't tested it)

It doesn't work, it gives compile errors.


I tried it this way and it compiles without any problems but I can't test it with the rink closed

I'll be back to work tomorrow.

  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_entry        =0;
      string   deal_symbol       ="";
      long     deal_magic        =0;
      long     deal_reason       =-1;
      if(HistoryDealSelect(trans.deal))
        {
         deal_entry=HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
         deal_symbol=HistoryDealGetString(trans.deal,DEAL_SYMBOL);
         deal_magic=HistoryDealGetInteger(trans.deal,DEAL_MAGIC);
         deal_reason=HistoryDealGetInteger(trans.deal,DEAL_REASON);
        }
      else
         return;

      if(deal_symbol==m_symbol.Name() && deal_magic==m_magic)
         if(deal_entry==DEAL_ENTRY_OUT)
           {
            if(deal_reason==DEAL_REASON_SL)
               SendNotification ("Закрыл  позицию > StopLoss");

            if(deal_reason==DEAL_REASON_TP)
               SendNotification ("Закрыл  позицию > TakeProfit");
           }
       }
  }
Files:
x4.PNG  11 kb
 
Eugen8519:

it doesn't work, it gives compile errors


I tried it this way, it compiles without any problems, but when the rink is closed I can't test it

I'll finish it tomorrow.

you didn't put the code in correctly!

I fixed it a little and it's working.

  datetime lastclouse; 
//+------------------------------------------------------------------+
int OnInit()
  {
   lastclouse=TimeCurrent();
   .....
  }
//+------------------------------------------------------------------+
void OnTick()
  {
   if(LastClouseProfit()!=EMPTY_VALUE)
     {
      SendNotification (LastClouseProfit()); lastclouse=TimeCurrent();
     }
   .....   
  }
//+------------------------------------------------------------------+
double LastClouseProfit()
  {
   ulong ticket=0;
   double profit=EMPTY_VALUE;
   HistorySelect(lastclouse,TimeCurrent());
   uint total=HistoryDealsTotal(); 
   for(uint i=0;i<total;i++) 
     { 
      if((ticket=HistoryDealGetTicket(i))>0) 
        { 
         profit+=HistoryDealGetDouble(ticket,DEAL_PROFIT); 
        }
     } 
   return(profit);
  }
//+------------------------------------------------------------------+
 
MakarFX:

you didn't put the code in correctly!

I corrected it a bit, it works (I checked it).

Here, we need to trace the fact that the deal was closed exactly by SL or TP. And usingOnTradeTransaction we get ready data right after closing of the deal.

double LastClouseProfit()
  {
   ulong ticket=0;
   double profit=EMPTY_VALUE;
   HistorySelect(lastclouse,TimeCurrent());
   uint total=HistoryDealsTotal(); 
   for(uint i=0;i<total;i++) 
     { 
      if((ticket=HistoryDealGetTicket(i))>0) 
        { 
         profit=HistoryDealGetDouble(ticket,DEAL_PROFIT); 
        }
     } 
   return(profit);
  }

If there is more than one trade closed at TimeCurrent, what result should be returned? And the presence of a profit does not mean that the trade was closed by the SL or TP. This needs to be tracked, as I said before. So, usingOnTradeTransaction, is the easiest solution!

 
Mihail Matkovskij:

Here, we also need to trace that the trade was closed exactly on the SL or TP.

I understood it as a notification of deal closing, maybe I got it wrong...

Mihail Matkovskij:

If more than one trade is closed at TimeCurrent, what result should I get back?

don't know, need to check

Reason: