コード例 #1
0
        /// <summary>
        /// 市价单取消接口 结算盈亏 退还保证金(平仓)
        /// </summary>
        /// <param name="delen">市价单取消信息</param>
        /// <returns>市价单取消结果</returns>
        public Marketorders WXDelOrder(DeliveryEnter delen)
        {
            var marketorders = new Marketorders { TradeOrder = new TradeOrder(), MoneyInventory = new MoneyInventory() };
            DateTime dt = DateTime.Now;//服务器当前本地时间
            var tdUser = new TradeUser();
            string order_ip = string.Empty;
            string order_mac = string.Empty;
            double realprice = 0;
            double _occmoney = 0;
            double _yingkuifee = 0;
            string tmpsql = string.Empty;
            string userId = string.Empty;
            string restrictions = string.Empty;
            string operUser = delen.TradeAccount;
            string ipmac = string.Empty;
            int operUserType = 0;
            bool IsCanOrgTrade = false;
            try
            {

                #region 判断用户登陆标识是否过期

                if (!ComFunction.ExistUserLoginID(delen.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(delen.TradeAccount, ref tdUser);
                }
                #endregion

                #region 交易手数验证
                if (!ComFunction.ValidateQuantity(delen.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 (!tdUser.PermitDelOrder)
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL019;
                    marketorders.Desc = ResCode.UL019Desc;
                    return marketorders;
                }
                #endregion

                #region 根据订单ID获取订单信息
                marketorders.TradeOrder = ComFunction.GetTradeOrder(delen.Orderid, ref order_ip, ref order_mac);
                if (null == marketorders.TradeOrder.OrderId)
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL028;
                    marketorders.Desc = ResCode.UL028Desc;
                    return marketorders;
                }
                #endregion

                #region 获取商品信息
                ProductConfig ptc = ComFunction.GetProductInfo(marketorders.TradeOrder.ProductCode);
                //订单类型(0买、1卖)
                //未能获取商品状态
                if (string.Empty == 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 (!(delen.Quantity - marketorders.TradeOrder.UseQuantity <= ComFunction.dzero && delen.Quantity > ComFunction.dzero))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL011;
                    marketorders.Desc = ResCode.UL011Desc;
                    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

                #region 最大交易时间差判断
                //当前客户端实时报价时间+允许最大交易时间差>=服务器时间
                if (!(delen.CurrentTime.AddSeconds(ptc.MaxTime) >= dt))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL014;
                    marketorders.Desc = ResCode.UL014Desc;
                    return marketorders;
                }
                #endregion

                realprice = ComFunction.GetRealPrice(ptc.PriceCode);
                //卖类型的订单 按买价平仓,买类型的订单 按卖价平仓
                if ("1" == marketorders.TradeOrder.OrderType)
                {
                    realprice += ptc.PriceDot * ptc.AdjustBase;
                }

                #region 滑点判断
                //if (!(System.Math.Abs(delen.RtimePrices - realprice) - delen.MaxPrice * ptc.AdjustBase <= ComFunction.dzero))
                if ((System.Math.Abs(delen.RtimePrices - realprice) - delen.MaxPrice * ptc.AdjustBase > ComFunction.dzero))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL016;
                    marketorders.Desc = ResCode.UL016Desc;
                    //marketorders.Desc = delen.RtimePrices + " " + realprice + " " + delen.MaxPrice + " " + ptc.AdjustBase;
                    return marketorders;
                }
                #endregion

                #region 获取资金库存
                marketorders.MoneyInventory = ComFunction.GetMoneyInventoryByUserId(userId);
                if (!marketorders.MoneyInventory.Result)
                {
                    marketorders.Result = false;
                    marketorders.Desc = "未能获取资金库存";
                    return marketorders;
                }
                #endregion

                int xs = 1;//工费收取倍数

                #region 根据平仓滑点重新计算平仓价
                UserGroups ugs = ComFunction.GetUserGroups(userId);

                if ("1" == marketorders.TradeOrder.OrderType)//卖单 平仓价向上滑
                {
                    realprice += ugs.FlatOrderSlipPoint * ptc.AdjustBase;
                }
                else//买单 平仓价向下滑
                {
                    realprice -= ugs.FlatOrderSlipPoint * ptc.AdjustBase;
                }
                if (realprice <= ComFunction.dzero)
                {
                    marketorders.Result = false;
                    marketorders.Desc = "平仓异常";
                    return marketorders;
                }
                #endregion

                realprice = Math.Round(realprice, ptc.AdjustCount, MidpointRounding.AwayFromZero);//把多余的小数位去掉

                #region 计算占用资金 盈亏
                _occmoney = Math.Round(marketorders.TradeOrder.OccMoney * delen.Quantity / marketorders.TradeOrder.UseQuantity, 2, MidpointRounding.AwayFromZero);
                //卖
                if ("1" == marketorders.TradeOrder.OrderType)
                {
                    //_yingkuifee =Math.Round(-(realprice - marketorders.TradeOrder.OrderPrice) / ptc.AdjustBase * ptc.ValueDot * delen.Quantity - (marketorders.TradeOrder.TradeFee * xs + marketorders.TradeOrder.StorageFee) * delen.Quantity / marketorders.TradeOrder.UseQuantity, 2, MidpointRounding.AwayFromZero);
                    _yingkuifee = Math.Round(-(realprice - marketorders.TradeOrder.OrderPrice) / ptc.AdjustBase * ptc.ValueDot * delen.Quantity, 2, MidpointRounding.AwayFromZero);

                }
                //买
                else
                {
                    //_yingkuifee =Math.Round((realprice - marketorders.TradeOrder.OrderPrice) / ptc.AdjustBase * ptc.ValueDot * delen.Quantity - (marketorders.TradeOrder.TradeFee * xs + marketorders.TradeOrder.StorageFee) * delen.Quantity / marketorders.TradeOrder.UseQuantity, 2, MidpointRounding.AwayFromZero);
                    _yingkuifee = Math.Round((realprice - marketorders.TradeOrder.OrderPrice) / ptc.AdjustBase * ptc.ValueDot * delen.Quantity, 2, MidpointRounding.AwayFromZero);
                }
                //体验券下单,用户赚钱了把钱转入用户账户,赔钱了不扣用户账户上的钱
                if (marketorders.TradeOrder.IsExperience && _yingkuifee < 0)
                    _yingkuifee = 0;

                #endregion

                #region 数据库事务处理
                if (System.Math.Abs(marketorders.TradeOrder.UseQuantity - delen.Quantity) <= ComFunction.dzero)
                {
                    tmpsql = string.Format("delete from Trade_Order where Orderid='{0}'", delen.Orderid);
                }
                else
                {
                    tmpsql = string.Format("update Trade_Order set usequantity = {1},OccMoney=OccMoney*{1}/usequantity,tradefee=tradefee*{1}/usequantity,storagefee=storagefee*{1}/usequantity where Orderid='{0}'", delen.Orderid, marketorders.TradeOrder.UseQuantity - delen.Quantity);
                }
                List<string> sqlList = new List<string>();
                double ResultOccMoney = marketorders.MoneyInventory.FdInfo.OccMoney - _occmoney;//用户账户的保证金
                if (ResultOccMoney < ComFunction.dzero)
                {
                    ResultOccMoney = 0;
                }
                if (ConfigurationManager.AppSettings["VersionFlag"] == "XS")
                {
                    double money = 0;
                    IsCanOrgTrade = ComFunction.IsCanOrgTrade(tdUser.OrgId, _yingkuifee,ref money);
                    //ComFunction.WriteErr(new Exception("IsCanOrgTrade:" + IsCanOrgTrade + "\t_yingkuifee:" + _yingkuifee + "\tOrgId:" + tdUser.OrgId + "\tmoney:" + money + "\tOrderID:" + delen.Orderid));
                    if (IsCanOrgTrade)//如果会员承接头寸
                    {
                        sqlList.Add(string.Format(@"update Trade_OrgFund set money=money - {0} where OrgID='{1}'",
                            _yingkuifee, tdUser.OrgId));
                        sqlList.Add(
                            string.Format(@"INSERT INTO [ORG_FundChange] ([orgId] ,[reason],[Oldvalue],[NewValue] ,[OperUser],[OperTime],[RelaOrder] ,
                        [ChangeValue]) VALUES ('{0}' ,'{1}' ,{2} ,{3} ,'{4}' ,'{5}' ,'{6}' ,{7})", tdUser.OrgId, "6",
                                money, money - _yingkuifee, operUser,
                                dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), delen.Orderid, -_yingkuifee));
                    }
                    else//会员不承接头寸
                    {
                        sqlList.Add(string.Format(@"update Base_Org set IsTrade =0 where OrgID='{0}'",tdUser.OrgId));
                        sqlList.Add(string.Format(@"update Trade_OrgFund set money=money  -  {0} where OrgID='{1}'",
                            _yingkuifee, "system"));
                        sqlList.Add(
                            string.Format(@"INSERT INTO [ORG_FundChange] ([orgId] ,[reason],[Oldvalue],[NewValue] ,[OperUser],[OperTime],[RelaOrder] ,
                        [ChangeValue]) VALUES ('{0}' ,'{1}' ,{2} ,{3} ,'{4}' ,'{5}' ,'{6}' ,{7})", "system", "6",
                                money, money - _yingkuifee, operUser,
                                dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), delen.Orderid,-_yingkuifee));
                    }
                    //string ex = "";
                    //foreach (var item in sqlList)
                    //    ex += item+"\t";
                    //ComFunction.WriteErr(new Exception(ex));
                }

                //体验券下单,下单时只计算所需保证金,并存入订单表,但未冻结用户账户上的保证金,故平仓时也就没有解除冻结的保证金操作
                if (marketorders.TradeOrder.IsExperience)
                {
                    if (_yingkuifee > 0)//用户此单赚钱了,则把钱转入用户账户余额,赔钱了不扣用户账户余额
                    {
                        sqlList.Add(string.Format("update Trade_FundInfo set money=money+{0} where userid='{1}' and [state]<>'4'", _yingkuifee, 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, 11, marketorders.MoneyInventory.FdInfo.Money, marketorders.MoneyInventory.FdInfo.Money + _yingkuifee, operUser, dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), delen.Orderid, _yingkuifee, marketorders.MoneyInventory.FdInfo.CashUser));

                    }
                    sqlList.Add(tmpsql);
                }
                else
                {
                    sqlList.Add(string.Format("update Trade_FundInfo set occMoney={0}, money=money+{1} where userid='{2}' and [state]<>'4'", ResultOccMoney, _yingkuifee, 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, 11, marketorders.MoneyInventory.FdInfo.Money, marketorders.MoneyInventory.FdInfo.Money + _yingkuifee, operUser, dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), delen.Orderid, _yingkuifee, marketorders.MoneyInventory.FdInfo.CashUser));
                    sqlList.Add(tmpsql);

                }
                if (ConfigurationManager.AppSettings["VersionFlag"] == "XS")
                {

                    sqlList.Add(string.Format("INSERT INTO L_Trade_Order([userId],[historyOrderId],[Orderid],[productCode],[OrderType],[Orderprice],[overprice],[quantity]," +
                                           "[lossprice],[profitPrice],[OccMoney],[tradefee],[storagefee],[Ordertime],[OperType],[profitValue],[Overtime],[overtype],[ip],[mac],[IsSystemPay])" +
                                           " VALUES('{0}','{1}','{2}','{3}',{4},{5},{6},{7},{8},{9},{10},{11},{12},'{13}',{14},{15},'{16}',{17},'{18}','{19}',{20})",
                     userId, ComFunction.GetOrderId(ComFunction.Order_His), marketorders.TradeOrder.OrderId, marketorders.TradeOrder.ProductCode, marketorders.TradeOrder.OrderType,
                     marketorders.TradeOrder.OrderPrice, realprice, delen.Quantity, marketorders.TradeOrder.LossPrice, marketorders.TradeOrder.ProfitPrice,
                     _occmoney, marketorders.TradeOrder.TradeFee * delen.Quantity / marketorders.TradeOrder.UseQuantity, marketorders.TradeOrder.StorageFee * delen.Quantity / marketorders.TradeOrder.UseQuantity,
                     marketorders.TradeOrder.OrderTime.ToString("yyyy-MM-dd HH:mm:ss.fff"),
                     marketorders.TradeOrder.OperType, _yingkuifee, dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), 1, order_ip, order_mac, Convert.ToByte(!IsCanOrgTrade)));
                }
                else
                {
                    sqlList.Add(string.Format("INSERT INTO L_Trade_Order([userId],[historyOrderId],[Orderid],[productCode],[OrderType],[Orderprice],[overprice],[quantity]," +
                                           "[lossprice],[profitPrice],[OccMoney],[tradefee],[storagefee],[Ordertime],[OperType],[profitValue],[Overtime],[overtype],[ip],[mac])" +
                                           " VALUES('{0}','{1}','{2}','{3}',{4},{5},{6},{7},{8},{9},{10},{11},{12},'{13}',{14},{15},'{16}',{17},'{18}','{19}')",
                     userId, ComFunction.GetOrderId(ComFunction.Order_His), marketorders.TradeOrder.OrderId, marketorders.TradeOrder.ProductCode, marketorders.TradeOrder.OrderType,
                     marketorders.TradeOrder.OrderPrice, realprice, delen.Quantity, marketorders.TradeOrder.LossPrice, marketorders.TradeOrder.ProfitPrice,
                     _occmoney, marketorders.TradeOrder.TradeFee * delen.Quantity / marketorders.TradeOrder.UseQuantity, marketorders.TradeOrder.StorageFee * delen.Quantity / marketorders.TradeOrder.UseQuantity,
                     marketorders.TradeOrder.OrderTime.ToString("yyyy-MM-dd HH:mm:ss.fff"),
                     marketorders.TradeOrder.OperType, _yingkuifee, dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), 1, order_ip, order_mac));
                }

                //添加操作记录
                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}", delen.Orderid, ipmac)));

                if (!ComFunction.SqlTransaction(sqlList))
                {
                    marketorders.Result = false;
                    marketorders.Desc = "平仓出错";
                    return marketorders;
                }
                #endregion

                //给返回对象赋值
                marketorders.Result = true;
                marketorders.Desc = "交易成功";
                marketorders.ChengjiaoPrice = realprice;
                marketorders.MoneyInventory.FdInfo.Money += _yingkuifee;
                marketorders.MoneyInventory.FdInfo.OccMoney -= _occmoney;
                marketorders.TradeOrder.OccMoney = Math.Round(marketorders.TradeOrder.OccMoney * (marketorders.TradeOrder.UseQuantity - delen.Quantity) / marketorders.TradeOrder.UseQuantity, 2, MidpointRounding.AwayFromZero);
                marketorders.TradeOrder.TradeFee = Math.Round(marketorders.TradeOrder.TradeFee * (marketorders.TradeOrder.UseQuantity - delen.Quantity) / marketorders.TradeOrder.UseQuantity, 2, MidpointRounding.AwayFromZero);
                marketorders.TradeOrder.StorageFee = Math.Round(marketorders.TradeOrder.StorageFee * (marketorders.TradeOrder.UseQuantity - delen.Quantity) / marketorders.TradeOrder.UseQuantity, 2, MidpointRounding.AwayFromZero);
                marketorders.TradeOrder.UseQuantity -= delen.Quantity;
                marketorders.TradeOrder.PriceCode = ptc.PriceCode;
            }
            catch (Exception ex)
            {
                ComFunction.WriteErr(ex);
                marketorders.Result = false;
                marketorders.Desc = "平仓失败";

            }

            return marketorders;
        }
