private Wh6Info ParseWh6Info(string info) { Wh6Info wh6Info = new Wh6Info(); // 客户号 info = info.Substring(info.IndexOf("客户号")); string accountNumber = info.Substring(4, info.IndexOf("客户名称") - 4).Trim(); // 客户名称 info = info.Substring(info.IndexOf("客户名称")); string customerName = info.Substring(5, info.IndexOf("日期") - 5).Trim(); wh6Info.AccountNumber = accountNumber; wh6Info.CustomerName = customerName; return(wh6Info); }
/// <summary> /// 从文华6月结单解析数据,得到蜡烛图数据 /// </summary> /// <param name="wh6MonthStatementsDirPath">从文华6软件导出的月结单文件夹</param> private void ImportStocksFromWh6MonthStatement(string wh6MonthStatementsDirPath) { try { List <Wh6Trade> wh6Trades = new List <Wh6Trade>(); using (StatementContext statement = new StatementContext(typeof(Account), typeof(Stock))) { var currentAccount = statement.Accounts.FirstOrDefault(model => model.Id == _Session.SelectedAccountId); var files = Directory.GetFileSystemEntries(wh6MonthStatementsDirPath, "*.*.txt"); //初始化状态 InitialStatusAndProgressBar(5); // foreach (var file in files) { //更新状态 UpdateStatusAndProgressBar(string.Format("正在处理月结算文件{0}", System.IO.Path.GetFileName(file))); // var reader = new StreamReader(File.OpenRead(file), Encoding.GetEncoding("gb2312")); string monthStatement = reader.ReadToEnd(); //****************************** // 客户信息 //更新状态 UpdateStatusAndProgressBar("导入客户信息", 1); monthStatement = monthStatement.Substring(monthStatement.IndexOf("交易结算")); string info = monthStatement.Substring(0, monthStatement.IndexOf("资金状况")); Wh6Info wh6Info = ParseWh6Info(info); //****************************** //******************* // 检验导入的数据是否与本地选定的账户匹配 if (currentAccount != null) { if (!currentAccount.AccountNumber.Contains(wh6Info.AccountNumber)) { this.AddLog(string.Format("不允许从<{0}>向<{1}>导入数据,导入数据必须与当前选定账户匹配!", wh6Info.AccountNumber, currentAccount.AccountNumber)); this.OverStatusAndProgressBar(); return; } } else { this.AddLog("未发现选定账户!"); this.OverStatusAndProgressBar(); return; } //******************* //****************************** // 成交记录 //更新状态 UpdateStatusAndProgressBar("导入成交记录", 1); monthStatement = monthStatement.Substring(monthStatement.IndexOf("成交记录")); string trades = monthStatement.Substring(0, monthStatement.IndexOf("平仓明细")); wh6Trades.AddRange(ImportWh6Trades(trades)); //****************************** } //******************************** // 得到蜡烛图数据 //更新状态 UpdateStatusAndProgressBar("生成蜡烛图数据", 1); var firstStock = statement.Stocks.Where(model => model.AccountId == currentAccount.Id).OrderBy(model => model.Date).FirstOrDefault(); var firstWh6Trade = wh6Trades.OrderBy(model => model.Date).FirstOrDefault(); if (firstStock != null) { if (firstWh6Trade != null) { DateTime firstDate = firstWh6Trade.Date; DateTime lastDate = firstStock.Date.AddDays(-1); decimal open = firstStock.Open, close = firstStock.Close, high = firstStock.High, low = firstStock.Low; while (firstDate <= lastDate) { if (lastDate.DayOfWeek != DayOfWeek.Saturday && lastDate.DayOfWeek != DayOfWeek.Sunday) { var tempWh6Trades = wh6Trades.Where(model => model.Date == lastDate); decimal profit = 0, volume = 0; foreach (var wh6Trade in tempWh6Trades) { profit += wh6Trade.ClosedProfit - wh6Trade.Commission; volume += wh6Trade.Amount; } Stock stock = new Stock(); stock.Id = Guid.NewGuid(); stock.Date = lastDate.Date.Date; stock.Close = open; stock.Open = stock.Close - profit; stock.High = stock.Close > stock.Open ? stock.Close : stock.Open; stock.Low = stock.Close < stock.Open ? stock.Close : stock.Open; stock.AccountId = currentAccount.Id; stock.Volume = volume; statement.AddStock(stock); } lastDate = lastDate.AddDays(-1); } // 更新状态 UpdateStatusAndProgressBar("保存数据", 1); this.AddLog(string.Format("从文华6月结单导入数据完毕!", currentAccount.AccountNumber)); this.OverStatusAndProgressBar(); } } else { this.AddLog(string.Format("本地没有<{0}>的数据,请先从保证金监控中心下载数据,然后再手动导入数据!", currentAccount.AccountNumber)); this.OverStatusAndProgressBar(); } //******************************** UpdateStatusAndProgressBar(progressBarValueIncrease: 1); } } catch (Exception) { throw; } finally { this.OverStatusAndProgressBar(); } }