Calculations find the profit point. MQL4

 

Hello everyone

I want a code, but whatever I think I can not get the result

We have an expert who opens 1 position in a row with every 10 pips (We have no problem so far)

Now I want to write a code that checks all the positions with the opening of each position to see where we are at the opening of the positions or in the provisions, then put all the TPs at the opening point of one of the positions.

Example

BUY Position No. 1: 1.12500

BUY Position No. 2: 1.12400

BUY Position No. 3:1.12300

BUY Position No. 4: 1.12200

BUY Position No. 5: 1.12100

BUY Position No. 6: 1.12000

With the opening of position number 2, our TP must move to the opening point of order number 1

In this case, position number 1 is free and position number 2 is closed with 10 pips of profit

Or

With the opening of position number 5, all TPs should be transferred to the opening point of order number 2 In this case, position number 5 to position number 3 will be closed with profit and position 2 will be closed free and position number 1 will be closed with 10 pips of loss.

With the opening of each position, the expert has to do the calculations again and see where in the opening of the orders we will benefit, then TP will move all the positions.

Thanks.

 
No Size Type Open Price  TP Move TP Transfer TP to position   Total Profit  
 1  0.01  Buy  1.12500 1.12600  10 pip  10 pip TP  10 Pip
 2  0.01  Buy  1.12400  ?  Move to 1.12500 (10 pip Profit) and  No:01 (0 pip Profit) Position No:1  10 Pip
 3  0.01  Buy  1.12300  ?  Move to 1.12500 (20 pip Profit) , No:02 (10 pip Profit) , No:01 (0 pip Profit) Position No:1  30 Pip
 4  0.01  Buy  1.12200  ?  Move to 1.12400 (20 pip Profit) , No:03 (10 pip Profit) , No:02 (0 pip Profit) ,  No:01 (10 pip loss) Position No:2  20 Pip
 5  0.01  Buy  1.12100  ?  Move to 1.12400 (30 pip Profit) , No:04 (20 pip Profit) , No:03 (10 pip Profit) , No:02 (0 pip Profit) , No:01 (10 pip loss) Position No:2  50 Pip
 6  0.01  Buy  1.12000  ?  Move to 1.12300 (30 pip Profit) , No:05(20 pip Profit) ,  No:04 (10 pip Profit) , No:03 (0 pip Profit) , No:02 (10 pip loss) ,  No:01 (20 pip loss) Position No:3  30 Pip
 

Dear professors, please help

I wrote this code and I think it stores all the prices in this Array

And I wrote another function to replace the profit limit

What should I do now to calculate the stored prices and give us the first number we get out of the loss ???

Thanks to the programming professors

void OnTick()
  {
GatPrise(0);
   
  }
//+------------------------------------------------------------------+
void GatPrise(int Magic)
 {
double openprice[];
ArrayResize(openprice,Ordersbuy(Magic));

if(Ordersbuy(Magic)>1)
{
short x=0;
   for(int i=0; i<OrdersTotal(); i++)                                     
     {
     if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
     {
     if(OrderMagicNumber()==Magic)
     {
     openprice[x]=OrderOpenPrice();
     x++;
      }
       }
      }
       int orderopenprice= ArrayMaximum(openprice);
       Comment(openprice[orderopenprice]);
        }
  }
 
 int Ordersbuy(int Magic)
  {
   int num=0;
   for(int i=OrdersTotal()-1;i>=0;i--)                                     
     {
     if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
     {               
       if(OrderMagicNumber()==Magic) 
       num++;
       }
     }
   return(num);
    }
//+----------------------------ChekcForTrail()--------------------------------------+
void ChekcForTrail()
{if(Ordersbuy(Magic)>0) 
   {
    for(int i=OrdersTotal()-1;i>=0;i--)
      {if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) &&(OrderSymbol()=="GBPUSD") && (OrderMagicNumber()==Magic) &&
         (OrderType()==OP_BUY))
          {ResetLastError();
           bool Modify=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),???????,0,Green);
           if(!Modify) {Comment("OrderModify failed with error :: ",GetLastError());ResetLastError();}
          }
      }
   }  
}