calculate Lot. Mql4 - page 2

 
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?


extern int Magic=0;
input double PercentageRiskValue=0.10;
double MaxPositionProfit;
bool   TheCloseState=0;//Mark if On Closing
double totalProfit = 0;
double totalOpenLots = 0;

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
//Closing
if(TheCloseState==true)
{
TheCloseState=false;

    for(int i = OrdersTotal() -1; i >= 0; i--)
    {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == "GBPUSD" && OrderMagicNumber() == Magic)//Exactly OrderType()should <=1 and many other conditions you want
        {
        bool result=OrderClose ( OrderTicket(), OrderLots(), OrderClosePrice(),5,Red);
        
            if(result==false)
            {
            TheCloseState=true;//If Close Failed Still On Closing
            continue;
            }
        }
    }//end for

    if(TheCloseState==true) return;
}

GetTotalLotsProfit();//get Lots and Profit

if(totalOpenLots > 0 && totalProfit > 0)
{
MaxPositionProfit=1000*totalOpenLots*PercentageRiskValue;
//100            =1000*1.0          *0.1;//for example: 1.0 Lot -- 10% RiskValue Need 100.00$ Profit

    if(totalProfit >=MaxPositionProfit)
    {
    TheCloseState=true;
    return;
    }
}
   
}
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
  void GetTotalLotsProfit()
  {
  totalProfit = 0;
  totalOpenLots = 0;

      for(int i = OrdersTotal() -1; i >= 0; i--)
      {
          if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == "GBPUSD" && OrderMagicNumber() == Magic)//Exactly OrderType()should <=1 and many other conditions you want
          {
          totalOpenLots += OrderLots();
          totalProfit += (OrderProfit() + OrderCommission() + OrderSwap());
          }
      }
   }

Just For Reference

 
 void closeatprofit()

 {
 //-------- total profits..  
   double lotstotal  = 0;
   double TotalProfit= 0;
   for(int i=OrdersTotal()-1; i >= 0; i--)
   {
   if (OrderSelect(i,SELECT_BY_POS))
   if(OrderSymbol() == Symbol()) 
     {
     TotalProfit= TotalProfit+ OrderProfit() + OrderSwap() + OrderCommission();
     }    
   }
   
 //---------------------total lots...
   for(int i=OrdersTotal()-1; i >= 0; i--)
   { 
       if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
            {
               if(OrderType() == OP_BUY || OP_SELL) //Counting total Size of buy+sell order (not pending).
                  lotstotal+=OrderLots();    
             }  
            }
  
 //-----------------------closing condition.. 
 
 if(TotalProfit > lotstotal*100) // here is the condition asked.  
 {  
  for(int i=OrdersTotal()-1;i >= 0; i--)
  {
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderType()==OP_BUY || OP_SELL))
     {
       double ticket = OrderClose (OrderTicket(), OrderLots(), OrderClosePrice(), 3, CLR_NONE);
     }
  }
 }



Try this one.... test yourself before using.....

 
Seb2021 #:

Try this one.... test yourself before using.....

Some Shortcomings :

First of All,one For() Loop cannot make sure that all orders can be colsed those should be,if some failed they will be ignored by your code;

Secondly,we can get total Lots and Profit in only one  For()  Loop,that is higher efficient.

 
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.


  • Loop through all the open trades and get the sum of the profits from each trade :


TotalProfitsFromAllTrades += OrderProfit() + OrderSwap() + OrderCommision();


  • Check if the total profits from all trades exceeds X $.
  • If total profits exceeds X $ close all the open trades one by one.
 
Ao Shen #:

Just For Reference

Hello dear teacher and friend

I tried your code, it works properly

But to become more professional, it needs a few modifications

We want this code to calculate the volume of positions (buy and sell) separately and also to close the positions separately.

When I use your code and just change the magic number, it does the calculations well

But if I want to use 2 magic separately, only the positions (bye with magic 3) are calculated correctly and it is closed and the positions (tuberculosis with magic 4) are not calculated and are not closed.

I modified the code like this and got no result

