Example #1
0
        private void JY_NewDeal(object sender, DealItem e)
        {
            Order order   = null;
            int   unit_id = 0;

            string[] keys = TradeRA.KeySearch("O_" + e.order_no + "_*");
            if (keys.Length > 0)
            {
                order   = OrderRA.Get(keys[0]);
                unit_id = order.unit_id;
            }

            string key = "D_" + e.deal_no + "_O_" + e.order_no + "_U_" + unit_id;

            if (TradeRA.KeyExists(key))
            {
                return;
            }

            Deal deal = new Deal()
            {
                code        = e.code,
                name        = e.name,
                type        = int.Parse(e.type),
                count       = (int)decimal.Parse(e.count),
                money       = decimal.Parse(e.money),
                time_dt     = DateTime.Parse(e.date.ToDate() + " " + e.time.ToTime()),
                deal_no     = e.deal_no,
                order_no    = e.order_no,
                price       = decimal.Parse(e.price),
                unit_id     = unit_id,
                account_id  = account_id,
                transferred = unit_id > 0 ? 0 : 1,
            };

            DealRA.Add(deal, key);

            //更新成交均价
            DealAveragePrice(deal);

            //系统内成交
            if (unit_id > 0)
            {
                MessageBiz.Send(order.user_id.ToString(), MessageTypeEnum.Order_Dealt, "[" + deal.code + "]" + deal.name + "已成交,成交数量:" + deal.count);
                TradeBiz.NewDeal(deal, order.price);
                MonitorRA.Increment("account_" + account_id, "deal_count");
            }
        }
Example #2
0
        public Result <List <Deal> > ListDeal(SearchUnit model)
        {
            if (model.unit_id <= 0)
            {
                return(Result <List <Deal> >(ApiResultEnum.Parameter_Error, null));
            }

            IEnumerable <Deal> lst = new List <Deal>();

            if (model.from_dt.Date < DateTime.Now.Date)
            {
                lst = lst.Union(DealDA.List(model));
            }
            if (model.from_dt.Date <= DateTime.Now.Date && model.to_dt.Date >= DateTime.Now.Date)
            {
                lst = lst.Union(DealRA.List4Unit(model.unit_id));
            }
            return(Result(lst.OrderBy(o => o.time_dt).ToList()));
        }
Example #3
0
        //更新成交均价
        private void DealAveragePrice(Deal deal)
        {
            string[] keys  = TradeRA.KeySearch("D_*_O_" + deal.order_no + "_U_*");
            decimal  money = 0;
            int      count = 0;

            foreach (string key in keys)
            {
                Deal dl = DealRA.Get(key);
                money += dl.price * dl.count;
                count += dl.count;
            }
            string[] keys_order = TradeRA.KeySearch("O_" + deal.order_no + "_*_U_" + deal.unit_id);
            if (keys_order.Length > 0)
            {
                decimal deal_average_price = (money + deal.price * deal.count) / (count + deal.count);
                OrderRA.UpdateAverageOrice(Math.Round(deal_average_price, 3), keys_order[0]);
            }
        }
Example #4
0
        public Result <List <Deal> > ListDeal(SearchDealStatus model)
        {
            IEnumerable <Deal> lst = new List <Deal>();

            if (model.from_dt.Date < DateTime.Now.Date)
            {
                lst = lst.Union(DealDA.List(model));
            }
            if (model.from_dt.Date <= DateTime.Now.Date && model.to_dt.Date >= DateTime.Now.Date)
            {
                if (model.status == DealStatusEnum.In)
                {
                    lst = lst.Union(DealRA.List().Where(d => d.unit_id > 0));
                }
                else if (model.status == DealStatusEnum.Out)
                {
                    lst = lst.Union(DealRA.List().Where(d => d.unit_id == 0));
                }
            }
            return(Result(lst.OrderBy(o => o.time_dt).ToList()));
        }
Example #5
0
        public static void NewDeal(Deal deal, decimal order_price, int type = 0)
        {
            //计算手续费,佣金最少收5元
            string key_unit = "U_" + deal.unit_id;
            Unit   unit     = UnitRA.Get(key_unit);

            deal.commission     = Math.Max(Math.Round(deal.money * unit.ratio_commission, 2), 5);
            deal.management_fee = Math.Round(deal.money * unit.ratio_management_fee, 2);

            //卖单计算盈亏
            Position position = PositionRA.Get("P_" + deal.code + "_A_" + deal.account_id + "_U_" + deal.unit_id);

            if (deal.type == 1 && position != null)
            {
                deal.profit = Math.Round((deal.price - position.price_cost) * deal.count - deal.commission, 2);
            }

            //更新手续费和盈亏
            DealRA.UpdateFee(deal, "D_" + deal.deal_no + "_O_" + deal.order_no + "_U_" + deal.unit_id);

            AdjustPosition(position, deal, order_price, type);
        }
Example #6
0
        public Result Transfer(Transfer model)
        {
            string[] keys = TradeRA.KeySearch("D_" + model.deal_no + "_*_U_0");
            if (keys.Length > 0 && TradeRA.KeyExists("U_" + model.unit_id))
            {
                Deal deal = DealRA.Get(keys[0]);
                if (deal.type_enum == OrderTypeEnum.Sell)
                {
                    int sellable_count = PositionRA.GetSellable(model.unit_id, deal.code, deal.account_id);
                    if (deal.count > sellable_count)
                    {
                        return(Result(ApiResultEnum.Order_Account_Negative_Position));
                    }
                }

                deal.unit_id = model.unit_id;
                DealRA.UpdateUnit(model.unit_id, keys[0]);
                TradeRA.KeyRename(keys[0], keys[0].Substring(0, keys[0].Length - 1) + model.unit_id);
                TradeBiz.NewDeal(deal, deal.price, 1);
                return(Result(ApiResultEnum.Success));
            }
            return(Result(ApiResultEnum.Failed));
        }
Example #7
0
 public static void SaveDeal(ref Dictionary <int, decimal[]> dic)
 {
     string[] keys = TradeRA.KeySearch("D_*");
     foreach (string key in keys)
     {
         Deal deal = DealRA.Get(key);
         DealDA.Add(deal);
         if (deal.unit_id > 0)
         {
             if (dic.ContainsKey(deal.unit_id))
             {
                 dic[deal.unit_id][0] = dic[deal.unit_id][0] + deal.commission;
                 dic[deal.unit_id][1] = dic[deal.unit_id][1] + deal.profit;
             }
             else
             {
                 dic.Add(deal.unit_id, new decimal[2] {
                     deal.commission, deal.profit
                 });
             }
         }
     }
 }