Lotsize calculator error

 

Hi,

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,

Thierry 

 

 

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(OrderSelect(lastTradeTicket(),MODE_HISTORY))
            
            
            
            if(OrderProfit()<0 && tradeNumber <=MaximumTrades)
              {
               if(OrderLots()==0)
               martinlot = LotSize;
               else 
               martinlot = OrderLots();
               
              double lots = martinlot * MartingaleAmount;
              martinsize = lots;
              tradeNumber++;
              }
            else
              {
              double lots = LotSize;
              martinsize = lots;
              tradeNumber=1;
              }
            
            return(martinsize); 
     }    
      
//+------------------------------------------------------------------+
//|                                 Enter trade                      |
//+------------------------------------------------------------------+

void EnterTrade(int type){

   Print("Entering Trade Number 1");
   tradeNumber =1;
   int err=0;
   double price=Bid,
          sl=0, 
          tp=0,
          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); 
   if(ticket>0){
      if(OrderSelect(ticket,SELECT_BY_TICKET)){
         sl = OrderOpenPrice()+(StopLoss*pips);
         if(StopLoss==0)sl=0;
         tp = OrderOpenPrice()-(TakeProfit*pips);
         if(OrderType()==OP_BUY){
            sl = OrderOpenPrice()-(StopLoss*pips);
            if(StopLoss==0)sl=0;
            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!");
   }
}
Reason: