Ejemplo n.º 1
0
        /// <summary>
        /// The method used by <b>Transaction</b> classes to calculate their attached fees.
        /// </summary>
        /// <param name="transaction">The transaction for which the fee is calculated.</param>
        /// <returns>The value of the fee.</returns>
        public Commission CalculateCommission(ITransactionOrder transaction)
        {
            if (transaction == null)
                throw new ApplicationException("It is not possible to calculate the commission when the transaction is null.");

            IOrder order = transaction.Order;
            if (transaction.TransactionType == TransactionTypes.Execution ||
                (transaction.AccountA.AccountType != AccountTypes.Customer && transaction.AccountB.AccountType != AccountTypes.Customer))
                return null;

            Money total = (transaction.CounterValueSize * -1);
            ICurrency commCur = transaction.TradedInstrument.CurrencyNominal;

            // Commission on the order is 0
            if (order.DoNotChargeCommission || order.Commission == null || (order.Commission != null && order.Commission.IsZero))
                return null;
            //else if (order.Commission != null && order.Commission.IsZero)
            //    return new CommValueDetails(new Money(0m, commCur), "");

            // AmountBased Order -> use commission from the order
            if (order.IsAmountBased)
            {
                Money orderAmount = order.Amount;
                Money diff;
                Money serviceCharge = transaction.ServiceCharge;

                // convert to transaction currency
                if (!orderAmount.Underlying.Equals(total.Underlying))
                    orderAmount = orderAmount.Convert(order.ExRate, (ICurrency)total.Underlying);
                // deduct serviceCharge
                orderAmount = MoneyMath.AdjustAmountForServiceCharge(orderAmount, serviceCharge, order.Side, MathOperator.Subtract);

                diff = orderAmount - total.GetMoney();

                // if the trade fills the Order completely -> take the Commission from the Order
                if (orderAmount.Equals(total) || diff.IsWithinTolerance(0.01M))
                {
                    //if (convert)
                    //{
                    //    Commission convCommDetails = new Commission(order.CommissionDetails, (ICurrency)total.Underlying, order.ExRate);
                    //    Money commConv = convCommDetails.Amount;
                        // No more 2 cent differences
                        //if (order.MoneyOrder != null && session != null)
                        //{
                        //    IList transactions = getMonetaryTransactions(order.MoneyOrder);
                        //    if (transactions != null && transactions.Count == 1)
                        //    {
                        //        Money tradeAmount = order.MoneyOrder.Transactions[0].ValueSize.GetMoney();
                        //        if (tradeAmount.Underlying.Equals(commConv.Underlying))
                        //        {
                        //            Money diffTx = tradeAmount + (transaction.CounterValueSize + commConv + transaction.ServiceCharge);
                        //            if (diffTx.IsNotZero && diffTx.Abs().IsWithinTolerance(0.02M))
                        //                convCommDetails.BreakupLines.GetItemByType(CommissionBreakupTypes.Commission).Amount -= diffTx;
                        //        }
                        //    }
                        //}
                    //    return convCommDetails;
                    //}
                    //else
                        return new Commission(order.CommissionDetails);
                }
            }

            if (order.Transactions != null && order.Transactions.Count > 0)
                total += (order.Transactions.TotalCounterValueSize * -1);

            CommClient client = new CommClient(transaction, total);
            Commission commdetails = CalculateCommission(client);
            Money commission = commdetails.Amount;

            //// convert the commission to instrument currency
            //if (!total.Underlying.Equals(commission.Underlying))
            //    commission = commission.Convert(transaction.Order.ExRate, commCur);

            if (order.Transactions != null && order.Transactions.Count > 0)
                commission -= order.Transactions.TotalCommission;

            return new Commission(CommissionBreakupTypes.Commission, commission, commdetails.CommissionInfo);
        }
Ejemplo n.º 2
0
 public ICommRule GetRelevantCommRule(IAccountTypeInternal account, IInstrument instrument, 
     Side side, OrderActionTypes actiontype, DateTime transactionDate, bool isAmountBased,
     out ICommClient client)
 {
     client = new CommClient(account, instrument, side, actiontype, transactionDate, !isAmountBased,
         null, null, null, null, false);
     return GetRelevantCommRule(client);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// The method used by <b>Order</b> classes to calculate their attached fees.
        /// </summary>
        /// <param name="order">The order for which the fee is calculated.</param>
        /// <returns>The value of the fee.</returns>
        public Commission CalculateCommission(IOrder order)
        {
            CommClient client = new CommClient(order);
            if (!order.IsAggregateOrder)
            {
                if (order.IsAmountBased && ((IOrderAmountBased)order).IsValueInclComm &&
                    order.RequestedInstrument.IsTradeable && !((ITradeableInstrument)order.RequestedInstrument).IsCommissionLinear)
                {
                    ITradeableInstrument instrument = (ITradeableInstrument)order.RequestedInstrument;
                    IExchange exchange = instrument.DefaultExchange ?? instrument.HomeExchange;

                    TransactionFillDetails details = instrument.GetTransactionFillDetails(order,
                        instrument.CurrentPrice.Price,
                        instrument.GetSettlementDate(order.CreationDate.Date, exchange),
                        this, order.ExRate, exchange);
                    return new Commission(details.Commission, "Commission determined by goalseek -> " + details.ToString());
                }
                else
                {
                    return CalculateCommission(client);
                }
            }
            else
                return null;
        }