Download MetaTrader 5
To add comments, please log in or register
Deposit to your account in any convenient way. There are 4 of them here!
Thierry F.
Thierry F. 2015.06.02 16:58 


Could someone please help me with my LotsizeCalc() function.

I would like to multiply my lotsize with the 'MartingaleAmount' if my last trade was a loser. If my last trade was a winner I would like to use the 'normal' lotsize. Unfortunately, when I backtest the EA I get error 4051 from the , which means "Invalid function parameter value.". 

It has something to do with the lotsize, because the EA works when I try to run the EA without the "LotsizeCalc()" function.

Does anyone know why my lotsize function won't work?


Thanks in advance,




extern int MaximumTrades = 10;
extern double StopLoss=25;
extern double TakeProfit=50;
extern double MartingaleAmount = 1.5;
input double   LotSize=0.1;

int magic;
double pips;
int tradeNumber;
string ScreenComment;
double martinlot;
double martinsize;
double LotsizeCalc()
            if(OrderProfit()<0 && tradeNumber <=MaximumTrades)
               martinlot = LotSize;
               martinlot = OrderLots();
              double lots = martinlot * MartingaleAmount;
              martinsize = lots;
              double lots = LotSize;
              martinsize = lots;
//|                                 Enter trade                      |

void EnterTrade(int type){

   Print("Entering Trade Number 1");
   tradeNumber =1;
   int err=0;
   double price=Bid,
          lotsize = LotsizeCalc();
   Comment(ScreenComment+"\nThis is trade number: "+(string)tradeNumber+"\nThe Lotsize is: "+DoubleToStr(lotsize,2));
   if(type == OP_BUY)
      price =Ask;
   int ticket =  OrderSend(Symbol(),type,lotsize,price,30,0,0,"1 ",magic,0,Magenta); 
         sl = OrderOpenPrice()+(StopLoss*pips);
         tp = OrderOpenPrice()-(TakeProfit*pips);
            sl = OrderOpenPrice()-(StopLoss*pips);
            tp = OrderOpenPrice()+(TakeProfit*pips);
         if(!OrderModify(ticket,price,sl,tp,0,Magenta)) {
            err = GetLastError();
            Print("Encountered an error during modification!"+(string)err+" "+ErrorDescription(err)  );
      else{//in case it fails to select the order for some reason 
         Print("Failed to Select Order ",ticket);
         err = GetLastError();
         Print("Encountered an error while seleting order "+(string)ticket+" error number "+(string)err+" "+ErrorDescription(err)  );
   else{//in case it fails to place the order and send us back a ticket number.
      err = GetLastError();
      Print("Encountered an error during order placement!"+(string)err+" "+ErrorDescription(err)  );
      if(err==ERR_TRADE_NOT_ALLOWED)MessageBox("You can not place a trade because \"Allow Live Trading\" is not checked in your options. Please check the \"Allow Live Trading\" Box!","Check Your Settings!");
To add comments, please log in or register