示例#1
0
 public static void SavePosition()
 {
     string[] keys = TradeRA.KeySearch("P_*");
     foreach (string key in keys)
     {
         Position position = PositionRA.Get(key);
         if (position.count == 0)
         {
             if (position.id > 0)
             {
                 PositionDA.Delete(position.id);
             }
         }
         else
         {
             position.price_latest = HQService.Get(position.code).Last;
             if (position.id > 0)
             {
                 PositionDA.Update(position);
             }
             else
             {
                 PositionDA.Add(position);
             }
         }
     }
 }
示例#2
0
        public Result PositionTransfer(PositionTransfer model)
        {
            if (model.from_unit_id > 0 && model.to_unit_id > 0)
            {
                HQItem  hq           = HQService.Get(model.code);
                decimal price_latest = hq != null ? hq.Last : 0;

                List <Position> list = new List <Position>();
                string[]        keys = TradeRA.KeySearch("P_" + model.code + "_*_U_" + model.from_unit_id);
                foreach (var key in keys)
                {
                    list.Add(PositionRA.Get(key));
                }
                if (model.count > list.Sum(p => p.count_sellable))
                {
                    int transfer_total = 0;
                    foreach (Position position in list)
                    {
                        int transfer = Math.Min(model.count, position.count_sellable);
                        PositionRA.UpdateTransfer(model.from_unit_id, model.to_unit_id, position.account_id, (model.to_account_id == 0 ? position.account_id : model.to_account_id), model.code, transfer, price_latest);
                        transfer_total += transfer;

                        if (transfer_total == model.count)
                        {
                            break;
                        }
                    }
                    return(Result(ApiResultEnum.Success));
                }
            }
            return(Result(ApiResultEnum.Failed));
        }
示例#3
0
        public static void Open()
        {
            HQService.SubscribeStart();
            var accounts = AccountDA.List <Account>();

            foreach (Account account in accounts)
            {
                LoadAccount(account);
            }

            var units = UnitDA.List <Unit>();

            foreach (Unit unit in units)
            {
                LoadUnit(unit);
            }

            var positions = PositionDA.List();

            foreach (Position position in positions)
            {
                LoadPosition(position);
            }

            var items = AccountGroupDA.ListItems();

            foreach (AccountGroupItem item in items)
            {
                LoadAccountGroupItem(item);
            }

            HQService.Get(positions.Select(p => p.code));
            TradeBiz.Start();
        }
示例#4
0
        public Result <long> OrderAutoAdd(OrderAutoAdd model)
        {
            if (model.count_min > model.count_max || model.price_min > model.price_max || model.time_min > model.time_max)
            {
                return(Result(ApiResultEnum.Parameter_Error, 0L));
            }

            HQItem hq    = HQService.Get(model.code);
            Order  order = new Order()
            {
                user_id  = user_id,
                unit_id  = model.unit_id,
                code     = model.code,
                price    = hq.Last,
                count    = model.count_total,
                type     = model.type,
                platform = platform
            };

            ApiResultEnum result = LimitBiz.CheckStatus(order);

            if (result != ApiResultEnum.Success)
            {
                return(Result(result, 0L));
            }
            result = LimitBiz.CheckUnit(order);
            if (result != ApiResultEnum.Success)
            {
                return(Result(result, 0L));
            }

            model.name     = StockInfoBiz.GetStock(model.code).name;
            model.id       = FuncHelper.GetUniqueID();
            model.user_id  = user_id;
            model.platform = platform;
            if (model.account_id > 0)
            {
                model.account_name = AccountRA.GetName(model.account_id);
            }
            else
            {
                model.account_name = "";
            }
            string key = "S_" + model.id + "_U_" + model.unit_id + "_D_0";

            OrderAutoRA.Add(model, key);
            return(Result(ApiResultEnum.Success, model.id));
        }
示例#5
0
        public static void AdjustPosition(Position position, Deal deal, decimal order_price, int type)
        {
            string key_unit     = "U_" + deal.unit_id;
            string key_position = "P_" + deal.code + "_A_" + deal.account_id + "_U_" + deal.unit_id;

            //更新单元余额和冻结
            if (deal.type_enum == OrderTypeEnum.Buy)
            {
                UnitRA.UpdateCapitalDealBuy(order_price * deal.count, type == 0, key_unit);
            }
            else if (deal.type_enum == OrderTypeEnum.Sell)
            {
                UnitRA.UpdateCapitalDealSell(deal.price * deal.count, key_unit);
            }

            //更新持仓
            decimal amount = deal.money + deal.commission;

            if (position != null)
            {
                if (deal.type == 0)
                {
                    position.price_cost = Math.Round((position.price_cost * position.count + amount) / (deal.count + position.count), 6);
                    position.count      = position.count + deal.count;
                    if (type == 0 || type == 1)
                    {
                        position.price_cost_today_buy = Math.Round((amount + position.price_cost_today_buy * position.count_today_buy) / (deal.count + position.count_today_buy), 6);
                        position.count_today_buy      = deal.count + position.count_today_buy;
                    }
                    PositionRA.UpdateBuy(position, key_position);
                }
                else
                {
                    position.count = position.count - deal.count;
                    if (type == 0 || type == 1)
                    {
                        position.price_cost_today_sell = Math.Round((amount + position.price_cost_today_sell * position.count_today_sell) / (deal.count + position.count_today_sell), 6);
                        position.count_today_sell      = deal.count + position.count_today_sell;
                    }
                    PositionRA.UpdateSell(position, key_position);
                }
            }
            else
            {
                HQItem    hq    = HQService.Get(deal.code);
                BlockInfo block = StockInfoBiz.GetBlock(deal.code);
                if (block == null)
                {
                    return;
                }

                position = new Position()
                {
                    code         = deal.code,
                    name         = deal.name,
                    unit_id      = deal.unit_id,
                    account_id   = deal.account_id,
                    block_enum   = block.block_type_enum,
                    price_latest = hq != null ? hq.Last : 0
                };
                if (deal.type == 0)
                {
                    position.price_cost = Math.Round(amount / deal.count, 6);
                    position.count      = deal.count;
                    if (type == 0 || type == 1)
                    {
                        position.price_cost_today_buy = position.price_cost;
                        position.count_today_buy      = position.count;
                    }
                    else if (type == 2)
                    {
                        position.count_sellable = position.count;
                    }
                }
                else
                {
                    position.price_cost = Math.Round(amount / deal.count, 6);
                    position.count      = position.count_sellable = -deal.count;
                    if (type == 0 || type == 1)
                    {
                        position.price_cost_today_sell = position.price_cost;
                        position.count_today_sell      = deal.count;
                    }
                }
                PositionRA.Add(position, key_position);
            }
            RunCalculateValue();
        }
