private static bool ParseIntradayData(StockSerie stockSerie, string fileName)
        {
            bool res = false;
             try
             {
            using (StreamReader sr = new StreamReader(fileName))
            {
               while (!sr.EndOfStream)
               {
                  string line = sr.ReadLine();
                  string[] row = line.Split(',');
                  if (row.Length <2) continue;

                  DateTime openDate;
                  if (!DateTime.TryParse(row[0], out openDate)) continue;
                  float value = float.Parse(row[1]);

                  if (!stockSerie.ContainsKey(openDate))
                  {
                     StockDailyValue dailyValue = new StockDailyValue(stockSerie.StockName,
                            value,
                            value,
                            value,
                            value,
                            0,
                            openDate);
                     stockSerie.Add(dailyValue.DATE, dailyValue);
                  }
               }

               stockSerie.ClearBarDurationCache();

               res = true;
            }
             }
             catch (System.Exception e)
             {
            StockLog.Write("Unable to parse intraday data for " + stockSerie.StockName);
            StockLog.Write(e);
             }
             return res;
        }
        private bool ParsePCRatioCSV(StockSerie stockSerie, string fileName)
        {
            if (File.Exists(fileName))
             {
            using (StreamReader sr = new StreamReader(fileName))
            {
               StockDailyValue readValue = null;

               while (!sr.ReadLine().EndsWith("P/C Ratio") && !sr.EndOfStream) ;

               while (!sr.EndOfStream)
               {
                  try
                  {
                     string[] row = sr.ReadLine().Split(',');
                     float ratio = float.Parse(row[4], usCulture);

                     //ratio = (float)Math.Log10(ratio);
                     //if (inverse) ratio = -ratio;

                     readValue = new StockDailyValue(
                         stockSerie.StockName,
                         ratio,
                         ratio,
                         ratio,
                         ratio,
                         long.Parse(row[3], usCulture),
                         DateTime.Parse(row[0], usCulture));
                     if (readValue != null && !stockSerie.ContainsKey(readValue.DATE))
                     {
                        stockSerie.Add(readValue.DATE, readValue);
                     }
                  }
                  catch (System.Exception e)
                  {
                     StockLog.Write(e);
                     return false;
                  }
               }
            }
            return true;
             }
             else
             {
            return false;
             }
        }
        private bool ParseRydexData(StockSerie stockSerie, string fileName)
        {
            if (File.Exists(fileName))
             {
            using (StreamReader sr = new StreamReader(fileName))
            {
               StockDailyValue readValue = null;
               string line;

               // Skip 2 first lines
               while (!sr.EndOfStream && !(line = sr.ReadLine()).Contains("Date")) ;

               while (!sr.EndOfStream)
               {
                  readValue = null;
                  line = sr.ReadLine();
                  if (string.IsNullOrEmpty(line))
                  {
                     break;
                  }
                  line = line.Replace("\",\"", ";");
                  line = line.Replace("\"", "");
                  string[] row = line.Split(';');
                  if (row[2] == "N/A" || row[3] == "N/A" || row[1] == "AM") continue;
                  float value = float.Parse(row[3], usCulture) / float.Parse(row[2], usCulture);
                  readValue = new StockDailyValue(
                      stockSerie.StockName,
                      value,
                      value,
                      value,
                      value,
                      0,
                      DateTime.Parse(row[0], usCulture));
                  if (!stockSerie.ContainsKey(readValue.DATE))
                  {
                     stockSerie.Add(readValue.DATE, readValue);
                  }
               }
            }
            return true;
             }
             return false;
        }
        private static bool ParseIntradayData(StockSerie stockSerie, string fileName)
        {
            bool res = false;
             try
             {
            using (StreamReader sr = new StreamReader(fileName))
            {
               string line;

               while ((line = sr.ReadLine()) != null)
               {
                  if (line.StartsWith("timezone")) break;
               }
               if (sr.EndOfStream) return false;
               string[] row = line.Split(new char[] { ':' });
               string timeZone = row[1];
               double gmtoffset = timeZone == "CEST" ? -32400 : 0;

               while ((line = sr.ReadLine()) != null)
               {
                  if (line.StartsWith("gmtoffset")) break;
               }
               if (sr.EndOfStream) return false;

               row = line.Split(new char[] { ':' });
               gmtoffset += double.Parse(row[1]);

               DateTime now = DateTime.Now;
               DateTime utcNow = now.ToUniversalTime();
               gmtoffset -= (now - utcNow).TotalSeconds;

               while (!(line = sr.ReadLine()).StartsWith("range")) ;

               // First Range, read second offest for date calculation.
               row = line.Split(new char[] { ',', ':' });
               DateTime startDate = new DateTime(int.Parse(row[1].Substring(0, 4)), int.Parse(row[1].Substring(4, 2)), int.Parse(row[1].Substring(6, 2)));

               //  startDate = startDate.AddHours(9);
               long startTimeStamp = long.Parse(row[2]);

               while (!(line = sr.ReadLine()).StartsWith("volume")) ;

               while (!sr.EndOfStream)
               {
                  line = sr.ReadLine();

                  row = line.Split(new char[] { ',' });

                  DateTime openDate = startDate;
                  openDate = startDate.AddSeconds(long.Parse(row[0]) - startTimeStamp);
                  openDate = startDate.AddSeconds(long.Parse(row[0]) - startTimeStamp - gmtoffset);
                  if (!stockSerie.ContainsKey(openDate))
                  {
                     StockDailyValue dailyValue = new StockDailyValue(stockSerie.StockName,
                            float.Parse(row[4], usCulture),
                            float.Parse(row[2], usCulture),
                            float.Parse(row[3], usCulture),
                            float.Parse(row[1], usCulture),
                            long.Parse(row[5]),
                            openDate);
                     stockSerie.Add(dailyValue.DATE, dailyValue);
                  }
               }

               stockSerie.ClearBarDurationCache();

               res = true;
            }
             }
             catch (System.Exception e)
             {
            StockLog.Write("Unable to parse intraday data for " + stockSerie.StockName);
            StockLog.Write(e);
             }
             return res;
        }
        private bool ParseBarChartFile(StockSerie stockSerie, string fileName)
        {
            bool res = false;
             try
             {
            if (File.Exists(fileName))
            {
               using (StreamReader sr = new StreamReader(fileName))
               {
                  DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response));
                  Response jsonResponse = jsonSerializer.ReadObject(sr.BaseStream) as Response;
                  if (jsonResponse != null && jsonResponse.error != null)
                  {
                     foreach (var data in jsonResponse.data.series[0].data)
                     {
                        DateTime date = refDate.AddMilliseconds(data[0]).Date;
                        if (!stockSerie.ContainsKey(date))
                        {
                           stockSerie.Add(date, new StockDailyValue(stockSerie.StockName, (float)data[1], (float)data[2], (float)data[3], (float)data[4], 0, date));
                        }
                     }

                     res = true;
                  }
               }
            }
             }
             catch (System.Exception e)
             {
            StockLog.Write(e);
            res = false;
             }
             return res;
        }
        private static bool ParseIntradayData(StockSerie stockSerie, string fileName)
        {
            bool res = false;
             try
             {
            using (StreamReader sr = new StreamReader(fileName))
            {
               string line = sr.ReadLine();

               while (!(line = sr.ReadLine()).StartsWith("INTERVAL")) ;
               int interval;
               interval = int.Parse(line.Split('=')[1]);

               while (!(line = sr.ReadLine()).StartsWith("TIMEZONE_OFFSET")) ;
               int offset;
               offset = int.Parse(line.Split('=')[1]);

               string[] row;
               DateTime startDate = DateTime.Now;
               while (!sr.EndOfStream)
               {
                  line = sr.ReadLine();
                  DateTime openDate = DateTime.Now;
                  row = line.Split(new char[] { ',' });

                  if (line.StartsWith("a"))
                  {
                     // new day detected
                     string startString = row[0].Replace("a", "");
                     double seconds = double.Parse(startString);

                     startDate = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(seconds).AddMinutes(offset);
                     openDate = startDate;
                  }
                  else if (line.StartsWith("TIMEZONE_OFFSET"))
                  {
                     offset = int.Parse(line.Split('=')[1]);
                     continue;
                  }
                  else
                  {
                     // just a new bar
                     openDate = startDate.AddSeconds(long.Parse(row[0]) * interval);
                  }

                  if (!stockSerie.ContainsKey(openDate))
                  {
                     StockDailyValue dailyValue = new StockDailyValue(stockSerie.StockName,
                            float.Parse(row[4], usCulture),
                            float.Parse(row[2], usCulture),
                            float.Parse(row[3], usCulture),
                            float.Parse(row[1], usCulture),
                            long.Parse(row[5]),
                            openDate);
                     stockSerie.Add(dailyValue.DATE, dailyValue);
                  }
               }

               stockSerie.ClearBarDurationCache();

               res = true;
            }
             }
             catch (System.Exception e)
             {
            StockLog.Write("Unable to parse intraday data for " + stockSerie.StockName);
            StockLog.Write(e);
             }
             return res;
        }
        // private functions
        protected override bool ParseCSVFile(StockSerie stockSerie, string fileName)
        {
            if (File.Exists(fileName))
            {
                using (StreamReader sr = new StreamReader(fileName))
                {
                    StockDailyValue readValue = null;
                    while (!sr.EndOfStream)
                    {
                        readValue = this.ReadMarketDataFromABCCSVStream(sr, stockSerie.StockName, true);
                        if (readValue != null)
                        {

                            if (!stockSerie.ContainsKey(readValue.DATE))
                            {
                                stockSerie.Add(readValue.DATE, readValue);
                                readValue.Serie = stockSerie;
                            }
                            else
                            { //
                                StockLog.Write("The dailyValue already exist in the serie");
                            }
                        }
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }
 //protected virtual bool ParseOptixFile(StockSerie stockSerie, string rootFolder)
 //{
 //   string fileName = rootFolder + OPTIX_SUBFOLDER + "\\" + stockSerie.ShortName + ".csv";
 //   if (File.Exists(fileName))
 //   {
 //      using (StreamReader sr = new StreamReader(fileName))
 //      {
 //         string line = sr.ReadLine();  // Skip the first line
 //         StockDailyValue dailyValue = null;
 //         while (!sr.EndOfStream)
 //         {
 //            line = sr.ReadLine();  // Skip the first line
 //            var fields = line.Split(',');
 //            if (fields.Count() < 2 || string.IsNullOrEmpty(fields[0])) continue;
 //            DateTime date = DateTime.Parse(fields[0], usCulture);
 //            // Try to find the matching value at date in the serie
 //            if (date >= stockSerie.Keys.First() && stockSerie.ContainsKey(date))
 //            {
 //               dailyValue = stockSerie[date];
 //               dailyValue.OPTIX = float.Parse(fields[1], usCulture);
 //            }
 //         }
 //      }
 //      return true;
 //   }
 //   else
 //   {
 //      return false;
 //   }
 //}
 //protected bool DownloadOptixData(string rootFolder, StockSerie stockSerie)
 //{
 //   if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() && stockSerie.HasOptix)
 //   {
 //      NotifyProgress("Downloading Optix for" + stockSerie.StockGroup.ToString());
 //      if (!stockSerie.Initialise())
 //      {
 //         return false;
 //      }
 //      if (stockSerie.Values.Last().OPTIX != 0.0f)
 //      {
 //         return false;
 //      }
 //      string optixFileName = rootFolder + OPTIX_SUBFOLDER + "\\" + stockSerie.ShortName + ".csv";
 //      using (WebClient wc = new WebClient())
 //      {
 //         wc.Proxy.Credentials = CredentialCache.DefaultCredentials;
 //         wc.DownloadFile(stockSerie.OptixURL, optixFileName);
 //         stockSerie.IsInitialised = false;
 //      }
 //   }
 //   return true;
 //}
 protected virtual bool ParseCSVFile(StockSerie stockSerie, string fileName)
 {
     if (File.Exists(fileName))
      {
     using (StreamReader sr = new StreamReader(fileName))
     {
        sr.ReadLine();  // Skip the first line
        StockDailyValue readValue = null;
        while (!sr.EndOfStream)
        {
           readValue = this.ReadMarketDataFromCSVStream(sr, stockSerie.StockName, true);
           if (readValue != null && readValue.DATE.Year >= LOAD_START_YEAR && !stockSerie.ContainsKey(readValue.DATE))
           {
              stockSerie.Add(readValue.DATE, readValue);
              readValue.Serie = stockSerie;
           }
        }
     }
     return true;
      }
      else
      {
     return false;
      }
 }