//+------------------------------------------------------------------+
void GetProfitBuy()
{
//Closing
if(TheCloseState==true)
{
TheCloseState=false;

    for(int i = OrdersTotal() -1; i >= 0; i--)
    {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == "GBPUSD" && OrderMagicNumber() == Magic3 && OrderType()==OP_BUY)//Exactly OrderType()should <=1 and many other conditions you want
        {
        bool result=OrderClose ( OrderTicket(), OrderLots(), OrderClosePrice(),5,Red);
        
            if(result==false)
            {
            TheCloseState=true;//If Close Failed Still On Closing
            continue;
            }
        }
    }//end for

    if(TheCloseState==true) return;
}

GetTotalLotsProfit();//get Lots and Profit

if(totalOpenLots > 0 && totalProfit > 0)
{
MaxPositionProfit=1000*totalOpenLots*PercentageRiskValue;
//100            =1000*1.0          *0.1;//for example: 1.0 Lot -- 10% RiskValue Need 100.00$ Profit

    if(totalProfit >=MaxPositionProfit)
    {
    TheCloseState=true;
    return;
    }
}
   
}
//+------------------------------------------------------------------+
  void GetTotalLotsProfit()
  {
  totalProfit = 0;
  totalOpenLots = 0;

      for(int i = OrdersTotal() -1; i >= 0; i--)
      {
          if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == "GBPUSD" && OrderMagicNumber() == Magic3 && OrderType()==OP_BUY)//Exactly OrderType()should <=1 and many other conditions you want
          {
          totalOpenLots += OrderLots();
          totalProfit += (OrderProfit() + OrderCommission() + OrderSwap());
          }
      }
   }
   
   
 ///////////////////////////////////////////////////////////////////////////////////////////////////
   
   
 void GetProfitSell()
{
//Closing
if(TheCloseState==true)
{
TheCloseState=false;

    for(int i = OrdersTotal() -1; i >= 0; i--)
    {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == "GBPUSD" && OrderMagicNumber() == Magic4 && OrderType()==OP_SELL)//Exactly OrderType()should <=1 and many other conditions you want
        {
        bool result=OrderClose ( OrderTicket(), OrderLots(), OrderClosePrice(),5,Red);
        
            if(result==false)
            {
            TheCloseState=true;//If Close Failed Still On Closing
            continue;
            }
        }
    }//end for

    if(TheCloseState==true) return;
}

GetTotalLotsProfitSell();//get Lots and Profit

if(totalOpenLots > 0 && totalProfit > 0)
{
MaxPositionProfit=1000*totalOpenLots*PercentageRiskValue;
//100            =1000*1.0          *0.1;//for example: 1.0 Lot -- 10% RiskValue Need 100.00$ Profit

    if(totalProfit >=MaxPositionProfit)
    {
    TheCloseState=true;
    return;
    }
}
   
}
//+------------------------------------------------------------------+
  void GetTotalLotsProfitSell()
  {
  totalProfit = 0;
  totalOpenLots = 0;

      for(int i = OrdersTotal() -1; i >= 0; i--)
      {
          if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == "GBPUSD" && OrderMagicNumber() == Magic4 && OrderType()==OP_SELL)//Exactly OrderType()should <=1 and many other conditions you want
          {
          totalOpenLots += OrderLots();
          totalProfit += (OrderProfit() + OrderCommission() + OrderSwap());
          }
      }
   }
  

 

Thanks to all the professors (for helping)

The problem was solved.

 
ghobar #:

Hello dear teacher and friend

I tried your code, it works properly

But to become more professional, it needs a few modifications

We want this code to calculate the volume of positions (buy and sell) separately and also to close the positions separately.

When I use your code and just change the magic number, it does the calculations well

But if I want to use 2 magic separately, only the positions (bye with magic 3) are calculated correctly and it is closed and the positions (tuberculosis with magic 4) are not calculated and are not closed.

I modified the code like this and got no result

#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input  int    Magic1=0;//if Magic1==-1 means don't separate by MagicNumber
input  int    Magic2=-1;
input  int    Magic3=-1;
input  double PercentageRiskValue=0.10;
double MaxPositionProfit;

bool   TheCloseStateBuy = false;//Mark if On Closing
bool   TheCloseStateSell = false;//Mark if On Closing
int    TheOnCloseMagicBuy=-2;
int    TheOnCloseMagicSell=-2;
double TheTotalLotsProfitBuy[3][2];//[LotsBuy_Magic1|ProfitBuy_Magic1||LotsBuy_Magic2|ProfitBuy_Magic2||LotsBuy_Magic3|ProfitBuy_Magic3]
double TheTotalLotsProfitSell[3][2];

