Optimal Auto Lot Size Calculation Function works fine in Scripts but not works when used inside a Expert Advisor ? Why is this happening ? Pl. help..

 

Hi, 


An Auto Lot Size Calculation Function is working fine in scripts I checked using Alert function it is returning actual values in rounded format for Lot sizes. 

But When I am using it inside an EA it is returning 0 value and Error 4051 "invalid function parameter" 

pl guide where i am wrong screenshot and code are attached.


Thanks in advance.

input string                   OrderDetails = "=============== < Order Settings > ===============";     

extern bool                    AutoLotSize  = true;               //Is Auto Lot Sizing ? 
extern double                  LotSize      = 0.01;                //Lotsize(For Fixed Lot Option Only)
 
extern double                  Riskpercentage = 2;                //Risk % of A/c Balance 

extern int                     Slipp        = 30;                  //Slippages
extern double                  SL           = 170;                 //Stoploss in Pips
extern double                  TP           = 145;                  //Takeprofit in Pips




void OnTick()
  {
      int MA1  =  MAStart; 
      int MA2  =  MAStart + MAIncreaser;
      int MA3  =  MA2 + MAIncreaser;  
      int MA4  =  MA3 + MAIncreaser;
      int MA5  =  MA4 + MAIncreaser;
      
      double MAValue1   =  iMA(NULL,0,MA1,MAShift,MATYPE,MAPrice,1);
      double MAValue2   =  iMA(NULL,0,MA2,MAShift,MATYPE,MAPrice,1);
      double MAValue3   =  iMA(NULL,0,MA3,MAShift,MATYPE,MAPrice,1);
      double MAValue4   =  iMA(NULL,0,MA4,MAShift,MATYPE,MAPrice,1);
      double MAValue5   =  iMA(NULL,0,MA5,MAShift,MATYPE,MAPrice,1);
      
      double ADXValue   =  iADX(NULL,0,ADXPeriod,MAPrice,MODE_MAIN,1);
      
      
      bool BuyTrade     =  (ADXValue > ADXLevel) && (MAValue1 > MAValue2) && (MAValue2 > MAValue3) && (MAValue3 > MAValue4) && (MAValue4 > MAValue5);
      bool SellTrade    =  (ADXValue > ADXLevel) && (MAValue1 < MAValue2) && (MAValue2 < MAValue3) && (MAValue3 < MAValue4) && (MAValue4 < MAValue5);
      
           
      if(BuyTrade)                  
         if (OrdersTotal() == 0){
              LotSize = LotSize;
              if(AutoLotSize == true)
                  LotSize = OptimalLotsize(SL,Riskpercentage);
              Ordersend("BUY",LotSize,Ask,Slipp,SL,TP,MagicNumber);
              Print("Lotsize is :" , LotSize);}
             
                 
      if(SellTrade)
         if (OrdersTotal() == 0){
              LotSize = LotSize;
              if(AutoLotSize == true)
                  LotSize = OptimalLotsize(SL,Riskpercentage);
              Ordersend("SELL",LotSize,Ask,Slipp,SL,TP,MagicNumber);
              Print("Lotsize is :" , LotSize);}
          
  }


//+------------------------------------------------------------------+ 
//| Function for Optimal Lot Caluculation based on Risk %.           |                                       
//+------------------------------------------------------------------+ 

double OptimalLotsize(double SLPips, double MaxRiskPerTrade)
  {

   double Lotsize = 0;

   double nTickValue = MarketInfo(Symbol(),MODE_TICKVALUE);
   if((Digits==3) || (Digits == 5))
     {
      nTickValue = nTickValue * 10;
     }

   Lotsize = (AccountBalance() * MaxRiskPerTrade / 100) / (SLPips * nTickValue);
   Lotsize = MathRound(Lotsize / MarketInfo(Symbol(),MODE_LOTSTEP)) * MarketInfo(Symbol(), MODE_LOTSTEP);
   return Lotsize;
  }


//+------------------------------------------------------------------+




 
  1. Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk. Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

    1. 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.

    2. 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.)

    3. 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 - MQL4 programming forum (2017)
                Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
                Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)

    4. You must normalize lots properly and check against min and max.

    5. You must also check FreeMargin to avoid stop out

    6. For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)

    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

  2. Lotsize = (AccountBalance() * MaxRiskPerTrade / 100) / (SLPips * nTickValue);

    PIP, Point, or Tick are all different in general.
              What is a TICK? - MQL4 programming forum (2014)