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

 
makssub #:

It's the last one, not the penultimate one(


Look at what you wrote:

" If price goes down and another order is opened, the order with the maximum OrderOpenPrice becomes the penultimate order. "

This function searches for max or min open price.

If you just want penultimate, you better remember the ticket when it opens.

 
MakarFX #:

Look at what you wrote:

" If price goes down and another order is opened, the order with the maximum OrderOpenPrice is the penultimate order. "

This function searches for max or min open price.

If you just want penultimate, you better remember the ticket when it opens.

I apologise, sometimes I don't make my point very clearly.

I have already done it by tick and by time, but in fluctuations, when price may go up and then down, it may not be the penultimate. These variants have already been worked out. So the opening price is the best option. Or a bunch of functions that will double-check it all, and there I'm sure I'll get very confused. I don't have a lot of time to keep everything in my head.

I've written before on the penultimate one and it gave out. One problem, didn't distinguish between OP_SELL and OP_BUY. When the second one appears, it starts counting it(

 
makssub #:

I apologise, sometimes I don't make my point very clearly.

I have already done on the tick, and on time, but in a fluctuation, when the price may go up, then down, it may not be the penultimate one. These variants have already been worked out. So the opening price is the best option. Or a bunch of functions that will double-check it all, and there I'm sure I'll get very confused. I don't have a lot of time to keep everything in my head.

I've written before on the penultimate one and it gave out. One problem, didn't distinguish between OP_SELL and OP_BUY. When I get the second one, it starts counting it(

Write simply what you need to find (if it happened, you need to get it)...because I'm already confused too

 
MakarFX #:

Write simply what you need to find (if it happened, you need to get it)...because I'm already confused too

I have highlighted which order price you need to find

It's not difficult if there is no OP_BUY. If OP_BUY appears, my code does not work(

double FindPenultSellPrice()
{
   double  UpPrice = DBL_MAX, profit = 0;
   int    i;
   for(i = OrdersTotal()-2; i >=0; i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == OP_SELL)
         {
            if ( OrderOpenPrice() < UpPrice)
            {
               UpPrice = OrderOpenPrice();
            }
         }
      }
   }
   return(UpPrice);
}
 
MakarFX #:

Write simply what you need to find (if it happened, you need to get it)...I'm already confused too

It's not the penultimate order by time, it's the penultimate order by price if the price has gone the other way. Alexei has correctly written, we should search by price. We should not change the values of the maximal or lowest ones.

Zy. I would have remembered the last order (it's a grid) and put the penultimate one in my variable. The algorithm should only be refined, if up one and immediately down one and again up one and immediately down one)))) If two orders are going in one direction, then we should remember the ticket of the penultimate and last order just in case. If a third order opens, the ticket of the penultimate order is equal to the last one and the ticket of the last order is equal to the third one.

 
makssub #:

Highlighted in colour which order price to find

It is not difficult if there is no OP_BUY. If OP_BUY appears, my code does not work(

Try it this way

//+------------------------------------------------------------------+
//|  Функция возвращает по символу и магику                          |
//|  1 - размер лота последней позиции                               |
//|  2 - цена последней открытой позиции                             |
//|  3 - время последней открытой позиции                            |
//+------------------------------------------------------------------+
double GetInfoLastPos(int a=1)
  {
   datetime t=0;
   double result=0,l=0,p=0,f=0;
   
   for(int pos=OrdersTotal()-1;pos>=0;pos--)
     {
      if(OrderSelect(pos, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderSymbol()==_Symbol && OrderMagicNumber()==Magic)
           {
            if(OrderType()==OP_BUY || OrderType()==OP_SELL)
              {
               if(t<OrderCloseTime()) {t=OrderOpenTime(); l=OrderLots(); p=OrderOpenPrice();}
              }
           }
        }
     }
   if(a==1) {result=l;} else
   if(a==2) {result=p;} else
   if(a==3) {result=(double)t;}
   else     {result=0;}
   return(result);
  }
//+------------------------------------------------------------------+
double FindOpenPrice()
  {
   double AbsPoint=DBL_MAX;
   double openprice=0;
   
   for(int pos=OrdersTotal()-2;pos>=0;pos--)
     {
      if(OrderSelect(pos, SELECT_BY_POS, MODE_TRADES))
        {
         if(OrderSymbol()==_Symbol && OrderMagicNumber()==Magic)
           {
            if(OrderType()==OP_BUY || OrderType()==OP_SELL)
              {
               if(AbsPoint>MathAbs(OrderOpenPrice()-GetInfoLastPos(2)))
                 {
                  AbsPoint=MathAbs(OrderOpenPrice()-GetInfoLastPos(2));
                  openprice = OrderOpenPrice();
                 }
              }
           }
        }
     }
   return(openprice);
  }
//+------------------------------------------------------------------+
 
MakarFX #:

Try this.

Thank you for your hard work.

There is one nuance) It finds at the top, marked in red. For OP_SELL it is at the bottom, marked blue. And let me tell you right away, I didn't check how it will count if OP_BUY appears.

 
makssub #:

Thank you for your hard work.

There is one nuance) It is found at the top, marked in red. For OP_SELL it needs to be at the bottom, marked blue. And I must say right away that I did not check how it will count if OP_BUY appears.

Above is the nearest!

I asked you to write what you must find and you give out some portions.

For example, I need: "If a Sell has opened, I should find the nearest Sell from above... etc.".

P.S. What did you mark in red? I don't see 0_o
 
MakarFX #:

The one at the top is the closest!

I asked you to write what you need to find, but you give out some portions...

For example I need: "If a Sell opened, then you need to find the nearest Sell from above... etc."

P.S. What did you mark in red? don't see 0_o

It happens to me. I think one thing and write another. Not attentive Sorry) At the expense of red itself in a shock)

 
makssub #:

It happens to me. I think one thing and write another. I'm not paying attention. Sorry. I'm shocked about the red one.)

So what do you want?)
Reason: