/// <summary> /// 获取持仓内存表,如果内存中不存在,那么再到数据库查找 /// </summary> /// <param name="accountHoldLogoId">持仓id</param> /// <returns></returns> public static QHHoldMemoryTable GetHoldMemoryTable(int accountHoldLogoId) { QHHoldMemoryTable holdMemory = null; try { holdMemory = MemoryDataManager.QHHoldMemoryList.GetByAccountHoldLogoId(accountHoldLogoId); //如果持仓为空,那么先从数据库加载,看是不是内存表没有加载 if (holdMemory == null) { QH_HoldAccountTableDal dal = new QH_HoldAccountTableDal(); var hold = dal.GetModel(accountHoldLogoId); //如果数据库有,那么直接加载到内存表中 if (hold != null) { MemoryDataManager.QHHoldMemoryList.AddQHHoldAccountTableToMemeory(hold); } else { //如果数据库也没有,那么代表无持仓 return(null); } holdMemory = MemoryDataManager.QHHoldMemoryList.GetByAccountHoldLogoId(accountHoldLogoId); } } catch (Exception ex) { LogHelper.WriteError("QHCommonLogic.GetHoldMemoryTable-" + ex.Message, ex); } return(holdMemory); }
/// <summary> /// 带事务更新 /// </summary> /// <param name="baseTable"></param> /// <param name="db"></param> /// <param name="transaction"></param> public void PersistBase(QH_HoldAccountTableInfo baseTable, Database db, DbTransaction transaction) { QH_HoldAccountTableDal dal = new QH_HoldAccountTableDal(); ReckoningTransaction tm = new ReckoningTransaction(); tm.Database = db; tm.Transaction = transaction; dal.Update(baseTable, tm); }
/// <summary> /// 获取指定Id、币种的持仓账户 /// </summary> /// <param name="id"></param> /// <param name="currencyType"></param> /// <returns></returns> private List <QH_HoldAccountTableInfo> GetHoldAccountTableList(string id, int?currencyType) { QH_HoldAccountTableDal dal = new QH_HoldAccountTableDal(); string where = string.Format("UserAccountDistributeLogo = '{0}' AND TradeCurrencyType= '{1}'", id, currencyType); // return DataRepository.QhHoldAccountTableProvider.Find(where); return(dal.GetListArray(where)); }
/// <summary> /// 带事务更新 /// </summary> /// <param name="delta"></param> /// <param name="db"></param> /// <param name="transaction"></param> public void PersistChangeWithTransaction(QH_HoldAccountTableInfo_Delta delta, Database db, DbTransaction transaction) { QH_HoldAccountTableDal dal = new QH_HoldAccountTableDal(); ReckoningTransaction tm = new ReckoningTransaction(); tm.Database = db; tm.Transaction = transaction; dal.AddUpdate(delta, tm); }
/// <summary> /// 用于故障恢复清算 /// </summary> /// <param name="recktime">要清算的日期</param> public static void DoFaultRecoveryClose(string recktime) { //add 2010-03-16 //2.可以清算时,先初始化所有的今日结算价 //3.检查当前持仓表中所有持仓合约都能获取得到结算价时再执行清算 //2. MCService.IniFutureTodayPreSettlementPriceRecovery(); //3. QH_HoldAccountTableDal dal = new QH_HoldAccountTableDal(); List <QH_HoldAccountTableInfo> models = dal.GetAllListArray(); decimal price; foreach (var item in models) { //如果持仓量有就要获取,如果都已经没有了持仓量证明已经清算完毕不再再清算 if (item.HistoryFreezeAmount + item.HistoryHoldAmount + item.TodayFreezeAmount + item.TodayHoldAmount > 0) { if (!MCService.GetFutureTodayPreSettlementPriceByCache(item.Contract, out price)) { LogHelper.WriteError("故障恢复期货清算获取今日结算价无法获取:" + item.Contract, new Exception("清算中止")); return; } } } //把是否正在做故障恢复清算状态修改回来为true,正在进行 ScheduleManager.IsFaultRecoveryFutureReckoning = true; ScheduleManager.CurrentFaultRecoveryFutureReckoningDate = DateTime.Parse(recktime); InternalDoClose(); //现货也设置清算完成,为了后面能提交现货的相关数据内存表中的数据 ScheduleManager.HasDoneStockReckoning = true; ScheduleManager.HasDoneFutureReckoning = true; ScheduleManager.ReckoningDoneNotify(); //证明已经故障恢复清算已经成功那么更新清算记录表记录 if (ScheduleManager.IsFutureReckoningErrorStopTrade == false) { //故障恢复清算完成修改清算日期标志,因为内部之前 的方法直接修改为当前的了,这里要修改回当招提交的数据日期 recktime = DateTime.Parse(recktime).ToShortDateString() + " " + DateTime.Now.ToString("HH:mm:ss"); StatusTableChecker.UpdateFutureReckoningDate(recktime, null); } //清空当前所有的缓存当前清算时添加的缓存数据 MCService.ClearFuterTodayPreSettlemmentPrice(); //把是否正在做故障恢复清算状态修改回来为false ScheduleManager.IsFaultRecoveryFutureReckoning = false; }
/// <summary> /// 检查资金表中的保证金总值是否等于所有持仓的保证金累加 /// </summary> private void DoMarginCheck() { QH_HoldAccountTableDal holdDal = new QH_HoldAccountTableDal(); var list = holdDal.GetMarginSum(); if (Utils.IsNullOrEmpty(list)) { return; } foreach (var hold in list) { //InternalDoMarginCheck(hold); smartPool.QueueWorkItem(InternalDoMarginCheck, hold); } }
/// <summary> /// 插入记录 /// </summary> /// <param name="baseTable"></param> /// <returns></returns> public bool InsertBaseTable(QH_HoldAccountTableInfo baseTable) { QH_HoldAccountTableDal dal = new QH_HoldAccountTableDal(); var hold = dal.GetQhAccountHoldTable(baseTable.UserAccountDistributeLogo, baseTable.Contract, baseTable.TradeCurrencyType, baseTable.BuySellTypeId); if (hold != null) { return(false); } int id = dal.Add(baseTable); baseTable.AccountHoldLogoId = id; return(true); }
/// <summary> /// 获取持仓内存表,如果内存中不存在,那么再到数据库查找 /// </summary> /// <param name="holdAccount">持仓账户</param> /// <param name="code">代码</param> /// <param name="tradeCurrencyType">币种</param> /// <param name="buySellType">买卖方向</param> /// <returns>内存表</returns> public static QHHoldMemoryTable GetHoldMemoryTable(string holdAccount, string code, int tradeCurrencyType, int buySellType) { QHHoldMemoryTable holdMemory = null; try { holdMemory = MemoryDataManager.QHHoldMemoryList.GetByHoldAccountAndCurrencyType(holdAccount, code, buySellType, tradeCurrencyType); //如果持仓为空,那么先从数据库加载,看是不是内存表没有加载 if (holdMemory == null) { QH_HoldAccountTableDal dal = new QH_HoldAccountTableDal(); var hold = dal.GetQhAccountHoldTable(holdAccount, code, tradeCurrencyType, buySellType); //如果数据库有,那么直接加载到内存表中 if (hold != null) { MemoryDataManager.QHHoldMemoryList.AddQHHoldAccountTableToMemeory(hold); } else { //如果数据库也没有,那么代表无持仓 return(null); } holdMemory = MemoryDataManager.QHHoldMemoryList.GetByHoldAccountAndCurrencyType(holdAccount, code, buySellType, tradeCurrencyType); } } catch (Exception ex) { LogHelper.WriteError("QHCommonLogic.GetHoldMemoryTable-" + ex.Message, ex); } return(holdMemory); }
public static void DoClose() { //还没到收市,不处理 if (DateTime.Now.Hour < ScheduleManager.LastEndTime.Hour) { return; } if (DateTime.Now.Hour == ScheduleManager.LastEndTime.Hour) { if (DateTime.Now.Minute < ScheduleManager.LastEndTime.Minute) { return; } } LogHelper.WriteInfo("执行期货收市动作DoClose"); //===update 李健华 2009-12-14 //这里已经在现货清算中提交过,也再得新加载过,在清算完成后会统一再提交一次 //先关闭内存管理器 // MemoryDataManager.End(); //========== MCService.DoMarketCloseJob(); //add 2010-03-16 //1.发现前一日没有清算完成今日也不能清算 (这个不用了,系统自动清算只要把当日或者前一日的清算完成即可,系统只要保证没有清算完成不可下单操作即可) //2.可以清算时,先初始化所有的今日结算价 //3.检查当前持仓表中所有持仓合约都能获取得到结算价时再执行清算 //=============== //1. string errMsg = ""; bool isReck = false; DateTime recktime; isReck = StatusTableChecker.IsFutureTodayReckoning(out recktime, out errMsg); //if (!isReck) //{ // LogHelper.WriteError("今日期货清算不能执行清算,时间获取为:" + recktime, new Exception(errMsg)); // return; //} //2. //3. QH_HoldAccountTableDal dal = new QH_HoldAccountTableDal(); List <QH_HoldAccountTableInfo> models = dal.GetAllListArray(); decimal price; foreach (var item in models) { //如果持仓量有就要获取,如果都已经没有了持仓量证明已经清算完毕不再再清算 if (item.HistoryFreezeAmount + item.HistoryHoldAmount + item.TodayFreezeAmount + item.TodayHoldAmount > 0) { if (!MCService.GetFutureTodayPreSettlementPriceByCache(item.Contract, out price)) { //如果代码还是可以交易的代码则中止清算,已经过期忽略不理,内部清算的时候获取不到会不会修改持仓均价相关的内容 if (!MCService.IsExpireLastedTradeDate(item.Contract)) { //但当代码当日为非交易日时也可以放过 //如果发现前一日没有清算完成今日获取不到价格不能放过 if (!isReck || MCService.CommonPara.IsTradeDate(item.Contract, DateTime.Now)) { //为了防止每日重启程序时时间已经过了收市时间即到了清算时间所作的每次清算而已经清算成功的再设置为不清算成功 //如:20100402的五点时重启程序,这时已经四点半已经清算成功,那些这里再清算而又获取不到行情就会设置错误,而这里应该不设置,放过 //因为内部还会检查是否清算成功过 if (!StatusTableChecker.HasDoneFutureReckoning(DateTime.Now)) { LogHelper.WriteError("今日期货清算获取今日结算价无法获取:" + item.Contract, new Exception("清算中止")); //暂停交易 ScheduleManager.IsFutureReckoningErrorStopTrade = true; return; } } ////如果发现前一日没有清算完成今日获取不到价格不能放过 //else if (!isReck) //{ // LogHelper.WriteError("今日期货清算获取今日结算价无法获取:" + item.Contract, new Exception("前一日清算异常,今日清算中止")); // //暂停交易 // ScheduleManager.IsFutureReckoningErrorStopTrade = true; // return; //} } } } } //清算之前把所有柜台缓存的清算撮合ID的数据清除 QHCounterCache.Instance.ResetDictionary(); InternalDoClose(); ScheduleManager.HasDoneFutureReckoning = true; ScheduleManager.ReckoningDoneNotify(); ////过了收市时间,开始处理,延时5分钟,等现货做完后再做 //timer = new Timer(); //timer.Interval = 1*60*1000; //timer.Elapsed += delegate // { // timer.Enabled = false; // LogHelper.WriteInfo("执行期货收市动作DoClose"); // InternalDoClose(); // InternalDoClose(); // InternalDoClose(); // }; //timer.Enabled = true; //期货清算完成后把资金管理的相关账号清除,释放内存 AccountManager.Instance.Reset(); }
/// <summary> /// 持久化变化量 /// </summary> /// <param name="delta"></param> public void PersistChange(QH_HoldAccountTableInfo_Delta delta) { QH_HoldAccountTableDal dal = new QH_HoldAccountTableDal(); dal.AddUpdate(delta); }
/// <summary> /// 删除 /// </summary> /// <param name="baseTable"></param> public void DeleteBaseTable(QH_HoldAccountTableInfo baseTable) { QH_HoldAccountTableDal dal = new QH_HoldAccountTableDal(); dal.Delete(baseTable.AccountHoldLogoId); }
/// <summary> /// 获取指定id持仓 /// </summary> /// <param name="k"></param> /// <returns></returns> public QH_HoldAccountTableInfo GetBaseTable(int k) { QH_HoldAccountTableDal dal = new QH_HoldAccountTableDal(); return(dal.GetModel(k)); }
/// <summary> /// 获取全部持仓 /// </summary> /// <returns></returns> public List <QH_HoldAccountTableInfo> GetAllBaseTable() { QH_HoldAccountTableDal dal = new QH_HoldAccountTableDal(); return(dal.GetAllListArray()); }
/// <summary> /// 更新 /// </summary> /// <param name="baseTable"></param> public void PersistBase(QH_HoldAccountTableInfo baseTable) { QH_HoldAccountTableDal dal = new QH_HoldAccountTableDal(); dal.Update(baseTable); }
/// <summary> /// 进行持仓检查,对已经过期的合约按昨日结算价进行强制平仓 /// </summary> private void CheckHoldTable() { //LogHelper.WriteDebug("FutureDayChecker.CheckHoldTable开始检查持仓表"); OrderAccepter orderAccepter = OrderAccepterService.Service; QH_HoldAccountTableDal dal = new QH_HoldAccountTableDal(); string where = string.Format("UserAccountDistributeLogo = '{0}'", holdAccount.UserAccountDistributeLogo); List <QH_HoldAccountTableInfo> listCheck = dal.GetListArray(where); // DataRepository.QhHoldAccountTableProvider.GetByUserAccountDistributeLogo( // holdAccount.UserAccountDistributeLogo); if (Utils.IsNullOrEmpty(listCheck)) { return; } decimal price = 0; List <QH_HoldAccountTableInfo> list = new List <QH_HoldAccountTableInfo>(); //这里是要分开处理,已经是过期的合约前面就已经强行平仓掉就没有后面的持仓限制和最小单位整数倍的判断 foreach (var holdTable in listCheck) { //如果不是交易日,不进行强行平仓 add by 董鹏 2010-05-05 if (!CommonParaProxy.GetInstance().IsTradeDate(holdTable.Contract)) { continue; } if (MCService.IsExpireLastedTradeDate(holdTable.Contract)) { //在最后交易日 进行平仓 //string msg = ""; //bool canGetPrice = MCService.GetFutureYesterdayPreSettlementPrice(holdTable.Contract, out price, // ref msg); //if (!canGetPrice) //{ // string format = "FutureDayChecker.CheckHoldTable无法获取合约{0}的昨日收盘结算价,错误信息:{1}"; // string msg2 = string.Format(format, holdTable.Contract, msg); // LogHelper.WriteDebug(msg2); // continue; //} //每天清算后,持仓均价就是结算价 price = holdTable.HoldAveragePrice; #region 账户类型进行商品期货、股指期货平仓 add by 董鹏 2010-02-03 UA_UserAccountAllocationTableDal accDal = new UA_UserAccountAllocationTableDal(); var acc = accDal.GetModel(holdTable.UserAccountDistributeLogo); if (acc.AccountTypeLogo == (int)Types.AccountType.CommodityFuturesHoldCode) { //此处原来只有HistoryHoldAmount,造成未清算成功的持仓平不掉,因此加上TodayHoldAmount; //而TodayFreezeAmount清算是否成功都没有影响 -- update by 董鹏 2010-03-29 //CloseCommoditiesContract(orderAccepter, holdTable, (float)price, (float)(holdTable.HistoryHoldAmount + holdTable.TodayHoldAmount), Types.QHForcedCloseType.Expired); //平历史 CloseCommoditiesContract(orderAccepter, holdTable, (float)price, (float)holdTable.HistoryHoldAmount, Types.QHForcedCloseType.Expired, ReckoningCounter.Entity.Contants.Types.FutureOpenCloseType.ClosePosition); //平今 CloseCommoditiesContract(orderAccepter, holdTable, (float)price, (float)holdTable.TodayHoldAmount, Types.QHForcedCloseType.Expired, ReckoningCounter.Entity.Contants.Types.FutureOpenCloseType.CloseTodayPosition); } if (acc.AccountTypeLogo == (int)Types.AccountType.StockFuturesHoldCode) { CloseStockIndexContract(orderAccepter, holdTable, (float)price, true); } #endregion //CloseStockIndexContract(orderAccepter, holdTable, (float)price, true); } else { list.Add(holdTable); } } #region 商品期货进行持仓限制、最小交割单位整数倍检验,并平仓超出量 add by 董鹏 2010-02-04 if (holdAccount.AccountTypeLogo == (int)Types.AccountType.CommodityFuturesHoldCode) { LogHelper.WriteDebug("---->商品期货持仓限制、最小小交割单位整数倍验证,UserAccountDistributeLogo=" + holdAccount.UserAccountDistributeLogo); List <QH_HoldAccountTableInfo> listCloseContract; price = 0; //记录超出持仓限制的量:<持仓记录主键,超出量> Dictionary <int, decimal> dicAmount; // 超过最大持仓限制 listCloseContract = FindWillClosedContractOverMaxHoldLimit(list, out dicAmount); foreach (var holdTable in listCloseContract) { CloseCommoditiesContract(orderAccepter, holdTable, (float)price, (float)dicAmount[holdTable.AccountHoldLogoId], Types.QHForcedCloseType.OverHoldLimit, ReckoningCounter.Entity.Contants.Types.FutureOpenCloseType.ClosePosition); //进行了持仓限制平仓的不再进行最小交割单位验证,下次持仓检查的时候才进行。 list.Remove(holdTable); } //超出最小交割量整数倍 listCloseContract = FindWillClosedContractNotModMinUnitLimit(list, out dicAmount); foreach (var holdTable in listCloseContract) { CloseCommoditiesContract(orderAccepter, holdTable, (float)price, (float)dicAmount[holdTable.AccountHoldLogoId], Types.QHForcedCloseType.NotModMinUnit, ReckoningCounter.Entity.Contants.Types.FutureOpenCloseType.ClosePosition); } } #endregion //LogHelper.WriteDebug("FutureDayChecker.CheckHoldTable结束检查持仓表"); }