string TheSymbol="GBPUSD";


void OnTick()
{
//OrderBuy On Closing
if(TheCloseStateBuy)
{
TheCloseStateBuy=false;

    for(int i = OrdersTotal() -1; i >= 0; i--)
    {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && 
           OrderSymbol() == TheSymbol && 
           OrderType()==OP_BUY &&
           (TheOnCloseMagicBuy==-1 || OrderMagicNumber() == TheOnCloseMagicBuy))
        {
        bool result=OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(),5,clrRed);
        
            if(result==false)
            {
            TheCloseStateBuy=true;//If Close Failed Still On Closing
            continue;
            }
        }
    }//end for

    if(TheCloseStateBuy==true) 
    {
    return;
    }else
    {
    TheOnCloseMagicBuy=-2;
    }
}

//OrderSell On Closing
if(TheCloseStateSell)
{
TheCloseStateSell=false;

    for(int i = OrdersTotal() -1; i >= 0; i--)
    {
        if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && 
           OrderSymbol() == TheSymbol && 
           OrderType()==OP_SELL &&
           (TheOnCloseMagicSell==-1 || OrderMagicNumber() == TheOnCloseMagicSell))
        {
        bool result=OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(),5,clrLime);
        
            if(result==false)
            {
            TheCloseStateSell=true;//If Close Failed Still On Closing
            continue;
            }
        }
    }//end for

    if(TheCloseStateSell==true) 
    {
    return;
    }else
    {
    TheOnCloseMagicSell=-2;
    }
}


GetTotalLotsProfit(TheSymbol,TheTotalLotsProfitBuy,TheTotalLotsProfitSell,Magic1,Magic2,Magic3);//get Lots and Profit

if(Magic1==-1)
{
   if(TheTotalLotsProfitBuy[0][0] > 0 && TheTotalLotsProfitBuy[0][1] > 0)
   {
   MaxPositionProfit=1000*TheTotalLotsProfitBuy[0][0]*PercentageRiskValue;
   
      if(TheTotalLotsProfitBuy[0][1] >= MaxPositionProfit)
      {
      TheCloseStateBuy=true;
      TheOnCloseMagicBuy=-1;
      return;
      }
   }
   
   if(TheTotalLotsProfitSell[0][0] > 0 && TheTotalLotsProfitSell[0][1] > 0)
   {
   MaxPositionProfit=1000*TheTotalLotsProfitSell[0][0]*PercentageRiskValue;
   
      if(TheTotalLotsProfitSell[0][1] >= MaxPositionProfit)
      {
      TheCloseStateSell=true;
      TheOnCloseMagicSell=-1;
      return;
      }
   }
   
}else
{
   if(TheTotalLotsProfitBuy[0][0] > 0 && TheTotalLotsProfitBuy[0][1] > 0)
   {
   MaxPositionProfit=1000*TheTotalLotsProfitBuy[0][0]*PercentageRiskValue;
   
      if(TheTotalLotsProfitBuy[0][1] >= MaxPositionProfit)
      {
      TheCloseStateBuy=true;
      TheOnCloseMagicBuy=Magic1;
      return;
      }
   }
   
   if(TheTotalLotsProfitBuy[1][0] > 0 && TheTotalLotsProfitBuy[1][1] > 0)
   {
   MaxPositionProfit=1000*TheTotalLotsProfitBuy[1][0]*PercentageRiskValue;
   
      if(TheTotalLotsProfitBuy[1][1] >= MaxPositionProfit)
      {
      TheCloseStateBuy=true;
      TheOnCloseMagicBuy=Magic2;
      return;
      }
   }
   
   if(TheTotalLotsProfitBuy[2][0] > 0 && TheTotalLotsProfitBuy[2][1] > 0)
   {
   MaxPositionProfit=1000*TheTotalLotsProfitBuy[2][0]*PercentageRiskValue;
   
      if(TheTotalLotsProfitBuy[2][1] >= MaxPositionProfit)
      {
      TheCloseStateBuy=true;
      TheOnCloseMagicBuy=Magic3;
      return;
      }
   }
   
   
   
   if(TheTotalLotsProfitSell[0][0] > 0 && TheTotalLotsProfitSell[0][1] > 0)
   {
   MaxPositionProfit=1000*TheTotalLotsProfitSell[0][0]*PercentageRiskValue;
   
      if(TheTotalLotsProfitSell[0][1] >= MaxPositionProfit)
      {
      TheCloseStateSell=true;
      TheOnCloseMagicSell=Magic1;
      return;
      }
   }

   if(TheTotalLotsProfitSell[1][0] > 0 && TheTotalLotsProfitSell[1][1] > 0)
   {
   MaxPositionProfit=1000*TheTotalLotsProfitSell[1][0]*PercentageRiskValue;
   
      if(TheTotalLotsProfitSell[1][1] >= MaxPositionProfit)
      {
      TheCloseStateSell=true;
      TheOnCloseMagicSell=Magic2;
      return;
      }
   }
   
   if(TheTotalLotsProfitSell[2][0] > 0 && TheTotalLotsProfitSell[2][1] > 0)
   {
   MaxPositionProfit=1000*TheTotalLotsProfitSell[2][0]*PercentageRiskValue;
   
      if(TheTotalLotsProfitSell[2][1] >= MaxPositionProfit)
      {
      TheCloseStateSell=true;
      TheOnCloseMagicSell=Magic3;
      return;
      }
   }

}



}
//+------------------------------------------------------------------+


void GetTotalLotsProfit(string MySymbol,double& MyTotalLPBuy[][2],double& MyTotalLPSell[][2],
                        int MyMagic1,int MyMagic2=-1,int MyMagic3=-1)
{
ArrayInitialize(MyTotalLPBuy,0.0);
ArrayInitialize(MyTotalLPSell,0.0);

   for(int i = OrdersTotal() -1; i >= 0; i--)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && 
         OrderSymbol() == MySymbol)
      {
         if(OrderType()==OP_BUY)
         {
            if(MyMagic1==-1)//if MyMagic==-1 means don't separate by MagicNumber
            {
            MyTotalLPBuy[0][0] += OrderLots();
            MyTotalLPBuy[0][1] += (OrderProfit() + OrderCommission() + OrderSwap());
            continue;
            }
            
            if(OrderMagicNumber()==MyMagic1)
            {
            MyTotalLPBuy[0][0] += OrderLots();
            MyTotalLPBuy[0][1] += (OrderProfit() + OrderCommission() + OrderSwap());
            }
            
            if(OrderMagicNumber()==MyMagic2)
            {
            MyTotalLPBuy[1][0] += OrderLots();
            MyTotalLPBuy[1][1] += (OrderProfit() + OrderCommission() + OrderSwap());
            }
            
            if(OrderMagicNumber()==MyMagic3)
            {
            MyTotalLPBuy[2][0] += OrderLots();
            MyTotalLPBuy[2][1] += (OrderProfit() + OrderCommission() + OrderSwap());;
            }
         }
         
         if(OrderType()==OP_SELL)
         {
            if(MyMagic1==-1)//if MyMagic==-1 means don't separate by MagicNumber
            {
            MyTotalLPSell[0][0] += OrderLots();
            MyTotalLPSell[0][1] += (OrderProfit() + OrderCommission() + OrderSwap());
            continue;
            }
            
            if(OrderMagicNumber()==MyMagic1)
            {
            MyTotalLPSell[0][0] += OrderLots();
            MyTotalLPSell[0][1] += (OrderProfit() + OrderCommission() + OrderSwap());
            }
            
            if(OrderMagicNumber()==MyMagic2)
            {
            MyTotalLPSell[1][0] += OrderLots();
            MyTotalLPSell[1][1] += (OrderProfit() + OrderCommission() + OrderSwap());
            }
            
            if(OrderMagicNumber()==MyMagic3)
            {
            MyTotalLPSell[2][0] += OrderLots();
            MyTotalLPSell[2][1] += (OrderProfit() + OrderCommission() + OrderSwap());
            }
         }
      
      }
   }//end for
}

I've tested only by mind because there  is no order open module in this code 

 
Ao Shen #:

I've tested only by mind because there  is no order open module in this code 

Thankful
This is an example of a complete code