Exemplo n.º 1
0
 public ApiResult InvokeMethod(MainAccount account, string methodName, object[] arguments)
 {
     var service = GetTradeService();
     var result = InvokeApi(service, account, methodName, arguments);
     //如果出错,调用其它服务
     if (!string.IsNullOrEmpty(result.Error))
     {
         foreach (var kv in _services)
         {
             if (kv.Key.GetType() == service.GetType())
             {
                 continue;
             }
             if (!kv.Value)
             {
                 result = InvokeApi(kv.Key, account, methodName, arguments);
                 if (string.IsNullOrEmpty(result.Error))
                 {
                     SetMainService(kv.Key);
                     break;
                 }
             }
         }
     }
     return result;
 }
Exemplo n.º 2
0
        private static ApiResult InvokeApi(ITradeService service, MainAccount account, string methodName, object[] arguments)
        {
            var host = ManagerCore.Instance.ApiHostManager.GetFastHost(service.GetType());
            service.Account = account;
            service.Host = host;
            service.Login();

            var method = service.GetType().GetMethod(methodName);
            if (method == null)
            {
                throw new Exception("没有找到接口" + methodName);
            }
            var result = (ApiResult)method.Invoke(service, arguments);

            if (!string.IsNullOrEmpty(result.Error))
            {
                //尝试一次登录
                if (result.Error.Contains("连接已断开"))
                {
                    service.Logout();
                    service.Login();
                    result = (ApiResult)method.Invoke(service, arguments);
                }
            }
            return result;
        }
Exemplo n.º 3
0
 public void UpdateMainAccount(MainAccount account)
 {
     _account = account;
     foreach (var service in _services)
     {
         service.Account = account;
     }
 }
Exemplo n.º 4
0
 public ServiceHost(MainAccount account)
 {
     _account = account;
     _services.Add(new AuthorizeService());
     _services.Add(new CloseAccountService());
     _services.Add(new RefreshDataService());
     _services.Add(new StockSyncService());
 }
Exemplo n.º 5
0
 /// <summary>
 /// 查询持仓接口
 /// </summary>
 /// <returns></returns>
 public List<ChildStock> QueryStocks(MainAccount account)
 {
     var list = new List<ChildStock>();
     var result = Core.ServiceManager.QueryStocks(account);
     if (result.Result)
     {
         foreach (var line in result.Data.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries))
         {
             list.Add(ChildStock.Parse(line));
         }
     }
     return list;
 }
Exemplo n.º 6
0
        /// <summary>
        /// 通过接口获取“当日委托”列表
        /// </summary>
        public List<ChildAuthorize> GetTodayAuthorize(MainAccount account)
        {
            var result = Core.ServiceManager.QueryAuthroizes(account);
            if (!result.Result)
            {
                throw new Exception("当日委托获取失败\n" + result.Error);
            }

            var list = new List<ChildAuthorize>();
            var rows = result.Data.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var row in rows)
            {
                list.Add(ChildAuthorize.Parse(row));
            }
            return list;
        }
Exemplo n.º 7
0
 /// <summary>
 /// 同步持仓股票市价
 /// </summary>
 public void SyncStocks(MainAccount account)
 {
     using (var db = GetDbContext())
     {
         var childIds = Core.AccountManager.GetChildIds(account.MainID);
         var list = db.ChildStocks.Where(e => childIds.Contains(e.ChildID));
         var stockCodes = list.Select(e => e.StockCode).ToArray();
         var data = QueryMarket(stockCodes);
         var rows = data.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
         foreach (var row in rows)
         {
             var market = StockMarket.Parse(row);
             var entity = list.FirstOrDefault(e => e.StockCode == market.StockCode);
             entity.CurrentPrice = market.CurrentPrice;
         }
         db.SaveChanges();
     }
 }
Exemplo n.º 8
0
 public ApiResult QueryHistoryMoney(MainAccount account, DateTime startTime, DateTime endTime)
 {
     return TradeServiceInvoker.InvokeMethod(account, "QueryHistoryMoney", new object[] { startTime, endTime });
 }
Exemplo n.º 9
0
 public ApiResult QueryMoney(MainAccount account)
 {
     return TradeServiceInvoker.InvokeMethod(account, "QueryMoney", null);
 }
Exemplo n.º 10
0
 public ApiResult QueryAuthroizes(MainAccount account)
 {
     return TradeServiceInvoker.InvokeMethod(account, "QueryAuthroizes", null);
 }
Exemplo n.º 11
0
 public ApiResult Cancel(MainAccount account, string stockCode, string authorizeIndex)
 {
     return TradeServiceInvoker.InvokeMethod(account, "Cancel", new object[] { stockCode, authorizeIndex });
 }
Exemplo n.º 12
0
 public ApiResult Sell(MainAccount account, string stockCode, int number, double price)
 {
     return TradeServiceInvoker.InvokeMethod(account, "Sell", new object[] { stockCode, number, price });
 }
Exemplo n.º 13
0
        public void UpdateChildAccountMoneyAndStocks(MainAccount account)
        {
            using (var db = GetDbContext())
            {
                //更新所有子帐户的可取余额
                var childIds = GetChildIds(account.MainID);
                foreach (var childId in childIds)
                {
                    var child = db.ChildAccounts.FirstOrDefault(e => e.ChildID == childId);
                    child.AdvisableMoney = child.UseableMoney;
                }
                //更新所有子帐户的持仓余额
                var stocks = db.ChildStocks.Where(e => childIds.Contains(e.ChildID));
                foreach (var stock in stocks)
                {
                    stock.UseableCount = stock.AllCount;
                    //如果持仓为0,则删除该股票
                    if (stock.UseableCount == 0)
                    {
                        db.ChildStocks.Remove(stock);
                    }
                }

                db.SaveChanges();
            }
        }