Ejemplo n.º 1
0
 /// <summary>
 /// 查询持仓
 /// </summary>
 public List<ChildStock> GetChildStocks(ChildAccount child)
 {
     using (var db = GetDbContext())
     {
         return db.ChildStocks.Where(e => e.ChildID == child.ChildID).ToList();
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 计算冻结资金
        /// </summary>
        /// <returns></returns>
        public double GetFrozenMoney(ChildAccount child)
        {
            //卖出不是
            if (TradeFlag == "0")
            {
                return(0);
            }
            var total = AuthorizePrice * AuthorizeCount + child.GetGuoHuFei(StockCode, AuthorizePrice, AuthorizeCount)
                        + child.GetShouXuFei(StockCode, AuthorizePrice, AuthorizeCount);

            switch (AuthorizeState)
            {
            case "失败":
            case "废单":
                return(0);

            case "已成":
            case "部成":
                var tradeMoney = child.GetGuoHuFei(StockCode, StrikePrice, StrikeCount) + child.GetShouXuFei(StockCode, StrikePrice, StrikeCount) + StrikeCount * StrikePrice;
                return(total - tradeMoney);

            default:
                if (AuthorizeState.Contains("撤"))
                {
                    return(0);
                }
                return(total);
            }
        }
Ejemplo n.º 3
0
 public List<ChildAuthorize> GetList(ChildAccount child)
 {
     using (var db = GetDbContext())
     {
         return db.ChildAuthorizes.Where(e => e.ChildID == child.ChildID).ToList();
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 计算冻结资金
        /// </summary>
        /// <returns></returns>
        public double GetFrozenMoney(ChildAccount child)
        {
            //卖出不是
            if (TradeFlag == "0") return 0;
            var total = AuthorizePrice * AuthorizeCount + child.GetGuoHuFei(StockCode, AuthorizePrice, AuthorizeCount)
                + child.GetShouXuFei(StockCode, AuthorizePrice, AuthorizeCount);

            switch (AuthorizeState)
            {
                case "失败":
                case "废单":
                    return 0;
                case "已成":
                case "部成":
                    var tradeMoney = child.GetGuoHuFei(StockCode, StrikePrice, StrikeCount) + child.GetShouXuFei(StockCode, StrikePrice, StrikeCount) + StrikeCount * StrikePrice;
                    return total - tradeMoney;
                default:
                    if (AuthorizeState.Contains("撤"))
                    {
                        return 0;
                    }
                    return total;
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// 获取冻结资金
 /// </summary>
 public double GetFrozenMoney(ChildAccount child)
 {
     //因为每天需要结算,所以如果是昨天的委托,则判断状态,如果是今天的,查询买入且没有撤单、废单、部撤的
     using (var db = GetDbContext())
     {
         var minDate = DateTime.Today.AddDays(-1).ToUnixTime();
         var list = db.ChildAuthorizes.Where(e => e.ChildID == child.ChildID && e.AuthorizeTimeValue > minDate).ToList();
         double result = 0;
         foreach (var item in list)
         {
             //如果是昨日成交的委托,则不参与计算
             if (item.TradeTime > 0 && item.TradeTime < DateTime.Today.ToUnixTime())
             {
                 continue;
             }
             result += item.GetFrozenMoney(child);
         }
         return result;
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// 买入股票
        /// </summary>
        public void ToBuy(string stockCode, int number, double price, ChildAccount child)
        {
            using (var db = GetDbContext())
            {
                if(!IsTradeTime())
                {
                    throw new Exception("当前时间不能交易");
                }
                //总费用
                var totalPrice = number * price + child.GetShouXuFei(stockCode, price, number) + child.GetGuoHuFei(stockCode, price, number);
                //判断余额
                if (child.UseableMoney < totalPrice)
                {
                    throw new Exception("余额不足");
                }
                //验证是否被禁止购买
                if (db.StockTradeSets.Any(e => e.ParentID == child.ParentID && e.StockCode == stockCode))
                {
                    throw new Exception("该股票禁止购买");
                }

                //调用接口购买
                var result = Core.ServiceManager.Buy(child.Parent, stockCode, number, price);
                var authorize = new ChildAuthorize
                {
                    ID = DateTime.Now.ToString("yyyyMMddHHmmssffff"),
                    AuthorizeIndex = 0,
                    AuthorizeCount = number,
                    AuthorizePrice = price,
                    ChildID = child.ChildID,
                    ChildCommission = child.Commission,
                    StockCode = stockCode,
                    StockName = stockCode,//委托创建时无法获得股票名称
                    TradeFlag = "1",
                    MainCommission = child.Parent.Commission,
                    MainGuoHuFei = child.Parent.GuoHuFei,
                    MainYinHuaShui = child.Parent.YinHuaShui,
                    AuthorizeState = "未报",
                    AuthorizeTime = DateTime.Now,
                    OverFlowMoney = child.UseableMoney - totalPrice,//佣金、过户费要在成交时扣除
                };

                if (result.Result)
                {
                    var toUpdateChildEntity = db.ChildAccounts.FirstOrDefault(e => e.ChildID == child.ChildID);
                    //冻结股票资金和相关费用
                    toUpdateChildEntity.UseableMoney -= totalPrice;
                    //委托编号
                    authorize.AuthorizeIndex = int.Parse(result.Data);

                    db.ChildAuthorizes.Add(authorize);
                    db.SaveChanges();
                }
                else
                {
                    authorize.AuthorizeState = "失败";
                    db.ChildAuthorizes.Add(authorize);
                    db.SaveChanges();
                    throw new Exception("委托失败\n" + result.Error);
                }

            }

        }
Ejemplo n.º 7
0
        /// <summary>
        /// 获取当日成交
        /// </summary>
        public List<StockTrade> GetTodayTrades(ChildAccount child)
        {
            var result = Core.ServiceManager.QueryTrades(child.Parent);
            if (result.Result)
            {
                var list = new List<StockTrade>();
                var lines = result.Data.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var line in lines)
                {
                    list.Add(StockTrade.Parse(line));
                }

                var childStockCodes = Core.StockManager.GetStockCodes(child.ChildID);
                return list.Where(e => childStockCodes.Contains(e.StockCode)).ToList();
            }
            throw new Exception(result.Error);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// 获取历史资金流水
 /// </summary>
 public List<FundFlow> GetHistoryMoney(DateTime startDate, DateTime endDate, ChildAccount child)
 {
     var result = Core.ServiceManager.QueryHistoryMoney(child.Parent, startDate, endDate);
     if (result.Result)
     {
         var list = new List<FundFlow>();
         var lines = result.Data.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
         foreach (var line in lines)
         {
             list.Add(FundFlow.Parse(line));
         }
         var childStockCodes = Core.StockManager.GetStockCodes(child.ChildID);
         return list.Where(e => childStockCodes.Contains(e.StockCode)).ToList();
     }
     throw new Exception(result.Error);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// 撤单
 /// </summary>
 public void CancelOrder(int authorizeIndex, string stockCode, ChildAccount child)
 {
     using (var db = GetDbContext())
     {
         if (!IsTradeTime())
         {
             throw new Exception("当前时间不能交易");
         }
         var authorize = db.ChildAuthorizes.FirstOrDefault(e => e.AuthorizeIndex == authorizeIndex && e.ChildID == child.ChildID);
         if (authorize == null)
         {
             throw new ArgumentException("没有找到该委托");
         }
         if (authorize.AuthorizeState.Contains("撤"))
         {
             throw new Exception("该委托已提交过撤单");
         }
         var result = Core.ServiceManager.Cancel(child.Parent, stockCode, authorize.AuthorizeIndex.ToString());
         if (result.Result)
         {
             authorize.AuthorizeState = "待撤";
             db.SaveChanges();
         }
         else
         {
             throw new Exception("撤单失败\n" + result.Error);
         }
     }
 }
Ejemplo n.º 10
0
        /// <summary>
        /// 卖出股票
        /// </summary>
        public void ToSell(string stockCode, int number, double price, ChildAccount child)
        {
            using (var db = GetDbContext())
            {
                if (!IsTradeTime())
                {
                    throw new Exception("当前时间不能交易");
                }
                var stock = db.ChildStocks.FirstOrDefault(e => e.StockCode == stockCode && e.ChildID == child.ChildID);
                if (stock == null)
                {
                    throw new ArgumentException("没有持有该股票");
                }
                //查看持仓 数量是否符合
                if (stock.UseableCount < number)
                {
                    throw new ArgumentException("没有足够的股票可以卖出");
                }
                //如果有零手股,则number必须是100的倍数或等于可用股票余额数
                if (stock.UseableCount % 100 != 0 && number % 100 != 0 && number != stock.UseableCount)
                {
                    throw new ArgumentException("卖出股票数量不正确");
                }

                //调用卖出接口 
                var result = Core.ServiceManager.Sell(child.Parent, stockCode, number, price);
                //声明一个新委托
                var model = new ChildAuthorize
                {
                    ID = DateTime.Now.ToString("yyyyMMddHHmmssffff"),
                    AuthorizeIndex = 0,
                    AuthorizeCount = number,
                    AuthorizePrice = price,
                    StockCode = stockCode,
                    AuthorizeState = "待报",
                    AuthorizeTime = DateTime.Now,
                    ChildCommission = child.Commission,
                    ChildID = child.ChildID,
                    StockName = stockCode,
                    TradeFlag = "0",
                    OverFlowMoney = child.UseableMoney,
                    MainCommission = child.Parent.Commission,
                    MainYinHuaShui = child.Parent.YinHuaShui,
                    MainGuoHuFei = child.Parent.GuoHuFei,
                };
                //如果调用接口成功
                if (result.Result)
                {
                    //如果是卖出,先扣除印花税,成交时不再扣,如果部分成交,则需要返还部分税
                    var toUpdateChild = db.ChildAccounts.FirstOrDefault(e => e.ChildID == child.ChildID);
                    toUpdateChild.UseableMoney -= child.GetYinHuaShui(model.StockCode, model.StrikePrice, model.StrikeCount);

                    //赋值委托编号
                    model.AuthorizeIndex = int.Parse(result.Data);
                    //持仓总量-卖出数量
                    stock.AllCount -= number;
                    //可用数量-卖出数量(可卖出股票必定是可用股票)
                    stock.UseableCount -= number;

                    db.ChildAuthorizes.Add(model);
                    db.SaveChanges();
                }
                else
                {
                    db.ChildAuthorizes.Add(model);
                    db.SaveChanges();
                    throw new Exception("卖出失败\n" + result.Error);
                }
            }
        }