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

 
makssub Sleep for each order. I would like to have something shorter.

When the exit/closing condition occurs, we should add the ticket to the array. Then, if the array is not empty, the program will loop through it, calling OrderClose for each ticket. Then check existence of each ticket, and remove it from the list. Repeat these actions at 1-3 second intervals until the list is empty.

 

Hello.

I need to calculate the net profit of the Expert Advisor at the end of work. I run a loop on OrdersHistoryTotal() in OnDeinit and add OrderProfit().

Profit=Profit+OrderProfit();

But in the end the profit is different from the one reported in the tester. I have found out that the point value differs on a part of orders.

What is the reason for this? Spread is set manually in the tester.

 
AlexandrVV #:

Hello.

I need to calculate the net profit of the Expert Advisor at the end of work. I run a loop on OrdersHistoryTotal() in OnDeinit and add OrderProfit().

Profit=Profit+OrderProfit();

But in the end the profit is different from the one reported in the tester. I have found out that the point value differs on a part of orders.

What is the reason for this? The spread is set manually in the tester.

Swap and commission were forgotten to be added! The spread is already taken into account since it is always between the entry and exit prices.

 
Mihail Matkovskij #:

Swap and commission have been forgotten to be added! The spread is already taken into account as it is always between the entry price and the exit price.

Oh, that's right! Thank you very much!!! )

 
Hello everyone. Please tell me where I can find a script or indicator that sends a notification to my phone, when the price reaches a certain level specified by me.
 

Can you tell me what the problem is, the code should close orders and if they are missing it should open a new one.

But for some reason, it closes only 1 order and stops until next signal.

2022.01.17 16:22:08.389 2022.01.04 08:00:00 e: Signal 1.1322 < clos 1.1304 ExistPosSell 0
2022.01.17 16:22:08.389 2022.01.04 08:00:00 e: Signal 1.1322 > clos 1.1304 ExistPosBuy 1
2022.01.17 16:22:08.366 2022.01.04 08:00:00 e: Signal 1.1325 < clos 1.1306 ExistPosSell 0
2022.01.17 16:22:08.366 2022.01.04 04:00:00 e: Signal 1.1325 > clos 1.1306 ExistPosBuy 1
2022.01.17 16:22:08.327 2022.01.04 2022.01.04 00:00:00 e: Signal 1.1326 < clos 1.1296 ExistPosSell 0
2022.01.17 16:22:08.327 2022.01.04 00:00:00 e: Signal 1.1326 > clos 1.1296 ExistPosBuy 1
2022.01.17 16:22:08.298 2022.01.03 20:00:00 e: Signal 1.1328 < clos 1.1283 ExistPosSell 0
2022.01.17 16:22:08.298 2022.01.03 20:00:00 e: close #1 buy 0.01 EURUSD at 1.13598 at price 1.12831

2022.01.17 16:22:08.298 2022.01.03 20:00:00 e: Signal 1.1328 > close 1.1283 ExistPosBuy 1
2022.01.17 16:22:08.190 2022.01.03 16:00:00 e: open #4 buy 0.01 EURUSD at 1.13512 ok
2022.01.17 16:22:08.190 2022.01.03 16:00:00 e: Signal 1.134 < close 1.135 ExistPosSell 0
2022.01.17 16:22:08.190 2022.01.03 16:00:00 e: Signal 1.134 > close 1.135 ExistPosBuy 1

if(isNewBar()==true) {  
if(Signal > clos ) {  //--------------------- МА выше цены закрытия предыдущего бара.
     if(ExistPositions( NULL, OP_BUY, Magic) == true) //---------------- Если есть позиция BUY то
ClosePosWithMaxProfitInCurrency(); //--------  закрытие одной позиции с максимальным профитом в валюте депозита.
         if(ExistPositions( NULL, OP_BUY, Magic) == False) //----------- если позиции BUY отсутствуют то
   ticket=OpenPosition(NULL,OP_SELL, NDLot(Lot),0,0, Magic, Com);} //------------ открываем позицию SELL

if(Signal < clos ) { //--------------------- МА ниже цены закрытия предыдущего бара.
    if(ExistPositions( NULL, OP_SELL, Magic) == true) //---------------- Если есть позиция SELL то
 ClosePosWithMaxProfitInCurrency(); //---------------------закрытие одной позиции с максимальным профитом в валюте депозита.
        if(ExistPositions( NULL, OP_SELL, Magic) == False)  //----------- если позиции SELL отсутствуют то
  ticket=OpenPosition(NULL,OP_BUY, NDLot(Lot),0,0, Magic, Com);} //------------ открываем позицию BUY

} return; 
 
Бахром Балтабаев a notification to my phone when the price reaches a certain level, I specify.

I recently wrote an alert:. But it can be improved by adding SendNotification:

// использовать алерт
input bool alert = true;
// использовать push-уведомления
input bool notification = true;
// уровень срабатывания
input double trigLv = 0.0;
// отклонение от trigLv в пунктах
input int deviation = 30; 


