Problem with calculating lot size - please help!

 

I am increasing the lot size with each subsequent Limit transaction but my code/formula just keeps on returning my starting lot size.

Here is the code to calculate the lot sizes with each subsequent limit trade that is generated:

input double    Start_LotSize = 0.01
input int       LotMultiplier = 150;

   Buy = 0;
   Sell = 0;
   BuyLimit = 0;
   SellLimit = 0;
   BuyTrade = false;
   SellTrade = false;
  

   Count = 0;

   for(Count = OrdersTotal()-1; Count >= 0; Count--)
     {
      if(OrderSelect(Count, SELECT_BY_POS, MODE_TRADES)
         && OrderMagicNumber() == MagicNumber
         && OrderSymbol() == Symbol())
         //&& OrderCloseTime() == 0)

        {
         if(OrderType()== OP_SELL)
            Sell++;

         if(OrderType()== OP_BUY)
            Buy++;
            
         if(OrderType() == OP_SELLLIMIT)
            SellLimit++;
            
         if(OrderType() == OP_BUYLIMIT)
            BuyLimit++;      
        }
     }
     
BuyLimitLotSize = Start_LotSize * MathPow((LotMultiplier/100),Buy+1);
 
Ernest Klokow: but my code/formula just keeps on returning my starting lot size.

Your code detects no open buy orders, so it returns Start_LotSize.

 

It actually does detect multiple open Buy orders:

From Journal:

2022.08.08 16:31:01.145 2021.08.04 03:04:06  Profit (2) EURUSD,M15: LotSize is 0.01 Buy is 4


 
Ernest Klokow #:

It actually does detect multiple open Buy orders:

From Journal:

2022.08.08 16:31:01.145 2021.08.04 03:04:06  Profit (2) EURUSD,M15: LotSize is 0.01 Buy is 4


The variable Buy is set to "0" right at the start of the code. (See below) So it cannot be changed to "0" after calculation when it reaches my lot calculation code.

Also, I have tested the formula on my calculator and it gives the correct value.

void OnTick()
  {
  
//Count number of open trades

   Buy = 0;
   Sell = 0;
   BuyLimit = 0;
   SellLimit = 0;
   BuyTrade = false;
   SellTrade = false;
  

   Count = 0;

   for(Count = OrdersTotal()-1; Count >= 0; Count--)
     {
      if(OrderSelect(Count, SELECT_BY_POS, MODE_TRADES)
         && OrderMagicNumber() == MagicNumber
         && OrderSymbol() == Symbol())
         //&& OrderCloseTime() == 0)

        {
         if(OrderType()== OP_SELL)
            Sell++;

         if(OrderType()== OP_BUY)
            Buy++;
            
         if(OrderType() == OP_SELLLIMIT)
            SellLimit++;
            
         if(OrderType() == OP_BUYLIMIT)
            BuyLimit++;      
        }
     } 
 

The proble lies with the following expression

MathPow((LotMultiplier/100),Buy+1)

that keeps on returning 1.0 no matter what the value of Buy is!

 
Ernest Klokow #:

It actually does detect multiple open Buy orders:

From Journal:

2022.08.08 16:31:01.145 2021.08.04 03:04:06  Profit (2) EURUSD,M15: LotSize is 0.01 Buy is 4

  1. Your posted code has no such Print statement. Always post all relevant code.

  2. input int       LotMultiplier = 150;
    ⋮
    BuyLimitLotSize = Start_LotSize * MathPow((LotMultiplier/100),Buy+1);
    150/100=1. 1^5=1. Start_LotSize * 1 = Start_LotSize
              On MT4 v434, division quotient don't give floating point values(Bug??) - MQL4 programming forum (2012)
              build 2504 & 2506 Bug - General - MQL5 programming forum - Page 2 #17 (2020)

 

Thank you so much WH Roeder!

I discovered the problem a few minutes a go. I changed the type of the LotMultiplier variable from "int" to "double" and now it works!

MAY I SAY THAT YOU ARE A GENIUS!!

Thank you again!

 
Ernest Klokow #:

The proble lies with the following expression

that keeps on returning 1.0 no matter what the value of Buy is!

Yes, that's right.

Because the calculations you do here ends up being either 1 or 0 as the first input to your MathPow, and since by definition 1 or 0 to the power of whatever will be 1.

Changing the variable to double type changes the outcome of your division and therefore changes the result of your MathPow function.



 

Thanks Dominik and Roeder!

Your help greatly appreciated!