コード例 #2
0
        /// <summary>
        /// 市价单取消接口 结算盈亏 退还保证金
        /// </summary>
        /// <param name="delen">市价单取消信息</param>
        /// <returns>市价单取消结果</returns>
        public Marketorders DelOrder(DeliveryEnter delen)
        {
            Marketorders marketorders = new Marketorders();
            marketorders.TradeOrder = new TradeOrder();
            marketorders.MoneyInventory = new MoneyInventory();
            DateTime dt = DateTime.Now;//服务器当前本地时间
            ProductConfig ptc = new ProductConfig();
            TradeUser TdUser = new TradeUser();
            string order_ip = string.Empty;
            string order_mac = string.Empty;
            double realprice = 0;
            double _occmoney = 0;
            double _yingkuifee = 0;
            string tmpsql = string.Empty;
            string userId = string.Empty;
            string restrictions = string.Empty;
            string operUser = delen.TradeAccount;
            string ipmac = string.Empty;
            int operUserType = 0;
            try
            {

                #region 判断用户登陆标识是否过期

                if (!ComFunction.ExistUserLoginID(delen.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(delen.TradeAccount, ref TdUser);
                }
                #endregion

                #region 交易手数验证
                if (!ComFunction.ValidateQuantity(delen.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 (!TdUser.PermitDelOrder)
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL019;
                    marketorders.Desc = ResCode.UL019Desc;
                    return marketorders;
                }
                #endregion

                #region 根据订单ID获取订单信息
                marketorders.TradeOrder = ComFunction.GetTradeOrder(delen.Orderid, ref order_ip, ref order_mac);
                if (null == marketorders.TradeOrder.OrderId)
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL028;
                    marketorders.Desc = ResCode.UL028Desc;
                    return marketorders;
                }
                #endregion

                #region 获取商品信息
                ptc = ComFunction.GetProductInfo(marketorders.TradeOrder.ProductCode);
                //订单类型(0买、1卖)
                //未能获取商品状态
                if (string.Empty == 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 (!(delen.Quantity - marketorders.TradeOrder.UseQuantity <= ComFunction.dzero && delen.Quantity > ComFunction.dzero))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL011;
                    marketorders.Desc = ResCode.UL011Desc;
                    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

                #region 最大交易时间差判断
                //当前客户端实时报价时间+允许最大交易时间差>=服务器时间
                if (!(delen.CurrentTime.AddSeconds(ptc.MaxTime) >= dt))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL014;
                    marketorders.Desc = ResCode.UL014Desc;
                    return marketorders;
                }
                #endregion

                realprice = ComFunction.GetRealPrice(ptc.PriceCode);
                //卖类型的订单 按买价平仓,买类型的订单 按卖价平仓
                if ("1" == marketorders.TradeOrder.OrderType)
                {
                    realprice += ptc.PriceDot * ptc.AdjustBase;
                }

                #region 滑点判断
                if (!(System.Math.Abs(delen.RtimePrices - realprice) - delen.MaxPrice * ptc.AdjustBase <= ComFunction.dzero))
                {
                    marketorders.Result = false;
                    marketorders.ReturnCode = ResCode.UL016;
                    marketorders.Desc = ResCode.UL016Desc;
                    return marketorders;
                }
                #endregion

                #region 获取资金库存
                marketorders.MoneyInventory = ComFunction.GetMoneyInventoryByUserId(userId);
                if (!marketorders.MoneyInventory.Result)
                {
                    marketorders.Result = false;
                    marketorders.Desc = "未能获取资金库存";
                    return marketorders;
                }
                #endregion

                int xs = 1;//工费收取倍数

                #region 根据平仓滑点重新计算平仓价
                UserGroups ugs = ComFunction.GetUserGroups(userId);

                if ("1" == marketorders.TradeOrder.OrderType)//卖单 平仓价向上滑
                {
                    realprice += ugs.FlatOrderSlipPoint * ptc.AdjustBase;
                }
                else//买单 平仓价向下滑
                {
                    realprice -= ugs.FlatOrderSlipPoint * ptc.AdjustBase;
                }
                if (realprice <= ComFunction.dzero)
                {
                    marketorders.Result = false;
                    marketorders.Desc = "平仓异常";
                    return marketorders;
                }
                #endregion
                realprice = System.Math.Round(realprice, ptc.AdjustCount, MidpointRounding.AwayFromZero);//把多余的小数位去掉
                #region 计算占用资金 盈亏
                _occmoney = System.Math.Round(marketorders.TradeOrder.OccMoney * delen.Quantity / marketorders.TradeOrder.UseQuantity, 2, MidpointRounding.AwayFromZero);
                //卖
                if ("1" == marketorders.TradeOrder.OrderType)
                {
                    //_yingkuifee = System.Math.Round(-(realprice - marketorders.TradeOrder.OrderPrice) / ptc.AdjustBase * ptc.ValueDot * delen.Quantity - (marketorders.TradeOrder.TradeFee * xs + marketorders.TradeOrder.StorageFee) * delen.Quantity / marketorders.TradeOrder.UseQuantity, 2, MidpointRounding.AwayFromZero);
                    _yingkuifee = System.Math.Round(-(realprice - marketorders.TradeOrder.OrderPrice) / ptc.AdjustBase * ptc.ValueDot * delen.Quantity, 2, MidpointRounding.AwayFromZero);

                }
                //买
                else
                {
                    //_yingkuifee = System.Math.Round((realprice - marketorders.TradeOrder.OrderPrice) / ptc.AdjustBase * ptc.ValueDot * delen.Quantity - (marketorders.TradeOrder.TradeFee * xs + marketorders.TradeOrder.StorageFee) * delen.Quantity / marketorders.TradeOrder.UseQuantity, 2, MidpointRounding.AwayFromZero);
                    _yingkuifee = System.Math.Round((realprice - marketorders.TradeOrder.OrderPrice) / ptc.AdjustBase * ptc.ValueDot * delen.Quantity, 2, MidpointRounding.AwayFromZero);
                }

                #endregion

                #region 数据库事务处理
                if (System.Math.Abs(marketorders.TradeOrder.UseQuantity - delen.Quantity) <= ComFunction.dzero)
                {
                    tmpsql = string.Format("delete from Trade_Order where Orderid='{0}'", delen.Orderid);
                }
                else
                {
                    tmpsql = string.Format("update Trade_Order set usequantity = {1},OccMoney=OccMoney*{1}/usequantity,tradefee=tradefee*{1}/usequantity,storagefee=storagefee*{1}/usequantity where Orderid='{0}'", delen.Orderid, marketorders.TradeOrder.UseQuantity - delen.Quantity);
                }
                List<string> sqlList = new List<string>();
                double ResultOccMoney = marketorders.MoneyInventory.FdInfo.OccMoney - _occmoney;
                if (ResultOccMoney < ComFunction.dzero)
                {
                    ResultOccMoney = 0;
                }
                sqlList.Add(string.Format("update Trade_FundInfo set occMoney={0}, money=money+{1} where userid='{2}' and [state]<>'4'", ResultOccMoney, _yingkuifee, 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, 2, marketorders.MoneyInventory.FdInfo.Money, marketorders.MoneyInventory.FdInfo.Money + _yingkuifee, operUser, dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), delen.Orderid, _yingkuifee, marketorders.MoneyInventory.FdInfo.CashUser));
                sqlList.Add(tmpsql);
                sqlList.Add(string.Format("INSERT INTO L_Trade_Order([userId],[historyOrderId],[Orderid],[productCode],[OrderType],[Orderprice],[overprice],[quantity],[lossprice],[profitPrice],[OccMoney],[tradefee],[storagefee],[Ordertime],[OperType],[profitValue],[Overtime],[overtype],[ip],[mac]) VALUES('{0}','{1}','{2}','{3}',{4},{5},{6},{7},{8},{9},{10},{11},{12},'{13}',{14},{15},'{16}',{17},'{18}','{19}')",
                    userId, ComFunction.GetOrderId(ComFunction.Order_His), marketorders.TradeOrder.OrderId, marketorders.TradeOrder.ProductCode, marketorders.TradeOrder.OrderType,
                    marketorders.TradeOrder.OrderPrice, realprice, delen.Quantity, marketorders.TradeOrder.LossPrice, marketorders.TradeOrder.ProfitPrice,
                    _occmoney, marketorders.TradeOrder.TradeFee * delen.Quantity / marketorders.TradeOrder.UseQuantity, marketorders.TradeOrder.StorageFee * delen.Quantity / marketorders.TradeOrder.UseQuantity,
                    marketorders.TradeOrder.OrderTime.ToString("yyyy-MM-dd HH:mm:ss.fff"),
                    marketorders.TradeOrder.OperType, _yingkuifee, dt.ToString("yyyy-MM-dd HH:mm:ss.fff"), 1, order_ip, order_mac));
                //添加操作记录
                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}", delen.Orderid, ipmac)));

                if (!ComFunction.SqlTransaction(sqlList))
                {
                    marketorders.Result = false;
                    marketorders.Desc = "平仓出错";
                    return marketorders;
                }
                #endregion

                //给返回对象赋值
                marketorders.Result = true;
                marketorders.Desc = "交易成功";
                marketorders.ChengjiaoPrice = realprice;
                marketorders.MoneyInventory.FdInfo.Money += _yingkuifee;
                marketorders.MoneyInventory.FdInfo.OccMoney -= _occmoney;
                marketorders.TradeOrder.OccMoney = System.Math.Round(marketorders.TradeOrder.OccMoney * (marketorders.TradeOrder.UseQuantity - delen.Quantity) / marketorders.TradeOrder.UseQuantity, 2, MidpointRounding.AwayFromZero);
                marketorders.TradeOrder.TradeFee = System.Math.Round(marketorders.TradeOrder.TradeFee * (marketorders.TradeOrder.UseQuantity - delen.Quantity) / marketorders.TradeOrder.UseQuantity, 2, MidpointRounding.AwayFromZero);
                marketorders.TradeOrder.StorageFee = System.Math.Round(marketorders.TradeOrder.StorageFee * (marketorders.TradeOrder.UseQuantity - delen.Quantity) / marketorders.TradeOrder.UseQuantity, 2, MidpointRounding.AwayFromZero);
                marketorders.TradeOrder.UseQuantity -= delen.Quantity;
                marketorders.TradeOrder.PriceCode = ptc.PriceCode;
            }
            catch (Exception ex)
            {
                ComFunction.WriteErr(ex);
                marketorders.Result = false;
                marketorders.Desc = "平仓失败";

            }

            return marketorders;
        }
コード例 #3
0
        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();
                DeliveryEnter orderln = new DeliveryEnter();

                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);
                orderln.Orderid = xmldoc.SelectSingleNode("JTW91G/MsgData/DataBody/Orders/Order/OrderId").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);

                string MaxPrice = xmldoc.SelectSingleNode("JTW91G/MsgData/DataBody/Orders/Order/MaxPrice").InnerText;
                orderln.MaxPrice = Convert.ToDouble(string.IsNullOrEmpty(MaxPrice) ? "0" : MaxPrice);

              //  orderln.UserType = 0; //客户端没有传递这个值 内部调用默认赋值0 表示普通用户

                Marketorders orders = trade.DelOrder(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></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;
        }