public override void OnInit() { DateTime curDate = Clock.Now.Date; ISeriesObject[] dailyBars = Util.GetNDailiesBeforeDate(this.instrument, curDate, 60); foreach (Daily dBar in dailyBars) { this.Bar.Add(dBar); } bool overWrite = false; FileSeries series = (FileSeries)this.instrument.GetDataSeries("RiskLevel"); if (series == null) { series = (FileSeries)this.instrument.AddDataSeries("RiskLevel"); } float prevRiskLevel = 1.0F; if (series.Count > 0) { DateTime lastDate = curDate; int i; do { lastDate = lastDate.AddDays(-1); i = series.IndexOf(lastDate); }while(i < 0 && lastDate > series.FirstDateTime); if (i >= 0) { ISeriesObject[] riskArray = series.GetArray(i, i); GMData gmData = (GMData)riskArray[0]; prevRiskLevel = gmData.DataValue; } } GMDaily gmLastDaily = (GMDaily)this.DailyBar.Last; double inc = (gmLastDaily.Close / gmLastDaily.LastClose - 1) * 100; double abs = Math.Abs(inc); double riskLevel = prevRiskLevel + inc / abs * (int)(abs / 0.5) * 0.1; riskLevel = riskLevel < 0.5?0.5:(riskLevel > 1.2?1.2:riskLevel); GMData gmData1 = new GMData(curDate, (float)riskLevel); if (overWrite) { series.Remove(curDate); series.Add(gmData1); } else if (!series.Contains(curDate)) { series.Add(gmData1); } //this.PositionLevel=(float)riskLevel; Console.WriteLine("昨日涨跌幅:{0},今日风险水平为:{1}", inc, riskLevel); }
public static void CoverDailyFormTrade(Daily daily, GMTrade gmTrade) { daily.DateTime = gmTrade.DateTime.Date; daily.High = gmTrade.High; daily.Open = gmTrade.Open; daily.Low = gmTrade.Low; daily.Close = gmTrade.Price; daily.Volume = (long)gmTrade.TotalSize; GMDaily gmDaily = daily as GMDaily; if (gmDaily != null) { gmDaily.LastClose = gmTrade.LastClose; gmDaily.Amount = gmTrade.Amount; } }
protected void addTodayDaily(Instrument inst) { Trade lastTrade = this.getLastTrade(inst); if (lastTrade != null) { GMTrade gmTrade = (GMTrade)lastTrade; GMDaily daily = new GMDaily(); daily.Date = gmTrade.DateTime.Date; daily.High = gmTrade.High; daily.Open = gmTrade.Open; daily.Low = gmTrade.Low; daily.Close = gmTrade.Price; daily.Volume = (long)gmTrade.TotalSize; BarSeries barSeries = this.getDailySeries(inst); barSeries.Add(daily); } }
private static void ReviseDailys(List <Daily> dailys, string symbol, Trade lastTrade) { GMTrade gmTrade = (GMTrade)lastTrade; //去掉当天日线后 int m = dailys.Count; if (dailys[m - 1].DateTime == dealDate) { BarUtils.CoverDailyFormTrade(dailys[m - 1], gmTrade); } else { dailys.RemoveAt(0); GMDaily newDaily = new GMDaily(dailys[m - 2] as GMDaily); BarUtils.CoverDailyFormTrade(newDaily, gmTrade); dailys.Add(newDaily); } //向前复权 BarUtils.AdjustDailys(dailys); }
//将日线向前复权 public static void AdjustDailys(ISeriesObject[] gmDailys) //进行向前复权 { int num = gmDailys.Length; if (num > 1) { GMDaily lastDaily = (GMDaily)gmDailys[num - 1]; for (int i = num - 2; i >= 0; i--) { GMDaily curDaily = (GMDaily)gmDailys[i]; if (curDaily.AdjFactor != lastDaily.AdjFactor) { curDaily.Close = curDaily.Close * curDaily.AdjFactor / lastDaily.AdjFactor; curDaily.High = curDaily.High * curDaily.AdjFactor / lastDaily.AdjFactor; curDaily.Low = curDaily.Low * curDaily.AdjFactor / lastDaily.AdjFactor; curDaily.Open = curDaily.Open * curDaily.AdjFactor / lastDaily.AdjFactor; curDaily.LastClose = curDaily.LastClose * curDaily.AdjFactor / lastDaily.AdjFactor; curDaily.Volume = (long)(curDaily.Volume * lastDaily.AdjFactor / curDaily.AdjFactor); curDaily.AdjFactor = lastDaily.AdjFactor; } lastDaily = curDaily; } } }
protected override bool doJob() { Console.WriteLine("正在读取Tick数据生成涨停板数据分析表..."); /*Console.WriteLine("测试作业........."); * return true;*/ try { GMRealTimeProvider provider = (GMRealTimeProvider)ProviderManager.MarketDataProviders["GMRealTimeProvider"]; provider.Connect(10000); if (!provider.IsConnected) { return(false); } string dateString = this.curDate.ToString("yyyy-MM-dd"); DateTime endTime = this.curDate.Add(new TimeSpan(24, 0, 0)); string endString = endTime.ToString("yyyy-MM-dd HH:mm:ss"); List <AnalysisItem> analysisList = new List <AnalysisItem>(); foreach (Instrument inst in InstrumentManager.Instruments) { if (inst.SecurityDesc.IndexOf("B") >= 0) { continue; //除去B股 } if (inst.SecurityType == "IDX") { continue; //除去指数 } FileSeries dailySeries = (FileSeries)inst.GetDataSeries(DataManager.EDataSeries.Daily); if (dailySeries == null || dailySeries.Count <= 0) { continue; } int i = dailySeries.IndexOf(this.curDate); if (i < 0) { continue; //没有日线,当日不开盘 } i = i - 10 > 0 ? i - 10 : 0; //往前推10个交易日 DateTime firstDate = dailySeries[i].DateTime; ISeriesObject[] dailys = dailySeries.GetArray(firstDate, this.curDate); int k = dailys.Length - 1; GMDaily gmDaily = (GMDaily)dailys[k]; if (gmDaily.Close / gmDaily.LastClose > 1.095) {//今日涨停 AnalysisItem ai = new AnalysisItem(); ai.Date = gmDaily.DateTime; ai.Symbol = inst.Symbol; GMSDK.ShareIndex si = null; //先读取当日股本,如果没有,则读取最近股本。一般情况下当日股本与最近股本相同,除非当天正好是除权日 List <GMSDK.ShareIndex> shareIndexList = provider.MdApi.GetShareIndex(ai.Symbol, dateString, dateString); if (shareIndexList.Count <= 0) { shareIndexList = provider.MdApi.GetLastShareIndex(ai.Symbol); } if (shareIndexList.Count > 0) { si = shareIndexList[0]; } if (si != null) { ai.FlowAShare = si.flow_a_share; } else { ai.FlowAShare = 0.0; } ai.LastClose = gmDaily.LastClose; ai.Open = gmDaily.Open; ai.High = gmDaily.High; ai.Low = gmDaily.Low; ai.Close = gmDaily.Close; //换手率 if (ai.FlowAShare > 0) { ai.TurnoverRate = gmDaily.Volume / ai.FlowAShare * 100; } else { ai.TurnoverRate = 0.0; } //当日量与前5日均量比 int m = k - 1; double sumVol = 0.0; while (m >= 0 && m >= k - 5) { sumVol += ((Daily)dailys[m]).Volume; m--; } double last5AvgVol = sumVol / (k - m - 1); ai.VolDivLast5AvgVol = gmDaily.Volume / last5AvgVol; //最后一笔判断是否是涨停 List <GMSDK.Tick> lastTicks = provider.MdApi.GetLastNTicks(inst.Symbol, 1, endString); if (lastTicks.Count <= 0) { continue; } GMTrade gmLastTrade = (GMTrade)GSKToGM.ConvertTrade(lastTicks[0]);//最后一笔交易数据 if (gmLastTrade.Price < gmLastTrade.UpperLimit) { continue; } //封成比 GMQuote gmLastQuote = (GMQuote)GSKToGM.ConvertQuote(lastTicks[0]);//最后一刻报价数据 ai.BidSizeDivVol = (double)gmLastQuote.BidSize / gmDaily.Volume; if (k > 0) { GMDaily lastGMDaily = (GMDaily)dailys[k - 1]; if (lastGMDaily.Close / lastGMDaily.LastClose >= 1.099) { ai.LastUpLimited = true; } } analysisList.Add(ai); } } Console.WriteLine("今日共有{0}只涨停。", analysisList.Count); foreach (AnalysisItem ai in analysisList) { Console.WriteLine(@"Date:{0},Symbol:{1},昨收价:{2},昨日是否涨停:{3},开盘价:{4}, 最高价:{5},最低价:{6},收盘价:{7},涨停时间:{8},是否开板:{9},涨停时长:{10}, 成交量与昨日5日均量比:{11},收盘买一量与成交量比:{12},流通A股:{13},换手率:{14}" , ai.Date, ai.Symbol, ai.LastClose, ai.LastUpLimited, ai.Open, ai.High, ai.Low, ai.Close, ai.WhenUpLimit, ai.UpLimitBreaked, ai.HowLongUpLimit, ai.VolDivLast5AvgVol, ai.BidSizeDivVol, ai.FlowAShare, ai.TurnoverRate); } MongoClient client = new MongoClient("mongodb://localhost:27017"); MongoServer server = client.GetServer(); MongoDatabase database = server.GetDatabase("finance"); MongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>("UpLimitAnalysis"); foreach (AnalysisItem ai in analysisList) { BsonElement[] eleArray = new BsonElement[2]; eleArray[0] = new BsonElement("date", ai.Date.ToString("yyyy-MM-dd")); eleArray[1] = new BsonElement("symbol", ai.Symbol); QueryDocument query = new QueryDocument(eleArray); collection.Remove(query); BsonElement[] eleArray1 = new BsonElement[19]; eleArray1[0] = new BsonElement("date", ai.Date.ToString("yyyy-MM-dd")); eleArray1[1] = new BsonElement("symbol", ai.Symbol); eleArray1[2] = new BsonElement("flowashare", ai.FlowAShare); eleArray1[3] = new BsonElement("lastclose", ai.LastClose); eleArray1[4] = new BsonElement("lastuplimited", ai.LastUpLimited); eleArray1[5] = new BsonElement("open", ai.Open); eleArray1[6] = new BsonElement("high", ai.High); eleArray1[7] = new BsonElement("low", ai.Low); eleArray1[8] = new BsonElement("close", ai.Close); eleArray1[9] = new BsonElement("whenuplimit", ai.WhenUpLimit.ToString()); eleArray1[10] = new BsonElement("uplimitbreaked", ai.UpLimitBreaked); eleArray1[11] = new BsonElement("howlonguplimit", ai.HowLongUpLimit.ToString()); eleArray1[12] = new BsonElement("turnoverrate", ai.TurnoverRate); eleArray1[13] = new BsonElement("voldivlast5avgvol", ai.VolDivLast5AvgVol); eleArray1[14] = new BsonElement("bidsizedivvol", ai.BidSizeDivVol); BsonDocument insert = new BsonDocument(eleArray1); collection.Insert(insert); } return(true); } catch (Exception ex) { Console.WriteLine(ex.Message); } return(false); }
protected override bool doJob() { Console.WriteLine("正在读取Tick数据生成涨停板数据分析表..."); /*Console.WriteLine("测试作业........."); * return true;*/ try { GMRealTimeProvider provider = (GMRealTimeProvider)ProviderManager.MarketDataProviders["GMRealTimeProvider"]; provider.Connect(10000); if (!provider.IsConnected) { return(false); } string path = this.dataPath + "/" + this.curDate.Year.ToString() + "/" + this.curDate.Month.ToString(); if (!Directory.Exists(path)) { Console.WriteLine("Trade数据目录不存在!"); return(false); } string dateString = this.curDate.ToString("yyyy-MM-dd"); List <AnalysisItem> analysisList = new List <AnalysisItem>(); DataFile file = null; try { file = DataFile.Open(path); foreach (Instrument inst in InstrumentManager.Instruments) { if (inst.SecurityDesc.IndexOf("B") >= 0) { continue; //除去B股 } if (inst.SecurityType == "IDX") { continue; //除去指数 } FileSeries dailySeries = (FileSeries)inst.GetDataSeries(DataManager.EDataSeries.Daily); if (dailySeries == null || dailySeries.Count <= 0) { continue; } int i = dailySeries.IndexOf(this.curDate); if (i < 0) { continue; //没有日线,当日不开盘 } i = i - 10 > 0?i - 10:0; //往前推10个交易日 DateTime firstDate = dailySeries[i].DateTime; ISeriesObject[] dailys = dailySeries.GetArray(firstDate, this.curDate); int k = dailys.Length - 1; GMDaily gmDaily = (GMDaily)dailys[k]; if (gmDaily.Close / gmDaily.LastClose > 1.095) //今日涨停 { AnalysisItem ai = new AnalysisItem(); ai.Date = gmDaily.DateTime; ai.Symbol = inst.Symbol; GMSDK.ShareIndex si = null; //先读取当日股本,如果没有,则读取最近股本。一般情况下当日股本与最近股本相同,除非当天正好是除权日 List <GMSDK.ShareIndex> shareIndexList = provider.MdApi.GetShareIndex(ai.Symbol, dateString, dateString); if (shareIndexList.Count <= 0) { shareIndexList = provider.MdApi.GetLastShareIndex(ai.Symbol); } if (shareIndexList.Count > 0) { si = shareIndexList[0]; } if (si != null) { ai.FlowAShare = si.flow_a_share; } else { ai.FlowAShare = 0.0; } ai.LastClose = gmDaily.LastClose; ai.Open = gmDaily.Open; ai.High = gmDaily.High; ai.Low = gmDaily.Low; ai.Close = gmDaily.Close; //换手率 if (ai.FlowAShare > 0) { ai.TurnoverRate = gmDaily.Volume / ai.FlowAShare * 100; } else { ai.TurnoverRate = 0.0; } //当日量与前5日均量比 int m = k - 1; double sumVol = 0.0; while (m >= 0 && m >= k - 5) { sumVol += ((Daily)dailys[m]).Volume; m--; } double last5AvgVol = sumVol / (k - m - 1); ai.VolDivLast5AvgVol = gmDaily.Volume / last5AvgVol; //计算封板时间、时长等 bool flag = false; string name = inst.Symbol + ".Trade"; if (!file.Series.Contains(name)) { continue; } FileSeries series = file.Series[name]; ISeriesObject[] trades = series.GetArray(gmDaily.DateTime, gmDaily.DateTime.AddDays(1)); if (trades.Length <= 0) { continue; } GMTrade gmLastTrade = (GMTrade)trades[trades.Length - 1]; //最后一笔交易数据 if (gmLastTrade.Price < gmLastTrade.UpperLimit) { continue; } GMTrade lastGMTrade = null; DateTime openTimeAM = gmDaily.Date.Add(new TimeSpan(9, 25, 0)); DateTime openTimePM = gmDaily.Date.Add(new TimeSpan(13, 0, 0)); foreach (ISeriesObject aTrade in trades) { GMTrade gmTrade = (GMTrade)aTrade; if (!flag && gmTrade.Price == gmTrade.UpperLimit && gmTrade.DateTime >= openTimeAM) { ai.WhenUpLimit = gmTrade.DateTime - ai.Date; flag = true; } if (flag && gmTrade.Price < gmTrade.UpperLimit) { ai.UpLimitBreaked = true; } //将下午13:00之后的每笔时间减去90分钟,这样便于计算封涨停的时长 if (gmTrade.DateTime >= openTimePM) { gmTrade.DateTime = gmTrade.DateTime.AddMinutes(-90); } if (flag && lastGMTrade != null && gmTrade.Price == gmTrade.UpperLimit && lastGMTrade.Price == gmTrade.UpperLimit) { ai.HowLongUpLimit += gmTrade.DateTime - lastGMTrade.DateTime; } if (flag) { lastGMTrade = gmTrade; } } //封成比 name = inst.Symbol + ".Quote"; if (!file.Series.Contains(name)) { continue; } series = file.Series[name]; ISeriesObject[] quotes = series.GetArray(gmDaily.DateTime, gmDaily.DateTime.AddDays(1)); if (quotes.Length <= 0) { continue; } GMQuote gmLastQuote = (GMQuote)quotes[quotes.Length - 1]; //最后一刻报价数据 ai.BidSizeDivVol = (double)gmLastQuote.BidSize / gmDaily.Volume; if (k > 0) { GMDaily lastGMDaily = (GMDaily)dailys[k - 1]; if (lastGMDaily.Close / lastGMDaily.LastClose >= 1.099) { ai.LastUpLimited = true; } } if (flag) { analysisList.Add(ai); } } } }catch (Exception ex) { throw ex; }finally{ file.Close(); } Console.WriteLine("今日共有{0}只涨停。", analysisList.Count); foreach (AnalysisItem ai in analysisList) { Console.WriteLine(@"Date:{0},Symbol:{1},昨收价:{2},昨日是否涨停:{3},开盘价:{4}, 最高价:{5},最低价:{6},收盘价:{7},涨停时间:{8},是否开板:{9},涨停时长:{10}, 成交量与昨日5日均量比:{11},收盘买一量与成交量比:{12},流通A股:{13},换手率:{14}" , ai.Date, ai.Symbol, ai.LastClose, ai.LastUpLimited, ai.Open, ai.High, ai.Low, ai.Close, ai.WhenUpLimit, ai.UpLimitBreaked, ai.HowLongUpLimit, ai.VolDivLast5AvgVol, ai.BidSizeDivVol, ai.FlowAShare, ai.TurnoverRate); } MongoClient client = new MongoClient("mongodb://localhost:27017"); MongoServer server = client.GetServer(); MongoDatabase database = server.GetDatabase("finance"); MongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>("UpLimitAnalysis"); foreach (AnalysisItem ai in analysisList) { BsonElement[] eleArray = new BsonElement[2]; eleArray[0] = new BsonElement("date", ai.Date.ToString("yyyy-MM-dd")); eleArray[1] = new BsonElement("symbol", ai.Symbol); QueryDocument query = new QueryDocument(eleArray); collection.Remove(query); BsonElement[] eleArray1 = new BsonElement[19]; eleArray1[0] = new BsonElement("date", ai.Date.ToString("yyyy-MM-dd")); eleArray1[1] = new BsonElement("symbol", ai.Symbol); eleArray1[2] = new BsonElement("flowashare", ai.FlowAShare); eleArray1[3] = new BsonElement("lastclose", ai.LastClose); eleArray1[4] = new BsonElement("lastuplimited", ai.LastUpLimited); eleArray1[5] = new BsonElement("open", ai.Open); eleArray1[6] = new BsonElement("high", ai.High); eleArray1[7] = new BsonElement("low", ai.Low); eleArray1[8] = new BsonElement("close", ai.Close); eleArray1[9] = new BsonElement("whenuplimit", ai.WhenUpLimit.ToString()); eleArray1[10] = new BsonElement("uplimitbreaked", ai.UpLimitBreaked); eleArray1[11] = new BsonElement("howlonguplimit", ai.HowLongUpLimit.ToString()); eleArray1[12] = new BsonElement("turnoverrate", ai.TurnoverRate); eleArray1[13] = new BsonElement("voldivlast5avgvol", ai.VolDivLast5AvgVol); eleArray1[14] = new BsonElement("bidsizedivvol", ai.BidSizeDivVol); BsonDocument insert = new BsonDocument(eleArray1); collection.Insert(insert); } return(true); }catch (Exception ex) { Console.WriteLine(ex.Message); } return(false); }
static void Main(string[] args) { // TO DO: Add your code here DateTime beginDate = new DateTime(2017, 3, 24); DateTime endDate = new DateTime(2017, 3, 25); MongoClient client = new MongoClient("mongodb://localhost:27017"); MongoServer server = client.GetServer(); MongoDatabase database = server.GetDatabase("finance"); string username = "******"; string password = "******"; int ret = _md.Init(username, password, GMSDK.MDMode.MD_MODE_NULL, "", "", ""); if (ret != 0) { string msg = _md.StrError(ret); Console.WriteLine(msg); return; } DateTime curDate = beginDate; //获取交易日历,以确定是否开市日 string dateString1 = ""; if (endDate.AddMonths(-1) > beginDate) { dateString1 = beginDate.ToString("yyyy-MM-dd"); } else { dateString1 = endDate.AddMonths(-1).ToString("yyyy-MM-dd"); } string dateString2 = endDate.AddDays(1).ToString("yyyy-MM-dd"); List <DateTime> tradeDates = GetTradeDates("SHSE", dateString1, dateString2); while (curDate < endDate) { Console.WriteLine("日期:" + curDate.ToString("yyyy-MM-dd")); if ((tradeDates.Count <= 0) || (!tradeDates.Contains(curDate))) { Console.WriteLine("今天不是交易日"); curDate = curDate.AddDays(1); continue; } string monthString = curDate.ToString("yyyy-MM"); //区域统计数据准备 MongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>("Areas." + monthString); Dictionary <string, List <string> > areaDict = new Dictionary <string, List <string> >(); foreach (BsonDocument document in collection.FindAll()) { List <string> ls = null; if (!areaDict.TryGetValue((string)document["code"], out ls)) { ls = new List <string>(); areaDict.Add((string)document["code"], ls); } if (!(document["area"] is BsonNull)) { ls.Add((string)document["area"]); } } Dictionary <string, StatisItem> areaUpLimitStatis = new Dictionary <string, StatisItem>(); //行业统计数据准备 collection = database.GetCollection <BsonDocument>("Industries." + monthString); Dictionary <string, List <string> > industryDict = new Dictionary <string, List <string> >(); foreach (BsonDocument document in collection.FindAll()) { List <string> ls = null; if (!industryDict.TryGetValue((string)document["code"], out ls)) { ls = new List <string>(); industryDict.Add((string)document["code"], ls); } if (!(document["c_name"] is BsonNull)) { ls.Add((string)document["c_name"]); } } Dictionary <string, StatisItem> industryUpLimitStatis = new Dictionary <string, StatisItem>(); //概念统计数据准备 collection = database.GetCollection <BsonDocument>("Concepts." + monthString); Dictionary <string, List <string> > conceptDict = new Dictionary <string, List <string> >(); foreach (BsonDocument document in collection.FindAll()) { List <string> ls = null; if (!conceptDict.TryGetValue((string)document["code"], out ls)) { ls = new List <string>(); conceptDict.Add((string)document["code"], ls); } if (!(document["c_name"] is BsonNull)) { ls.Add((string)document["c_name"]); } } Dictionary <string, StatisItem> conceptUpLimitStatis = new Dictionary <string, StatisItem>(); //找出日线当天接近涨停股 List <Instrument> toUpLimit = new List <Instrument>(); foreach (Instrument instrument in InstrumentManager.Instruments) { if (instrument.SecurityType == "CS" && instrument.SecurityDesc.IndexOf('B') < 0) { DailySeries series = instrument.GetDailySeries(curDate.AddMonths(-3), curDate); //下面这里要求日线数大于20,是为了排除新股 if (series.Count > 20) { GMDaily gmDaily = (GMDaily)series[series.Count - 1]; //最近一天的日线 if (gmDaily.Date != curDate) { continue; } //下面是对于st,*st的,因为名字判断不准确,故去掉 //if ((instrument.SecurityDesc.IndexOf("ST")>=0||instrument.SecurityDesc.IndexOf('S')>=0) //&&(gmDaily.Close/gmDaily.LastClose>1.044)){ // toUpLimit.Add(instrument); //} //下面是对于普通股 if (gmDaily.Close / gmDaily.LastClose > 1.094) { toUpLimit.Add(instrument); } } } } //读出Trade数据,确定是否真的涨停及涨停时间,交统计数据 string dataPath = "e:/QDData"; string path = dataPath + "/" + curDate.Year.ToString() + "/" + curDate.Month.ToString(); if (!Directory.Exists(path)) { Console.WriteLine("Trade数据目录不存在!"); return; } try { DataFile file = DataFile.Open(path); try { foreach (Instrument instrument in toUpLimit) { string name = instrument.Symbol + ".Trade"; if (!file.Series.Contains(name)) { throw new Exception(instrument.Symbol + "的Trade数据不存在,请补充完整。"); } FileSeries series = file.Series[name]; ISeriesObject[] trades = series.GetArray(curDate, curDate.AddDays(1)); if (trades.Length <= 0) { continue; } GMTrade gmLastTrade = (GMTrade)trades[trades.Length - 1]; //最后一笔交易数据 if (gmLastTrade.Price < gmLastTrade.UpperLimit) { continue; } foreach (ISeriesObject aTrade in trades) { GMTrade gmTrade = (GMTrade)aTrade; if (gmTrade.Price == gmTrade.UpperLimit && gmTrade.DateTime >= curDate.Add(new TimeSpan(9, 25, 0))) { Console.WriteLine(instrument.Symbol + instrument.SecurityDesc + "在" + gmTrade.DateTime + "时涨停"); //区域统计 if (areaDict.ContainsKey(instrument.SecurityID)) { List <string> areaList = areaDict[instrument.SecurityID]; foreach (string area in areaList) { StatisItem si = null; if (!areaUpLimitStatis.TryGetValue(area, out si)) { si = new StatisItem(); areaUpLimitStatis.Add(area, si); } si.AddSymbol(instrument.Symbol, gmTrade.DateTime); } } //行业统计 if (industryDict.ContainsKey(instrument.SecurityID)) { List <string> industryList = industryDict[instrument.SecurityID]; foreach (string industry in industryList) { StatisItem si = null; if (!industryUpLimitStatis.TryGetValue(industry, out si)) { si = new StatisItem(); industryUpLimitStatis.Add(industry, si); } si.AddSymbol(instrument.Symbol, gmTrade.DateTime); } } //概念统计 if (conceptDict.ContainsKey(instrument.SecurityID)) { List <string> conceptList = conceptDict[instrument.SecurityID]; foreach (string concept in conceptList) { StatisItem si = null; if (!conceptUpLimitStatis.TryGetValue(concept, out si)) { si = new StatisItem(); conceptUpLimitStatis.Add(concept, si); } si.AddSymbol(instrument.Symbol, gmTrade.DateTime); } } break; } } } }catch (Exception ex) { throw ex; } finally{ file.Close(); } //区域统计写入数据库 MongoCollection <BsonDocument> collection1 = database.GetCollection <BsonDocument>("AreasUpLimitStatis"); BsonElement[] eleArray = new BsonElement[1]; eleArray[0] = new BsonElement("date", curDate.ToString("yyyy-MM-dd")); QueryDocument query = new QueryDocument(eleArray); collection1.Remove(query); foreach (KeyValuePair <string, StatisItem> kvp in areaUpLimitStatis) { BsonElement[] eleArray1 = new BsonElement[4]; eleArray1[0] = new BsonElement("date", curDate.ToString("yyyy-MM-dd")); eleArray1[1] = new BsonElement("area", kvp.Key); eleArray1[2] = new BsonElement("count", kvp.Value.Count); BsonArray bsonArray = new BsonArray(kvp.Value.Count); foreach (KeyValuePair <string, DateTime> kvp1 in kvp.Value.Securities) { bsonArray.Add(new BsonDocument(new BsonElement[2] { new BsonElement("symbol", kvp1.Key), new BsonElement("time", kvp1.Value.ToString("yyyy-MM-dd HH:mm:ss")) })); } eleArray1[3] = new BsonElement("securities", bsonArray); BsonDocument insert = new BsonDocument(eleArray1); collection1.Insert(insert); } //行业统计写入数据库 MongoCollection <BsonDocument> collection2 = database.GetCollection <BsonDocument>("IndustriesUpLimitStatis"); collection2.Remove(query); foreach (KeyValuePair <string, StatisItem> kvp in industryUpLimitStatis) { BsonElement[] eleArray1 = new BsonElement[4]; eleArray1[0] = new BsonElement("date", curDate.ToString("yyyy-MM-dd")); eleArray1[1] = new BsonElement("industry", kvp.Key); eleArray1[2] = new BsonElement("count", kvp.Value.Count); BsonArray bsonArray = new BsonArray(kvp.Value.Count); foreach (KeyValuePair <string, DateTime> kvp1 in kvp.Value.Securities) { bsonArray.Add(new BsonDocument(new BsonElement[2] { new BsonElement("symbol", kvp1.Key), new BsonElement("time", kvp1.Value.ToString("yyyy-MM-dd HH:mm:ss")) })); } eleArray1[3] = new BsonElement("securities", bsonArray); BsonDocument insert = new BsonDocument(eleArray1); collection2.Insert(insert); } //概念统计写入数据库 MongoCollection <BsonDocument> collection3 = database.GetCollection <BsonDocument>("ConceptsUpLimitStatis"); collection3.Remove(query); foreach (KeyValuePair <string, StatisItem> kvp in conceptUpLimitStatis) { BsonElement[] eleArray1 = new BsonElement[4]; eleArray1[0] = new BsonElement("date", curDate.ToString("yyyy-MM-dd")); eleArray1[1] = new BsonElement("concept", kvp.Key); eleArray1[2] = new BsonElement("count", kvp.Value.Count); BsonArray bsonArray = new BsonArray(kvp.Value.Count); foreach (KeyValuePair <string, DateTime> kvp1 in kvp.Value.Securities) { bsonArray.Add(new BsonDocument(new BsonElement[2] { new BsonElement("symbol", kvp1.Key), new BsonElement("time", kvp1.Value.ToString("yyyy-MM-dd HH:mm:ss")) })); } eleArray1[3] = new BsonElement("securities", bsonArray); BsonDocument insert = new BsonDocument(eleArray1); collection3.Insert(insert); } }catch (Exception ex) { Console.WriteLine(ex.Message); } curDate = curDate.AddDays(1); } }
static void Main(string[] args) { // TO DO: Add your code here string username = "******"; string password = "******"; int ret = _md.Init(username, password, GMSDK.MDMode.MD_MODE_NULL, "", "", ""); if (ret != 0) { string msg = _md.StrError(ret); Console.WriteLine(msg); return; } string dataPath = "e:/QDData"; DateTime beginDate = new System.DateTime(2017, 4, 10); DateTime endDate = new System.DateTime(2017, 4, 17); string beginDateString = beginDate.ToString("yyyy-MM-dd"); string endDateString = endDate.ToString("yyyy-MM-dd"); Dictionary <string, Dictionary <string, double> > shareDict = new Dictionary <string, Dictionary <string, double> >();//存放股本 List <AnalysisItem> analysisList = new List <AnalysisItem>(); foreach (Instrument inst in InstrumentManager.Instruments) { if (inst.SecurityDesc.IndexOf("B") >= 0) { continue; //除去B股 } FileSeries dailySeries = (FileSeries)inst.GetDataSeries(DataManager.EDataSeries.Daily); if (dailySeries == null || dailySeries.Count <= 0) { continue; } int i; DateTime firstDate = beginDate; do { i = dailySeries.IndexOf(firstDate); firstDate = firstDate.AddDays(1); }while(i < 0 && firstDate < endDate); if (i < 0) { continue; } i = i - 5 > 0?i - 5:0; firstDate = dailySeries[i].DateTime; ISeriesObject[] dailys = dailySeries.GetArray(firstDate, endDate); for (int k = 0; k < dailys.Length; k++) { GMDaily gmDaily = (GMDaily)dailys[k]; if (gmDaily.DateTime >= beginDate && gmDaily.DateTime < endDate) { if (gmDaily.Close / gmDaily.LastClose > 1.095) //今日涨停 { AnalysisItem ai = new AnalysisItem(); ai.Date = gmDaily.DateTime; ai.Symbol = inst.Symbol; Dictionary <string, double> sd = null; if (!shareDict.TryGetValue(inst.Symbol, out sd)) { sd = new Dictionary <string, double>(); List <GMSDK.ShareIndex> shareIndexList = _md.GetShareIndex(inst.Symbol, beginDateString, endDateString); foreach (GMSDK.ShareIndex si in shareIndexList) { sd.Add(si.pub_date, si.flow_a_share); } shareDict.Add(inst.Symbol, sd); } sd.TryGetValue(ai.Date.ToString("yyyy-MM-dd"), out ai.FlowAShare); ai.LastClose = gmDaily.LastClose; ai.Open = gmDaily.Open; ai.High = gmDaily.High; ai.Low = gmDaily.Low; ai.Close = gmDaily.Close; if (ai.FlowAShare > 0) { ai.TurnoverRate = gmDaily.Volume / ai.FlowAShare * 100; } int m = k - 1; double sumVol = 0.0; while (m >= 0 && m >= k - 5) { sumVol += ((Daily)dailys[m]).Volume; m--; } double last5AvgVol = sumVol / (k - m - 1); ai.VolDivLast5AvgVol = gmDaily.Volume / last5AvgVol; string path = dataPath + "/" + gmDaily.DateTime.Year.ToString() + "/" + gmDaily.DateTime.Month.ToString(); if (!Directory.Exists(path)) { Console.WriteLine("Trade数据目录不存在!"); return; } DataFile file = null; bool flag = false; try { file = DataFile.Open(path); string name = inst.Symbol + ".Trade"; if (!file.Series.Contains(name)) { continue; } FileSeries series = file.Series[name]; ISeriesObject[] trades = series.GetArray(gmDaily.DateTime, gmDaily.DateTime.AddDays(1)); if (trades.Length <= 0) { continue; } GMTrade gmLastTrade = (GMTrade)trades[trades.Length - 1]; //最后一笔交易数据 if (gmLastTrade.Price < gmLastTrade.UpperLimit) { continue; } GMTrade lastGMTrade = null; DateTime openTimeAM = gmDaily.Date.Add(new TimeSpan(9, 25, 0)); DateTime openTimePM = gmDaily.Date.Add(new TimeSpan(13, 0, 0)); foreach (ISeriesObject aTrade in trades) { GMTrade gmTrade = (GMTrade)aTrade; if (!flag && gmTrade.Price == gmTrade.UpperLimit && gmTrade.DateTime >= openTimeAM) { ai.WhenUpLimit = gmTrade.DateTime - ai.Date; flag = true; } if (flag && gmTrade.Price < gmTrade.UpperLimit) { ai.UpLimitBreaked = true; } //将下午13:00之后的每笔时间减去90分钟,这样便于计算封涨停的时长 if (gmTrade.DateTime >= openTimePM) { gmTrade.DateTime = gmTrade.DateTime.AddMinutes(-90); } if (flag && lastGMTrade != null && gmTrade.Price == gmTrade.UpperLimit && lastGMTrade.Price == gmTrade.UpperLimit) { ai.HowLongUpLimit += gmTrade.DateTime - lastGMTrade.DateTime; } if (flag) { lastGMTrade = gmTrade; } } //封成比 name = inst.Symbol + ".Quote"; if (!file.Series.Contains(name)) { continue; } series = file.Series[name]; ISeriesObject[] quotes = series.GetArray(gmDaily.DateTime, gmDaily.DateTime.AddDays(1)); if (quotes.Length <= 0) { continue; } GMQuote gmLastQuote = (GMQuote)quotes[quotes.Length - 1]; //最后一笔报价数据 ai.BidSizeDivVol = (double)gmLastQuote.BidSize / gmDaily.Volume; }catch (Exception ex) { Console.WriteLine(ex.Message); }finally{ file.Close(); } if (k > 0) { GMDaily lastGMDaily = (GMDaily)dailys[k - 1]; if (lastGMDaily.Close / lastGMDaily.LastClose > 1.098) { ai.LastUpLimited = true; } } if (k < dailys.Length - 1) { GMDaily nextGMDaily = (GMDaily)dailys[k + 1]; ai.NextOpen = nextGMDaily.Open; ai.NextHigh = nextGMDaily.High; ai.NextLow = nextGMDaily.Low; ai.NextClose = nextGMDaily.Close; } if (flag) { analysisList.Add(ai); } } } } } Console.WriteLine(analysisList.Count); analysisList.Sort(delegate(AnalysisItem ai1, AnalysisItem ai2){ return(ai1.Date.CompareTo(ai2.Date)); }); foreach (AnalysisItem ai in analysisList) { Console.WriteLine(@"Date:{0},Symbol:{1},昨收价:{2},昨日是否涨停:{3},开盘价:{4}, 最高价:{5},最低价:{6},收盘价:{7},涨停时间:{8},是否开板:{9},涨停时长:{10}, 成交量与昨日5日均量比:{11},收盘买一量与成交量比:{12}, 明日开盘价:{13},明日最高价:{14},明日最低价:{15},明日收盘价:{16},流通A股:{17},换手率:{18}" , ai.Date, ai.Symbol, ai.LastClose, ai.LastUpLimited, ai.Open, ai.High, ai.Low, ai.Close, ai.WhenUpLimit, ai.UpLimitBreaked, ai.HowLongUpLimit, ai.VolDivLast5AvgVol, ai.BidSizeDivVol, ai.NextOpen, ai.NextHigh, ai.NextLow, ai.NextClose, ai.FlowAShare, ai.TurnoverRate); } MongoClient client = new MongoClient("mongodb://localhost:27017"); MongoServer server = client.GetServer(); MongoDatabase database = server.GetDatabase("finance"); MongoCollection <BsonDocument> collection = database.GetCollection <BsonDocument>("UpLimitAnalysis"); foreach (AnalysisItem ai in analysisList) { BsonElement[] eleArray = new BsonElement[2]; eleArray[0] = new BsonElement("date", ai.Date.ToString("yyyy-MM-dd")); eleArray[1] = new BsonElement("symbol", ai.Symbol); QueryDocument query = new QueryDocument(eleArray); collection.Remove(query); BsonElement[] eleArray1 = new BsonElement[19]; eleArray1[0] = new BsonElement("date", ai.Date.ToString("yyyy-MM-dd")); eleArray1[1] = new BsonElement("symbol", ai.Symbol); eleArray1[2] = new BsonElement("flowashare", ai.FlowAShare); eleArray1[3] = new BsonElement("lastclose", ai.LastClose); eleArray1[4] = new BsonElement("lastuplimited", ai.LastUpLimited); eleArray1[5] = new BsonElement("open", ai.Open); eleArray1[6] = new BsonElement("high", ai.High); eleArray1[7] = new BsonElement("low", ai.Low); eleArray1[8] = new BsonElement("close", ai.Close); eleArray1[9] = new BsonElement("whenuplimit", ai.WhenUpLimit.ToString()); eleArray1[10] = new BsonElement("uplimitbreaked", ai.UpLimitBreaked); eleArray1[11] = new BsonElement("howlonguplimit", ai.HowLongUpLimit.ToString()); eleArray1[12] = new BsonElement("turnoverrate", ai.TurnoverRate); eleArray1[13] = new BsonElement("voldivlast5avgvol", ai.VolDivLast5AvgVol); eleArray1[14] = new BsonElement("bidsizedivvol", ai.BidSizeDivVol); eleArray1[15] = new BsonElement("nextopen", ai.NextOpen); eleArray1[16] = new BsonElement("nexthigh", ai.NextHigh); eleArray1[17] = new BsonElement("nextlow", ai.NextLow); eleArray1[18] = new BsonElement("nextclose", ai.NextClose); BsonDocument insert = new BsonDocument(eleArray1); collection.Insert(insert); } }