Esempio n. 1
0
 public List <ChildAuthorize> GetList(ChildAccount child)
 {
     using (var db = GetDbContext())
     {
         return(db.ChildAuthorizes.Where(e => e.ChildID == child.ChildID).ToList());
     }
 }
Esempio n. 2
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);
         }
     }
 }
Esempio n. 3
0
 /// <summary>
 /// 查询持仓
 /// </summary>
 public List <ChildStock> GetChildStocks(ChildAccount child)
 {
     using (var db = GetDbContext())
     {
         return(db.ChildStocks.Where(e => e.ChildID == child.ChildID).ToList());
     }
 }
Esempio n. 4
0
 protected override void OnActionExecuting(ActionExecutingContext filterContext)
 {
     base.OnActionExecuting(filterContext);
     if (CurrentUser.IsAuthenticated)
     {
         CurrentAccount = Core.AccountManager.GetChildAccount(CurrentUser.ID);
     }
 }
Esempio n. 5
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);
        }
Esempio n. 6
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);
        }
Esempio n. 7
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);
     }
 }
Esempio n. 8
0
        public bool Login(ChildAccount child)
        {
            var hasLogin = Cache.HGet <DateTime?>(CacheKey, child.ParentID);

            if (hasLogin != null)
            {
                return(true);
            }

            using (var db = GetDbContext())
            {
                var main = db.MainAccounts.FirstOrDefault(e => e.MainID == child.ParentID);
                TdxTrade.SetAccount(main.MainID, main.TradePassword, main.MessagePassword);
                if (TdxTrade.Login())
                {
                    Cache.HSet(CacheKey, child.ParentID, DateTime.Now);
                    return(true);
                }
                else
                {
                    throw new Exception("登录失败");
                }
            }
        }
Esempio n. 9
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);
                }
            }
        }
Esempio 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);
                }
            }
        }