# What is Error 4051 and how to fix it

Hi guys,

I’m currently in a bind with my EA. Everything was fine and dandy until i tried adding an auto lot sizing function or money mangement

The following are the codes I’ve added in and got me the error.

``````
input bool isSizingOn = true;   // Auto Lot Sizing
input int Risk = 1;  // Risk level (%)

int OnInit(){
if(Digits == 5 || Digits == 3 || Digits == 1)P = 10;else P = 1; // To account for 5 digit brokers
if(Digits == 3 || Digits == 2) isYenPair = true; // Adjust for YenPair

if (isSizingOn == true)
{
Lots = Entry_Amount;
Lots = Risk * 0.01 * AccountBalance() / (MarketInfo(Symbol(),MODE_LOTSIZE) * Stop_Loss * P * Point); // Sizing Algo based on account size
if(isYenPair == true) Lots = Lots * 100; // Adjust for Yen Pairs
Lots = NormalizeDouble(Lots, 2); // Round to 2 decimal place
}
}

``````

All variables are declared, no errors, not warnings.

Is my calculation wrong? Or am I doing it entirely wrong?

``````* if(Digits == 3 || Digits == 2) isYenPair = true; // Adjust for YenPair
``````

If you change symbol, your EA goes through a deinit/init cycle, but your variable will be wrong.

``````isYenPair = Digits == 3 || Digits == 2;
``````
``````* if (isSizingOn == true)
``````

You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don’t write if(bool == true) , just use if(bool) or if( ! bool) . Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and “if long entry” is an incomplete sentence.

• Never risk more than a small percentage of your account, certainly less than 2% per trade, 6% total.
1. In code (MT 4 ): Risk depends on your initial stop loss, lot size, and the value of the pair.
2. You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
3. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread , and DeltaPerLot is usually around \$10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
4. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument’s base currency.
MODE_TICKVALUE is not reliable on non-fx instruments with many brokers.
5. You must normalize lots properly and check against min and max
6. You must also check FreeMargin to avoid stop out Most pairs are worth about \$10 per

Thank you very much csertin1n! I am going through the codes now. Your help is very much appreciated.

Try this little modification.

`````` if (isYenPair == true ) Lots = Lots * 100.0 ; // Adjust for Yen Pairs
``````

Never multiply a Double value with an Int value because the result could be an Int (depending on the language).

The following variables should be type Double: Risk, Stop_Loss, P and Lots.

Never in any language that I know of. They all will up cast - D•I always becomes D•double(I). All you save is a cast that the optimizer should do at compile time.

Only an int÷int is a problem if you expect a fraction.

You good Sir, have solved my problem!

Sorry it took me so long to get back to my EA’s issue, and since tomorrow is a holiday, I had time now to really sit down and go through your comment properly.

Your comment on if (bool) and if (!bool) made me take a good look at the code in detail. And I realized I have forgotten to add in the !bool portion. With that last bit of code, everything is in order. Auto sizing can now be selected; and it is working.

``````if (isSizingOn)
{
Lots = Entry_Amount;
Lots = Risk * 0.01 * AccountBalance() / (MarketInfo(Symbol(),MODE_LOTSIZE) * Stop_Loss * P * Point); // Sizing Algo based on account size
if(isYenPair == true) Lots = Lots * 100.0; // Adjust for Yen Pairs
}
if (!isSizingOn)
{
Lots = Entry_Amount;
``````