Download MetaTrader 5

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

To add comments, please log in or register
toie
28
toie  

Hello guys.


I'd like to place every consequitive order using multiplier. Now I've hardcoded my prefered lots using an array. I don't like it.

For the first trade the lot size should be 0.01 for the second 0.03 for the third 0.08 for the forth 0.22 for the fifth 0.61 and so on.

I know if I use a variable to keep last order's lot size I can achieve this using multiplier of 2.8. But you know I want to use as less as possible variables.

So if I have 3 opened orders so far for instance if I want to get for the forth order lot size I should multiply 4 by 0.01 by 2.8 but it equals to 0.112 which is not correct.

I'm a little confused.

So please help.

Thanx.

Fernando Carreiro
3951
Fernando Carreiro  
toie: I'd like to place every consequitive order using multiplier. Now I've hardcoded my prefered lots using an array. I don't like it.

For the first trade the lot size should be 0.01 for the second 0.03 for the third 0.08 for the forth 0.22 for the fifth 0.61 and so on.

I know if I use a variable to keep last order's lot size I can achieve this using multiplier of 2.8. But you know I want to use as less as possible variables.

So if I have 3 opened orders so far for instance if I want to get for the forth order lot size I should multiply 4 by 0.01 by 2.8 but it equals to 0.112 which is not correct.

I'm a little confused.

So please help.

And so are we! It will be helpful if show some code so that we can understand what it is you want to achieve.

And for the math, some algebra might help as well explain your logic!

0.01, 0.03, 0.08, 0.22, 0.61 is a geometric series based around a common ratio ("r") of 2.8 as you yourself have stated. So, use the Geometric Sequence basics to calculate the elements, but remember to use rounding or ceiling/floor to get the actual final value.

Also, you should always check the brokers conditions such as Minimum Lot Size, Maximum Lot Size and Lot Step Value to adjust your volume.

PS! There is nothing wrong with using variables and it is highly recommend! They help make your code more readable and believe it or not, can make your code much more efficient and faster.

EDIT: No, it should be "0.01 * 2.8 ^ 4 = 0.614656" from the algebraic representation "a * r ^ k" (see the link above)
Geometric progression - Wikipedia
Geometric progression - Wikipedia
  • en.wikipedia.org
Diagram illustrating three basic geometric sequences of the pattern 1(rn−1) up to 6 iterations deep. The first block is a unit block and the dashed line represents the infinite sum of the sequence, a number that it will forever approach but never touch: 2, 3/2, and 4/3 respectively. In mathematics, a geometric progression, also known as a...
toie
28
toie  
Fernando Carreiro:

And so are we! It will be helpful if show some code so that we can understand what it is you want to achieve.

And for the math, some algebra might help as well explain your logic!

0.01, 0.03, 0.08, 0.22, 0.61 is a geometric series based around a common ratio ("r") of 2.8 as you yourself have stated. So, use the Geometric Sequence basics to calculate the elements, but remember to use rounding or ceiling/floor to get the actual final value.

Also, you should always check the brokers conditions such as Minimum Lot Size, Maximum Lot Size and Lot Step Value to adjust your volume.

PS! There is nothing wrong with using variables and it is highly recommend! They help make your code more readable and believe it or not, can make your code much more efficient and faster.

EDIT: No, it should be "0.01 * 2.8 ^ 4 = 0.614656" from the algebraic representation "a * r ^ k" (see the link above)

Thank you.

So it should be:

double nextLotSize=0.01*2.8^OpenedTrades();

The function OpenedTrades() explains itself...

uff... Forgot rounding..

Fernando Carreiro
3951
Fernando Carreiro  

toie: So it should be:

double nextLotSize=0.01*2.8^OpenedTrades();

The function OpenedTrades() explains itself...

No! You must use proper MQL maths notation. In MQL, the "^" symbol is not "raised to the power of". you have to use either "MathsPow()" function or "pow()".

However, it is not that "simple" because you still have to align your volume by the "Lot Step" and check "Lots Minimum" and "Lots Maximum"!

And please use variables. Don't Hard-Code. That is messy coding!

toie
28
toie  

Thank you for your time.

My prefinal result is:

double nextLotSize=NormalizeDouble(0.01*MathPow(2.8,OpenedTrades()),2);


I say prefinal because you mentioned I should check my brokers requirements.

Thanx again.

Fernando Carreiro
3951
Fernando Carreiro  
toie: Thank you for your time.

My prefinal result is:

double nextLotSize=NormalizeDouble(0.01*MathPow(2.8,OpenedTrades()),2);

I say prefinal because you mentioned I should check my brokers requirements.

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
toie
28
toie  

Looks great.

I'll put it in a function.

Thank you.

To add comments, please log in or register