For loop causing "not all control paths return a value"

 

I'm getting an "'}' - not all control paths return a value" error with my code but when I take out a for loop its fine. Here's the code:

#property copyright "Copyright 2015, Forex Software Ltd."
#property link      "http://forexsb.com"
#property version   "2.00"

void  OnStart()
  {
   Comment("Loading...");
   int maxBars= MathMin(TerminalInfoInteger(TERMINAL_MAXBARS),100000);
   string comment="";
   ENUM_TIMEFRAMES periods[] = {PERIOD_M1, PERIOD_M5, PERIOD_M15, PERIOD_M30, PERIOD_H1, PERIOD_H4, PERIOD_D1};
   for(int p=0;p<7;p++)
     {
      comment+=ExportBars(periods[p], maxBars)+"\n";
      Comment(comment);
     }
   comment+="Ready";
   Comment(comment);
  }
  
string ExportBars(ENUM_TIMEFRAMES period, int maxBars)
{
   string Tickers[]={"AUDCAD", "AUDCHF", "AUDJPY", "AUDNZD", "AUDUSD", "CADCHF", "CADJPY", "CHFJPY", "EURAUD", "EURCAD", "EURCHF", "EURGBP", "EURJPY", "EURNZD", "EURUSD", "GBPAUD", "GBPCAD", "GBPCHF", "GBPJPY", "GBPNZD", "GBPUSD", "NZDCAD", "NZDCHF", "NZDJPY", "NZDUSD", "USDCAD", "USDCHF", "USDJPY"

};
   Print(ArraySize(Tickers));

   for(int j=0;j<7;j++)
   {
   //int j= 27; putting in the index and then running for each ticker works but running a for loop doesn't
   MqlRates  rates_array[];
   ArraySetAsSeries(rates_array,true);
   int bars=CopyRates(Tickers[j],period,0,maxBars,rates_array);
   string fileName=Tickers[j]+PeriodToStr(period)+".csv";
   string comment="";
   if(bars>1)
     {
     
      int filehandle=FileOpen(fileName,FILE_WRITE|FILE_CSV);
      for(int i=bars-1; i>=0; i--)
        {
        
        if(i==bars-1)
        {
        FileWrite(filehandle,
            "ID",
            "Time",
            "Open",
            "High",
            "Low",
            "Close",
            "Tick Volume",
            "Real Volume",
            "Spread"
            );
        }
        else
        {
        int k=i;     
         FileWrite(filehandle,
            bars-k-1,
            rates_array[k].time,
            rates_array[k].open,
            rates_array[k].high,
            rates_array[k].low,
            rates_array[k].close,
            rates_array[k].tick_volume,
            rates_array[k].real_volume,
            rates_array[k].spread
            );
        }
        }
      FileFlush(filehandle);
      FileClose(filehandle);
      comment="File exported: "+fileName+", "+IntegerToString(bars)+" bars";
      Print(comment);
     }
   else
     {
     
      comment="Error with exporting: "+fileName;
     }
   return (comment);
   }
}
string PeriodToStr(ENUM_TIMEFRAMES period)
  {
   string strper;
   switch(period)
     {
      case PERIOD_M1  : strper="1";      break;
      case PERIOD_M5  : strper="5";      break;
      case PERIOD_M15 : strper="15";     break;
      case PERIOD_M30 : strper="30";     break;
      case PERIOD_H1  : strper="60";     break;
      case PERIOD_H4  : strper="240";    break;
      case PERIOD_D1  : strper="1440";   break;
      case PERIOD_W1  : strper="10080";  break;
      case PERIOD_MN1 : strper="302400"; break;
      default : strper="";
     }
   return (strper);
   
  }
//+------------------------------------------------------------------+

 It feels like I've tried every solution.

 
      else
        {

         comment="Error with exporting: "+fileName;
        }
      return (comment);
     }
   return("");
  }

comment is declared and returned in the for loop

You need to return something after the loop.

 

Hi David, 

It looks like you got your answer from Keith. I'd also like to offer some advice... Whenever you hard-code symbols you will have a bad time when switch to ECN brokers since most of them will append different chars to the end of the symbol name. It's always best practice to do sub string comparisons instead. I've actually written a similar script to yours in the past... maybe this can help you with some ideas... 


//+------------------------------------------------------------------+
//|                                          history_dump_to_csv.mq4 |
//|                                                      nicholishen |
//|                         https://www.forexfactory.com/nicholishen |
//+------------------------------------------------------------------+
#property copyright "nicholishen"
#property link      "https://www.forexfactory.com/nicholishen"
#property version   "1.00"
#property strict
#ifdef __MQL4__ 
   #define __VERSION__ "MQL4"
#else 
   #define __VERSION__ "MQL5"
#endif 
#define __FILES_PATH__ "\\" + __VERSION__+ "\\Files\\"
#define __DIRECTORY__ "HistoryCsv"
#define ERR_INVALID_SYMBOL ERR_USER_ERROR_FIRST + 1
//+------------------------------------------------------------------+
#include <Files\File.mqh>
//+------------------------------------------------------------------+
// common template function for membership testing
template <typename T>
bool in(T item, T &array[])
{
   for(int i=ArraySize(array)-1; i>=0; --i)
      if(item == array[i])
         return true;
   return false;
}
//+------------------------------------------------------------------+
class MajorsHistoryFile : public CFile
{  
public: 
   bool dump(string symbol){
      ResetLastError();
      string currencies[] = {
         "AUD", "CAD", "CHF", "EUR", 
         "GBP", "JPY", "NZD", "USD"
      };
      ENUM_TIMEFRAMES timeframes[] = {
         PERIOD_M1, PERIOD_M5, PERIOD_M15, 
         PERIOD_M30, PERIOD_H1, PERIOD_H4, PERIOD_D1
      };
      if(!this._is_allowed_symbol(symbol, currencies)){
         SetUserError(1);
         return false;
      }
      bool res = true;
      for(int i=ArraySize(timeframes)-1; i>=0; --i){
         MqlRates rates[];
         ArraySetAsSeries(rates, true);
         int copied = CopyRates(symbol, timeframes[i], 0, 10000, rates);
         if(copied < 1){
            res = false;
            continue;
         }
         string file_name = StringConcatenate(
            __DIRECTORY__, "\\", symbol, "\\", symbol, "_",
            EnumToString(timeframes[i]), ".csv"
         );
         if(this.Open(file_name, FILE_CSV|FILE_WRITE, ',') == INVALID_HANDLE)
            return false;
         FileWrite(this.Handle(), 
            "Time","Open","High","Low","Close","Tick Volume","Real Volume","Spread"
         );
         for(int j=0; j<copied; j++){
            FileWrite(this.Handle(),
               rates[j].time,
               rates[j].open,
               rates[j].high,
               rates[j].low,
               rates[j].close,
               rates[j].tick_volume,
               rates[j].real_volume,
               rates[j].spread
            );
         }
      }
      return res;
   }
   string abs_path() const {
      return StringConcatenate(
         TerminalInfoString(TERMINAL_DATA_PATH),
         __FILES_PATH__,
         this.FileName()
      );
   }
protected:
   bool _is_allowed_symbol(string symbol, string &allowed_currencies[])
   {
      return(in(StringSubstr(symbol, 0, 3), allowed_currencies)
         && in(StringSubstr(symbol, 3, 3), allowed_currencies)
      );
   }
};
//+------------------------------------------------------------------+
void OnStart()
{
   for(int i=SymbolsTotal(false)-1; i>=0; --i){
      MajorsHistoryFile file;
      if(file.dump(SymbolName(i, false))){
         Print("SUCCESS: ", file.abs_path());
      }else if(_LastError != ERR_INVALID_SYMBOL){
         Print("FAILED: ", _LastError);
      }
   }
}
//+------------------------------------------------------------------+
Reason: