Add lotsize next order after a loss on previuos order

 
input string symbol = NULL;
input int timeframe = 0;
input double lotsize=0.1;
input int stoploss = 0;
input int takeprofit=0;
input double Martingale_multiple_value = 2;




//+------------------------------------------------------------------+
//| Calculation of volume martingale                              |
//+------------------------------------------------------------------+

double lotsizem()
{
      double j =0;
      for(int i=OrdersHistoryTotal()-1; i < OrdersHistoryTotal(); i++)
      {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)
         j = OrderLots(); 
      }
      if(j>0) return(true);
      else return(false);       
}

//+------------------------------------------------------------------+
//| Entry of order Martingale                                        |
//+------------------------------------------------------------------+
void enter_martingale_order(ENUM_ORDER_TYPE type)
  {
   if(type==OP_BUY || type==OP_BUYSTOP || type==OP_BUYLIMIT)
      if(!long_allowed) return;
   if(type==OP_SELL || type==OP_SELLSTOP || type==OP_SELLLIMIT)
      if(!short_allowed) return;
   double volume=lotsizem();
   entry(NULL,type,volume,0,max_slippage,stoploss,takeprofit,order_comment,order_magic,order_expire,arrow_color_short,market_exec);
  }

/+------------------------------------------------------------------+
//| Checking before send_order() function is called                                                                 |
//+------------------------------------------------------------------+
int entry(string ins,int cmd,double volume,int distance,int slippage,int sl,int tp,string comment=NULL,int magic=0,int expire=0,color a_clr=clrNONE,bool market=false,int retries=3,int sleep=500)
  {
   int ticket=0;
   for(int i=0;i<retries;i++)
     {
      if(IsStopped()) Print("Expert was stopped");
      else if(!IsConnected()) Print("No internet connection");
      else if(!IsExpertEnabled()) Print("Experts not enabled in trading platform");
      else if(IsTradeContextBusy()) Print("Trade context is busy");
      else if(!IsTradeAllowed()) Print("Trade is not allowed in trading platform");
      else ticket=send_order(ins,cmd,volume,distance,slippage,sl,tp,comment,magic,expire,a_clr,market);
      if(ticket>0)
         break;
      else Print("Error in sending order ("+IntegerToString(GetLastError(),0)+"), retry: "+IntegerToString(i,0)+"/"+IntegerToString(retries));
      Sleep(sleep);
     }
   return ticket;
  }

//+------------------------------------------------------------------+
//| Sending an order                                                      |
//+------------------------------------------------------------------+
int send_order(string ins,int cmd,double volume,int distance,int slippage,int sl,int tp,string comment=NULL,int magic=0,int expire=0,color a_clr=clrNONE,bool market=false)
  {
   double price=0;
   double price_sl = 0;
   double price_tp = 0;
   double point=MarketInfo(ins,MODE_POINT);
   datetime expiry= 0;
   int order_type = -1;
   RefreshRates();
   if(cmd==OP_BUY)
     {
      if(distance>0) order_type=OP_BUYSTOP;
      else if(distance<0) order_type=OP_BUYLIMIT;
      else order_type=OP_BUY;
      if(order_type==OP_BUY) distance=0;
      price=MarketInfo(ins,MODE_ASK)+distance*point;
      if(!market)
        {
         if(sl>0) price_sl = price-sl*point;
         if(tp>0) price_tp = price+tp*point;
        }
     }
   else if(cmd==OP_SELL)
     {
      if(distance>0) order_type=OP_SELLLIMIT;
      else if(distance<0) order_type=OP_SELLSTOP;
      else order_type=OP_SELL;
      if(order_type==OP_SELL) distance=0;
      price=MarketInfo(ins,MODE_BID)+distance*point;
      if(!market)
        {
         if(sl>0) price_sl = price+sl*point;
         if(tp>0) price_tp = price-tp*point;
        }
     }
   if(order_type<0) return 0;
   else  if(order_type==0 || order_type==1) expiry=0;
   else if(expire>0)
      expiry=(datetime)MarketInfo(ins,MODE_TIME)+expire;
   if(market)
     {
      int ticket=OrderSend(ins,order_type,volume,price,slippage,0,0,comment,magic,expiry,a_clr);
      if(ticket>0)
        {
         if(OrderSelect(ticket,SELECT_BY_TICKET))
           {
            if(cmd==OP_BUY)
              {
               if(sl>0) price_sl = OrderOpenPrice()-sl*point;
               if(tp>0) price_tp = OrderOpenPrice()+tp*point;
              }
            else if(cmd==OP_SELL)
              {
               if(sl>0) price_sl = OrderOpenPrice()+sl*point;
               if(tp>0) price_tp = OrderOpenPrice()-tp*point;
              }
            bool result=modify(ticket,price_sl,price_tp);
           }
        }
      return ticket;
     }
   return OrderSend(ins,order_type,volume,price,slippage,price_sl,price_tp,comment,magic,expiry,a_clr);
  }


//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
if(OrdersHistoryTotal()>0)

   { 
      for(int i=OrdersHistoryTotal()-1; i < OrdersHistoryTotal(); i++)
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==true)
      {
         if(OrderProfit() < 0)
         {
            int count_orders=0;
            if(entry>0)
            {
               if(entry==TRADE_SIGNAL_BUY)
                  {
                     if(exit_opposite_signal)
                        exit_all_set(ORDER_SET_SELL,order_magic);
                     count_orders=count_orders(-1,order_magic);
                     if(maxtrades>count_orders)
                     {
                        if(!entry_new_bar || (entry_new_bar && is_new_bar(symbol,timeframe,wait_next_bar_on_load)))
                        enter_martingale_order(OP_BUY);
                        Print(OrderLots());
                     }
                  }
               else if(entry == TRADE_SIGNAL_SELL)
                  {
                  if(exit_opposite_signal)
                   exit_all_set(ORDER_SET_BUY,order_magic);
                     count_orders=count_orders(-1,order_magic);
                     if(maxtrades>count_orders)
                     {
                        if(!entry_new_bar || (entry_new_bar && is_new_bar(symbol,timeframe,wait_next_bar_on_load)))
                        enter_martingale_order(OP_SELL);
                        Print(OrderLots());
                     }
                  }
         
            }
         }

}

}
I have a problem which is the first loss is 0.10 lotsize and when the second order opened the lotsize instead 0.20, it turn to 1.00 @William Roeder could you help me on this error?
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Requests to execute trade operations are formalized as orders. Each order has a variety of properties for reading. Information on them can be obtained using functions Position identifier that is set to an order as soon as it is executed. Each executed order results in a deal that opens or modifies an already existing position. The identifier of...
 

Martingale, guaranteed to blow your account eventually. If it's not profitable without, it is definitely not profitable with.
          Martingale vs. Non Martingale (Simplified RoR vs Profit and the Illusions) - MQL5 programming forum 2015.02.11

Why it won't work: Calculate Loss from Lot Pips - MQL5 programming forum 2017.07.11

 
William Roeder:

Martingale, guaranteed to blow your account eventually. If it's not profitable without, it is definitely not profitable with.
          Martingale vs. Non Martingale (Simplified RoR vs Profit and the Illusions) - MQL5 programming forum 2015.02.11

Why it won't work: Calculate Loss from Lot Pips - MQL5 programming forum 2017.07.11

Much appreciated your concern and advise. Because I would like to test it, but having the error, it's possible you could guide me about the problem mention above.

Reason: