algorithmmql4metatrader4mt4

How to group a lot exponent in MQL4 / MT4?


I manage to get normal lot exponent but I need to change it a bit by grouping example in the picture. Can someone guide me how to get the lot exponent but with a grouping technique Group=5 Exponent=1.8?

double GroupExponent(int type)
{
   double lot=0,exponent=1.8,group=5,initialLot=0.01;
   if(type==OP_SELL)                         
   .............                            //<---- Do i need to loop this area ?
      lot= initialLot * MathPow(exponent,TotalSell());   
   return lot;
}

int TotalSell()
{
   int Sell=0;
   for(int trade=OrdersTotal()-1; trade>=0; trade--)
   {
      if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))continue;
      if(OrderSymbol()==Symbol() && OrderType()==OP_SELL))
         Sell++;
   }
   return Sell;
}

enter image description here


Solution

  • No, you don't need a cycle. You can attain your layer system like this:

    double GroupExponent(int type)
    {
       double lot=0,exponent=1.8,initialLot=0.01;
       int group = 5;
       if(type==OP_SELL)                         
       {
          lot = NormalizeDouble(initialLot * MathPow(exponent, (TotalSell() - 1) / group), 2);
       }
       return lot;
    }
    
    int TotalSell()
    {
       int Sell=0;
       for(int trade=OrdersTotal()-1; trade>=0; trade--)
       {
          if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))continue;
          if(OrderSymbol()==Symbol() && OrderType()==OP_SELL))
             Sell++;
       }
       return Sell;
    }
    

    Note: This will work as intended only if Layer == 1 equals TotalSell() == 1. If Layer == 1 equals TotalSell() == 0, then there is no need to subtract 1 from TotalSell() inside MathPow().