Pip Distance and Martingale

 

It is giving me some Invalid LotSize 4051, any ideas why and what could I correct?

I want to calculate the distance from one signal until the other one. Lets say the signals are very good but 'who is without a flaw'? Then, I want to calculate the lotsize for a Martingale if the 1st signal goes wrong in order for it to be covered by the second signal, per distance.

Basically it is a Price per Pip, Distance in Pips, Multiply Lots.

I am a bit lost. Am I missing something?

void OpenOpBuy()
  {
   double slb=0;
   double KLot=1;
   double PPP = (MarketInfo(Symbol(), MODE_TICKVALUE) / MarketInfo(Symbol(), MODE_TICKSIZE));

   if(StopLoss>0)
      slb=NormalizeDouble(Ask-StopLoss*Point,Digits);

   for(int i=OrdersTotal(); i>=0; i--)
      if(OrderSelect(i-1,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
            if(IsNewCandle())
               if(OrderMagicNumber()==Magic)
                 {
                  if(OrderType()==OP_SELL)
                    {
                     if(Bid>OrderOpenPrice())
                        KLot= 1+NormalizeDouble((Bid-OrderOpenPrice())*MathPow(10,_Digits)+MODE_SPREAD/AccountLeverage(),1)/100;
                     if(OrderOpenPrice()>Bid)
                        KLot= 1+NormalizeDouble((OrderOpenPrice()-Bid)*MathPow(10,_Digits)+MODE_SPREAD/AccountLeverage(),1)/100;
                       {
                        int  r= OrderSend(Symbol(),OP_BUY,Lot()*KLot,Ask,Slip,slb,0,"",Magic,0,Blue);
                       }

                    }
                 }

  }
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
Reason: