private TimeSpan m_nextDayRange = new TimeSpan(18, 0, 0); // 下一交易日的时间分界点 #endregion // member #region methods /// <summary> /// 启动结算价下载。 /// </summary> public int Start() { //确定更新结算价成功之后,需要再次计算资金沉淀,盘中的时候用的是最新价计算,有结算价之后,就改为结算价重新刷新一遍计算 //计算公式:资金沉淀=品种持仓量*结算价*合约规模*交易所多头保证金比例 //计算资金总沉淀(盘中就用最新价计算资金沉淀,待下午结算价出来之后,按照结算价再更新一次) //int perSharesContract = GetInstrumentPerSharesContract(marketData.Instrument.InstrumentCode);//合约规模 //decimal exchangeLongMarginRatio = GetExchangeLongMarginRatio(marketData.Instrument.InstrumentCode);//保证金 //m_kLine.SendimentaryMoney = marketData.OpenInterest * marketData.LastPrice * perSharesContract * exchangeLongMarginRatio;//资金沉淀 //取出UseinstrumentDetail品种合约信息取出合约规模和保证金额比例,再次执行SQL语句 try { m_eventLogger = AppLogger.InitInstance(); } catch { m_eventLogger = new NullLogger("DownloadProcessor_DefaultLogger"); } m_eventLogger.LineFeed(); string text = "开始下载结算价服务"; m_eventLogger.WriteInformation(text); WriteConsoleLog(text); if (ReadConfig() == false) { return(-1); } //下载所有合约详情,失败直接退出因为后面需要基础数据 try { USeTradingInstrumentManager tradingInsManager = CreateInstrumentManager(); m_insDetailList = tradingInsManager.GetAllInstrumentDetails(); Debug.Assert(m_insDetailList != null); } catch (Exception ex) { text = "下载数据库所有合约详情InstrumentDetail失败," + ex.Message; WriteConsoleLog(text); m_eventLogger.WriteError(text); return(-1); } CtpOrderQuerier ctpApp = new CtpOrderQuerier(); try { ctpApp.Connect(m_ctpDriverConfig.Address, m_ctpDriverConfig.Port, m_ctpDriverConfig.LoginTimeOut, m_ctpDriverConfig.QueryTimeOut); text = "连接CTP交易服务器成功"; WriteConsoleLog(text); m_eventLogger.WriteInformation(text); } catch (Exception ex) { text = "连接CTP交易服务器失败," + ex.Message; WriteConsoleLog(text); m_eventLogger.WriteError(text); ctpApp.Disconnect(); return(-1); } try { ctpApp.Login(m_ctpAccountConfig.ID, m_ctpAccountConfig.Password, m_ctpAccountConfig.BrokerID); text = "登陆CTP交易服务器成功"; WriteConsoleLog(text); m_eventLogger.WriteInformation(text); } catch (Exception ex) { text = "登陆CTP交易服务器失败," + ex.Message; WriteConsoleLog(text); m_eventLogger.WriteError(text); ctpApp.Disconnect(); return(-1); } try { List <InstrumentField> instrumentList = ctpApp.QueryInstument(); foreach (InstrumentField item in instrumentList) { if (item.ProductClass != ProductClass.Futures) { continue; } USeInstrumentDetail entity = InsturmentFiledToUSeInstrumentDetail(item); instrumentDic.Add(entity.Instrument.InstrumentCode, entity); } text = string.Format("查询期货合约数据完成,共计{0}个合约", instrumentDic.Count); WriteConsoleLog(text); m_eventLogger.WriteInformation(text); } catch (Exception ex) { text = "查询期货合约数据失败," + ex.Message; WriteConsoleLog(text); m_eventLogger.WriteError(text); ctpApp.Disconnect(); return(-1); } DateTime tradingDate = GetTradingDay(); if (m_refrashDate == QueryDay.Today) { int queryNum = m_queryNum; while (queryNum > 0) { try { List <DepthMarketDataField> tempDepthMarketDataFieldList = ctpApp.QueryDepthMarketData(); Debug.Assert(tempDepthMarketDataFieldList != null); foreach (DepthMarketDataField marketData in tempDepthMarketDataFieldList) { if (marketData.SettlementPrice <= 0d || marketData.SettlementPrice == double.MaxValue) { continue; } if (instrumentDic.ContainsKey(marketData.InstrumentID)) { //[hanyu]未防返回的MarketData没有ExchangeID,改用查询回来的合约交易所信息 USeMarket market = instrumentDic[marketData.InstrumentID].Instrument.Market; SaveInstumentsSettlementPriceToDB(marketData, marketData.InstrumentID, market, tradingDate, marketData.SettlementPrice); //保存完一笔的同时刷新一次资金沉淀[暂时不在这里处理,改用直接在特定时间执行SQL语句,因为涉及到8888指数和9999主力合约也需要刷新资金沉淀的问题] //RefreashSendimentaryMoney(marketData, marketData.InstrumentID, market, tradingDate,marketData.SettlementPrice); instrumentDic.Remove(marketData.InstrumentID); USeConsole.WriteLine(string.Format("保存{0}成功", marketData.InstrumentID)); } } if (instrumentDic.Count <= 0) { //所有合约存储完毕,退出 break; } queryNum--; if (queryNum > 0) { Thread.Sleep(m_queryFrequence); } } catch (Exception ex) { text = string.Format("查询,保存当日结算价异常:{0}", ex.Message); WriteConsoleLog(text); m_eventLogger.WriteInformation(text); ctpApp.Disconnect(); return(-1); } } } else { try { List <DepthMarketDataField> tempDepthMarketDataFieldList = ctpApp.QueryDepthMarketData(); Debug.Assert(tempDepthMarketDataFieldList != null); foreach (DepthMarketDataField marketData in tempDepthMarketDataFieldList) { if (marketData.PreSettlementPrice <= 0d || marketData.PreSettlementPrice == double.MaxValue) { continue; } if (instrumentDic.ContainsKey(marketData.InstrumentID)) { //[hanyu]未防返回的MarketData没有ExchangeID,改用查询回来的合约交易所信息 USeMarket market = instrumentDic[marketData.InstrumentID].Instrument.Market; //保存结算价 SaveInstumentsSettlementPriceToDB(marketData, marketData.InstrumentID, market, tradingDate, marketData.PreSettlementPrice); //RefreashSendimentaryMoney(marketData, marketData.InstrumentID, market, tradingDate,marketData.PreSettlementPrice); instrumentDic.Remove(marketData.InstrumentID); USeConsole.WriteLine(string.Format("保存{0}成功", marketData.InstrumentID)); } } } catch (Exception ex) { text = string.Format("保存昨日结算价异常:{0}", ex.Message); WriteConsoleLog(text); m_eventLogger.WriteInformation(text); ctpApp.Disconnect(); return(-1); } } if (ctpApp != null) { ctpApp.Disconnect(); } if (instrumentDic.Count > 0) { //未查询到的合约写入文件 foreach (USeInstrumentDetail field in instrumentDic.Values) { text = string.Format("[{0}]结算价查询失败", field.Instrument.InstrumentCode); WriteConsoleLog(text); m_eventLogger.WriteInformation(text); } } else { try { #if DEBUG bool iDownLoadResult = VerifyDBSettlementPrice(tradingDate); Debug.Assert(iDownLoadResult == true); #endif RefreshDBSettlementDownLoad(tradingDate, true); } catch (Exception ex) { text = string.Format("更新结算状态失败,错误:{0}", ex.Message); WriteConsoleLog(text); m_eventLogger.WriteInformation(text); } } return(0); }
/// <summary> /// 启动午盘休盘价下载。 /// </summary> public int Start() { try { m_eventLogger = AppLogger.InitInstance(); } catch { m_eventLogger = new NullLogger("DownloadProcessor_DefaultLogger"); } m_eventLogger.LineFeed(); string text = "开始下载午盘收盘价服务"; m_eventLogger.WriteInformation(text); USeConsole.WriteLine(text); if (ReadConfig() == false) { return(-1); } CtpOrderQuerier ctpApp = new CtpOrderQuerier(); try { ctpApp.Connect(m_ctpDriverConfig.Address, m_ctpDriverConfig.Port, m_ctpDriverConfig.LoginTimeOut, m_ctpDriverConfig.QueryTimeOut); text = "连接CTP交易服务器成功"; USeConsole.WriteLine(text); m_eventLogger.WriteInformation(text); } catch (Exception ex) { text = "连接CTP交易服务器失败," + ex.Message; USeConsole.WriteLine(text); m_eventLogger.WriteError(text); ctpApp.Disconnect(); return(-1); } try { ctpApp.Login(m_ctpAccountConfig.ID, m_ctpAccountConfig.Password, m_ctpAccountConfig.BrokerID); text = "登陆CTP交易服务器成功"; USeConsole.WriteLine(text); m_eventLogger.WriteInformation(text); } catch (Exception ex) { text = "登陆CTP交易服务器失败," + ex.Message; USeConsole.WriteLine(text); m_eventLogger.WriteError(text); ctpApp.Disconnect(); return(-1); } try { List <InstrumentField> instrumentList = ctpApp.QueryInstument(); foreach (InstrumentField item in instrumentList) { if (item.ProductClass != ProductClass.Futures) { continue; } USeInstrumentDetail entity = InsturmentFiledToUSeInstrumentDetail(item); instrumentDic.Add(entity.Instrument.InstrumentCode, entity); } text = string.Format("查询期货合约数据完成,共计{0}个合约", instrumentDic.Count); USeConsole.WriteLine(text); m_eventLogger.WriteInformation(text); } catch (Exception ex) { text = "查询期货合约数据失败," + ex.Message; USeConsole.WriteLine(text); m_eventLogger.WriteError(text); ctpApp.Disconnect(); return(-1); } List <DepthMarketDataField> depthMarketDataFieldList = new List <DepthMarketDataField>(); depthMarketDataFieldList = ctpApp.QueryDepthMarketData(); //while (true) //{ // depthMarketDataFieldList = ctpApp.QueryDepthMarketData(); // //返回大于下午开盘的行情时间 // if (VerfiyIsNoonBeginTime(depthMarketDataFieldList) == true) // { // text = string.Format("[{0}]行情已经进入下午开盘时间不在午盘时间内", DateTime.Now); // USeConsole.WriteLine(text); // m_eventLogger.WriteError(text); // ctpApp.Disconnect(); // return -1; // } // //未找到大于11:30:00。 // if (VerfiyIsNoonEndTime(depthMarketDataFieldList) == false) // { // Thread.Sleep(m_queryFrequence); // continue; // } // else // { // break; // } //} ctpApp.Disconnect(); try { foreach (DepthMarketDataField marketData in depthMarketDataFieldList) { if (instrumentDic.ContainsKey(marketData.InstrumentID)) { USeMarket market = instrumentDic[marketData.InstrumentID].Instrument.Market; //保存收盘价 SaveClosePriceToDB(marketData, marketData.InstrumentID, market, DateTime.Today, marketData.LastPrice); instrumentDic.Remove(marketData.InstrumentID); USeConsole.WriteLine(string.Format("保存{0}成功", marketData.InstrumentID)); } } RefrashPriceCloseDownLoad(DateTime.Today, 1); } catch (Exception ex) { text = string.Format("查询,保存当日午盘数据异常:{0}", ex.Message); USeConsole.WriteLine(text); m_eventLogger.WriteInformation(text); return(-1); } return(0); }