static void Main(string[] args) { //調べたい銘柄をリストする string[] stocks = new string[] { "MSFT", "AAPL", "GOOG", "AMZN" }; //最高の勝率とその際の保有期間 double BestRate = 0; double BestHold = 0; //保有期間を20日から120日まで5日ステップでチェックする for (int h = 20; h <= 120; h += 5) { Console.WriteLine("保有期間: " + h.ToString() + "日 ============"); double totalRate = 0; //指定した銘柄をそれぞれカップ分析する foreach (var stock in stocks) { //過去の株価を取得 List <HistoryPrice> history = YahooFinanceAPI.GetStockPrices(stock, new DateTime(2008, 7, 1), new DateTime(2020, 7, 1)); //カップ分析+勝率の計算 double rate = GetWinningRate(history, 60, 120, h); Console.WriteLine(stock + ": " + rate.ToString()); totalRate += rate; } Console.WriteLine("勝率: " + (totalRate / stocks.Length).ToString() + "\n"); //最高勝率をチェックする if (BestRate < totalRate) { BestRate = totalRate; BestHold = h; } } //最高勝率と保有期間の表示 Console.WriteLine(); Console.WriteLine("最適株保有期間: " + BestHold.ToString()); Console.WriteLine("最高勝率: " + (BestRate / stocks.Length).ToString()); Console.ReadLine(); }
/// <summary> /// 株価取得のメソッド /// </summary> /// <param name="symbol">NYSEのTicker Symbol</param> /// <param name="FromDate">開始日時</param> /// <param name="ToDate">終了日時</param> /// <returns></returns> public static List <HistoryPrice> GetStockPrices(string symbol, DateTime FromDate, DateTime ToDate) { //Check local cache string filepath = @"StockData\" + symbol + "_" + FromDate.ToShortDateString().Replace(@"/", "") + "_" + ToDate.ToShortDateString().Replace(@"/", "") + ".txt"; if (File.Exists(filepath)) { return(GetStockPricesFromFile(symbol, FromDate, ToDate)); } else { Task <List <HistoryPrice> > historyData = Task.Run(() => { return(YahooFinanceAPI.GetPriceAsync(symbol, FromDate, ToDate)); }); //株価データをローカルに保存 SaveStockPricesInFile(symbol, FromDate, ToDate, historyData.Result); return(historyData.Result); } }