private void InternalDoCheck(XH_CapitalAccountFreezeSum freezeSum) { int capitalAccountId = freezeSum.CapitalAccountLogo; decimal sum = freezeSum.FreezeCapitalSum; var capMemory = MemoryDataManager.XHCapitalMemoryList.GetByCapitalAccountLogo(capitalAccountId); if (capMemory == null) { return; } var capital = capMemory.Data; if (capital.FreezeCapitalTotal != sum) { decimal oldSum = capital.FreezeCapitalTotal; XH_CapitalAccountTable_DeltaInfo deltaInfo = new XH_CapitalAccountTable_DeltaInfo(); deltaInfo.CapitalAccountLogo = capitalAccountId; deltaInfo.FreezeCapitalTotalDelta = sum - oldSum; capMemory.AddDelta(deltaInfo); string format2 = "XHReckonUnitManager.DoCapitalFreezeCheck修正现货总冻结资金[初始总冻结资金={0},实际总冻结资金={1},资金账户ID={2}]"; string desc = string.Format(format2, oldSum, sum, capitalAccountId); LogHelper.WriteDebug(desc); } }
public static void Target3(object obj) { TableObj <XHCapitalMemoryTable> tableObj = obj as TableObj <XHCapitalMemoryTable>; if (tableObj == null) { return; } //ShowMessage(0, tableObj.val, 0); int val = tableObj.val; var table = tableObj.Table;//.AddDelta(val, val, val, val); XH_CapitalAccountTable_DeltaInfo delta = new XH_CapitalAccountTable_DeltaInfo(); delta.AvailableCapitalDelta = -val; bool isSuccess = false; bool isCapitalSuccess = false; Database database = DatabaseFactory.CreateDatabase(); using (DbConnection connection = database.CreateConnection()) { connection.Open(); DbTransaction transaction = connection.BeginTransaction(); try { isCapitalSuccess = table.CheckAndAddDelta(func, delta, database, transaction); if (!isCapitalSuccess) { Console.WriteLine("----------capitalfailure"); throw new Exception(); } if (val / 2 != 0) { throw new Exception(); } transaction.Commit(); isSuccess = true; } catch (Exception) { transaction.Rollback(); isSuccess = false; Console.WriteLine("事务失败!"); } } if (!isSuccess) { if (isCapitalSuccess) { table.RollBackMemory(delta); Console.WriteLine("rollback data=" + table.Data.AvailableCapital); } } }
/// <summary> /// 增加一条数据 /// </summary> public int Add(XH_CapitalAccountTable_DeltaInfo model, Database db, DbTransaction transaction) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into XH_CapitalAccountTable_Delta("); strSql.Append("CapitalAccountLogo,AvailableCapitalDelta,FreezeCapitalTotalDelta,TodayOutInCapital,HasDoneProfitLossTotalDelta,DeltaTime)"); strSql.Append(" values ("); strSql.Append("@CapitalAccountLogo,@AvailableCapitalDelta,@FreezeCapitalTotalDelta,@TodayOutInCapital,@HasDoneProfitLossTotalDelta,@DeltaTime)"); strSql.Append(";select @@IDENTITY"); //Database db = DatabaseFactory.CreateDatabase(); DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()); db.AddInParameter(dbCommand, "CapitalAccountLogo", DbType.Int32, model.CapitalAccountLogo); db.AddInParameter(dbCommand, "AvailableCapitalDelta", DbType.Decimal, model.AvailableCapitalDelta); db.AddInParameter(dbCommand, "FreezeCapitalTotalDelta", DbType.Decimal, model.FreezeCapitalTotalDelta); db.AddInParameter(dbCommand, "TodayOutInCapital", DbType.Decimal, model.TodayOutInCapital); db.AddInParameter(dbCommand, "HasDoneProfitLossTotalDelta", DbType.Decimal, model.HasDoneProfitLossTotalDelta); db.AddInParameter(dbCommand, "DeltaTime", DbType.DateTime, model.DeltaTime); int result; object obj = db.ExecuteScalar(dbCommand, transaction); if (!int.TryParse(obj.ToString(), out result)) { return(0); } return(result); }
public static void Target2(object obj) { TableObj <XHCapitalMemoryTable> tableObj = obj as TableObj <XHCapitalMemoryTable>; if (tableObj == null) { return; } //ShowMessage(0, tableObj.val, 0); int val = tableObj.val; var table = tableObj.Table;//.AddDelta(val, val, val, val); XH_CapitalAccountTable_DeltaInfo delta = new XH_CapitalAccountTable_DeltaInfo(); delta.AvailableCapitalDelta = -val; if (table.Data.AvailableCapital - val > 0) { table.AddDelta(delta); } else { Console.WriteLine("-"); } }
private static bool func(XH_CapitalAccountTableInfo baseData, XH_CapitalAccountTable_DeltaInfo delta) { Console.WriteLine("base:" + baseData.AvailableCapital + " delta:" + delta.AvailableCapitalDelta); if (baseData.AvailableCapital < 0) { return(false); } return(baseData.AvailableCapital + delta.AvailableCapitalDelta >= 0); }
/// <summary> /// 写现货资金增量调试信息 /// </summary> public static void WriteXHCapitalInfo(XH_CapitalAccountTable_DeltaInfo deltaInfo) { string format = "现货capitalAccountLogo={0},可用资金增量={1},冻结资金增量={2},当日出入金增量={3},已实现盈亏增量={4}"; string msg = string.Format(format, deltaInfo.CapitalAccountLogo, deltaInfo.AvailableCapitalDelta, deltaInfo.FreezeCapitalTotalDelta, deltaInfo.TodayOutInCapital, deltaInfo.HasDoneProfitLossTotalDelta); capitalLog.Debug(msg); }
private bool CapitalCheck(XH_CapitalAccountTableInfo capital, XH_CapitalAccountTable_DeltaInfo change) { //只有买时才检查 if (Request.BuySell == GTA.VTS.Common.CommonObject.Types.TransactionDirection.Buying) { if (capital.AvailableCapital <= 0) { return(false); } return(capital.AvailableCapital + change.AvailableCapitalDelta >= 0); } //卖时只要成交额大于费用即可,前面已经检查过,此处不再检查 return(true); }
/// <summary> /// 对象实体绑定数据 /// </summary> public XH_CapitalAccountTable_DeltaInfo ReaderBind2(IDataReader dataReader) { XH_CapitalAccountTable_DeltaInfo model = new XH_CapitalAccountTable_DeltaInfo(); object ojb; //ojb = dataReader["ID"]; //if (ojb != null && ojb != DBNull.Value) //{ // model.ID = (int)ojb; //} ojb = dataReader["CapitalAccountLogo"]; if (ojb != null && ojb != DBNull.Value) { model.CapitalAccountLogo = (int)ojb; } ojb = dataReader["AvailableCapitalDelta"]; if (ojb != null && ojb != DBNull.Value) { model.AvailableCapitalDelta = (decimal)ojb; } ojb = dataReader["FreezeCapitalTotalDelta"]; if (ojb != null && ojb != DBNull.Value) { model.FreezeCapitalTotalDelta = (decimal)ojb; } ojb = dataReader["TodayOutInCapital"]; if (ojb != null && ojb != DBNull.Value) { model.TodayOutInCapital = (decimal)ojb; } ojb = dataReader["HasDoneProfitLossTotalDelta"]; if (ojb != null && ojb != DBNull.Value) { model.HasDoneProfitLossTotalDelta = (decimal)ojb; } //ojb = dataReader["DeltaTime"]; //if (ojb != null && ojb != DBNull.Value) //{ // model.DeltaTime = (DateTime)ojb; //} return(model); }
/*/// <summary> * /// 更新一条数据 * /// </summary> * public void Update(XH_CapitalAccountTable_DeltaInfo model) * { * StringBuilder strSql = new StringBuilder(); * strSql.Append("update XH_CapitalAccountTable_Delta set "); * strSql.Append("AvailableCapitalDelta=@AvailableCapitalDelta,"); * strSql.Append("FreezeCapitalTotalDelta=@FreezeCapitalTotalDelta,"); * strSql.Append("HasDoneProfitLossTotalDelta=@HasDoneProfitLossTotalDelta,"); * strSql.Append("DeltaTime=@DeltaTime"); * strSql.Append(" where ID=@ID "); * Database db = DatabaseFactory.CreateDatabase(); * DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()); * db.AddInParameter(dbCommand, "ID", DbType.Int32, model.ID); * db.AddInParameter(dbCommand, "AvailableCapitalDelta", DbType.Decimal, model.AvailableCapitalDelta); * db.AddInParameter(dbCommand, "FreezeCapitalTotalDelta", DbType.Decimal, model.FreezeCapitalTotalDelta); * db.AddInParameter(dbCommand, "HasDoneProfitLossTotalDelta", DbType.Decimal, * model.HasDoneProfitLossTotalDelta); * db.AddInParameter(dbCommand, "DeltaTime", DbType.DateTime, model.DeltaTime); * db.ExecuteNonQuery(dbCommand); * } * * /// <summary> * /// 删除一条数据 * /// </summary> * public void Delete(int ID) * { * StringBuilder strSql = new StringBuilder(); * strSql.Append("delete from XH_CapitalAccountTable_Delta "); * strSql.Append(" where ID=@ID "); * Database db = DatabaseFactory.CreateDatabase(); * DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()); * db.AddInParameter(dbCommand, "ID", DbType.Int32, ID); * db.ExecuteNonQuery(dbCommand); * } */ /// <summary> /// 得到一个对象实体 /// </summary> public XH_CapitalAccountTable_DeltaInfo GetModel(int ID) { StringBuilder strSql = new StringBuilder(); strSql.Append( "select ID,CapitalAccountLogo,AvailableCapitalDelta,FreezeCapitalTotalDelta,TodayOutInCapital,HasDoneProfitLossTotalDelta,DeltaTime from XH_CapitalAccountTable_Delta "); strSql.Append(" where ID=@ID "); Database db = DatabaseFactory.CreateDatabase(); DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()); db.AddInParameter(dbCommand, "ID", DbType.Int32, ID); XH_CapitalAccountTable_DeltaInfo model = null; using (IDataReader dataReader = db.ExecuteReader(dbCommand)) { if (dataReader.Read()) { model = ReaderBind(dataReader); } } return(model); }
/// <summary> /// 得到某个资金帐号的汇总增量 /// </summary> public XH_CapitalAccountTable_DeltaInfo GetSum(int capitalAccountLogo) { StringBuilder strSql = new StringBuilder(); strSql.Append( "select CapitalAccountLogo,sum(AvailableCapitalDelta) AvailableCapitalDelta,sum(FreezeCapitalTotalDelta) FreezeCapitalTotalDelta,sum(TodayOutInCapital) TodayOutInCapital,sum(HasDoneProfitLossTotalDelta) HasDoneProfitLossTotalDelta from XH_CapitalAccountTable_Delta "); strSql.Append(" where CapitalAccountLogo=@CapitalAccountLogo "); Database db = DatabaseFactory.CreateDatabase(); DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString()); db.AddInParameter(dbCommand, "CapitalAccountLogo", DbType.Int32, capitalAccountLogo); XH_CapitalAccountTable_DeltaInfo model = null; using (IDataReader dataReader = db.ExecuteReader(dbCommand)) { if (dataReader.Read()) { model = ReaderBind2(dataReader); } } return(model); }
/// <summary> /// 检验并持久化柜台委托单 /// </summary> /// <param name="strMessage">错误消息</param> /// <param name="outEntity">柜台持久化后的对象</param> /// <returns>是否成功</returns> private bool PersistentOrder(ref XH_TodayEntrustTableInfo outEntity, ref string strMessage) { #region 初始化参数 //取代码对应品种的交易币种 if (!GetCurrencyType()) { strMessage = "GT-2226:[现货委托持久化]无法获取交易币种"; return(false); } if (CurrencyType == -1) { strMessage = "GT-2226:[现货委托持久化]无法获取交易币种"; return(false); } //预成交金额 decimal predealCapital = 0; //预成交费用 decimal predealCost = 0; //预成交总金额(根据买卖的不同,不一定=predealCapital+predealCost) decimal preDealSum = 0; //资金及持仓帐户 string strHoldingAccount; string strCapitalAccount; //依据交易员及委托信息取对应资金及持仓帐户 if (!CheckAccount(out strCapitalAccount, out strHoldingAccount, out strMessage)) { return(false); } GetAccountId(strCapitalAccount, strHoldingAccount); if (CapitalAccountId == -1) { strMessage = "GT-2227:[现货委托持久化]无法获取资金帐号ID"; return(false); } #endregion #region 检查 try { //计算预成交金额和预成交费用 if (!this.PO_ComputePreCapital(ref strMessage, ref predealCapital, ref predealCost)) { PO_ValidateFailureProcess(ref strMessage); return(false); } //四舍五入 predealCapital = Utils.Round(predealCapital); predealCost = Utils.Round(predealCost); //1.资金检查 if ( !this.PO_CapitalValidate(predealCost, predealCapital, ref strMessage)) { PO_ValidateFailureProcess(ref strMessage); return(false); } //2.持仓检查 if (!PO_HoldValidate(ref strMessage)) { PO_ValidateFailureProcess(ref strMessage); return(false); } } catch (Exception ex) { strMessage = ex.Message; PO_ValidateFailureProcess(ref strMessage); return(false); } #endregion #region 创建委托(处理失败时要删除此条委托) try { EntrustNumber = XHCommonLogic.BuildXhOrder(ref outEntity, Request, HoldingAccount, CapitalAccount, CurrencyType, ref strMessage); } catch (Exception ex) { DeleteEntrust(EntrustNumber); LogHelper.WriteError(ex.Message, ex); strMessage = "GT-2225:[现货委托持久化]无法创建委托"; LogHelper.WriteDebug(strMessage + ":" + outEntity); return(false); } #endregion #region Persist流程 //persistent流程 //1.资金处理,可用资金减去预成交总金额,总冻结资金加上预成交总金额 //2.冻结资金处理,生成一条冻结记录 //3.持仓处理:只有卖出时才会有持仓处理,可用持仓减少委托量,总冻结持仓增加委托量 //4.持仓冻结处理:只有卖出时才会有持仓冻结处理,生成一条冻结记录 //实际处理: //2.4放在一个事务中进行,当成功后再进行1.3的处理 bool isSuccess = false; #region 资金预处理 var caMemory = MemoryDataManager.XHCapitalMemoryList.GetByCapitalAccountLogo(CapitalAccountId); if (caMemory == null) { strMessage = "GT-2211:[现货委托持久化]资金帐户不存在:" + CapitalAccount; return(false); } if (Request.BuySell == GTA.VTS.Common.CommonObject.Types.TransactionDirection.Buying) { //预成交总金额 = 预成交金额 + 预成交费用 preDealSum = predealCapital + predealCost; } else { //预成交总金额 = 预成交费用(卖不需要加金额) preDealSum = predealCost; } XH_CapitalAccountTable_DeltaInfo capitalDelta = new XH_CapitalAccountTable_DeltaInfo(); capitalDelta.CapitalAccountLogo = caMemory.Data.CapitalAccountLogo; capitalDelta.AvailableCapitalDelta = -preDealSum; capitalDelta.FreezeCapitalTotalDelta = preDealSum; //return caMemory.AddDelta(-preDealCapitalAmount, preDealCapitalAmount, 0, 0); //return caMemory.AddDelta(capitalDelta); #endregion #region 持仓预处理(买入不处理) decimal orderAmount = Convert.ToDecimal(Request.OrderAmount); XHHoldMemoryTable ahtMemory = null; XH_AccountHoldTableInfo_Delta holdDelta = null; if (Request.BuySell == GTA.VTS.Common.CommonObject.Types.TransactionDirection.Selling) { ahtMemory = MemoryDataManager.XHHoldMemoryList.GetByAccountHoldLogoId(HoldingAccountId); if (ahtMemory == null) { ahtMemory = XHCommonLogic.GetHoldMemoryTable(HoldingAccount, Code, CurrencyType); } if (ahtMemory == null) { strMessage = "GT-2212:[现货委托持久化]持仓帐户不存在:" + HoldingAccount; return(false); } holdDelta = new XH_AccountHoldTableInfo_Delta(); var holdData = ahtMemory.Data; holdDelta.AccountHoldLogoId = holdData.AccountHoldLogoId; holdDelta.AvailableAmountDelta = -orderAmount; holdDelta.FreezeAmountDelta = orderAmount; holdDelta.Data = holdData; } #endregion #region 数据库提交动作 bool isCapitalSuccess = false; bool isHoldingSuccess = false; Database database = DatabaseFactory.CreateDatabase(); try { using (DbConnection connection = database.CreateConnection()) { connection.Open(); DbTransaction transaction = connection.BeginTransaction(); try { ReckoningTransaction tm = new ReckoningTransaction(); tm.Database = database; tm.Transaction = transaction; //1.资金处理 //首先提交资金到数据库 isCapitalSuccess = caMemory.CheckAndAddDelta(CapitalCheck, capitalDelta, database, transaction); if (!isCapitalSuccess) { strMessage = "GT-2218:[现货委托持久化]资金检查,无足够可用资金"; throw new CheckException("XHCapitalMemoryTable.CheckAddAddDelta失败"); } //2.冻结资金处理,生成一条冻结记录 PO_BuildCapitalFreezeRecord(predealCapital, predealCost, Request.BuySell, tm); //3.持仓冻结处理:只有卖出是才会有持仓处理 if (Request.BuySell == GTA.VTS.Common.CommonObject.Types.TransactionDirection.Selling) { //持仓冻结处理,生成一条冻结记录 PO_BuildHoldFreezeRecord(tm); } //4.持仓处理 if (Request.BuySell == GTA.VTS.Common.CommonObject.Types.TransactionDirection.Selling) { isHoldingSuccess = ahtMemory.CheckAndAddDelta(HodingCheck, holdDelta, tm.Database, tm.Transaction); if (!isHoldingSuccess) { strMessage = holdMessage; holdMessage = ""; throw new CheckException("XHHoldingMemoryTable.CheckAddAddDelta失败"); } } transaction.Commit(); isSuccess = true; } catch (Exception ex) { transaction.Rollback(); LogHelper.WriteError(ex.Message, ex); if (!(ex is CheckException)) { strMessage = "GT-2213:[现货委托持久化]持久化失败,无法提交到数据库"; } isSuccess = false; } } } catch (Exception ex) { strMessage = "GT-2214:[现货委托持久化]持久化失败"; LogHelper.WriteError(ex.Message, ex); } //事务失败 if (!isSuccess) { DeleteEntrust(EntrustNumber); if (isCapitalSuccess) { caMemory.RollBackMemory(capitalDelta); } if (Request.BuySell == GTA.VTS.Common.CommonObject.Types.TransactionDirection.Selling) { if (isHoldingSuccess) { ahtMemory.RollBackMemory(holdDelta); } } return(false); } //if(isSuccess) //{ // //当提交资金、持仓 到数据库成功后,同步到内存 // caMemory.AddDeltaToMemory(capitalDelta); // if (Request.BuySell == CommonObject.Types.TransactionDirection.Selling) // { // ahtMemory.AddDeltaToMemory(holdDelta); // } //} //else //{ // DeleteEntrust(EntrustNumber); // return false; //} #endregion #endregion return(true); }
/// <summary> /// 一个资金id多次插入 /// </summary> private static void Test1T() { MemoryDataManager.Start(); var table = MemoryDataManager.XHCapitalMemoryList.GetByCapitalAccountLogo(2); var table1 = MemoryDataManager.XHHoldMemoryList.GetByAccountHoldLogoId(4); Console.WriteLine("开始Test1T"); XH_CapitalAccountTable_DeltaInfo delta = new XH_CapitalAccountTable_DeltaInfo(); delta.AvailableCapitalDelta = 1; XH_AccountHoldTableInfo_Delta holdDelta = new XH_AccountHoldTableInfo_Delta(); var data = table1.Data; holdDelta.Data = data; holdDelta.AccountHoldLogoId = data.AccountHoldLogoId; holdDelta.AvailableAmountDelta = 1; holdDelta.FreezeAmountDelta = 1; Database database = DatabaseFactory.CreateDatabase(); using (DbConnection connection = database.CreateConnection()) { connection.Open(); DbTransaction transaction = connection.BeginTransaction(); bool isSuccess = false; try { //table.AddDeltaToDB(delta, database, transaction); //MakeException(); //table.AddDeltaToDB(delta, database, transaction); //table.AddDeltaToDB(delta, database, transaction); //table1.AddDeltaToDB(holdDelta,database,transaction); transaction.Commit(); isSuccess = true; } catch (Exception ex) { transaction.Rollback(); isSuccess = false; Console.WriteLine("Exception:" + ex.Message); } if (isSuccess) { //table.AddDeltaToMemory(delta); //table.AddDeltaToMemory(delta); //table.AddDeltaToMemory(delta); //table1.AddDeltaToMemory(holdDelta); } } Console.WriteLine(table.Data.AvailableCapital); Console.WriteLine(table1.Data.AvailableAmount); ExitConsole(); MemoryDataManager.End(); }