Esempio n. 1
0
        internal void CalculateNotValuedInterestAndStorage(CurrencyRate currencyRate, out decimal interestPL, out decimal storagePL)
        {
            interestPL = storagePL = 0;
            foreach (var eachInterest in _notValuedDayInterests)
            {
                interestPL += currencyRate.Exchange(eachInterest);
            }

            foreach (var eachStorage in _notValuedDayStorages)
            {
                storagePL += currencyRate.Exchange(eachStorage);
            }
        }
Esempio n. 2
0
        internal static decimal Calculate(TradePLFormula tradePLFormula, decimal quantity, decimal buyPrice,
                                          decimal sellPrice, decimal closePrice, CurrencyRate currencyRate)
        {
            var tradePL = Calculate(tradePLFormula, quantity, buyPrice, sellPrice, closePrice);

            return(currencyRate.Exchange(tradePL));
        }
Esempio n. 3
0
        internal static decimal CaculateInstalmentAdministrationFee(decimal value, decimal lot, InstalmentPolicyDetail instalmentPolicyDetail, CurrencyRate currencyRate)
        {
            decimal result = 0;

            switch (instalmentPolicyDetail.InstalmentFeeType)
            {
            case InstalmentFeeType.FixedAmountPerLot:
                result = lot * instalmentPolicyDetail.InstalmentFeeRate;
                result = Math.Round(result, currencyRate.TargetCurrency.Decimals, MidpointRounding.AwayFromZero);
                break;

            case InstalmentFeeType.ValueProportion:
                result = value * instalmentPolicyDetail.InstalmentFeeRate;
                result = -currencyRate.Exchange(-result);
                break;

            case InstalmentFeeType.FixedAmount:
                result = instalmentPolicyDetail.InstalmentFeeRate;
                result = Math.Round(result, currencyRate.TargetCurrency.Decimals, MidpointRounding.AwayFromZero);
                break;

            default:
                result = 0;
                break;
            }
            return(result);
        }
Esempio n. 4
0
        internal static decimal CalculateNecessary(Order order, MarginFormula marginFormula, decimal lotBalance, decimal contractSize, Price price, CurrencyRate currencyRate)
        {
            if (order.IsRisky)
            {
                decimal margin = 0;

                switch (marginFormula)
                {
                case MarginFormula.FixedAmount:
                    margin = lotBalance;
                    margin = Math.Round(margin, currencyRate.TargetCurrency.Decimals, MidpointRounding.AwayFromZero);
                    break;

                case MarginFormula.CS:
                    margin = lotBalance * contractSize;
                    margin = -currencyRate.Exchange(-margin);
                    break;

                case MarginFormula.CSiPrice:
                case MarginFormula.CSiMarketPrice:
                    margin = lotBalance * contractSize / (decimal)price;
                    margin = -currencyRate.Exchange(-margin);
                    break;

                case MarginFormula.CSxPrice:
                case MarginFormula.CSxMarketPrice:
                    margin = lotBalance * contractSize * (decimal)price;
                    margin = -currencyRate.Exchange(-margin);
                    break;

                case MarginFormula.FKLI:
                case MarginFormula.FCPO:
                    margin = lotBalance;
                    margin = Math.Round(margin, currencyRate.TargetCurrency.Decimals, MidpointRounding.AwayFromZero);
                    break;

                default:
                    throw new NotSupportedException(string.Format("{0} is not a supported MarginFormula", marginFormula));
                }

                return(margin);
            }
            else
            {
                return(0);
            }
        }
Esempio n. 5
0
        private decimal CaculatePaidPledgeForShortSell(decimal shortSellDownPayment, DateTime?tradeDay)
        {
            var          tran         = this.Owner;
            CurrencyRate currencyRate = tran.CurrencyRate(tradeDay);
            decimal      contractSize = tran.ContractSize(tradeDay);
            decimal      lotBalance   = this.LotBalance;
            Price        price        = this.ExecutePrice;
            decimal      paidPledge   = 0;

            switch (tran.SettingInstrument().MarginFormula)
            {
            case MarginFormula.FixedAmount:
                paidPledge = lotBalance * shortSellDownPayment;
                paidPledge = -Math.Round(paidPledge, currencyRate.TargetCurrency.Decimals, MidpointRounding.AwayFromZero);
                break;

            case MarginFormula.CS:
                paidPledge = lotBalance * contractSize * shortSellDownPayment;
                paidPledge = currencyRate.Exchange(-paidPledge);
                break;

            case MarginFormula.CSiPrice:
            case MarginFormula.CSiMarketPrice:
                paidPledge = lotBalance * contractSize * shortSellDownPayment / (decimal)price;
                paidPledge = currencyRate.Exchange(-paidPledge);
                break;

            case MarginFormula.CSxPrice:
            case MarginFormula.CSxMarketPrice:
                paidPledge = lotBalance * contractSize * (decimal)price * shortSellDownPayment;
                paidPledge = currencyRate.Exchange(-paidPledge);
                break;

            // TODO: Process marginFormula 4-FKLI,5-FCPO for bursa instrument when calculate on Order.
            case MarginFormula.FKLI:
            case MarginFormula.FCPO:
                paidPledge = lotBalance * shortSellDownPayment;
                paidPledge = -Math.Round(paidPledge, currencyRate.TargetCurrency.Decimals, MidpointRounding.AwayFromZero);
                break;
            }
            return(paidPledge);
        }
