public String AnalysisXml(string ReqXml) { string ResXml = string.Empty; string ReqCode = string.Empty; try { System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument(); xmldoc.LoadXml(ReqXml); //请求指令 ReqCode = xmldoc.SelectSingleNode("JTW91G/MsgData/ReqHeader/ReqCode").InnerText; Trade.CTrade trade = new Trade.CTrade(); MarOrdersLn orderln = new MarOrdersLn(); orderln.Mac = xmldoc.SelectSingleNode("JTW91G/MsgData/ReqHeader/Mac").InnerText; orderln.LoginID = xmldoc.SelectSingleNode("JTW91G/MsgData/DataBody/LoginId").InnerText; orderln.TradeAccount = xmldoc.SelectSingleNode("JTW91G/MsgData/DataBody/TradeAccount").InnerText; orderln.CurrentTime = Convert.ToDateTime(xmldoc.SelectSingleNode("JTW91G/MsgData/DataBody/Orders/Order/CurrentTime").InnerText); //可以不设置止盈止损价 如果为空 不能直接转换 string ProfitPrice = xmldoc.SelectSingleNode("JTW91G/MsgData/DataBody/Orders/Order/ProfitPrice").InnerText; string LossPrice = xmldoc.SelectSingleNode("JTW91G/MsgData/DataBody/Orders/Order/LossPrice").InnerText; orderln.ProfitPrice = Convert.ToDouble(string.IsNullOrEmpty(ProfitPrice) ? "0" : ProfitPrice); orderln.LossPrice = Convert.ToDouble(string.IsNullOrEmpty(LossPrice) ? "0" : LossPrice); string MaxPrice = xmldoc.SelectSingleNode("JTW91G/MsgData/DataBody/Orders/Order/MaxPrice").InnerText; orderln.MaxPrice = Convert.ToDouble(string.IsNullOrEmpty(MaxPrice) ? "0" : MaxPrice); orderln.OrderType = xmldoc.SelectSingleNode("JTW91G/MsgData/DataBody/Orders/Order/OrderType").InnerText; orderln.ProductCode = xmldoc.SelectSingleNode("JTW91G/MsgData/DataBody/Orders/Order/ProductCode").InnerText; orderln.Quantity = Convert.ToDouble(xmldoc.SelectSingleNode("JTW91G/MsgData/DataBody/Orders/Order/Quantity").InnerText); orderln.RtimePrices = Convert.ToDouble(xmldoc.SelectSingleNode("JTW91G/MsgData/DataBody/Orders/Order/RtimePrices").InnerText); // orderln.UserType = 0; //客户端没有传递这个值 内部调用默认赋值0 表示普通用户 orderln.OrderMoney = 0; //客户端没有传递这个值 这个值本身也没有使用 随便赋个值 Marketorders orders = trade.GetMarketorders(orderln); if (!orders.Result) { string CodeDesc = ResCode.UL005Desc; string ReturnCode = GssGetCode.GetCode(orders.ReturnCode,orders.Desc, ref CodeDesc); ResXml = GssResXml.GetResXml(ReqCode, ReturnCode, CodeDesc, string.Format("<DataBody></DataBody>")); } else { StringBuilder strb = new StringBuilder(); strb.Append("<Order>"); strb.AppendFormat("<OrderId>{0}</OrderId>", orders.TradeOrder.OrderId); strb.AppendFormat("<ProductName>{0}</ProductName>", orders.TradeOrder.ProductName); strb.AppendFormat("<ProductCode>{0}</ProductCode>", orders.TradeOrder.ProductCode); strb.AppendFormat("<PriceCode>{0}</PriceCode>", orders.TradeOrder.PriceCode); strb.AppendFormat("<OrderPrice>{0}</OrderPrice>", orders.TradeOrder.OrderPrice); strb.AppendFormat("<Quantity>{0}</Quantity>", orders.TradeOrder.Quantity); strb.AppendFormat("<UseQuantity>{0}</UseQuantity>", orders.TradeOrder.UseQuantity); strb.AppendFormat("<OccMoney>{0}</OccMoney>", orders.TradeOrder.OccMoney); strb.AppendFormat("<LossPrice>{0}</LossPrice>", orders.TradeOrder.LossPrice); strb.AppendFormat("<ProfitPrice>{0}</ProfitPrice>", orders.TradeOrder.ProfitPrice); strb.AppendFormat("<OrderType>{0}</OrderType>", orders.TradeOrder.OrderType); strb.AppendFormat("<OrderTime>{0}</OrderTime>", orders.TradeOrder.OrderTime.ToString(Const.dateformat)); strb.AppendFormat("<TradeFee>{0}</TradeFee>", orders.TradeOrder.TradeFee); strb.AppendFormat("<StorageFee>{0}</StorageFee>", orders.TradeOrder.StorageFee); strb.AppendFormat("<TotalWeight>{0}</TotalWeight>", orders.TradeOrder.TotalWeight); strb.Append("</Order>"); StringBuilder fundinfo = new StringBuilder(); fundinfo.Append("<FundInfo>"); fundinfo.AppendFormat("<Money>{0}</Money>", orders.MoneyInventory.FdInfo.Money); fundinfo.AppendFormat("<OccMoney>{0}</OccMoney>", orders.MoneyInventory.FdInfo.OccMoney); fundinfo.AppendFormat("<FrozenMoney>{0}</FrozenMoney>", orders.MoneyInventory.FdInfo.FrozenMoney); fundinfo.Append("</FundInfo>"); //响应消息体 string DataBody = string.Format("<DataBody><Orders>{0}</Orders>{1}</DataBody>", strb.ToString(), fundinfo.ToString()); ResXml = GssResXml.GetResXml(ReqCode, ResCode.UL004, ResCode.UL004Desc, DataBody); } } catch (Exception ex) { com.individual.helper.LogNet4.WriteErr(ex); //业务处理失败 ResXml = GssResXml.GetResXml(ReqCode, ResCode.UL005, ResCode.UL005Desc, string.Format("<DataBody></DataBody>")); } return ResXml; }
/// <summary> /// 市价单下单 /// </summary> /// <param name="marorderln">下单信息</param> /// <returns>下单结果</returns> public Marketorders GetMarketorders(MarOrdersLn marorderln) { Marketorders marketorders = new Marketorders(); marketorders.MoneyInventory = new MoneyInventory(); marketorders.TradeOrder = new TradeOrder(); ProductConfig ptc = new ProductConfig(); TradeUser TdUser = new TradeUser(); DateTime dt = DateTime.Now;//服务器当前本地时间 double _occMoney = 0;//占用资金 double realprice = 0;//服务器端实时价格(即此时此刻的建仓价) double _tradefee = 0;//工费 double fxrate = 0; string orderid = string.Empty;//市价单ID string userId = string.Empty; string allowStore = "1";//是否允许入库 "1"允许入库 "0" 不允许入库 string operUser = marorderln.TradeAccount; string ipmac = string.Empty; int operUserType = 0; try { #region 判断用户登陆标识是否过期 if (!ComFunction.ExistUserLoginID(marorderln.LoginID, ref TdUser)) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL003; marketorders.Desc = ResCode.UL003Desc; return marketorders; } operUser = TdUser.Account; ipmac = ComFunction.GetIpMac(TdUser.Ip, TdUser.Mac); operUserType = (int)TdUser.UType; if (UserType.NormalType == TdUser.UType) { userId = TdUser.UserID; } else { userId = ComFunction.GetUserId(marorderln.TradeAccount, ref TdUser); } #endregion #region 交易手数验证 if (!ComFunction.ValidateQuantity(marorderln.Quantity, TdUser.OrderUnit)) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL044; //marketorders.Desc = ResCode.UL044Desc; marketorders.Desc = string.Format("交易手数必须是{0}的倍数", TdUser.OrderUnit); return marketorders; } #endregion #region 判断用户是否允许下买单或卖单 if ("0" == marorderln.OrderType) //下买单 { if (!TdUser.PermitDhuo) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL017; marketorders.Desc = ResCode.UL017Desc; return marketorders; } } else if ("1" == marorderln.OrderType) //下卖单 { if (!TdUser.PermitHshou) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL018; marketorders.Desc = ResCode.UL018Desc; return marketorders; } allowStore = ComFunction.AllowStore;//是否允许卖单入库,通过配置文件读取 } else { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL021; marketorders.Desc = ResCode.UL021Desc; return marketorders; } #endregion #region 获取商品信息 ptc = ComFunction.GetProductInfo(marorderln.ProductCode); //订单类型(0买、1卖) //未能获取商品状态 if (string.IsNullOrEmpty(ptc.State)) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL024; marketorders.Desc = ResCode.UL024Desc; return marketorders; } #endregion #region 判断当前时间是否允许交易 if (!ComFunction.GetDateset(ptc.PriceCode,dt)) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL022; marketorders.Desc = ResCode.UL022Desc; return marketorders; } #endregion #region 判断商品是否处于交易时段 if (!ComFunction.ProductCanTrade(ptc.Starttime, ptc.Endtime)) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL025; marketorders.Desc = ResCode.UL025Desc; return marketorders; } #endregion //获取服务器端实时价 realprice = ComFunction.GetRealPrice(ptc.PriceCode); #region 判断商品是否允许交易 并确定下单价格 //商品的状态(0 正常交易, 1 只报价, 2 只买, 3 只卖, 4 全部禁止) //商品允许正常交易 if ("0" == ptc.State) { //买 if ("0" == marorderln.OrderType) { marketorders.Desc = "买成功"; realprice += ptc.PriceDot * ptc.AdjustBase; } //卖 else if ("1" == marorderln.OrderType) { marketorders.Desc = "卖成功"; } else { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL021; marketorders.Desc = ResCode.UL021Desc; return marketorders; } } //商品只允许买 else if ("2" == ptc.State) { //买 if ("0" == marorderln.OrderType) { marketorders.Desc = "买成功"; realprice += ptc.PriceDot * ptc.AdjustBase; } else { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL026; marketorders.Desc = ResCode.UL026Desc; return marketorders; } } //商品只允许卖 else if ("3" == ptc.State) { //卖 if ("1" == marorderln.OrderType) { marketorders.Desc = "卖成功"; } else { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL027; marketorders.Desc = ResCode.UL027Desc; return marketorders; } } else { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL025; marketorders.Desc = ResCode.UL025Desc; return marketorders; } #endregion #region 下单价判断 //最小成交价格 < 建仓价 <最大成交价格 if (!(realprice - ptc.MinPrice >= ComFunction.dzero && realprice - ptc.MaxPrice <= ComFunction.dzero)) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL015; marketorders.Desc = ResCode.UL015Desc; return marketorders; } #endregion #region 最大交易时间差判断 //当前客户端实时报价时间+允许最大交易时间差>=服务器时间 if (!(marorderln.CurrentTime.AddSeconds(ptc.MaxTime) >= dt)) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL014; marketorders.Desc = ResCode.UL014Desc; return marketorders; } #endregion #region 滑点判断 //当前客户端实时价+滑点*点差基值>=实时价&&当前客户端实时价-滑点*点差基值<=实时价 if (!(System.Math.Abs(marorderln.RtimePrices - realprice) - marorderln.MaxPrice * ptc.AdjustBase <= ComFunction.dzero)) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL016; marketorders.Desc = ResCode.UL016Desc; return marketorders; } #endregion UserGroups ugs = ComFunction.GetUserGroups(userId); #region 对同一行情的商品,平仓后xxx秒不能下单的判断 //实现方法:在历史订单表中查询此用户此行情在最近xxx秒内是否存在历史订单 //如果存在,则不允许下单;否则允许下单 if (ComFunction.JudgeExistLTradeOrder(userId, ptc.PriceCode, ugs.AfterSecond)) { marketorders.Result = false; marketorders.Desc = string.Format("连接服务器失败,请重试!", ugs.AfterSecond); return marketorders; } #endregion #region 根据下单滑点重新计算下单价 if ("1" == marorderln.OrderType)//卖单 下单价向下滑 { realprice -= ugs.PlaceOrderSlipPoint * ptc.AdjustBase; } else//买单 下单价向上滑 { realprice += ugs.PlaceOrderSlipPoint * ptc.AdjustBase; } if (realprice <= ComFunction.dzero) { marketorders.Result = false; marketorders.Desc = "市价单下单异常"; return marketorders; } #endregion realprice = System.Math.Round(realprice, ptc.AdjustCount, MidpointRounding.AwayFromZero);//把多余的小数位去掉 #region 风险率判断 marketorders.MoneyInventory = ComFunction.GetMoneyInventoryByUserId(userId); if (!marketorders.MoneyInventory.Result) { marketorders.Result = false; marketorders.Desc = "未能获取资金库存"; return marketorders; } //计算本次下单的占用资金 //_occMoney = System.Math.Round(marorderln.RtimePrices / ptc.AdjustBase * ptc.ValueDot * marorderln.Quantity * marorderln.OrderMoney, 2, MidpointRounding.AwayFromZero); _occMoney = System.Math.Round(ComFunction.Getfee(ptc.OrderMoneyFee, marorderln.OrderMoney, realprice, marorderln.Quantity), 2, MidpointRounding.AwayFromZero); //工费 _tradefee = System.Math.Round(ComFunction.Getfee(ptc.ExpressionFee, marorderln.OrderMoney, realprice, marorderln.Quantity), 2, MidpointRounding.AwayFromZero); /* 老的风险率判断代码 //风险率<=60%(加上本单的占用保证金和工费) fxrate = (_tradefee + _occMoney + marketorders.MoneyInventory.FdInfo.OccMoney + marketorders.MoneyInventory.FdInfo.FrozenMoney) / marketorders.MoneyInventory.FdInfo.Money; if (marketorders.MoneyInventory.FdInfo.Money - _tradefee - _occMoney - marketorders.MoneyInventory.FdInfo.OccMoney - marketorders.MoneyInventory.FdInfo.FrozenMoney <= ComFunction.dzero) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL012; marketorders.Desc = ResCode.UL012Desc; //com.individual.helper.LogNet4.WriteMsg(string.Format("Money={0},_tradefee + _occMoney + OccMoney={1}+{2}+{3}", // marketorders.MoneyInventory.FdInfo.Money, _tradefee, _occMoney, marketorders.MoneyInventory.FdInfo.OccMoney)); return marketorders; } if (!(fxrate - ComFunction.fenxian_rate <= ComFunction.dzero)) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL013; marketorders.Desc = ResCode.UL013Desc; return marketorders; } */ #region 新的风险率判断代码 double DongJieMoney = 0;//冻结资金 if (marketorders.MoneyInventory.FdInfo.DongJieMoney > ComFunction.dzero) { DongJieMoney = marketorders.MoneyInventory.FdInfo.DongJieMoney; } double UseMoney = _occMoney + marketorders.MoneyInventory.FdInfo.OccMoney + marketorders.MoneyInventory.FdInfo.FrozenMoney; if (UseMoney <= ComFunction.dzero)//如果使用金额为0 则说明有问题 使用金额不可能小于0 { marketorders.Result = false; marketorders.Desc = "市价单下单失败!"; return marketorders; } double yingkui = ComFunction.GetUserYingKui(userId);//用户的盈亏 //com.individual.helper.LogNet4.WriteMsg(yingkui.ToString()); fxrate = (marketorders.MoneyInventory.FdInfo.Money - DongJieMoney - _tradefee + yingkui) / UseMoney;//分子=当前帐户余额-冻结资金-本次的工费 if (marketorders.MoneyInventory.FdInfo.Money - DongJieMoney - _tradefee - _occMoney - marketorders.MoneyInventory.FdInfo.OccMoney - marketorders.MoneyInventory.FdInfo.FrozenMoney <= ComFunction.dzero) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL012; marketorders.Desc = ResCode.UL012Desc; //com.individual.helper.LogNet4.WriteMsg(string.Format("Money={0},_tradefee + _occMoney + OccMoney={1}+{2}+{3}", // marketorders.MoneyInventory.FdInfo.Money, _tradefee, _occMoney, marketorders.MoneyInventory.FdInfo.OccMoney)); return marketorders; } if (fxrate - ComFunction.fenxian_rate <= ComFunction.dzero) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL013; marketorders.Desc = ResCode.UL013Desc; return marketorders; } #endregion #endregion //下单的时候不计算仓储费,直接赋值为0 marketorders.TradeOrder.StorageFee = 0; orderid = ComFunction.GetOrderId(ComFunction.Order); #region 数据库事务处理 List<string> sqlList = new List<string>(); sqlList.Add(string.Format("INSERT INTO Trade_Order([userId],[Orderid],[productCode],[Ordertype],[Orderprice],[usequantity],[quantity],[lossprice],[profitPrice],[OccMoney],[tradefee],[storagefee],[Ordertime],[OperType],[ip],[mac],[AllowStore]) VALUES('{0}','{1}','{2}',{3},{4},{5},{6},{7},{8},{9},{10},{11},'{12}',{13},'{14}','{15}',{16})", userId, orderid, marorderln.ProductCode, marorderln.OrderType, realprice, marorderln.Quantity, marorderln.Quantity, marorderln.LossPrice, marorderln.ProfitPrice, _occMoney, _tradefee, marketorders.TradeOrder.StorageFee, dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), 1, ComFunction.GetClientIp(), marorderln.Mac, allowStore)); sqlList.Add(string.Format("update Trade_FundInfo set occMoney = occMoney+{0},[money]=[money]-{1} where userid='{2}' and [state]<>'4'", _occMoney,_tradefee, userId)); //添加操作记录 sqlList.Add(string.Format("insert into Base_OperrationLog([OperTime],[Account],[UserType],[Remark]) values('{0}','{1}',{2},'{3}')", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), operUser,operUserType, string.Format("{1}在手订单{0}", orderid,ipmac))); if (!ComFunction.SqlTransaction(sqlList)) { marketorders.Result = false; marketorders.Desc = "市价单下单时出错"; return marketorders; } #endregion #region 给返回对象赋值 marketorders.Result = true; marketorders.ChengjiaoPrice = realprice; marketorders.MoneyInventory.FdInfo.OccMoney += _occMoney; marketorders.MoneyInventory.FdInfo.Money -= _tradefee; marketorders.TradeOrder.LossPrice = marorderln.LossPrice; marketorders.TradeOrder.OccMoney = _occMoney; marketorders.TradeOrder.OrderId = orderid; marketorders.TradeOrder.OrderPrice = realprice; marketorders.TradeOrder.OrderTime = dt; marketorders.TradeOrder.OrderType = marorderln.OrderType; marketorders.TradeOrder.ProductCode = marorderln.ProductCode; marketorders.TradeOrder.ProductName = ptc.ProductName; marketorders.TradeOrder.ProfitPrice = marorderln.ProfitPrice; marketorders.TradeOrder.Quantity = marorderln.Quantity; marketorders.TradeOrder.TradeFee = _tradefee; marketorders.TradeOrder.UseQuantity = marorderln.Quantity; marketorders.TradeOrder.PriceCode = ptc.PriceCode; #endregion } catch (Exception ex) { ComFunction.WriteErr(ex); marketorders.Result = false; marketorders.Desc = "市价单下单失败"; } return marketorders; }
/// <summary> /// 下单 /// </summary> /// <param name="marorderln">下单信息</param> /// <returns>下单结果</returns> public Marketorders GetWXMarketorders(MarOrdersLn marorderln, List<UserExperience> experiences) { ComFunction.WriteErr(new Exception("1")); Marketorders marketorders = new Marketorders(); marketorders.MoneyInventory = new MoneyInventory(); marketorders.TradeOrder = new TradeOrder(); ProductConfig ptc = new ProductConfig(); TradeUser TdUser = new TradeUser(); DateTime dt = DateTime.Now;//服务器当前本地时间 double _occMoney = 0;//占用资金 double realprice = 0;//服务器端实时价格(即此时此刻的建仓价) double _tradefee = 0;//工费 double fxrate = 0;//风险率 string orderid = string.Empty;//市价单ID string userId = string.Empty; string allowStore = "1";//是否允许入库 "1"允许入库 "0" 不允许入库 string operUser = marorderln.TradeAccount; string ipmac = string.Empty; int operUserType = 0; bool isExperience=false;//是否以体验券下单 try { if (experiences != null && experiences.Count > 0) isExperience = true; #region 判断用户登陆标识是否过期 if (!ComFunction.ExistUserLoginID(marorderln.LoginID, ref TdUser)) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL003; marketorders.Desc = ResCode.UL003Desc; return marketorders; } operUser = TdUser.Account; ipmac = ComFunction.GetIpMac(TdUser.Ip, TdUser.Mac); operUserType = (int)TdUser.UType; if (UserType.NormalType == TdUser.UType) { userId = TdUser.UserID; } else { userId = ComFunction.GetUserId(marorderln.TradeAccount, ref TdUser); } #endregion #region 交易手数验证 if (!ComFunction.ValidateQuantity(marorderln.Quantity, TdUser.OrderUnit)) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL044; marketorders.Desc = string.Format("交易手数必须是{0}的倍数", TdUser.OrderUnit); return marketorders; } #endregion #region 判断用户是否允许下买单或卖单 if ("0" == marorderln.OrderType) //下买单 { if (!TdUser.PermitDhuo) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL017; marketorders.Desc = ResCode.UL017Desc; return marketorders; } } else if ("1" == marorderln.OrderType) //下卖单 { if (!TdUser.PermitHshou) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL018; marketorders.Desc = ResCode.UL018Desc; return marketorders; } allowStore = ComFunction.AllowStore;//是否允许卖单入库,通过配置文件读取 } else { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL021; marketorders.Desc = ResCode.UL021Desc; return marketorders; } #endregion #region 获取商品信息 ptc = ComFunction.GetProductInfo(marorderln.ProductCode); //订单类型(0买、1卖) //未能获取商品状态 if (string.IsNullOrEmpty(ptc.State)) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL024; marketorders.Desc = ResCode.UL024Desc; return marketorders; } #endregion #region 判断当前时间是否允许交易 if (!ComFunction.GetDateset(ptc.PriceCode, dt)) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL022; marketorders.Desc = ResCode.UL022Desc; return marketorders; } #endregion #region 判断商品是否处于交易时段 if (!ComFunction.ProductCanTrade(ptc.Starttime, ptc.Endtime)) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL025; marketorders.Desc = ResCode.UL025Desc; return marketorders; } #endregion ComFunction.WriteErr(new Exception("2")); //获取服务器端实时价 realprice = ComFunction.GetRealPrice(ptc.PriceCode); #region 判断商品是否允许交易 并确定下单价格 //商品的状态(0 正常交易, 1 只报价, 2 只买, 3 只卖, 4 全部禁止) //商品允许正常交易 if ("0" == ptc.State) { //买 if ("0" == marorderln.OrderType) { marketorders.Desc = "买成功"; realprice += ptc.PriceDot * ptc.AdjustBase; } //卖 else if ("1" == marorderln.OrderType) { marketorders.Desc = "卖成功"; } else { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL021; marketorders.Desc = ResCode.UL021Desc; return marketorders; } } //商品只允许买 else if ("2" == ptc.State) { //买 if ("0" == marorderln.OrderType) { marketorders.Desc = "买成功"; realprice += ptc.PriceDot * ptc.AdjustBase; } else { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL026; marketorders.Desc = ResCode.UL026Desc; return marketorders; } } //商品只允许卖 else if ("3" == ptc.State) { //卖 if ("1" == marorderln.OrderType) { marketorders.Desc = "卖成功"; } else { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL027; marketorders.Desc = ResCode.UL027Desc; return marketorders; } } else { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL025; marketorders.Desc = ResCode.UL025Desc; return marketorders; } #endregion #region 下单价判断 //最小成交价格 < 建仓价 <最大成交价格 if (!(realprice - ptc.MinPrice >= ComFunction.dzero && realprice - ptc.MaxPrice <= ComFunction.dzero)) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL015; marketorders.Desc = ResCode.UL015Desc; return marketorders; } #endregion #region 最大交易时间差判断 //当前客户端实时报价时间+允许最大交易时间差>=服务器时间 if (!(marorderln.CurrentTime.AddSeconds(ptc.MaxTime) >= dt)) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL014; marketorders.Desc = ResCode.UL014Desc; return marketorders; } #endregion #region 滑点判断 //当前客户端实时价+滑点*点差基值>=实时价&&当前客户端实时价-滑点*点差基值<=实时价 if (!(System.Math.Abs(marorderln.RtimePrices - realprice) - marorderln.MaxPrice * ptc.AdjustBase <= ComFunction.dzero)) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL016; marketorders.Desc = ResCode.UL016Desc; return marketorders; } #endregion UserGroups ugs = ComFunction.GetUserGroups(userId); #region 对同一行情的商品,平仓后xxx秒不能下单的判断 //实现方法:在历史订单表中查询此用户此行情在最近xxx秒内是否存在历史订单 //如果存在,则不允许下单;否则允许下单 if (ComFunction.JudgeExistLTradeOrder(userId, ptc.PriceCode, ugs.AfterSecond)) { marketorders.Result = false; marketorders.Desc = string.Format("连接服务器失败,请重试!", ugs.AfterSecond); return marketorders; } #endregion #region 根据下单滑点重新计算下单价 if ("1" == marorderln.OrderType)//卖单 下单价向下滑 { realprice -= ugs.PlaceOrderSlipPoint * ptc.AdjustBase; } else//买单 下单价向上滑 { realprice += ugs.PlaceOrderSlipPoint * ptc.AdjustBase; } if (realprice <= ComFunction.dzero) { marketorders.Result = false; marketorders.Desc = "市价单下单异常"; return marketorders; } #endregion realprice = Math.Round(realprice, ptc.AdjustCount, MidpointRounding.AwayFromZero);//把多余的小数位去掉 marketorders.MoneyInventory = ComFunction.GetMoneyInventoryByUserId(userId); #region 计算占用资金和工费 if (!marketorders.MoneyInventory.Result) { marketorders.Result = false; marketorders.Desc = "未能获取资金库存"; return marketorders; } //计算本次下单的占用资金,根据产品表中[保证金计算公式]进行计算 _occMoney = Math.Round(ComFunction.Getfee(ptc.OrderMoneyFee, marorderln.OrderMoney, realprice, marorderln.Quantity), 2, MidpointRounding.AwayFromZero); if (!isExperience)//体验券下单不扣工费 { //计算本次下单工费,根据产品表中[交易工费公式]进行计算 _tradefee = Math.Round(ComFunction.Getfee(ptc.ExpressionFee, marorderln.OrderMoney, realprice, marorderln.Quantity), 2, MidpointRounding.AwayFromZero); } #endregion #region 新的风险率判断代码 if (!isExperience)//体验券下单不用计算风险率 { double DongJieMoney = 0;//冻结资金 if (marketorders.MoneyInventory.FdInfo.DongJieMoney > ComFunction.dzero) { DongJieMoney = marketorders.MoneyInventory.FdInfo.DongJieMoney; } //客户总共的使用金额=本次下单的保证金+客户账户中的保证金+客户账户中的预付款 double UseMoney = _occMoney + marketorders.MoneyInventory.FdInfo.OccMoney + marketorders.MoneyInventory.FdInfo.FrozenMoney; if (UseMoney <= ComFunction.dzero)//如果使用金额为0 则说明有问题 使用金额不可能小于0 { marketorders.Result = false; marketorders.Desc = "市价单下单失败!"; return marketorders; } //如果客户账户余额-客户账户冻结资金-本次下单工费-本次下单的占用资金-客户账户的保证金-客户账户的预付款<=0 if (marketorders.MoneyInventory.FdInfo.Money - DongJieMoney - _tradefee - _occMoney - marketorders.MoneyInventory.FdInfo.OccMoney - marketorders.MoneyInventory.FdInfo.FrozenMoney <= ComFunction.dzero) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL012; marketorders.Desc = ResCode.UL012Desc; return marketorders; } //用户的盈亏 double yingkui = ComFunction.GetUserYingKui(userId); //风险率=(当前帐户余额-冻结资金-本次的工费+盈亏)/客户账户总共的使用金额 fxrate = (marketorders.MoneyInventory.FdInfo.Money - DongJieMoney - _tradefee + yingkui) / UseMoney;//分子=当前帐户余额-冻结资金-本次的工费 if (fxrate - ComFunction.fenxian_rate <= ComFunction.dzero) { marketorders.Result = false; marketorders.ReturnCode = ResCode.UL013; marketorders.Desc = ResCode.UL013Desc; return marketorders; } } #endregion #region 计算止盈止损价 double lose = marorderln.LossPrice;//微交易下单时传送的是一个百分比 if (lose > 0) { //止损价 //买涨:止损价=实时价-(保证金*止损百分比/点值)*点差基值 //买跌:止损价=实时价+(保证金*止损百分比/点值)*点差基值 var temp = (marorderln.OrderMoney * lose / ptc.ValueDot) * ptc.AdjustBase;//保证金没有乘数量所以公式也没有数量 if ("0" == marorderln.OrderType)//买涨 { marorderln.LossPrice = realprice - temp; } else//买跌 { marorderln.LossPrice = realprice + temp; } marorderln.LossPrice = Math.Round(marorderln.LossPrice, ptc.AdjustCount); } double gain = marorderln.ProfitPrice; if (gain > 0)//微交易下单时传送的是一个百分比 { //止盈价 //买涨:止盈价=实时价+((保证金*止盈百分比+手续费)/点值)*点差基值 //买跌:止盈价=实时价-((保证金*止盈百分比+手续费)/点值)*点差基值 var temp = (marorderln.OrderMoney * gain / ptc.ValueDot) * ptc.AdjustBase;//保证金没有乘数量所以公式也没有数量 if ("0" == marorderln.OrderType)//买涨 { marorderln.ProfitPrice = realprice + temp; } else//买跌 { marorderln.ProfitPrice = realprice - temp; } marorderln.ProfitPrice = Math.Round(marorderln.ProfitPrice, ptc.AdjustCount); } #endregion ComFunction.WriteErr(new Exception("3")); //下单的时候不计算仓储费,直接赋值为0 marketorders.TradeOrder.StorageFee = 0; orderid = ComFunction.GetOrderId(ComFunction.Order); #region 数据库事务处理 List<string> sqlList = new List<string>(); sqlList.Add(string.Format("INSERT INTO Trade_Order([userId],[Orderid],[productCode],[Ordertype],[Orderprice],[usequantity],[quantity],[lossprice],[profitPrice]," + "[OccMoney],[tradefee],[storagefee],[Ordertime],[OperType],[ip],[mac],[AllowStore],[IsExperience]) " + "VALUES('{0}','{1}','{2}',{3},{4},{5},{6},{7},{8},{9},{10},{11},'{12}',{13},'{14}','{15}',{16},'{17}')", userId, orderid, marorderln.ProductCode, marorderln.OrderType, realprice, marorderln.Quantity, marorderln.Quantity, marorderln.LossPrice, marorderln.ProfitPrice, _occMoney, _tradefee, marketorders.TradeOrder.StorageFee, dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), 1, ComFunction.GetClientIp(), marorderln.Mac, allowStore,isExperience)); if (!isExperience)//体验券下单不扣用户资金 { sqlList.Add(string.Format("update Trade_FundInfo set occMoney = occMoney+{0},[money]=[money]-{1} where userid='{2}' and [state]<>'4'", _occMoney, _tradefee, userId)); sqlList.Add(string.Format("INSERT INTO Fund_Change([userId],[reason],[Oldvalue],[NewValue],[OperUser],[OperTime],[RelaOrder],[ChangeValue],[CashUser]) VALUES('{0}','{1}',{2},{3},'{4}','{5}','{6}',{7},'{8}')", userId, 10, marketorders.MoneyInventory.FdInfo.Money, marketorders.MoneyInventory.FdInfo.Money - _tradefee, operUser, dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), orderid, -_tradefee, marketorders.MoneyInventory.FdInfo.CashUser)); } //添加操作记录 sqlList.Add(string.Format("insert into Base_OperrationLog([OperTime],[Account],[UserType],[Remark]) values('{0}','{1}',{2},'{3}')", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), operUser, operUserType, string.Format("{1}在手订单{0}", orderid, ipmac))); if (isExperience) { foreach (UserExperience item in experiences) { sqlList.Add(string.Format("UPDATE [USER_EXPERIENCE] SET [USE_NUM] ={0} WHERE ID={1}", item.Num, item.ID)); } } if (!ComFunction.SqlTransaction(sqlList)) { marketorders.Result = false; marketorders.Desc = "市价单下单时出错"; return marketorders; } #endregion #region 给返回对象赋值 marketorders.Result = true; marketorders.ChengjiaoPrice = realprice; marketorders.MoneyInventory.FdInfo.OccMoney += _occMoney; marketorders.MoneyInventory.FdInfo.Money -= _tradefee; marketorders.TradeOrder.LossPrice = marorderln.LossPrice; marketorders.TradeOrder.OccMoney = _occMoney; marketorders.TradeOrder.OrderId = orderid; marketorders.TradeOrder.OrderPrice = realprice; marketorders.TradeOrder.OrderTime = dt; marketorders.TradeOrder.OrderType = marorderln.OrderType; marketorders.TradeOrder.ProductCode = marorderln.ProductCode; marketorders.TradeOrder.ProductName = ptc.ProductName; marketorders.TradeOrder.ProfitPrice = marorderln.ProfitPrice; marketorders.TradeOrder.Quantity = marorderln.Quantity; marketorders.TradeOrder.TradeFee = _tradefee; marketorders.TradeOrder.UseQuantity = marorderln.Quantity; marketorders.TradeOrder.PriceCode = ptc.PriceCode; #endregion } catch (Exception ex) { ComFunction.WriteErr(ex); marketorders.Result = false; marketorders.Desc = "市价单下单失败" + ex.Message + ex.InnerException; } return marketorders; }