Math and Lotsize

 
in my code i have a fibo sequence, and multiply that by 0.01 to get my lotsize. simple enuf. But why do i often get some weird numbers? while other times, the lotsize is correct, while other times it is not? This seems to happen with many eas, and not just in mt4 either. in the example in the former sentence, I get 3.25 when 13 is the current step in my martingale system, sometimes the correct number is opened with the new trade, other times, I get 2.75. Any suggestions of what i can test? I have made comments, printing of the lotsize before sending the ordersend, and get the same weird numbers that i have mentioned.
 
Revo Trades: in my code i have a fibo sequence, and multiply that by 0.01 to get my lotsize. simple enuf. But why do i often get some weird numbers? while other times, the lotsize is correct, while other times it is not? This seems to happen with many eas, and not just in mt4 either. in the example in the former sentence, I get 3.25 when 13 is the current step in my martingale system, sometimes the correct number is opened with the new trade, other times, I get 2.75. Any suggestions of what i can test? I have made comments, printing of the lotsize before sending the ordersend, and get the same weird numbers that i have mentioned.

Without a code sample, we don’t have the foggiest idea of what you are doing (correctly or incorrectly), nor can we guess what you should be doing.

All I can ask is, are you properly adjusting to the symbol’s volume step and verifying for the minimum and maximum volume allowed?

Forum on trading, automated trading systems and testing trading strategies

How to calculate lots using multiplier according to number of opened orders?

Fernando Carreiro, 2017.09.01 21:57

Don't use NormalizeDouble(). Here is some guidance (code is untested, just serves as example):

// Variables for Symbol Volume Conditions
double
   dblLotsMinimum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MIN  ),
   dblLotsMaximum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MAX  ),
   dblLotsStep    = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_STEP );
   
// Variables for Geometric Progression
double
   dblGeoRatio = 2.8,
   dblGeoInit  = dblLotsMinimum;
   
// Calculate Next Geometric Element
double
   dblGeoNext  = dblGeoInit * pow( dblGeoRatio, intOrderCount + 1 );
   
// Adjust Volume for allowable conditions
double
   dblLotsNext = fmin( dblLotsMaximum,                                     // Prevent too greater volume
                   fmax( dblLotsMinimum,                                   // Prevent too smaller volume
                     round( dblGeoNext / dblLotsStep ) * dblLotsStep ) );  // Align to Step value

 
ok i will try that and report back here. thanks again fernando
 
Revo Trades: ok i will try that and report back here. thanks again fernando
You are welcome!
 
  1. Revo Trades: But why do i often get some weird numbers?

    Floating-point has an infinite number of decimals, it's you, not understanding floating-point and that some numbers can't be represented exactly. (like 1/10.)
              Double-precision floating-point format - Wikipedia

    See also The == operand. - MQL4 programming forum 2013.06.07

    If you want to see the correct number of digits, convert it to a string with the correct/wanted accuracy.
              question about decima of marketinfo() - MQL4 programming forum 2016.05.18


  2. Fernando Carreiro:

                         round( dblGeoNext / dblLotsStep ) * dblLotsStep ) );  // Align to Step value

    I prefer to make an explicit function call.
              MT4:NormalizeDouble - General - MQL5 programming forum #3.3 2017.01.04
              How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum #2 2017.05.19

 
William Roeder:
  1. Floating-point has an infinite number of decimals, it's you, not understanding floating-point and that some numbers can't be represented exactly. (like 1/10.)
              Double-precision floating-point format - Wikipedia

    See also The == operand. - MQL4 programming forum 2013.06.07

    If you want to see the correct number of digits, convert it to a string with the correct/wanted accuracy.
              question about decima of marketinfo() - MQL4 programming forum 2016.05.18


  2. I prefer to make an explicit function call.
              MT4:NormalizeDouble - General - MQL5 programming forum #3.3 2017.01.04
              How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum #2 2017.05.19

ok. i will read up. thanks for responding.

 

so would i be correct in thinking that when i use Digits in Normalize(price,Digits), that Digits does not always report the correct value? and by using the DoubleToString function, how does that improve the end result since that that same function uses the same Digits value?

edit == i think i answered my own question, but please answer the q anyways, so i can confirm my own reasoning. thanks.

Since i believe it is all down to using NormalizeDouble where I shouldnt be, i will try inserting this in my mg/lotsize calculations. If you suspect any issues resulting, please point them out in a reply.


#property strict

//global
double fibo[]={1,1,2,3,5,8,13,21,55};
input double iStartLot = 0.25;

//mg lotsize func
double round (double z) // z == place in the fibo buffer
  {
   int D= MathPow(10,Digits);

   double value = fibo[z] * iStartLot;
   double x =  ( MathRound (value * D)) / D * z;

   return(x);
  } 

//example OrderSend == round(6) == lotsize = (expected value) = fibo[6] * 0.25 == 13 * 0.25, or 3.25
OrderSend(_Symbol,OP_BUY,round(6),Ask,1,0,0,iComment,iMagic,0,clrBlue);

Note: completely untested. Any suggestions or criticism accepted.

 
Revo Trades: so would i be correct in thinking that when i use Digits in Normalize(price,Digits), that Digits does not always report the correct value? and by using the DoubleToString function, how does that improve the end result since that that same function uses the same Digits value? edit == i think i answered my own question, but please answer the q anyways, so i can confirm my own reasoning. thanks.


Since i believe it is all down to using NormalizeDouble where I shouldnt be, i will try inserting this in my mg/lotsize calculations. If you suspect any issues resulting, please point them out in a reply.

Note: completely untested. Any suggestions or criticism accepted.

No NormaliseDouble needed! Is this what you are after (untested/uncompiled)?

#property strict

//global
fibo[]={1,1,2,3,5,8,13,21,55};
input iStartLot = 0.25;

//mg lotsize func
double FiboLotSize( int index )
{
   // Variables for Symbol Volume Conditions
      double
         dblLotsMinimum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MIN  ),
         dblLotsMaximum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MAX  ),
         dblLotsStep    = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_STEP );
       
   // Calculate Lot Size based on Fibonacci Sequence
      double
         dblLots = iStartLot * fibo[ index ];
       
   // Adjust Volume for allowable conditions
         dblLots = fmin( dblLotsMaximum,                                   // Prevent too greater volume
                     fmax( dblLotsMinimum,                                 // Prevent too smaller volume
                       round( dblLots / dblLotsStep ) * dblLotsStep ) );   // Align to Step value
   
   // Return Calculated Value of Lot Size
      return dblLots;
};

void OnTick(void)
{
   //example OrderSend == round(7) == lotsize = (expected value) = fibo[6] * 0.01 == 13 * 0.01, or 0.13
   OrderSend( _Symbol, OP_BUY, FiboLotSize(7), Ask, 1, 0, 0, iComment, iMagic, 0, clrBlue );
};
 
Fernando Carreiro:

No NormaliseDouble needed! Is this what you are after (untested/uncompiled)?

i had normalizeDouble in my original code, and already had the checking of lotstep/maxlot/minlot, but yours does look more elegant haha. thanks again.

 
i have yet to prove anything, however, I was using NormalizeDouble like this...
      double
         dblLots = NormalizeDouble(fmin( dblLotsMaximum,                                   // Prevent too greater volume
                     fmax( dblLotsMinimum,                                 // Prevent too smaller volume
                       round( dblLots / dblLotsStep ) * dblLotsStep ) ),2);   // Align to Step value

But i was/am getting weird numbers sometimes, like instead of 3.25 lots, getting 2.75. If I am understanding the readings correctly, NormalizeDouble does not round the same way as the func round does, so this may be the root of my issue. If you concur please respond with a yay or nay. either way I will test the code during london tomoz and report back in 24 hours if nothing has changed. If I dont get any weird lots in 48, then I will jump for joy, and report after that.

 
Revo Trades: i have yet to prove anything, however, I was using NormalizeDouble like this...

But i was/am getting weird numbers sometimes, like instead of 3.25 lots, getting 2.75. If I am understanding the readings correctly, NormalizeDouble does not round the same way as the func round does, so this may be the root of my issue. If you concur please respond with a yay or nay. either way I will test the code during london tomoz and report back in 24 hours if nothing has changed. If I dont get any weird lots in 48, then I will jump for joy, and report after that.

I make it a point in my code to NEVER use NormaliseDouble(), EVER. If you search the forum you will find that this has been debated many, many times in a very heated way. Some in favour of using it and a few like myself, against using it.

I will leave it up to you to decide for yourself what you prefer doing.

Suffice to say, that by using the calculations I have shared above, I've have never had any problems or any errors about invalid volume when placing an order.

Reason: