private void loadStockChart(string stockId, bool getRealTimeData) { List <Classes.StockPrice> stockPriceList = model.GetStockPriceData(new string[] { stockId }).First().Value.OrderByDescending(x => x.Date).Take(100).OrderBy(x => x.Date).ToList(); // List<MyStockAnalyzer.Classes.StockPrice> result = stockHelper.GetStockRealTimePrice if (getRealTimeData) { List <Classes.StockPrice> realTimePrice = stockHelper.GetStockRealTimePrice(new List <Classes.StockData>() { model.GetStockDataById(stockId) }, DateTime.Now.Date); if (realTimePrice.Count > 0) { stockPriceList.Add(realTimePrice.First()); } } List <StockChartData> chartData = StockAnalysisHelper.StockPriceDataToChart(stockPriceList); chartKBar.Series.Clear(); chartKBar.Series.Add("KChart"); chartKBar.Series[0].ChartType = SeriesChartType.Candlestick; chartKBar.Series[0]["PriceUpColor"] = "Red"; chartKBar.Series[0]["PriceDownColor"] = "LimeGreen"; foreach (StockChartData data in chartData) { chartKBar.Series[0].Points.AddXY(data.PriceToday.Date.ToString("yyyy/MM/dd"), (double)data.PriceToday.High, (double)data.PriceToday.Low, (double)data.PriceToday.Open, (double)data.PriceToday.Close); chartKBar.Series[0].Points.Last().Color = Color.Black; } chartKBar.ChartAreas[0].AxisY.Minimum = Math.Floor((double)(chartData.Select(x => x.PriceToday.Low).Min() * 0.98m)); chartKBar.ChartAreas[0].AxisY.Maximum = Math.Ceiling((double)(chartData.Select(x => x.PriceToday.High).Max() * 1.02m)); }
private void startStockSelection(List <MyStockAnalyzer.Classes.StockData> stockData, List <MyStockAnalyzer.Classes.StockPrice> realTimeData) { // 分析股票資料 foreach (MyStockAnalyzer.Classes.StockData data in stockData) { Dictionary <string, List <MyStockAnalyzer.Classes.StockPrice> > stockPrice = model.GetStockPriceData(new string[] { data.StockId }, dtSelectionBgn.Value.Date.AddMonths(-12), dtSelectionEnd.Value.Date); foreach (KeyValuePair <string, List <MyStockAnalyzer.Classes.StockPrice> > kvp in stockPrice) { // 如果要分析即時資料,將目前抓到的即時資料加入股價資訊中 if (chkRealData.Checked && realTimeData.Where(x => x.StockId == kvp.Key).Count() > 0) { kvp.Value.AddRange(realTimeData.Where(x => x.StockId == kvp.Key)); } List <StockChartData> chartData = StockAnalysisHelper.StockPriceDataToChart(kvp.Value); selectStockByAlgorithms(data, chartData); } } }
static void Main(string[] args) { // Write stocks symbol/titl data to file var stocksTitleFile = GetListOfStockSymbols(); string stockDetailsFileWithPath = string.Empty; IEnumerable <string> existingStockDetailsFiles; appFolder = StockAnalysisHelper.GetConfigStringValues(Constants.appFolderKey); var stocksDetailsFile = StockAnalysisHelper.GetConfigStringValues(Constants.StocksDetailsDataFileName); var stocksDetailsRefreshPeriod = StockAnalysisHelper.GetConfigIntValues(Constants.StocksDetailsRefreshPeriod); if (StockAnalysisHelper.RefreshFile(stocksDetailsFile, out existingStockDetailsFiles, out stockDetailsFileWithPath, stocksDetailsRefreshPeriod)) { //Get stocks title data from json file var serializer = new JsonSerializer(); List <StockTitleModel> stockTitles; using (StreamReader sr = new StreamReader(stocksTitleFile)) using (JsonTextReader jtr = new JsonTextReader(sr)) { stockTitles = serializer.Deserialize <List <StockTitleModel> >(jtr); } //var stockSymbols = new List<string> { "viceroy" }; List <StockDetailsModel> stockDetails = new List <StockDetailsModel>(); Parallel.ForEach(stockTitles, x => { var stocksDetails = GetStockDetails(x); stockDetails.Add(stocksDetails); }); stockDetailsFileWithPath = string.Concat(appFolder, "\\", stocksDetailsFile, "_", DateTime.Now.ToString("ddMMyy"), ".json"); existingStockDetailsFiles.ToList().ForEach(x => File.Delete(x)); using (StreamWriter sw = new StreamWriter(stockDetailsFileWithPath, false)) { serializer.Serialize(sw, stockDetails); } } }
public List <StockSelectionResult> GetSelectionResult(List <StockChartData> stockPriceList, DateTime checkBgn, DateTime checkEnd) { List <StockSelectionResult> result = new List <StockSelectionResult>(); for (DateTime date = checkBgn; date <= checkEnd; date = date.AddDays(1)) { // 該日期沒股價資訊則跳過 if (stockPriceList.Where(x => x.PriceToday.Date == date.Date).Count() == 0) { continue; } StockChartData currentDatePrice = stockPriceList.Where(x => x.PriceToday.Date == date).First(); if (currentDatePrice.ChartIdx < 30) { continue; } // 量必須為5日均量的1.5倍以上為爆量 if (currentDatePrice.PriceToday.Amount < currentDatePrice.VMA5 * 1.5m) { continue; } // 漲幅必須大於3% if ((currentDatePrice.PriceToday.Close - currentDatePrice.PriceYesterday.Close) / currentDatePrice.PriceYesterday.Close <= 0.03m) { continue; } // 漲停買不到,跳過 if (currentDatePrice.IsLimitUp) { continue; } List <StockChartData> stock10Days = StockAnalysisHelper.GetNDaysChart(stockPriceList, currentDatePrice.ChartIdx - 1, 10).ToList(); // 10日內最高點 if (currentDatePrice.PriceToday.Close > stock10Days.Select(x => x.PriceToday.Close).Max()) { // 10日內布林上下緣在7%以內 bool chk = true; foreach (StockChartData data in stock10Days) { if (((data.BBUB - data.BBLB) / data.BBLB) > 0.07m) { chk = false; } } if (chk) { StockChartData nextDayData = StockAnalysisHelper.GetNextNDaysChart(stockPriceList, currentDatePrice.ChartIdx, 1); result.Add(new StockSelectionResult() { Date = currentDatePrice.PriceToday.Date, Memo = String.Format("布林拳; 布林平均: {0}%;漲幅: {1}%;爆量: {2}倍; 今日收盤: {3}; 明日開盤: {4}", (stock10Days.Select(x => (x.BBUB - x.BBLB) / x.BBLB).Average() * 100m).ToString("0.00"), (((currentDatePrice.PriceToday.Close - currentDatePrice.PriceYesterday.Close) / currentDatePrice.PriceYesterday.Close) * 100m).ToString("0.00"), (currentDatePrice.PriceToday.Amount / currentDatePrice.VMA5).ToString("0.00"), currentDatePrice.PriceToday.Close.ToString("0.00"), nextDayData == null ? "0.00": nextDayData.PriceToday.Open.ToString("0.00") ) }); } } } return(result); }
private static string GetListOfStockSymbols() { string stocksTitleFileWithPath; IEnumerable <string> existingStockTitleFiles; appFolder = StockAnalysisHelper.GetConfigStringValues(Constants.appFolderKey); var stockstitlefile = StockAnalysisHelper.GetConfigStringValues(Constants.stocksTitleDataFileName); int stocksTitleRefreshPeriod = StockAnalysisHelper.GetConfigIntValues(Constants.stocksTitleDataFileName); if (StockAnalysisHelper.RefreshFile(stockstitlefile, out existingStockTitleFiles, out stocksTitleFileWithPath, stocksTitleRefreshPeriod)) { List <StockTitleModel> stocksTitleList = new List <StockTitleModel>(); string NSEFileWithPath = string.Concat(appFolder, "\\", "NSEBhavCopy.csv"); string BSEFileWithPath = string.Concat(appFolder, "\\", "BSEBhavCopy.zip"); StockAnalysisHelper.DownloadFile(Constants.NSEBhavData, NSEFileWithPath); StockAnalysisHelper.DownloadFile(Constants.BSEBhavData.Replace("<%date%>", GetPastMarketTradingDate()), BSEFileWithPath); List <string> stocksList = new List <string>(); using (var reader = new StreamReader(NSEFileWithPath)) { // Leave out header reader.ReadLine(); while (!reader.EndOfStream) { stocksList.Add(reader.ReadLine().Split(',')[0]); } } Parallel.ForEach(stocksList, x => { var summaryUrl = FormUrl(string.Concat(x, ".NS"), UrlType.Summary); var obj = new HtmlWeb(); obj.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"; var document = obj.Load(summaryUrl); var yahooFinTitleNode = document.DocumentNode.SelectSingleNode(Constants.StockTitleHtml); if (yahooFinTitleNode != null) { string stockInfoTitle = yahooFinTitleNode.InnerText; string stockcode = string.Empty; var title = GetTitleAndCodeFromText(stockInfoTitle, out stockcode); stocksTitleList.Add(new StockTitleModel { StockTitle = title, NSECode = x, StockType = StockType.NSE }); } }); //GetBSEStockSymbols using (ZipArchive archive = ZipFile.OpenRead(BSEFileWithPath)) { List <string> BseStockSymbol = new List <string>(); Stream bseBhavStream = archive.Entries[0].Open(); using (var reader = new StreamReader(bseBhavStream)) { // Leave out header reader.ReadLine(); while (!reader.EndOfStream) { BseStockSymbol.Add(reader.ReadLine().Split(',')[0]); } } AddBSEStocksTitleData(BseStockSymbol, stocksTitleList); } existingStockTitleFiles.ToList().ForEach(x => File.Delete(x)); stocksTitleFileWithPath = string.Concat(appFolder, "\\", stockstitlefile, "_", DateTime.Now.ToString("ddMMyy"), ".json"); //Write data to json file using (StreamWriter sw = new StreamWriter(stocksTitleFileWithPath, false)) { var serializer = new JsonSerializer(); serializer.Serialize(sw, stocksTitleList); } } return(stocksTitleFileWithPath); }