# Avoiding OrderSend Error 131

1017

It's a minor problem, but i would like to clear it, can't figured out the logic computation, so I might need some help from the community.

Elaboration:
Test data starts with 0.01 min lot. I would have no problem if my test amount is \$1000 or \$100, 10% of these amount, it will still be sufficient to ordersend with a 0.01 min lot.
Test data starts with 0.1 min lot, I would have problem with \$100, 10% of \$100, it won't be sufficient to ordersend, and thus error 131 occurs. Invalid lot.
I would like to stop the error at the check lot size calculation before flowing it to ordersend for the error to occur.

Own theory:
I will need MinLot, I will need MarginFree, I will need to ensure (MarginFree * Risk) is able to open MinLot, if unable, means (MarginFree * Risk) is insufficient.
So either increase MarginFree or Risk to make the MinLot else, if Risk and MarginFree is already 100 (Max) and still insufficient to open MinLot, do something else.

Not sure if these checks are useful or just redundant, some advise will be good.

```input double MarginPercent   = 100;
input double RiskPercent     = 10;
input double FixLots         = 0.0;
input double MaxLots         = 3.0;

double LotSizeCal()
{
double Risk = (RiskPercent / 100);
double MarginFree = AccountFreeMargin() * (MarginPercent / 100);
double MinLot = MarketInfo(Symbol(), MODE_MINLOT);
double OneLot = MarketInfo(Symbol(), MODE_MARGINREQUIRED);
double LotSize = MathFloor(MarginFree * Risk / OneLot / MinLot) * MinLot;//calculate the lot size base on free margin and risk percent

//check with min lot, always carry on trading with min lot
if((LotSize - MinLot) < 0)//LotSize < MinLot
{
LotSize = MinLot;
return(LotSize);
}

//check with max lot
if((LotSize - MaxLots) > 0)//LotSize > MaxLots
{
LotSize = MaxLots;
return(LotSize);
}
return(LotSize);
}```
48499

Teck Hua Chew:

It's a minor problem, but i would like to clear it, can't figured out the logic computation, so I might need some help from the community.

Elaboration:
Test data starts with 0.01 min lot. I would have no problem if my test amount is \$1000 or \$100, 10% of these amount, it will still be sufficient to ordersend with a 0.01 min lot.
Test data starts with 0.1 min lot, I would have problem with \$100, 10% of \$100, it won't be sufficient to ordersend, and thus error 131 occurs. Invalid lot.
I would like to stop the error at the check lot size calculation before flowing it to ordersend for the error to occur.

Own theory:
I will need MinLot, I will need MarginFree, I will need to ensure (MarginFree * Risk) is able to open MinLot, if unable, means (MarginFree * Risk) is insufficient.
So either increase MarginFree or Risk to make the MinLot else, if Risk and MarginFree is already 100 (Max) and still insufficient to open MinLot, do something else.

Not sure if these checks are useful or just redundant, some advise will be good.

Try to create scripts and print the value "LotSize", so you can adjust your calculated values.
11751

try this

```if(LotSize <= MinLot)//LotSize < MinLot
{
LotSize = MinLot;
return(LotSize);
}

//check with max lot
if(LotSize >= MaxLots)//LotSize > MaxLots
{
LotSize = MaxLots;
return(LotSize);
}```

if you it still insufficient, try to increase you leverage

1017

Siti Latifah:

try this

if you it still insufficient, try to increase you leverage

Ahhh, yes of course! Thanks!