Esempio n. 6
0
        internal static decimal CalculateCGSELevy(decimal lot, bool isOpen, SpecialTradePolicyDetail policy, CurrencyRate currencyRate)
        {
            int     roundValue = (int)Math.Round(lot, MidpointRounding.AwayFromZero);
            decimal levy       = roundValue * (isOpen ? policy.CGSENewLevyMultipler : policy.CGSECloseLevyMultipler);

            if (lot > roundValue)
            {
                levy += (isOpen ? policy.CGSENewLevyRemainder : policy.CGSECloseLevyRemainder);
            }
            return(-currencyRate.Exchange(-levy));
        }
Esempio n. 7
0
        internal decimal Sum()
        {
            if (this.unclearDeposits == null)
            {
                return(0m);
            }
            decimal sum = 0;

            foreach (UnclearDeposit deposit in this.unclearDeposits.Values)
            {
                if (this.owner.Setting().IsMultiCurrency)
                {
                    CurrencyRate currencyRate = Settings.Setting.Default.GetCurrencyRate(deposit.TargetCurrencyId, this.owner.Setting().CurrencyId);
                    sum += currencyRate.Exchange(deposit.TargetAmount);
                }
                else
                {
                    sum += deposit.TargetAmount;
                }
            }
            return(sum);
        }
Esempio n. 8
0
        internal static TransactionError HasEnoughMoneyToTransfer(this Account account, string connectionString, Guid currencyId, decimal amount)
        {
            decimal notClearAmount = 0;
            var     fund           = account.GetFund(currencyId);

            if (!TransferHelper.GetVisaAmount2(connectionString, account.Id, currencyId, out notClearAmount))
            {
                return(TransactionError.DbOperationFailed);
            }

            var systemParameter = Settings.Setting.Default.SystemParameter;

            if ((fund.Equity - fund.Necessary - notClearAmount) + amount < 0 ||
                (!systemParameter.BalanceDeficitAllowPay && (fund.Balance - (fund.Necessary - fund.PartialPaymentPhysicalNecessary) - notClearAmount) + amount < 0))
            {
                return(TransactionError.MarginIsNotEnough);
            }

            if (account.IsMultiCurrency)
            {
                Guid accountCurrencyId = account.Setting().CurrencyId;
                if (!TransferHelper.GetVisaAmount2(connectionString, account.Id, accountCurrencyId, out notClearAmount))
                {
                    return(TransactionError.DbOperationFailed);
                }

                CurrencyRate currencyRate = Settings.Setting.Default.GetCurrencyRate(currencyId, accountCurrencyId);
                amount = currencyRate.Exchange(amount);

                if ((account.Equity - account.Necessary - notClearAmount) + amount < 0 ||
                    (!systemParameter.BalanceDeficitAllowPay && (account.Balance - (account.Necessary - account.SumFund.PartialPaymentPhysicalNecessary) - notClearAmount) + amount < 0))
                {
                    return(TransactionError.MarginIsNotEnough);
                }
            }

            return(TransactionError.OK);
        }
