Exemplo n.º 1
0
        public Account GetAccountDetail(string hash, string userLogin, long localTime,
                                        int accountId, bool calculateEquity,
                                        out decimal brokerLeverage, out decimal exposure)
        {
            brokerLeverage = 0;
            exposure       = 0;

            try
            {
                using (var ctx = DatabaseContext.Instance.Make())
                {
                    var user = ctx.PLATFORM_USER.FirstOrDefault(u => u.Login == userLogin);
                    if (user == null)
                    {
                        return(null);
                    }

                    var userHash = CredentialsHash.MakeCredentialsHash(userLogin, user.Password, localTime);
                    if (userHash != hash)
                    {
                        return(null);
                    }

                    // авторизован?
                    if (!PlatformUser.IsManager(user.RoleMask) && !PlatformUser.IsAdmin(user.RoleMask))
                    {
                        if (!ctx.PLATFORM_USER_ACCOUNT.Any(pa => pa.PlatformUser == user.ID && pa.Account == accountId))
                        {
                            return(null);
                        }
                    }

                    var account = LinqToEntity.DecorateAccount(ctx.ACCOUNT.First(a => a.ID == accountId));
                    if (!calculateEquity)
                    {
                        return(account);
                    }

                    // получить открытые ордера по счету
                    var orders = ctx.POSITION.Where(p => p.AccountID == accountId && p.State ==
                                                    (int)PositionState.Opened).ToList().Select(LinqToEntity.DecorateOrder).ToList();

                    // посчитать открытый результат и экспозицию в валюте депо
                    var quotes = QuoteStorage.Instance.ReceiveAllData();
                    var errors = new List <string>();
                    exposure = DalSpot.Instance.CalculateExposure(orders, quotes, account.Currency, errors);
                    var group = ctx.ACCOUNT_GROUP.First(g => g.Code == account.Group);
                    brokerLeverage     = group.BrokerLeverage;
                    account.UsedMargin = exposure / brokerLeverage;
                    return(account);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Ошибка в GetAccountDetail()", ex);
                return(null);
            }
        }
Exemplo n.º 2
0
        public bool SetBalance(string hash, string userLogin, long localTime,
                               int accountId, decimal newBalance, string comment, out string errorString)
        {
            errorString = string.Empty;
            try
            {
                using (var ctx = DatabaseContext.Instance.Make())
                {
                    var user = ctx.PLATFORM_USER.FirstOrDefault(u => u.Login == userLogin);
                    if (user == null)
                    {
                        errorString = "Unauthorised (not found)";
                        return(false);
                    }

                    var userHash = CredentialsHash.MakeCredentialsHash(userLogin, user.Password, localTime);
                    if (userHash != hash)
                    {
                        errorString = "Unauthorised (wrong credentials)";
                        return(false);
                    }

                    if (!PlatformUser.IsAdmin(user.RoleMask) &&
                        !PlatformUser.IsManager(user.RoleMask))
                    {
                        errorString = "Unauthorised (insufficient rights)";
                        return(false);
                    }

                    var account = ctx.ACCOUNT.FirstOrDefault(a => a.ID == accountId);
                    if (account == null)
                    {
                        errorString = "Account " + accountId + " was not found";
                        return(false);
                    }

                    var delta = newBalance - account.Balance;
                    if (delta == 0)
                    {
                        return(true);
                    }

                    // сформировать транзакцию и поправить баланс
                    var sign   = Math.Sign(delta);
                    var amount = Math.Abs(delta);
                    var trans  = new BALANCE_CHANGE
                    {
                        AccountID   = accountId,
                        ChangeType  = sign > 0 ? (int)BalanceChangeType.Deposit : (int)BalanceChangeType.Withdrawal,
                        Amount      = amount,
                        ValueDate   = DateTime.Now,
                        Description = comment
                    };
                    account.Balance = newBalance;
                    ctx.BALANCE_CHANGE.Add(trans);
                    ctx.SaveChanges();
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Ошибка в SetBalance()", ex);
            }
            return(false);
        }