calculate Lot. Mql4

 

Hello to all professors, with this code I want to come and calculate the whole Lot first (for example, purchase)

Then we calculate that if 10% of the imported Lot goes to profit, it will close all the orders.

But it has a little problem and it does not do that.


#property version   "1.00"
#property strict
input double PercentageRiskValue=0.1;
double MaxPositionLoss;
double totalOpenLots = 0;

//+------------------------------------------------------------------+
void OnTick()
  {
    if (OrdersTotal()<1)

   int buyticket= OrderSend(Symbol(),OP_BUY,10.00,Ask,3,0,Ask+10*_Point,NULL,0,0,Green);

    for(int i=OrdersTotal();i>=0;i--) 
    {
    if(OrderSelect(i,SELECT_BY_POS)== true) 
    {
    
    // PostionProfit=OrderProfit();
    double totalOpenLots = OrderLots();
    MaxPositionLoss=(OrderProfit()/100*PercentageRiskValue);
    
    Print ("totalOpenLots:" ,totalOpenLots);    
    Print ("maxpostionprofit:" ,MaxPositionLoss); 
    
    if (totalOpenLots < MaxPositionLoss) 
    
    {
   OrderClose ( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(),MODE_BID),5,Red);
      }
       }
        }
        
  }



 
ghobar:

Hello to all professors, with this code I want to come and calculate the whole Lot first (for example, purchase)

Then we calculate that if 10% of the imported Lot goes to profit, it will close all the orders.

But it has a little problem and it does not do that.


you want to check if order profit has reached 10% of lots?
 
Vitor Palmeira Abbehusen #:
you want to check if order profit has reached 10% of lots?
Hi dear,

Yes
Sample
Order number 1 with a price of 0.10
Order number 2 with a price of 0.15
The sum of 2 orders is equal to 0.25
Orders must be closed whenever we make a profit of $ 25
 
ghobar #:
Hi dear,

Yes
Sample
Order number 1 with a price of 0.10
Order number 2 with a price of 0.15
The sum of 2 orders is equal to 0.25
Orders must be closed whenever we make a profit of $ 25
If we had 1 position, which was 0.10, and if $ 10 was in profit, it should be closed
Or if we had 20 open positions with a volume of 2.30 and if we made a profit at $ 230, it should be closed
I'm looking for this
 
ghobar #:
If we had 1 position, which was 0.10, and if $ 10 was in profit, it should be closed
Or if we had 20 open positions with a volume of 2.30 and if we made a profit at $ 230, it should be closed
I'm looking for this

So you should get the sum of OrderLots() for all open orders. (let's call it totalLots)

Then sum of OredrProfit() for all open orders. (let's call it totalProfit)


if(totalProfit >= totalLots*100/PercentageRisValue) {
        //close all open orders   
}

is this what you want?

 
Vitor Palmeira Abbehusen #:

So you should get the sum of OrderLots() for all open orders. (let's call it totalLots)

Then sum of OredrProfit() for all open orders. (let's call it totalProfit)


is this what you want?

Yes, that's what I'm looking for
 
ghobar #:
Yes, that's what I'm looking for

I apologize if my explanation was not complete and confused you professors

Sample :

TotalLots x TotalProfit =10%of Profit Lot

Postion No1: 1.12000       0.10

Postion No1: 1.11900       0.15

Postion No1: 1.11800       0.20

Postion No1: 1.11700       0.25

Postion No1: 1.11600       0.30

TotalLot Is : 1.00 

Whenever our profit reaches 10% lot, the total profit is $ 100, all these 5 positions must be closed


Thanks,

 

Hello to all professors

I wrote another code with higher accuracy

But I think it should work properly

But I could not draw conclusions from this code

Where is my code problem?


extern int Magic=0;
input double PercentageRiskValue=0.10;
double MaxPositionProfit;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
totalOpenLots();
totalProfit();
   
  }
//+------------------------------------------------------------------+

double totalOpenLots()
  {
        double totalOpenLots = 0;
        for(int i = OrdersTotal() -1; i >= 0; i--)
        {
        if(OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == "GBPUSD" && OrderMagicNumber() == Magic)
       
        totalOpenLots += OrderLots();
        Comment ("totalOpenLots",totalOpenLots);
       
         }
        return totalOpenLots;
  }  
  
//+------------------------------------------------------------------+
  double totalProfit()
  {
        double totalProfit = 0;
        for(int i = OrdersTotal() -1; i >= 0; i--)
    {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == "GBPUSD" && OrderMagicNumber() == Magic)
       
    {
        totalProfit += (OrderProfit() + OrderCommission() + OrderSwap());
        MaxPositionProfit=(totalOpenLots()/100*PercentageRiskValue);
        if (totalProfit > MaxPositionProfit) 
    {
         bool result=OrderClose ( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(),MODE_BID),5,Red);
         Comment ("Error - failed to OrderClose.");
    }
    }
    }
    return totalProfit;
     }
    
  
   
   

 

It's simple, but I think I'm a little wrong

With this code, every time every purification is closed after 10 pips in profit

int Magic=0;
int Magic8=8;

//+------------------------------------------------------------------+
void OnTick()
  {
 Trail2();
   
  }
//+------------------------------------------------------------------+
void Trail2()
  {
  bool rest;
   int total = OrdersTotal();
   for(int i=total-1; i>=0; i--)
     {
      rest=OrderSelect(i, SELECT_BY_POS);
      int type   = OrderType();

      bool result = false;
// Buy
      if(
         OrderMagicNumber()==Magic
         &&
         (((Bid-OrderOpenPrice())/Point)>100 )
         &&
         OrderType()==OP_BUY
      )
         result = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE);
// Sell
      if(
         OrderMagicNumber()==Magic8
         &&
         (((OrderOpenPrice()-Ask)/Point)>100)
         &&
         OrderType()==OP_SELL
         )
        
         result = OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE);
}
  
  }
  
 



And here I came up with another function to write the sum of the lots

  void totalOpenLots()    
    
    { 
    double totalOpenLots = 0;
    for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
    &&  OrderMagicNumber()  == Magic                 // my magic number
    &&  OrderSymbol()       == "GBPUSD"                 // and my pair.
    ){  totalOpenLots += OrderLots(); 
    Print ("totalOpenLots:" ,totalOpenLots);
  }
   }

Now how to close all positions whenever the sum of all positions is multiplied by 10 pips

I mean, how do I integrate this first code with 2 and

Thankful

 
ghobar #:

It's simple, but I think I'm a little wrong

With this code, every time every purification is closed after 10 pips in profit



And here I came up with another function to write the sum of the lots

Now how to close all positions whenever the sum of all positions is multiplied by 10 pips

I mean, how do I integrate this first code with 2 and

Thankful



if (AccountProfit() > totalOpenLots * 100) // is the condition you looking for.
 
ghobar #:

Hello to all professors

I wrote another code with higher accuracy

But I think it should work properly

But I could not draw conclusions from this code

Where is my code problem?


when you're calculating this part:

MaxPositionProfit=(totalOpenLots()/100*PercentageRiskValue);

if totalOpenLots is 1, and Risk is 10, then MaxPositionsProfit equals 0.1 . but you want it to be 100.


look at what @Seb2021 said
Reason: