2017-06-22 61 views
1

我用下面雅虎API來獲取歷史數據,C#:無法獲取歷史雅虎數據(一天區間)最大範圍

  • 指數:GDX
  • 範圍:最大
  • 間隔:1D

https://query1.finance.yahoo.com/v7/finance/chart/GDX?range=max&interval=1d&indicators=quote&includeTimestamps=true&includePrePost=false&corsDomain=finance.yahoo.com

在這裏,我有兩個場景

  • 我想要最近25年的數據,如果任何指數的數據超過25年。 (例如,MSFT)
  • 我希望從第一個日期的數據到當前日期的數據(如果任何索引的數據少於25年)。 (例如,GDX)

但是,我只得到了一個月的結果。我無法獲得一天數據的間隔。請參考下面的圖片。

enter image description here

回答

1

我通過檢查從雅虎API響應meta標籤firstTradaDate值解決了這個問題。

firstTradeDate-Meta

firstTradeDate保存每個指數的首個交易日的價值。

在我的情況下, GDX的第一交易日爲:(Unix的時間戳),相當於GMT:週一,2006年5月22日上午08時零零分00秒

所以,我只是簡單地傳遞參數,

  • 指數:GDX
  • 範圍:25Y
  • 間隔:1D

現在,迭代響應數組和檢查來自firstTradeDate的回覆的時間戳是否小於。如果它小於firstTradeDate,那麼我只是繼續循環。否則,我將該記錄添加爲有效的記錄。

這下面是我的代碼片段

public class YahooJson2CsvController : ApiController 
{ 
    public HttpResponseMessage GetJson2Csv(string code, string range, string interval) 
    { 
     try 
     { 
      AppendLog("============================================"); 
      AppendLog("Starting to download { CODE: " + code + "; RANGE: " + range + "; INTERVAL: " + interval + " }"); 

      var csvBasePath = HttpContext.Current.Server.MapPath("~/YahooCSV/"); 
      var objCsvBasePath = new DirectoryInfo(csvBasePath); 
      if (!objCsvBasePath.Exists) { objCsvBasePath.Create(); AppendLog("YahooCSV folder created"); } 

      var csvYesterdayPath = HttpContext.Current.Server.MapPath("~/YahooCSV/" + DateTime.Now.AddDays(-1).ToString("MMddyyyy") + "/"); 
      var objCsvYesterdayPath = new DirectoryInfo(csvYesterdayPath); 
      if (objCsvYesterdayPath.Exists) { objCsvYesterdayPath.Delete(true); AppendLog("Deleted yesterday's download folder"); } 

      var csvTodayPath = HttpContext.Current.Server.MapPath("~/YahooCSV/" + DateTime.Now.ToString("MMddyyyy") + "/"); 
      var objCsvTodayPath = new DirectoryInfo(csvTodayPath); 
      if (!objCsvTodayPath.Exists) { objCsvTodayPath.Create(); AppendLog("Created today's download folder"); } 

      if (string.IsNullOrEmpty(code.Trim())) return new HttpResponseMessage(HttpStatusCode.BadRequest); 
      if (string.IsNullOrEmpty(range.Trim())) return new HttpResponseMessage(HttpStatusCode.BadRequest); 
      if (string.IsNullOrEmpty(interval.Trim())) return new HttpResponseMessage(HttpStatusCode.BadRequest); 

      var wc = new WebClient(); 
      var url = ConfigurationManager.AppSettings["YahooURL"].Replace("@C", code).Replace("@R", range).Replace("@I", interval); 
      var str = wc.DownloadString(url); 
      if (string.IsNullOrEmpty(str)) { AppendLog("No content for current code"); return new HttpResponseMessage(HttpStatusCode.NoContent); } 
      AppendLog("Downloaded content for current code"); 

      var data = JsonConvert.DeserializeObject<RootObject>(str); 
      if (data == null) { AppendLog("Empty deserialized object"); return new HttpResponseMessage(HttpStatusCode.NoContent); } 

      var result = new List<string>(); 
      var quotesInfo = data.chart.result.First(); 

      for (var i = 0; i < quotesInfo.timestamp.Count; i++) 
      { 
       if (quotesInfo.meta.firstTradeDate != null && quotesInfo.timestamp[i] < quotesInfo.meta.firstTradeDate) continue; 

       var quotesStr = new List<string>(); 
       var quoteData = quotesInfo.indicators.quote.First(); 
       var quoteAdjData = quotesInfo.indicators.unadjclose.First(); 

       quotesStr.Add(UnixTimeStampToDateTime(quotesInfo.timestamp[i]).ToString(CultureInfo.InvariantCulture)); 
       quotesStr.Add(quoteData.open[i].HasValue ? quoteData.open[i].ToString() : string.Empty); 
       quotesStr.Add(quoteData.high[i].HasValue ? quoteData.high[i].ToString() : string.Empty); 
       quotesStr.Add(quoteData.low[i].HasValue ? quoteData.low[i].ToString() : string.Empty); 
       quotesStr.Add(quoteData.close[i].HasValue ? quoteData.close[i].ToString() : string.Empty); 
       quotesStr.Add(quoteData.volume[i] != null ? quoteData.volume[i].ToString() : string.Empty); 
       quotesStr.Add(quoteAdjData.unadjclose[i].HasValue ? quoteAdjData.unadjclose[i].ToString() : string.Empty); 
       result.Add(string.Join(",", quotesStr)); 
      } 
      if (result.Count <= 0) { AppendLog("No valid content to deserialize"); return new HttpResponseMessage(HttpStatusCode.NoContent); } 
      AppendLog("Deserialized successful"); 

      var tempFileName = code + "_" + DateTime.Now.ToString("MMddyyyyHHmmss") + ".csv"; 
      File.WriteAllLines(csvTodayPath + tempFileName, result); 
      AppendLog("Created temp csv file to download"); 

      var memStream = new MemoryStream(); 

      using (var fileStream = File.OpenRead(csvTodayPath + tempFileName)) 
      { 
       memStream.SetLength(fileStream.Length); 
       fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length); 
      } 

      var csvResult = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(memStream) }; 
      csvResult.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv"); 
      csvResult.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = code + ".csv" }; 
      AppendLog("Downloaded: " + tempFileName); 
      return csvResult; 
     } 
     catch (Exception ex) 
     { 
      AppendLog(ex.Message); 
      return new HttpResponseMessage(HttpStatusCode.ExpectationFailed); 
     } 
    } 

    public static DateTime UnixTimeStampToDateTime(double unixTimeStamp) 
    { 
     var dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); 
     dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToUniversalTime(); 
     return dtDateTime; 
    } 

    public static void AppendLog(string Log) 
    { 
     StreamWriter sw = File.AppendText(System.AppDomain.CurrentDomain.BaseDirectory + "Log.log"); 
     sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " :: " + Log); 
     sw.Close(); 
     sw.Dispose(); 
    } 
} 

public class Pre 
{ 
    public string timezone { get; set; } 
    public int end { get; set; } 
    public int start { get; set; } 
    public int gmtoffset { get; set; } 
} 

public class Regular 
{ 
    public string timezone { get; set; } 
    public int end { get; set; } 
    public int start { get; set; } 
    public int gmtoffset { get; set; } 
} 

public class Post 
{ 
    public string timezone { get; set; } 
    public int end { get; set; } 
    public int start { get; set; } 
    public int gmtoffset { get; set; } 
} 

public class CurrentTradingPeriod 
{ 
    public Pre pre { get; set; } 
    public Regular regular { get; set; } 
    public Post post { get; set; } 
} 

public class Meta 
{ 
    public string currency { get; set; } 
    public string symbol { get; set; } 
    public string exchangeName { get; set; } 
    public string instrumentType { get; set; } 
    public int? firstTradeDate { get; set; } 
    public int gmtoffset { get; set; } 
    public string timezone { get; set; } 
    public string exchangeTimezoneName { get; set; } 
    public CurrentTradingPeriod currentTradingPeriod { get; set; } 
    public string dataGranularity { get; set; } 
    public List<string> validRanges { get; set; } 
} 

public class Quote 
{ 
    public List<object> volume { get; set; } 
    public List<double?> low { get; set; } 
    public List<double?> high { get; set; } 
    public List<double?> close { get; set; } 
    public List<double?> open { get; set; } 
} 

public class Unadjclose 
{ 
    public List<double?> unadjclose { get; set; } 
} 

public class Unadjquote 
{ 
    public List<double?> unadjopen { get; set; } 
    public List<double?> unadjclose { get; set; } 
    public List<double?> unadjhigh { get; set; } 
    public List<double?> unadjlow { get; set; } 
} 

public class Indicators 
{ 
    public List<Quote> quote { get; set; } 
    public List<Unadjclose> unadjclose { get; set; } 
    public List<Unadjquote> unadjquote { get; set; } 
} 

public class Result 
{ 
    public Meta meta { get; set; } 
    public List<int> timestamp { get; set; } 
    public Indicators indicators { get; set; } 
} 

public class Chart 
{ 
    public List<Result> result { get; set; } 
    public object error { get; set; } 
} 

public class RootObject 
{ 
    public Chart chart { get; set; } 
}