예제 #1
0
        private void InsertToSellQueue(StoneDelegateSellOrderInfo item, CustomerMySqlTransaction myTrans)
        {
            //需要分别处理Waiting和Splited两种情况
            if (item.SellState != StoneDelegateSellState.Waiting && item.SellState != StoneDelegateSellState.Splited)
            {
                return;
            }

            int index = this._todayTradeInfo.InsertSellUnit(item.SellUnit);

            ListStoneDelegateSellOrderInfoCollection queueSell = null;

            if (_dicWaitingSellInfos.ContainsKey(item.SellUnit.Price))
            {
                queueSell = _dicWaitingSellInfos[item.SellUnit.Price];
            }
            else
            {
                queueSell = new ListStoneDelegateSellOrderInfoCollection();
                _dicWaitingSellInfos[item.SellUnit.Price] = queueSell;
            }

            queueSell.Enqueue(item);

            if (myTrans != null)
            {
                DBProvider.StoneStackDBProvider.SaveWaitingStoneDelegateSellOrderInfo(item, myTrans);
            }
        }
예제 #2
0
        /// <summary>
        /// 返回买单处理结果
        /// </summary>
        /// <param name="sellPrice"></param>
        /// <param name="queueSellOrders"></param>
        /// <param name="listTradeSucceedSellOrders"></param>
        /// <param name="buyOrder"></param>
        /// <returns></returns>
        private StackTradeResult TradeSamePriceSellOrder(decimal sellPrice, ListStoneDelegateSellOrderInfoCollection queueSellOrders,
                                                         List <StoneDelegateSellOrderInfo> listTradeSucceedSellOrders, StackTradeUnit buyUnit, string buyerUserName)
        {
            StackTradeResult buyOrderResult = new StackTradeResult();

            //取出卖价对应的卖单
            StoneDelegateSellOrderInfo sellOrder = queueSellOrders.Dequeue();

            if (sellOrder == null)
            {
                return(buyOrderResult);
            }

            int surplusStoneHandCount = buyUnit.TradeStoneHandCount - sellOrder.SellUnit.TradeStoneHandCount;

            //买单量大于或等于卖单量
            while (surplusStoneHandCount > 0)//继续找其他卖单凑量
            {
                sellOrder.SellState = StoneDelegateSellState.Succeed;
                sellOrder.FinishedStoneTradeHandCount = sellOrder.SellUnit.TradeStoneHandCount;
                sellOrder.FinishedTime = new MyDateTime(DateTime.Now);
                listTradeSucceedSellOrders.Add(sellOrder);

                var operResult = this._todayTradeInfo.DecreaseSellUnit(sellOrder.SellUnit.Price, sellOrder.SellUnit.TradeStoneHandCount);
                if (operResult.OperResultCode != OperResult.RESULTCODE_TRUE)
                {
                    LogHelper.Instance.AddErrorLog("TradeBuy1Orders DecreaseSellUnit Error1. " + operResult.Message, null);
                }

                var nextSellOrder = queueSellOrders.Dequeue();
                if (nextSellOrder == null)
                {
                    //说明已经没有当前价格的卖单了。
                    //买单只消化一部,需要被拆分
                    buyOrderResult.State = StackTradeState.Splited;
                    buyOrderResult.SucceedStoneHandCount = buyUnit.TradeStoneHandCount - surplusStoneHandCount;
                    return(buyOrderResult);
                }
                else
                {
                    //判断下一卖单
                    sellOrder             = nextSellOrder;
                    surplusStoneHandCount = surplusStoneHandCount - sellOrder.SellUnit.TradeStoneHandCount;
                }
            }
            //卖单量等于买单量
            if (surplusStoneHandCount == 0)
            {
                sellOrder.SellState = StoneDelegateSellState.Succeed;
                sellOrder.FinishedStoneTradeHandCount = sellOrder.SellUnit.TradeStoneHandCount;
                sellOrder.FinishedTime = new MyDateTime(DateTime.Now);
                listTradeSucceedSellOrders.Add(sellOrder);

                var operResult = this._todayTradeInfo.DecreaseSellUnit(sellOrder.SellUnit.Price, sellOrder.SellUnit.TradeStoneHandCount);
                if (operResult.OperResultCode != OperResult.RESULTCODE_TRUE)
                {
                    LogHelper.Instance.AddErrorLog("TradeBuy1Orders DecreaseSellUnit Error2. " + operResult.Message, null);
                }
            }
            else
            {
                //卖单量大于买单量,需要将当前卖单拆分。将已成功的部分拆成一新订单添加到数据库中;将老订单缩减到剩余部分,继续留在市场中。
                sellOrder.SellState = StoneDelegateSellState.Splited;
                sellOrder.FinishedStoneTradeHandCount = sellOrder.SellUnit.TradeStoneHandCount + surplusStoneHandCount;
                sellOrder.FinishedTime = new MyDateTime(DateTime.Now);
                listTradeSucceedSellOrders.Add(sellOrder);

                var operResult = this._todayTradeInfo.DecreaseSellUnit(sellOrder.SellUnit.Price, sellOrder.FinishedStoneTradeHandCount);
                if (operResult.OperResultCode != OperResult.RESULTCODE_TRUE)
                {
                    LogHelper.Instance.AddErrorLog("TradeBuy1Orders DecreaseSellUnit Error3. " + operResult.Message, null);
                }


                DateTime nowTime = DateTime.Now;

                //拆分后的订单,再保存到数据库,并添加到集合中。
                StoneDelegateSellOrderInfo newSellOrder = new StoneDelegateSellOrderInfo()
                {
                    OrderNumber  = OrderController.Instance.CreateOrderNumber(sellOrder.UserName, nowTime, AlipayTradeInType.StackStoneSell),
                    UserID       = sellOrder.UserID,
                    UserName     = sellOrder.UserName,
                    SellState    = StoneDelegateSellState.Waiting,
                    DelegateTime = new MyDateTime(nowTime),
                    SellUnit     = new StackTradeUnit()
                    {
                        Price = sellOrder.SellUnit.Price,
                        TradeStoneHandCount = sellOrder.SellUnit.TradeStoneHandCount - sellOrder.FinishedStoneTradeHandCount
                    },
                    IsSubOrder = true
                };

                try
                {
                    DBProvider.StoneStackDBProvider.SaveWaitingStoneDelegateSellOrderInfo(newSellOrder);
                    queueSellOrders.Enqueue(newSellOrder);
                }
                catch (Exception exc)
                {
                    LogHelper.Instance.AddErrorLog("股票操作。TradeSamePriceSellOrder.SaveWaitingStoneDelegateSellOrderInfo 异常。信息为:" + newSellOrder.ToString(), exc);
                }
            }

            //买单已全部被消化
            buyOrderResult.State = StackTradeState.Succeed;
            buyOrderResult.SucceedStoneHandCount = buyUnit.TradeStoneHandCount;
            return(buyOrderResult);
        }