Exemple #1
0
        /// <summary>
        /// 计算锁仓单成交产生的盈亏
        /// </summary>
        private decimal CalculateLockOrderTradePLFloat(decimal buyQuantitySum, decimal sellQuantitySum)
        {
            bool      isBuyForLockOrder = (buyQuantitySum < sellQuantitySum);
            decimal   quantityForLockOrder = Math.Abs(buyQuantitySum - sellQuantitySum);
            Quotation quotation = _owner.GetQuotation();
            Price     buy = quotation.BuyPrice, sell = quotation.SellPrice;

            Price close, executePrice;

            if (isBuyForLockOrder)
            {
                executePrice = sell;
                close        = buy;

                buy  = executePrice;
                sell = close;
            }
            else
            {
                executePrice = buy;
                close        = sell;

                buy  = close;
                sell = executePrice;
            }

            return(TradePLCalculator.Calculate(_owner.Setting.TradePLFormula, quantityForLockOrder, (decimal)buy, (decimal)sell, (decimal)close, _owner.CurrencyRate()));
        }
 private void CalculateOrderFloatPL(Order order, OrderResetResult resetResult)
 {
     if (_info.Settings.BuyPrice != null && _info.Settings.SellPrice != null)
     {
         Price   dayClosePrice = order.IsBuy ? _info.Settings.SellPrice : _info.Settings.BuyPrice;
         Price   buyPrice      = order.IsBuy ? order.ExecutePrice : dayClosePrice;
         Price   sellPrice     = !order.IsBuy ? order.ExecutePrice : dayClosePrice;
         decimal tradePLFloat  = TradePLCalculator.Calculate(_info.Instrument.TradePLFormula, order.LotBalance * order.Owner.ContractSize(_info.TradeDay), (decimal)buyPrice, (decimal)sellPrice, (decimal)dayClosePrice);
         resetResult.DayClosePrice = dayClosePrice;
         resetResult.TradePLFloat  = _exchanger.Exchange(tradePLFloat, _info.RateSetting.RateIn, _info.RateSetting.RateOut, _info.RateSetting.RoundDecimals.Common);
     }
 }
Exemple #3
0
        protected virtual decimal CalculateTradePL(ExecuteContext context)
        {
            Price buyPrice, sellPrice, closePrice;

            if (this.CloseOrder.IsBuy)
            {
                buyPrice  = this.CloseOrder.ExecutePrice;
                sellPrice = this.OpenOrder.ExecutePrice;
            }
            else
            {
                buyPrice  = this.OpenOrder.ExecutePrice;
                sellPrice = this.CloseOrder.ExecutePrice;
            }
            closePrice = this.CloseOrder.ExecutePrice;
            var     instrument   = this.CloseOrder.Owner.SettingInstrument(context.TradeDay);
            decimal contractSize = this.OpenOrder.Owner.ContractSize(context.TradeDay);
            var     result       = TradePLCalculator.Calculate(instrument.TradePLFormula, this.ClosedLot, contractSize, (decimal)buyPrice, (decimal)sellPrice, (decimal)closePrice, CloseOrder.Owner.AccountInstrument.Currency(context.TradeDay).Decimals);

            return(result);
        }
Exemple #4
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);
        }
Exemple #5
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);
        }
Exemple #6
0
        private decimal CaculateFeeForCutting(decimal closedLot, Price cutPrice, ICollection <CuttingItem> cuttingItems)
        {
            if ((decimal)cutPrice == 0)
            {
                return(0m);
            }
            decimal commission = 0m, levy = 0m, otherFee = 0m;

            Settings.Instrument      instrument               = _owner.Setting;
            Settings.Account         account                  = _owner.Owner.Setting();
            TradePolicyDetail        tradePolicyDetail        = _owner.TradePolicyDetail();
            SpecialTradePolicyDetail specialTradePolicyDetail = _owner.SpecialTradePolicyDetail(null);
            decimal      contractSize                         = tradePolicyDetail.ContractSize;
            CurrencyRate currencyRate                         = _owner.CurrencyRate(null);

            if (instrument.ExchangeSystem == ExchangeSystem.Local &&
                (account.RiskLevelAction == RiskLevelAction.CloseNetPosition || account.RiskLevelAction == RiskLevelAction.CloseAll))
            {
                if (!instrument.CommissionFormula.TakeFeeAsCost() || !instrument.LevyFormula.TakeFeeAsCost() || !instrument.OtherFeeFormula.TakeFeeAsCost())
                {
                    foreach (CuttingItem eachCuttingItem in cuttingItems)
                    {
                        Price buyPrice, sellPrice, closePrice;
                        if (eachCuttingItem.IsBuy)
                        {
                            buyPrice  = cutPrice;
                            sellPrice = eachCuttingItem.ExecutePrice;
                        }
                        else
                        {
                            sellPrice = cutPrice;
                            buyPrice  = eachCuttingItem.ExecutePrice;
                        }
                        closePrice = cutPrice;
                        decimal subCommission = 0m, subLevy = 0m, subOtherFee = 0m;
                        decimal tradePL      = TradePLCalculator.Calculate(instrument.TradePLFormula, eachCuttingItem.CuttingLot, eachCuttingItem.ContractSize, (decimal)buyPrice, (decimal)sellPrice, (decimal)closePrice, _owner.Currency(null).Decimals);
                        var     feeParameter = new FeeParameter()
                        {
                            Account                  = account,
                            TradePolicyDetail        = tradePolicyDetail,
                            SpecialTradePolicyDetail = specialTradePolicyDetail,
                            Instrument               = instrument,
                            CurrencyRate             = currencyRate,
                            ContractSize             = contractSize,
                            OpenOrderExecuteTime     = eachCuttingItem.ExecuteTime,
                            ClosedLot                = eachCuttingItem.CuttingLot,
                            ExecutePrice             = cutPrice,
                            TradePL                  = tradePL
                        };
                        OrderRelation.CalculateFee(feeParameter, out subCommission, out subLevy, out subOtherFee);
                        commission += subCommission;
                        levy       += subLevy;
                        otherFee   += subOtherFee;
                    }
                }

                if (instrument.LevyFormula.TakeFeeAsCost())
                {
                    levy = this.CalculateFeeCommon(account.RateLevy, tradePolicyDetail.LevyClose, closedLot, contractSize);
                }

                if (instrument.OtherFeeFormula.TakeFeeAsCost())
                {
                    otherFee = this.CalculateFeeCommon(account.RateOtherFee, tradePolicyDetail.OtherFeeClose, closedLot, contractSize);
                }

                if (instrument.CommissionFormula.TakeFeeAsCost())
                {
                    commission = this.CalculateFeeCommon(account.RateCommission, tradePolicyDetail.CommissionCloseD, closedLot, contractSize);
                }
                else
                {
                    if (commission >= 0)
                    {
                        commission = Math.Max(commission, tradePolicyDetail.MinCommissionClose);
                    }
                }
            }
            else
            {
                if (instrument.CommissionFormula.TakeFeeAsCost()) //Adjust PricePips
                {
                    commission = this.CalculateFeeCommon(account.RateCommission, tradePolicyDetail.CommissionOpen, closedLot, contractSize);
                }
                else
                {
                    if (!instrument.CommissionFormula.IsDependOnPL() && specialTradePolicyDetail != null && specialTradePolicyDetail.IsFractionCommissionOn)
                    {
                        commission = FeeCalculator.CalculateCommission(instrument.CommissionFormula, instrument.TradePLFormula, account.RateCommission * tradePolicyDetail.CommissionOpen,
                                                                       (int)closedLot, contractSize, cutPrice, currencyRate) +
                                     FeeCalculator.CalculateCommission(instrument.CommissionFormula, instrument.TradePLFormula, account.RateCommission * tradePolicyDetail.CommissionOpen, closedLot - (int)closedLot,
                                                                       contractSize, cutPrice, currencyRate);
                    }
                    else
                    {
                        commission = FeeCalculator.CalculateCommission(instrument.CommissionFormula, instrument.TradePLFormula, account.RateCommission * tradePolicyDetail.CommissionOpen, closedLot, contractSize, cutPrice, currencyRate);
                    }

                    if (commission >= 0)
                    {
                        commission = Math.Max(commission, tradePolicyDetail.MinCommissionOpen);
                    }
                }

                if (instrument.LevyFormula.TakeFeeAsCost()) //Adjust PricePips
                {
                    levy = this.CalculateFeeCommon(account.RateLevy, tradePolicyDetail.LevyOpen, closedLot, contractSize);
                }
                else
                {
                    if (!instrument.LevyFormula.IsDependOnPL() && specialTradePolicyDetail != null && specialTradePolicyDetail.IsFractionCommissionOn)
                    {
                        levy = FeeCalculator.CalculateLevy(instrument.LevyFormula, instrument.TradePLFormula, account.RateLevy * tradePolicyDetail.LevyOpen, (int)closedLot
                                                           , contractSize, cutPrice, currencyRate) +
                               FeeCalculator.CalculateLevy(instrument.LevyFormula, instrument.TradePLFormula, account.RateLevy * tradePolicyDetail.LevyOpen, closedLot - (int)closedLot
                                                           , contractSize, cutPrice, currencyRate);
                    }
                    else
                    {
                        levy = FeeCalculator.CalculateLevy(instrument.LevyFormula, instrument.TradePLFormula, account.RateLevy * tradePolicyDetail.LevyOpen, closedLot, contractSize, cutPrice, currencyRate);
                    }

                    if (!instrument.LevyFormula.IsDependOnPL() && specialTradePolicyDetail != null)
                    {
                        CurrencyRate cgseLevyCurrencyRate = FeeCalculator.GetCGSELevyCurrencyRate(account, instrument, specialTradePolicyDetail, currencyRate, null);
                        levy += FeeCalculator.CalculateCGSELevy(closedLot, true, specialTradePolicyDetail, cgseLevyCurrencyRate);
                    }
                }

                if (instrument.OtherFeeFormula.TakeFeeAsCost())
                {
                    otherFee = this.CalculateFeeCommon(account.RateOtherFee, tradePolicyDetail.OtherFeeOpen, closedLot, contractSize);
                }
                else
                {
                    otherFee = FeeCalculator.CalculateLevy(instrument.LevyFormula, instrument.TradePLFormula, account.RateOtherFee * tradePolicyDetail.OtherFeeOpen, closedLot, contractSize, cutPrice, currencyRate);
                }
            }

            return(commission + levy + otherFee);;
        }