示例#6
0
        public static void RunCalculateValue()
        {
            //单元的市值
            Dictionary <int, decimal[]> dic_unit = new Dictionary <int, decimal[]>();
            //主账户的市值
            Dictionary <int, decimal> dic_account = new Dictionary <int, decimal>();
            //单元下主账户组中的主账户市值
            Dictionary <string, decimal> dic_account_group = new Dictionary <string, decimal>();

            string[] keys = TradeRA.KeySearch("P_*");
            foreach (string key in keys)
            {
                Position position = PositionRA.Get(key);
                HQItem   hq       = HQService.Get(position.code);
                decimal  price    = hq == null ? position.price_latest : hq.Last;
                decimal  value    = price * position.count;
                decimal  profit   = (price - position.price_cost) * position.count;
                if (dic_unit.ContainsKey(position.unit_id))
                {
                    dic_unit[position.unit_id][position.block] += value;
                    dic_unit[position.unit_id][4] += value;
                    dic_unit[position.unit_id][5] += profit;
                }
                else
                {
                    decimal[] values = new decimal[6] {
                        0, 0, 0, 0, value, profit
                    };
                    values[position.block] = value;
                    dic_unit.Add(position.unit_id, values);
                }

                //主账户市值暂只计算创业板
                if (position.block_enum == BlockEnum.gem)
                {
                    if (dic_account.ContainsKey(position.account_id))
                    {
                        dic_account[position.account_id] += value;
                    }
                    else
                    {
                        dic_account.Add(position.account_id, value);
                    }
                }

                string ua = "U_" + position.unit_id + "_A_" + position.account_id;
                if (dic_account_group.ContainsKey(ua))
                {
                    dic_account_group[ua] += value;
                }
                else
                {
                    dic_account_group.Add(ua, value);
                }
            }
            foreach (var kvp in dic_unit)
            {
                UnitRA.UpdateCapitalStockValue(kvp.Value, "U_" + kvp.Key);
            }
            foreach (var kvp in dic_account)
            {
                AccountRA.UpdateCapitalStockValue(kvp.Value, "A_" + kvp.Key);
            }
            foreach (var kvp in dic_account_group)
            {
                AccountGroupRA.UpdateCapitalStockValue(kvp.Key, kvp.Value);
            }
        }
示例#7
0
文件: DataBiz.cs 项目: wshPrt/trading
        public static decimal GetPriceByPriceType(string code, OrderPriceEnum priceEnum)
        {
            HQItem  hq    = HQService.Get(code);
            decimal price = hq.Last;

            switch (priceEnum)
            {
            case OrderPriceEnum.Buy1:
                price = hq.Buy_1;
                break;

            case OrderPriceEnum.Buy2:
                price = hq.Buy_2;
                break;

            case OrderPriceEnum.Buy3:
                price = hq.Buy_3;
                break;

            case OrderPriceEnum.Buy4:
                price = hq.Buy_4;
                break;

            case OrderPriceEnum.Buy5:
                price = hq.Buy_5;
                break;

            case OrderPriceEnum.Sell1:
                price = hq.Sell_1;
                break;

            case OrderPriceEnum.Sell2:
                price = hq.Sell_2;
                break;

            case OrderPriceEnum.Sell3:
                price = hq.Sell_3;
                break;

            case OrderPriceEnum.Sell4:
                price = hq.Sell_4;
                break;

            case OrderPriceEnum.Sell5:
                price = hq.Sell_5;
                break;
            }
            if (price == 0)
            {
                switch (priceEnum)
                {
                case OrderPriceEnum.Buy1:
                case OrderPriceEnum.Buy2:
                case OrderPriceEnum.Buy3:
                case OrderPriceEnum.Buy4:
                case OrderPriceEnum.Buy5:
                    price = hq.Limit_Low;
                    break;

                case OrderPriceEnum.Sell1:
                case OrderPriceEnum.Sell2:
                case OrderPriceEnum.Sell3:
                case OrderPriceEnum.Sell4:
                case OrderPriceEnum.Sell5:
                    price = hq.Limit_High;
                    break;
                }
            }
            return(price);
        }