// функция взята из https://www.mql5.com/ru/docs/basis/types/double
bool EqualDoubles(double d1,double d2,double epsilon)
  {
   if(epsilon<0) 
      epsilon=-epsilon;
//---
   if(d1-d2>epsilon) 
      return false;
   if(d1-d2<-epsilon) 
      return false;
//---
   return true;
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit() {
  return(INIT_SUCCEEDED);
}

int OnCalculate(const int rates_total,
                 const int prev_calculated,
                 const datetime &time[],
                 const double &open[],
                 const double &high[],
                 const double &low[],
                 const double &close[],
                 const long &tick_volume[],
                 const long &volume[],
                 const int &spread[]
) {
  static bool triggered = false;
  static datetime time_ = 0;
  if (!alert && !notification)
    return rates_total;
  if (EqualDoubles(trigLv, close[rates_total - 1], deviation * SymbolInfoDouble(NULL, SYMBOL_POINT))) { // сравнение цены Close с trigLv
    if (time_ != time[rates_total - 1])
      time_ = time[rates_total - 1];
    else
      return rates_total;
    if (!triggered) {
      if (alert)
        Alert("Level ", NormalizeDouble(trigLv, (int)SymbolInfoInteger(NULL, SYMBOL_DIGITS)), " triggered!");
      if (notification)
        SendNotification("Level " + (string)NormalizeDouble(trigLv, (int)SymbolInfoInteger(NULL, SYMBOL_DIGITS)) + " triggered!");
    }
    triggered = true;
  }
  else
    triggered = false;
  
  return rates_total;
}

Add the lines, which I have highlighted to the source code, drop it in the Indicators folder and compile it. This indicator can be added to the chart as many times as you want and receive signals when different levels are crossed. The code was finalized online. The result is not verified.

Любые вопросы новичков по MQL4 и MQL5, помощь и обсуждение по алгоритмам и кодам
Любые вопросы новичков по MQL4 и MQL5, помощь и обсуждение по алгоритмам и кодам
  • www.mql5.com
В этой ветке я хочу начать свою помощь тем, кто действительно хочет разобраться и научиться программированию на новом MQL4 и желает легко перейти н...
 

Forum on trading, automated trading systems & strategy testing

Any questions from newbies on MQL4 and MQL5, help and discussion on algorithms and codes

GlaVredFX, 2022.01.17 18:59

I'm looking for the code to close orders and if i don't see any, i will open a new order.

But for some reason, it closes only 1 order and stops before the next signal.

2022.01.17 16:22:08.389 2022.01.04 08:00:00 e: Signal 1.1322 < clos 1.1304 ExistPosSell 0
2022.01.17 16:22:08.389 2022.01.04 08:00:00 e: Signal 1.1322 > clos 1.1304 ExistPosBuy 1
2022.01.17 16:22:08.366 2022.01.04 08:00:00 e: Signal 1.1325 < clos 1.1306 ExistPosSell 0
2022.01.17 16:22:08.366 2022.01.04 04:00:00 e: Signal 1.1325 > clos 1.1306 ExistPosBuy 1
2022.01.17 16:22:08.327 2022.01.04 2022.01.04 00:00:00 e: Signal 1.1326 < clos 1.1296 ExistPosSell 0
2022.01.17 16:22:08.327 2022.01.04 00:00:00 e: Signal 1.1326 > clos 1.1296 ExistPosBuy 1
2022.01.17 16:22:08.298 2022.01.03 20:00:00 e: Signal 1.1328 < clos 1.1283 ExistPosSell 0
2022.01.17 16:22:08.298 2022.01.03 20:00:00 e: close #1 buy 0.01 EURUSD at 1.13598 at price 1.12831

2022.01.17 16:22:08.298 2022.01.03 20:00:00 e: Signal 1.1328 > close 1.1283 ExistPosBuy 1
2022.01.17 16:22:08.190 2022.01.03 16:00:00 e: open #4 buy 0.01 EURUSD at 1.13512 ok
2022.01.17 16:22:08.190 2022.01.03 16:00:00 e: Signal 1.134 < close 1.135 ExistPosSell 0
2022.01.17 16:22:08.190 2022.01.03 16:00:00 e: Signal 1.134 > close 1.135 ExistPosBuy 1

if(isNewBar()==true) {  
if(Signal > clos ) {  //--------------------- МА выше цены закрытия предыдущего бара.
     if(ExistPositions( NULL, OP_BUY, Magic) == true) //---------------- Если есть позиция BUY то
ClosePosWithMaxProfitInCurrency(); //--------  закрытие одной позиции с максимальным профитом в валюте депозита.
         if(ExistPositions( NULL, OP_BUY, Magic) == False) //----------- если позиции BUY отсутствуют то
   ticket=OpenPosition(NULL,OP_SELL, NDLot(Lot),0,0, Magic, Com);} //------------ открываем позицию SELL

if(Signal < clos ) { //--------------------- МА ниже цены закрытия предыдущего бара.
    if(ExistPositions( NULL, OP_SELL, Magic) == true) //---------------- Если есть позиция SELL то
 ClosePosWithMaxProfitInCurrency(); //---------------------закрытие одной позиции с максимальным профитом в валюте депозита.
        if(ExistPositions( NULL, OP_SELL, Magic) == False)  //----------- если позиции SELL отсутствуют то
  ticket=OpenPosition(NULL,OP_BUY, NDLot(Lot),0,0, Magic, Com);} //------------ открываем позицию BUY

} return; 
If you pay a little more attention to your question and your code, you will see for yourself what the problem is.
 
Mihail Matkovskij #:

If you pay a little more attention to your question and your code, you will see for yourself.

The question is that the 1 most profitable order should close on 1 bar and then we wait for a new bar and close 1 profitable order again. We should not close all of them at once.

This means we close on the first bar, but nothing happens on the next bar.

 
GlaVredFX #:

The issue there is that the 1 most profitable order should close on 1 bar, then wait for a new bar and close 1 profitable order again. You should not close all of them at once.

Then you have to formulate your questions correctly. You have only 1 signal. You will get only 1 signal at the opening of a new bar. If there is only 1 signal, how many orders will be closed? And only if the signal appears on a new bar. And, if there is no signal, the orders will be closed on the next bars. Again, if there is a signal.

Reason: