Calculating Lotsize causes error 4051 invalid lots amount

 

Dear ladies and gents,

I wrote a this code to calculate the Lotsize depending on where the price is in addiction to the StandardDeviation:

 //Calculating Lotsize
      double longTradeLotsunrond = (InitialLotSize + ((Ask - MAlong) * ((2 * InitialLotSize) / (4 * StdDevlong)) *(-1)));
      double longTradeLots=NormalizeDouble(longTradeLotsunrond,1);
      double shortTradeLotsunround = (InitialLotSize - ((Ask - MAlong) * ((2 * InitialLotSize) / (4 * StdDevlong)) *(-1)));
      double shortTradeLots = NormalizeDouble(shortTradeLotsunround,1);

When I start my EA it works pretty fine, but after some hours of trading it starts to show the error 4051. Do you have any idea what causes this error?

Thanks in advance.

 
marve7ou_marc:

I wrote a this code to calculate the Lotsize depending on where the price is in addiction to the StandardDeviation:

When I start my EA it works pretty fine, but after some hours of trading it starts to show the error 4051. Do you have any idea what causes this error?

  1. If Ask is above MAlong then (Ask - MAlong) *(-1) is negative. You reduce lots below InitialLotSize. Not checking for min/max.
  2. You are changing your risk all over the place. I suggest you define your risk as 0.5% - 3% depending on your standard deviation, and then compute lots correctly.
    • 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.
    • Account Balance * 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.)
    • Do NOT use TickValue by itself - DeltaPerLot
    • You must normalize lots properly and check against min and max.
    • You must also check FreeMargin to avoid stop out
  3. Short trades open at the Bid