Esempio n. 9
0
        internal static decimal CalculateCommission(FeeFormula commissionFormula, TradePLFormula tradePLFormula, decimal unitCommission, decimal lot, decimal contractSize, Price price, CurrencyRate currencyRate, decimal tradePL = 0)
        {
            if (unitCommission > 1)
            {
                unitCommission = Math.Round(unitCommission, currencyRate.TargetCurrency.Decimals, MidpointRounding.AwayFromZero);
            }

            decimal commission = 0;

            switch (commissionFormula)
            {
            case FeeFormula.FixedAmount:
                commission = unitCommission * lot;
                commission = Math.Round(commission, currencyRate.TargetCurrency.Decimals, MidpointRounding.AwayFromZero);
                break;

            case FeeFormula.CS:
                commission = unitCommission * lot * contractSize;
                commission = -currencyRate.Exchange(-commission);
                break;

            case FeeFormula.CSDividePrice:
                commission = unitCommission * lot * contractSize / (decimal)price;
                commission = -currencyRate.Exchange(-commission);
                break;

            case FeeFormula.CSMultiplyPrice:
                commission = unitCommission * lot * contractSize * (decimal)price;
                commission = -currencyRate.Exchange(-commission);
                break;

            case FeeFormula.Pips:
                Price buyPrice, sellPrice;
                if ((int)tradePLFormula != 2)
                {
                    buyPrice  = price;
                    sellPrice = price + (int)unitCommission;
                }
                else
                {
                    buyPrice  = price + (int)unitCommission;
                    sellPrice = price;
                }
                Price closePrice = price;

                commission = TradePLCalculator.Calculate(tradePLFormula, lot, contractSize, (decimal)buyPrice, (decimal)sellPrice, (decimal)closePrice, currencyRate.SourceCurrency.Decimals);
                commission = -currencyRate.Exchange(-commission);
                break;

            case FeeFormula.PricePips:
                commission = 0;
                break;

            case FeeFormula.RealizedProfit:
                commission = tradePL > 0 ? unitCommission * tradePL : 0;
                break;

            case FeeFormula.RealizedLoss:
                commission = tradePL < 0 ? unitCommission * tradePL : 0;
                break;

            case FeeFormula.RealizedPL:
                commission = unitCommission * Math.Abs(tradePL);
                break;

            case FeeFormula.SharedPL:
                commission = unitCommission * tradePL;
                break;
            }

            return(commission);
        }
Esempio n. 10
0
        internal static decimal CalculateLevy(FeeFormula levyFormula, TradePLFormula tradePLFormula, decimal unitLevy, decimal lot, decimal contractSize, Price price, CurrencyRate currencyRate, decimal tradePL = 0)
        {
            if (unitLevy > 1)
            {
                unitLevy = Math.Round(unitLevy, currencyRate.TargetCurrency.Decimals, MidpointRounding.AwayFromZero);
            }

            decimal levy = 0;

            switch (levyFormula)
            {
            case FeeFormula.FixedAmount:
                levy = unitLevy * lot;
                levy = Math.Round(levy, currencyRate.TargetCurrency.Decimals, MidpointRounding.AwayFromZero);
                break;

            case FeeFormula.CS:
                levy = unitLevy * lot * contractSize;
                levy = -currencyRate.Exchange(-levy);
                break;

            case FeeFormula.CSDividePrice:
                levy = unitLevy * lot * contractSize / (decimal)price;
                levy = -currencyRate.Exchange(-levy);
                break;

            case FeeFormula.CSMultiplyPrice:
                levy = unitLevy * lot * contractSize * (decimal)price;
                levy = -currencyRate.Exchange(-levy);
                break;

            case FeeFormula.Pips:
                Price buyPrice, sellPrice;
                if ((int)tradePLFormula != 2)
                {
                    buyPrice  = price;
                    sellPrice = price + (int)unitLevy;
                }
                else
                {
                    buyPrice  = price + (int)unitLevy;
                    sellPrice = price;
                }
                Price closePrice = price;

                levy = TradePLCalculator.Calculate(tradePLFormula, lot, contractSize, (decimal)buyPrice, (decimal)sellPrice, (decimal)closePrice, currencyRate.SourceCurrency.Decimals);
                levy = -currencyRate.Exchange(-levy);
                break;

            case FeeFormula.PricePips:
                levy = 0;
                break;

            case FeeFormula.RealizedProfit:
                levy = tradePL > 0 ? unitLevy * tradePL : 0;
                break;

            case FeeFormula.RealizedLoss:
                levy = tradePL < 0 ? unitLevy * tradePL : 0;
                break;

            case FeeFormula.RealizedPL:
                levy = unitLevy * Math.Abs(tradePL);
                break;

            case FeeFormula.SharedPL:
                levy = unitLevy * tradePL;
                break;
            }

            return(levy);
        }
Esempio n. 11
0
 internal static decimal AddByExchange(this CurrencyRate currencyRate, decimal value)
 {
     return(currencyRate == null ? value : currencyRate.Exchange(value));
 }