Ejemplo n.º 1
0
        public CommClient(ITransactionOrder transaction, InstrumentSize newValue)
        {
            if (transaction == null)
                throw new ApplicationException("It is not possible to calculate the commission when the transaction is null.");

            if (newValue == null)
                throw new ApplicationException("It is not possible to calculate the commission when the transaction value is null.");

            this.transaction = transaction;
            this.orderValue = newValue;
            this.amount = newValue.GetMoney();
            type = CommClientType.Transaction;
        }
Ejemplo n.º 2
0
        private void processMoneyFill(Price price, ITransactionOrder newTrade)
        {
            //// If MoneyOrder on order -> set ExRate
            //if (IsMonetary)
            //{
            //    IStgMonetaryOrder mo = null;
            //    if (IsStgOrder && IsMonetary)
            //        mo = this as IStgMonetaryOrder;

            //    if (mo != null && mo.MoneyParent != null)
            //    {
            //        // First check if already exist
            //        if (mo.MoneyParent.ParentOrder != null)
            //            throw new ApplicationException("Monetary orders can only be filled once.");

            //        decimal exRate = Math.Round(1 / price.Quantity, 6);
            //        Money amount = mo.MoneyParent.Amount.Convert(exRate, (ICurrency)price.Instrument);
            //        StgAmtOrder newParent = new StgAmtOrder((IStgAmtOrder)mo.MoneyParent, amount, exRate);
            //        mo.MoneyParent.SetParentOrder(newParent);
            //        newTrade.ConvertedOrder = newParent;
            //        ((IOrder)mo.MoneyParent).SetExRate(exRate);

            //        //newParent.Approve();
            //    }
            //}
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Fills an order by setting the agreed size and price, exchange rate, counter party and transaction date.
        /// </summary>
        /// <param name="trade">The order to be filled</param>
        /// <param name="size">Size of the instrument</param>
        /// <param name="price">Agreed price of the instrument</param>
        /// <param name="amount">Amount of the order (=size * price)</param>
        /// <returns>True if it succeeded</returns>
        protected bool fillOrder(ITransactionOrder trade, InstrumentSize size, Price price, Money amount, Money serviceCharge, Money accruedInterest)
        {
            decimal ratio = 1m;
            Transactions.AddTransactionOrder(trade);
            FilledValue += fillOrderValue(size, amount, serviceCharge, accruedInterest);
            if (this.IsSecurity)
            {
                ((ISecurityOrder)this).ServiceCharge += serviceCharge;
                if (this.IsAmountBased)
                    ((ISecurityOrder)this).AccruedInterest += accruedInterest;
            }

            // If OrderExecution -> set FillRatio
            if (trade.TransactionType == TransactionTypes.Execution)
            {
                if (IsCompleteFilled)
                    // if order is already partly filled -> subtract rest
                    ratio = 1M - Transactions.TotalFillRatio();
                else
                {
                    InstrumentSize diff = PlacedValue.Abs() - fillOrderValue(size, amount, serviceCharge, accruedInterest).Abs();
                    if (diff.IsZero || diff.IsWithinTolerance(0.02M))
                        ratio = 1M;
                    else
                    {
                        ratio = fillOrderValue(size, amount, serviceCharge, accruedInterest).Abs().Quantity / PlacedValue.Abs().Quantity;
                        // If this trade filled the order (OpenSize = 0) -> take the remainder ratio
                        if (OpenValue.IsZero)
                        {
                            decimal var = ratio - (1M - getFillatioTransactions());
                            if (Math.Abs(var) < 0.0001M)
                                ratio = 1M - getFillatioTransactions();
                        }
                    }
                }
            }
            trade.FillRatio = ratio;
            OrderStateMachine.SetNewStatus(this, OrderStateEvents.Fill);

            //// If MoneyOrder on order -> set ExRate
            //if (IsMonetary)
            //{
            //    // Use the size as the amount -> because it is a conversion
            //    processMoneyFill(price, trade);
            //}

            return true;
        }
Ejemplo n.º 4
0
        public bool RemoveTransaction(ITransactionOrder trade)
        {
            bool retVal = false;

            if (Transactions.ContainsTrade(trade))
            {
                Transactions.RemoveTrade(trade);
                FilledValue = fillOrderValue(Transactions.TotalValueSize, Transactions.TotalCounterValueSize, Transactions.TotalServiceCharge, Transactions.TotalAccruedInterest);
                if (this.IsSecurity)
                {
                    ((ISecurityOrder)this).ServiceCharge = Transactions.TotalServiceCharge.IsZero ? null : Transactions.TotalServiceCharge;
                    if (this.IsAmountBased)
                        ((ISecurityOrder)this).AccruedInterest = Transactions.TotalAccruedInterest.IsZero ? null : Transactions.TotalAccruedInterest;
                }
                IsCompleteFilled = false;

                OrderStateMachine.ResetStatus(this);
                retVal = true;

                //// If MoneyOrder on order -> set ExRate
                //if (IsMonetary)
                //{
                //    // Use the size as the amount -> because it is a conversion
                //    processMoneyFill(price, trade);
                //}
            }
            else
                throw new Exception("Can not find the trade on the order");

            return retVal;
        }
Ejemplo n.º 5
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.º 6
0
        public static DataSet GetOrderTransactions(int orderId)
        {
            IDalSession session = NHSessionFactory.CreateSession();
            DataSet ds = null;
            ITransactionOrder[] transactions = null;

            IOrder order = OrderMapper.GetOrder(session, (int)orderId);

            transactions = new ITransactionOrder[order.Transactions.Count];
            order.Transactions.CopyTo(transactions, 0);

            ds = DataSetBuilder.CreateDataSetFromBusinessObjectList(
                transactions,
                @"Key, Order.OrderID, ValueSize.DisplayString, CounterValueSize.DisplayString, Price.DisplayString, ExchangeRate,
                  TransactionDate, CreationDate");

            session.Close();

            return ds;
        }
Ejemplo n.º 7
0
        public static DataSet GetOrderTransactions(object orderId)
        {
            using (IDalSession session = NHSessionFactory.CreateSession())
            {
                DataSet ds = null;

                if (orderId != null)
                {
                    ITransactionOrder[] transactions = null;

                    IOrder order = OrderMapper.GetOrder(session, (int)orderId, SecurityInfoOptions.NoFilter);

                    transactions = new ITransactionOrder[order.Transactions.Count];
                    order.Transactions.CopyTo(transactions, 0);

                    //"Order.OrderID, ValueSize.DisplayString, CounterValueSize.DisplayString, Price.DisplayString, Commission.DisplayString, ServiceCharge.DisplayString, ExchangeRate, TransactionDate");
                    ds = transactions
                        .Select(c => new
                        {
                            Order_OrderID = c.Order.OrderID,
                            ValueSize_DisplayString = c.ValueSize.DisplayString,
                            CounterValueSize_DisplayString = (c.CounterValueSize != null ? c.CounterValueSize.DisplayString : ""),
                            Price_DisplayString = c.Price.DisplayString,
                            Commission_DisplayString = (c.Commission != null ? c.Commission.DisplayString : ""),
                            ServiceCharge_DisplayString = (c.ServiceCharge != null ? c.ServiceCharge.DisplayString : ""),
                            AccruedInterest_DisplayString = (c.AccruedInterest != null ? c.AccruedInterest.DisplayString : ""),
                            c.ExchangeRate,
                            c.TransactionDate
                        })
                        .ToDataSet();
                }
                return ds;
            }
        }