Exemplo n.º 1
0
        public ACCOUNT_SHARE_HISTORY CalculateAccountShare(TradeSharpConnection ctx,
                                                           ACCOUNT_SHARE share,
                                                           ACCOUNT_SHARE ownerShare,
                                                           ref decimal ownersMoney,
                                                           decimal equity, 
                                                           PaidService serviceWithFeeScale,
                                                           out bool feeWasTaken)
        {
            feeWasTaken = false;
            var shareMoney = equity*share.Share/100M;
            var hist = new ACCOUNT_SHARE_HISTORY
                {
                    Account = share.Account,
                    Date = DateTime.Now,
                    OldShare = share.Share,
                    OldHWM = share.HWM,
                    NewShare = share.Share,
                    NewHWM = Math.Max(share.HWM ?? 0, shareMoney),
                    ShareOwner = share.ShareOwner,
                    ShareAmount = shareMoney
                };

            share.HWM = hist.NewHWM;
            if ((hist.OldHWM ?? 0) == 0)
                return hist;
            if (shareMoney <= hist.OldHWM)
                return hist;
            if (serviceWithFeeScale == null)
                return hist;

            // был установлен новый HWM?
            var aboveHwm = shareMoney - hist.OldHWM.Value;
            // посчитать сумму комиссии
            var fee = serviceWithFeeScale.CalculateFee(shareMoney, aboveHwm);
            if (fee < 0.01M) return hist;

            feeWasTaken = true;

            // этот самый кусочек приписать владельцу и списать его с пайщика
            ownersMoney += fee;
            shareMoney -= fee;
            ownerShare.Share = ownersMoney*100M/equity;
            share.Share = shareMoney*100M/equity;

            var newHwm = shareMoney;
            if (newHwm > share.HWM)
            {
                share.HWM = newHwm;
                hist.NewHWM = newHwm;
            }
            return hist;
        }