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

 
Snajper007 sell order is opened, the function calculates the average price. However, when the 3rd order is opened, this function shows the previous and the new value (I looked through the print in the journal) but as a result, the previous value remains. What have I done wrong?
//+----------------------------------------------------------------------------+
//| Расчет среденй цены (0)-buy (1)-sell                                       |
//+----------------------------------------------------------------------------+
double GetAveragePrice(int ot=-1)
  {
   double order_lots = 0, order_price = 0, avg_price = 0;
     {
      for(int i = OrdersTotal()-1; i>=0; i--)
        {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
              {
               if(OrderType()==ot||ot<0)
                 {
                  order_lots += OrderLots();
                  order_price += OrderOpenPrice() * OrderLots();
                 }
              }
           }
        }
     }
   avg_price = NormalizeDouble(order_price / order_lots, Digits);
   return(avg_price);
  }
//+----------------------------------------------------------------------------+
 
MakarFX #:
Thank you!
 
Good afternoon, who can help implement a trade opening filter based on indicators?
 
Sergei Pimenov #:
Good afternoon, who can help implement a trade opening filter based on indicators?
Read more
 

Help me refine the logic. I need a pause in hours between the closed orders and the opening of a new one. I have worked out a few things. But it does not work.

OrderCloseTime() > 0 && TimeCurrent() - OrderCloseTime() > FstOrderTimeHour*3600 // сигнал на открытие ордера

OrderCloseTime() == 0 // сигнал на открытие первого ордера

где int FstOrderTimeHour = 1 // пауза в часах
 
Порт-моне тв #:

Help me refine the logic. I need a pause in hours between the closed orders and the opening of a new one. I have worked out a few things. But it does not work.

//--- input parameters
input int HourPause = 1;   // Пауза в часах
//--- global parameters
datetime  pause;
//+------------------------------------------------------------------+
void OnTick()
  {
   pause=GetInfoLastPos(3)+(HourPause*60*60);
   // выключаем торговлю
   if(TimeCurrent()<pause) return;
  .......
  }
//+----------------------------------------------------------------------------+
//|  Функция возвращает по символу и магику                             MakarFX|
//|  1 - размер лота последней закрытой позиции                                |
//|  2 - размер профита с учетом комиссии и свопа последней закрытой позиции   |
//|  3 - время последней закрытой позиции                                      |
//+----------------------------------------------------------------------------+
double GetInfoLastPos(int a=1)
  {
   datetime t=0;
   double result=0,l=0,p=0,f=0;
   int i=OrdersHistoryTotal();
   for(int pos=0; pos<i; pos++)
     {
      if(OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY))
        {
         if(OrderSymbol()==_Symbol && OrderMagicNumber()==Magic)
           {
            if(OrderType()==OP_BUY || OrderType()==OP_SELL)
              {
               if(t<OrderCloseTime()) {t=OrderCloseTime(); l=OrderLots(); p=OrderProfit()+OrderCommission()+OrderSwap();}
              }
           }
        }
     }
   if(a==1) {result=l;} else
   if(a==2) {result=p;} else
   if(a==3) {result=(double)t;}
   else     {result=0;}
   return(result);
  }
//+----------------------------------------------------------------------------+

If the magician is not necessary - delete yellow.

 
MakarFX #:

Before opening an order, you store the current max_ticket in the prev_ticket

Good time Makar, this method of the penultimate order ticket definition has a serious disadvantage. This method works when the maximal order in the grid will be opened and if that doesn't happen, the penultimate order is the last one in the grid (see the picture). I am trying to write a function but it does not work, the last ticket is detected. The highlighted condition does not want to work.

//+----------------------------------------------------------------------------+
//| Расчет тикета предпоследнего ордера в сетке                                 |
//+----------------------------------------------------------------------------+
int GetTicketPenultimateOrder()
  {
   penultimate_ticket = 0;
     {
      for(int cnt = OrdersTotal() - 1; cnt >= 0; cnt--)
        {
         if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
           {
            if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
              {
               if(OrderType() == OP_BUY || OrderType() == OP_SELL)
                 {
                  if(OrderTicket() > penultimate_ticket)
                     if(penultimate_ticket < GetTicketMaxOrder())
                        penultimate_ticket = OrderTicket();

                 }
              }
           }
        }
     }
   return(penultimate_ticket);
  }

 
EVGENII SHELIPOV #:

Good time Makar, this method for the penultimate order ticket has a serious disadvantage. This method works when the maximal order is opened in the grid. If it is not, the penultimate order is equal to the last one, see the picture. I am trying to write a function but it does not work, the last ticket is detected. The highlighted condition does not want to work.

I gave you a working code!
 
MakarFX #:
I gave you a working code!

This code is what I wrote above and there is even a picture of it

 
EVGENII SHELIPOV #:

This code is what I wrote above and there is even a picture of it

The error is not in the penultimate search code, but most likely in the output of the information to text