'}' - not all control paths return a value

 

Hello everyone. I have coded this, and I think that I cover all possibilities. Why is it still saying that this function/method does not return a value?

double Lots()
   {
    if(OrdersHistoryTotal()>0)
      {
       for(int i=OrdersHistoryTotal();i>0;i--)
         {
          //Select closed orders starting at the end (most recent first)
          bool order = OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
          
          if(OrderSymbol()==Symbol())
            {
             //Check for orders with the same symbol as the chart
             if(OrderProfit()>0)
               {
                //If the order was closed with a profit, increase the amount of lots by 0.01
                Print("Previous order closed with profit. Lots increased by 0.01");
                return OrderLots()+0.01;
               }
             else
               {
                //If the order was closed break even or with a loss
                Print("Previous order closed without profit. Lots = 0.01");
                return 0.01;
               }
            }
          else
            {
             //If there is no closed order of this pair on the history
             Print("No history for this pair. Lots = 0.01");
             return 0.01;
            }
         }
      }
    else
      {
       //If there is no history at all
       Print("No history. Lots = 0.01");
       return 0.01;
      }
   }
 
  1. double Lots()
       {
        if(OrdersHistoryTotal()>0)
          {
           for(int i=OrdersHistoryTotal();i>0;i--)
             {
              //Select closed orders starting at the end (most recent first)
              bool order = OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
              
              if(OrderSymbol()==Symbol())
                {...}
              else
                {...}
             } // FOR
             // No history
             // You know that this is unreachable, the compiler does not.
          }
        else
          {}
        // end if(OrdersHistoryTotal()>0) ... else ...
        // You know that this is unreachable the compiler does not.
       }

  2. The else implies you do the (A) or the else (B) and then continue at C.
    if(...){ ... return } // A
    else ...              // B
                          // C
    
    There is no else when the previous returns. Simplify
    if(...){ ... return } // A
    ...                   // B
    
    If you do this your code simplifies to
    double Lots()
       {
           for(int i=OrdersHistoryTotal();i>0;i--)
             {
              //Select closed orders starting at the end (most recent first)
              bool order = OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
              
              if(OrderSymbol()!=Symbol()) continue;
    
                 //Check for orders with the same symbol as the chart
                 if(OrderProfit()>0)
                   {
                    //If the order was closed with a profit, increase the amount of lots by 0.01
                    Print("Previous order closed with profit. Lots increased by 0.01");
                    return OrderLots()+0.01;
                   }
                    //If the order was closed break even or with a loss
                    Print("Previous order closed without profit. Lots = 0.01");
                    return 0.01;
              } // Not my symbol
           } // FOR
           //If there is no history at all
           Print("No history. Lots = 0.01");
           return 0.01;
       }

  3. Your code stops when you find the first history record that is not _Symbol. The above version doesn't.
  4. Both versions assume that history is ordered by close time. It isn't always. Don't assume Could EA Really Live By Order_History Alone? - MQL4 forum
  5. You also have to test order type to ignore deleted pending orders, deposits and withdraws.
  6. You just check OrderProfit() what about swap and commissions?
  7. On some brokers (e.g. FXCM) swap and commission is not part of the order but separate entries (type=6) with comment "commission - NN" or "rollover - NNNN" Those must be handled also.
 
    //If there is no history at all
       Print("No history. Lots = 0.01");
       return 0.01;
      }

return(0.01); 

   }
 

Thank you very much. Thank you also for the explanations, I'll have a look at the problem with sorting orders. Thank very much. The code would be like this (correcting a small mistake)

double Lots()
   {
    for(int i=OrdersHistoryTotal();i>0;i--)
      {
       //Select closed orders starting at the end (most recent first)
       bool order = OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
       
       if(OrderSymbol()!=Symbol()) continue;
       //Check for orders with the same symbol as the chart
       if(OrderProfit()>0)
         {
          //If the order was closed with a profit, increase the amount of lots by 0.01
          Print("Previous order closed with profit. Lots increased by 0.01");
          return OrderLots()+0.01;
         }
       //If the order was closed break even or with a loss (without a profit)
       Print("Previous order closed without profit. Lots = 0.01");
       return 0.01;
      }
    //If there is no history at all
    Print("No history. Lots = 0.01");
    return 0.01;
   }
Reason: