how to create changing Lots ?

 

hello:

in my ea,i want the order lots change with AccountFreeMargin or AccountEquity,but when i test it, freemargin arise from 10000 to 20000 or above, the Lots is still 0.1, it should be 0.2 0.3 0.4

my code as follow,may someone help me ? thanks

#property copyright ".................."
#property link "......................"

extern double Lots ;

..............

int start()
{

.................

int lot =MathRound(AccountFreeMargin()/10000);

if (lot==0) Lots =0.1;

else Lots=lot/10;

.... ....... ...... .....

ticket=OrderSend(...,Lots,............);

...... ...... ..... .......

return;

}

 
YALEWANG:

hello:

in my ea,i want the order lots change with AccountFreeMargin or AccountEquity,but when i test it, freemargin arise from 10000 to 20000 or above, the Lots is still 0.1, it should be 0.2 0.3 0.4

[...]

int lot =MathRound(AccountFreeMargin()/10000);

if (lot==0) Lots =0.1;

else Lots=lot/10;

It's because you're storing the result of a fractional calculation in an int. Let's say that free margin is 20,000. The calculation MathRound(AccountFreeMargin()/10000) produces the value 2. This is then divided by 10, to 0.2. However, although the result of 2 / 10 is stored in the double Lots, it is evaluated by MQL as an integer because the lot variable is an int, and gets rounded down to zero. For comparison, try out the following and see what result you get:


int iVal = 2;
double dVal = iVal / 10;
MessageBox("Result of calculation: " + dVal);

In short: declare the lot variable as a double, not an int.


Reason: