示例#1
0
        public static void UpdateOrderFill(OrderFillView orderFillView)
        {
            IDalSession session = NHSessionFactory.CreateSession();

            try
            {
                ICurrency orderCurrency;
                ITransaction transaction;
                IExchange exchange = null;
                Money serviceCharge = null;
                decimal serviceChargePercentage = 0m;

                ISecurityOrder order = (ISecurityOrder)OrderMapper.GetOrder(session, orderFillView.OrderId);

                IAccount counterpartyAccount = AccountMapper.GetAccountByNumber(session, FUND_SETTLE);
                if (counterpartyAccount == null)
                    throw new ApplicationException(string.Format("The counterparty account {0} can not be found in the database.", FUND_SETTLE));

                if (orderFillView.ExchangeId != 0 && orderFillView.ExchangeId != int.MinValue)
                    exchange = ExchangeMapper.GetExchange(session, orderFillView.ExchangeId);
                if (exchange == null)
                    throw new ApplicationException("Select an exchange when filling the order");

                if (order.Value.Underlying.IsCash)
                    orderCurrency = (ICurrency)order.Value.Underlying;
                else
                    orderCurrency = ((ITradeableInstrument)order.Value.Underlying).CurrencyNominal;

                Price price = new Price(orderFillView.Price, orderCurrency, ((ISecurityOrder)(order)).TradedInstrument);

                InstrumentSize size = new InstrumentSize(orderFillView.Size, order.TradedInstrument);

                ICurrency txCurrency = null;
                if (order.TradedInstrument.IsCash)
                    txCurrency = (ICurrency)order.TradedInstrument;
                else
                    txCurrency = (ICurrency)order.TradedInstrument.CurrencyNominal;

                decimal exRate = order.TradedInstrument.CurrencyNominal.ExchangeRate.Rate;
                if (orderFillView.ServiceChargeAmount != 0)
                {
                    serviceCharge = new Money(orderFillView.ServiceChargeAmount, txCurrency);
                    serviceChargePercentage = orderFillView.ServiceChargePercentage;
                }

                //if (orderFillView.IsCompleteFill)
                //{
                Money amount = new Money(orderFillView.Amount, txCurrency);
                //transaction = order.Fill(size, price, amount, exRate, counterpartyAccount,
                //    orderFillView.TransactionDate, orderFillView.TransactionTime, exchange,
                //    orderFillView.IsCompleteFill, serviceCharge, serviceChargePercentage);
                //}
                //else
                //    transaction = order.Fill(size, price, exRate, counterpartyAccount, orderFillView.TransactionDate, serviceCharge, serviceChargePercentage);

                //if (Util.IsNotNullDate(orderFillView.SettlementDate))
                //    transaction.ContractualSettlementDate = orderFillView.SettlementDate;
                //transaction.Approve();

                //TransactionMapper.Insert(session, transaction);

                //if (transaction.Approved)
                //{
                //    IFeeFactory fees = FeeFactory.GetInstance(session);
                //    //order.Allocate(transaction, fees);

                //    OrderMapper.Update(session, order);
                //}
            }
            finally
            {
                session.Close();
            }
        }
示例#2
0
        public static void UpdateOrderFill(OrderFillView orderFillView)
        {
            IDalSession session = NHSessionFactory.CreateSession();

            try
            {
                ICurrency orderCurrency;
                IOrderExecution transaction;
                IAccount counterpartyAccount = null;
                IExchange exchange = null;
                Money serviceCharge = null;
                decimal serviceChargePercentage = 0m;

                //ISecurityOrder order = (ISecurityOrder)OrderMapper.GetOrder(session, orderFillView.OrderId);
                IStgOrder order = (IStgOrder)OrderMapper.GetOrder(session, orderFillView.OrderId);

                if (orderFillView.CounterpartyAccountId != 0)
                    counterpartyAccount = AccountMapper.GetAccount(session, orderFillView.CounterpartyAccountId);

                if (orderFillView.ExchangeId != 0 && orderFillView.ExchangeId != int.MinValue)
                    exchange = ExchangeMapper.GetExchange(session, orderFillView.ExchangeId);

                if (counterpartyAccount == null)
                    throw new ApplicationException("Select a counterparty account when filling the order");

                if (exchange == null)
                    throw new ApplicationException("Select an exchange when filling the order");

                if (((ISecurityOrder)order).Value.Underlying.IsCash)
                    orderCurrency = (ICurrency)(((ISecurityOrder)order).Value.Underlying);
                else
                    orderCurrency = ((ITradeableInstrument)(((ISecurityOrder)order).Value.Underlying)).CurrencyNominal;

                Price price = new Price(orderFillView.Price, ((ISecurityOrder)order).TradedInstrument.CurrencyNominal, ((ISecurityOrder)order).TradedInstrument);

                InstrumentSize size = new InstrumentSize(orderFillView.Size, ((ISecurityOrder)order).TradedInstrument);

                ICurrency txCurrency = null;
                if (((ISecurityOrder)order).TradedInstrument.IsCash)
                    txCurrency = (ICurrency)(((ISecurityOrder)order).TradedInstrument);
                else
                    txCurrency = (ICurrency)(((ISecurityOrder)order).TradedInstrument).CurrencyNominal;
                if (txCurrency.IsObsoleteCurrency)
                    txCurrency = txCurrency.ParentInstrument as ICurrency;

                decimal exRate = 0M;
                if (txCurrency.IsBase)
                    exRate = 1M;
                else if (txCurrency.ExchangeRate != null)
                    exRate = txCurrency.ExchangeRate.Rate;

                if (orderFillView.ServiceChargeAmount != 0)
                {
                    serviceCharge = new Money(orderFillView.ServiceChargeAmount, txCurrency);
                    serviceChargePercentage = orderFillView.ServiceChargePercentage;
                }

                Money amount = new Money(orderFillView.Amount, txCurrency);
                Money accruedInterest = null;
                if (orderFillView.AccruedInterestAmount != 0M)
                    accruedInterest = new Money(orderFillView.AccruedInterestAmount, txCurrency);

                ITradingJournalEntry tradingJournalEntry = TransactionAdapter.GetNewTradingJournalEntry(session, txCurrency.Symbol, orderFillView.TransactionDate);
                IGLLookupRecords lookups = GlLookupRecordMapper.GetGLLookupRecords(session, BookingComponentParentTypes.Transaction);

                transaction = order.Fill(size, price, amount, exRate, counterpartyAccount, orderFillView.TransactionDate,
                                        orderFillView.TransactionTime, exchange, orderFillView.IsCompleteFill,
                                        serviceCharge, serviceChargePercentage, accruedInterest, tradingJournalEntry, lookups);

                transaction.ContractualSettlementDate = orderFillView.SettlementDate;

                TransactionMapper.Update(session, transaction);
            }
            finally
            {
                session.Close();
            }
        }
示例#3
0
        public static IList GetOrderFills(int orderId, IExchange exchange)
        {
            IDalSession session = NHSessionFactory.CreateSession();
            ArrayList orderFills = new ArrayList();

            try
            {
                ISecurityOrder order = (ISecurityOrder)OrderMapper.GetOrder(session, (int)orderId);
                if (order != null)
                {
                    OrderFillView orderFillView = new OrderFillView(order.OrderID,
                                                                (order.IsSizeBased ? Math.Abs(order.OpenValue.Quantity) : 0m),
                                                                (order.IsSizeBased ? 0m : Math.Abs(order.OpenValue.Quantity)),
                                                                0m,1m,
                                                                order.IsSizeBased);

                    if (exchange != null)
                        orderFillView.ExchangeId = exchange.Key;
                    else if (order.TradedInstrument.HomeExchange != null)
                        orderFillView.ExchangeId = order.TradedInstrument.HomeExchange.Key;
                    else
                        orderFillView.ExchangeId = int.MinValue;

                    // ServiceCharge Percentage
                    if (order.TradedInstrument != null && order.TradedInstrument.InstrumentExchanges.Count > 0)
                    {
                        IInstrumentExchange instrumentExchange = order.TradedInstrument.InstrumentExchanges.GetDefault();
                        if (instrumentExchange != null)
                        {
                            // Default Counterparty
                            if (instrumentExchange.DefaultCounterParty != null)
                                orderFillView.CounterpartyAccountId = instrumentExchange.DefaultCounterParty.Key;

                            orderFillView.TickSize = instrumentExchange.TickSize;

                            // Service Charge
                            orderFillView.DoesSupportServiceCharge = instrumentExchange.DoesSupportServiceCharge;
                            if (instrumentExchange.DoesSupportServiceCharge)
                            {
                                orderFillView.ServiceChargeDisplayInfo = instrumentExchange.ServiceChargeDisplayInfo;
                                orderFillView.ServiceChargePercentage = instrumentExchange.GetServiceChargePercentageForOrder(order);
                                if (orderFillView.ServiceChargePercentage > 0 && orderFillView.IsAmountBased)
                                {
                                    orderFillView.ServiceChargeAmount =
                                        decimal.Round(orderFillView.Amount * (orderFillView.ServiceChargePercentage /
                                                        (1m + orderFillView.ServiceChargePercentage)), 2);
                                    orderFillView.Amount -= orderFillView.ServiceChargeAmount;
                                }
                            }
                        }
                    }

                    orderFillView.ShowExRate = !order.IsAmountBased && !order.TradedInstrument.CurrencyNominal.IsBase && !order.TradedInstrument.CurrencyNominal.IsObsoleteCurrency;
                    if (orderFillView.ShowExRate)
                        orderFillView.ExchangeRate = order.TradedInstrument.CurrencyNominal.ExchangeRate.Rate;

                    ICurrency currency = (order.IsSizeBased ? order.TradedInstrument.CurrencyNominal as ICurrency : order.Value.Underlying as ICurrency);
                    if (currency != null)
                    {
                        if (currency.IsObsoleteCurrency)
                            currency = currency.ParentInstrument as ICurrency;
                    }
                    orderFillView.AmountSymbol = (currency != null ? currency.AltSymbol : currency.AltSymbol);

                    orderFillView.SizeDecimals = order.TradedInstrument.DecimalPlaces;
                    orderFillView.AmountDecimals = (currency != null ? currency.DecimalPlaces : 2);

                    if (order.TradedInstrument.SecCategory.Key == SecCategories.Bond)
                    {
                        IBond bond = (IBond)order.TradedInstrument;
                        orderFillView.IsBondOrder = true;
                        if (bond.NominalValue != null)
                            orderFillView.TickSize = bond.NominalValue.Quantity;
                        if (bond.DoesPayInterest)
                        {
                            orderFillView.ShowAccruedInterest = true;
                            if (order.IsSizeBased)
                            {
                                if (order.AccruedInterest != null)
                                    orderFillView.AccruedInterestAmount = order.AccruedInterest.Quantity;
                            }
                        }
                    }

                    orderFills.Add(orderFillView);
                }
            }
            finally
            {
                session.Close();
            }

            return orderFills;
        }
示例#4
0
        public static PrefillCheckReturnValues CheckCompleteFill(OrderFillView orderFillView)
        {
            PrefillCheckReturnValues retVal = PrefillCheckReturnValues.OK;
            Price price;
            InstrumentSize size;
            Money amount;
            InstrumentSize serviceCharge = null;
            Money accruedInterest = null;
            IInstrument tradedInstrument;
            InstrumentSize diff;
            decimal percLeft;
            orderFillView.Warning = "";

            IDalSession session = NHSessionFactory.CreateSession();
            IOrder order = OrderMapper.GetOrder(session, orderFillView.OrderId);

            if (order.IsMonetary)
            {
                tradedInstrument = (IInstrument)((IMonetaryOrder)order).RequestedCurrency;
                if (order.IsSizeBased)
                {
                    price = new Price(orderFillView.Price, (ICurrency)tradedInstrument, order.Value.Underlying);
                    size = new InstrumentSize(orderFillView.Size, order.Value.Underlying);
                    amount = new Money(orderFillView.Amount, (ICurrency)tradedInstrument);
                }
                else
                {
                    price = new Price(orderFillView.Price, (ICurrency)order.Value.Underlying, tradedInstrument);
                    amount = new Money(orderFillView.Amount, (ICurrency)order.Value.Underlying);
                    size = new InstrumentSize(orderFillView.Size, tradedInstrument);
                }
            }
            else
            {
                tradedInstrument = (IInstrument)((ISecurityOrder)order).TradedInstrument;
                size = new InstrumentSize(orderFillView.Size, tradedInstrument);

                if (!order.IsSizeBased)
                {
                    // Exchange rate (in base currency)
                    price = new Price(orderFillView.Price, ((ITradeableInstrument)tradedInstrument).CurrencyNominal, tradedInstrument);
                    amount = new Money(orderFillView.Amount, (ICurrency)order.Value.Underlying);
                    serviceCharge = new InstrumentSize(orderFillView.ServiceChargeAmount, order.Value.Underlying);
                }
                else
                {
                    ICurrency currency = (ICurrency)((ITradeableInstrument)tradedInstrument).CurrencyNominal;
                    price = new Price(orderFillView.Price, currency, tradedInstrument);
                    if (currency.IsObsoleteCurrency)
                        currency = currency.ParentInstrument as ICurrency;
                    amount = new Money(orderFillView.Amount, currency);
                    serviceCharge = new InstrumentSize(orderFillView.ServiceChargeAmount, currency);
                }
            }
            if (orderFillView.AccruedInterestAmount != 0M)
                accruedInterest = new Money(orderFillView.AccruedInterestAmount * (decimal)order.Side * -1M, amount.Underlying.ToCurrency);

            Order.CheckMaximalRoundOffError(order.IsSizeBased, size, amount, price, accruedInterest, order.Side);
            decimal contractSize = ((ITradeableInstrument)(order.RequestedInstrument)).ContractSize;

            InstrumentSize calcAmt = size.CalculateAmount(price) * (decimal)order.Side * -1M;
            //if (order.IsAmountBased && accruedInterest != null && accruedInterest.IsNotZero)
            //    calcAmt += accruedInterest;

            diff = (calcAmt.Abs() - amount.Abs());
            if (diff.IsNotZero && !diff.IsWithinTolerance(0.02M))
            {
                orderFillView.Warning = string.Format("Price times Size ({0}) does not equal the provided Amount ({1}).",
                                                      calcAmt.DisplayString, amount.DisplayString);

                retVal = PrefillCheckReturnValues.Warning;
                percLeft = diff.Quantity / amount.Abs().Quantity;
                orderFillView.FillPercentage = 1M - percLeft;
            }

            if (order.Transactions.Count == 0)
            {
                if (order.IsAmountBased)
                {
                    diff = order.PlacedValue.Abs() - amount.Abs();
                    if (serviceCharge != null && serviceCharge.IsNotZero)
                        diff -= serviceCharge.Abs();
                    //if (accruedInterest != null)
                    //    diff += accruedInterest;
                }
                else
                    diff = order.PlacedValue.Abs() - size.Abs();

                if (diff.IsNotZero && !diff.IsWithinTolerance(0.02M))
                {
                    percLeft = diff.Quantity / order.PlacedValue.Abs().Quantity;
                    orderFillView.FillPercentage = 1M - percLeft;

                    orderFillView.Warning += string.Format("{0}The order is {1} filled.",
                                                (orderFillView.Warning != string.Empty ? "\n" : ""), orderFillView.DisplayFillPercentage);
                    retVal = PrefillCheckReturnValues.Warning;

                    if (percLeft <= 0.05m)
                    {
                        orderFillView.Warning += " If you want this order to be completely filled, check 'Complete Fill' on.";
                        retVal |= PrefillCheckReturnValues.AskCompleteFill;
                    }
                }

                if (order.IsAmountBased && orderFillView.TickSize > 0M)
                {
                    decimal factor = size.Quantity / orderFillView.TickSize;
                    if (Math.Abs(factor - (decimal)Convert.ToInt32(factor)) > 0.0001M)
                        orderFillView.Warning += string.Format("{0}The size is not in multiples of the tick size {1}.",
                                                    (orderFillView.Warning != string.Empty ? "\n" : ""), orderFillView.TickSize);
                    if (retVal == PrefillCheckReturnValues.OK)
                        retVal = PrefillCheckReturnValues.Warning;
                }
            }

            if ((retVal & PrefillCheckReturnValues.Warning) == PrefillCheckReturnValues.Warning)
                orderFillView.Warning += "\nPress OK again to fill the order.";

            return retVal;
        }
示例#5
0
        public static void PriceChanged(OrderFillView orderFillView)
        {
            IDalSession session = NHSessionFactory.CreateSession();

            try
            {
                Price price;
                InstrumentSize size;
                Money amount;
                ITradeableInstrument tradedInstrument;

                IOrder order = OrderMapper.GetOrder(session, orderFillView.OrderId);

                if (!orderFillView.IsSizeBased)
                {
                    // Exchange rate (in base currency)
                    tradedInstrument = ((IOrderAmountBased)order).TradedInstrument;
                    price = new Price(orderFillView.Price, tradedInstrument.CurrencyNominal, tradedInstrument);
                    amount = new Money(orderFillView.Amount, (ICurrency)order.Value.Underlying);
                    if (tradedInstrument.CurrencyNominal.Key != amount.Underlying.Key)
                    {
                        if (!(tradedInstrument.CurrencyNominal.IsObsoleteCurrency && tradedInstrument.CurrencyNominal.ParentInstrument.Key == amount.Underlying.Key))
                            throw new ApplicationException("It is not possible to fill an order in a different currency.");
                    }
                    size = amount.CalculateSize(price);
                    orderFillView.Size = size.Quantity;
                }
                else
                {
                    tradedInstrument = ((IOrderSizeBased)order).TradedInstrument;
                    price = new Price(orderFillView.Price, tradedInstrument.CurrencyNominal, tradedInstrument);
                    size = new InstrumentSize(orderFillView.Size, tradedInstrument);
                    amount = size.CalculateAmount(price);
                    amount.XRate = orderFillView.ExchangeRate;
                    orderFillView.Amount = amount.Quantity;
                }

                // Check if the Price is still reliable
                IPriceDetail lastValidHistoricalPrice = HistoricalPriceMapper.GetLastValidHistoricalPrice(
                                                                                    session, tradedInstrument, orderFillView.TransactionDate);
                if (lastValidHistoricalPrice == null || lastValidHistoricalPrice.Price.IsZero)
                    orderFillView.Warning = string.Format("No price was found for {0:d}, so validation is not very reliable.",
                                                             orderFillView.TransactionDate);
                else
                {
                    // check if the price is within 1% of the last historical price
                    decimal rate = lastValidHistoricalPrice.Price.Quantity;

                    decimal diff = (price.Quantity - rate) / rate;
                    decimal diffPct = Math.Round(Math.Abs(diff), 4) * 100;
                    if (diffPct > 1)
                        orderFillView.Warning = string.Format("The price entered is {0:0.##}% {1} than the last known price for {2:d} ({3}).",
                                                              diffPct, (diff < 0 ? "lower" : "higher"), orderFillView.TransactionDate,
                                                              lastValidHistoricalPrice.Price.ShortDisplayString);

                    if (lastValidHistoricalPrice.WasOldDateBy(orderFillView.TransactionDate))
                        orderFillView.Warning += (orderFillView.Warning != string.Empty ? "\n" : "") +
                            string.Format("The last known price for {0:d} is {1} days old (last updated on {2:d}), so validation is not very reliable.",
                                          orderFillView.TransactionDate, (orderFillView.TransactionDate - lastValidHistoricalPrice.Date).Days,
                                          lastValidHistoricalPrice.Date);
                }
            }
            finally
            {
                session.Close();
            }
        }
示例#6
0
 protected void OnTransactionDateChanged(OrderFillView orderFillView)
 {
     if (TransactionDateChanged != null)
         TransactionDateChanged(this, new OrderFillEventArgs(orderFillView));
 }
示例#7
0
 protected void OnPriceChanged(OrderFillView orderFillView)
 {
     if (PriceChanged != null)
         PriceChanged(this, new OrderFillEventArgs(orderFillView));
 }
示例#8
0
    protected void dvOrderFill_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        try
        {
            if (!IsCheckDone)
            {
                OrderFillView orderFillView = new OrderFillView(OrderId, Size, Amount, Price, ExRate, IsSizeBased, ServiceChargeAmount, AccruedInterest, ExchangeId);
                orderFillView.TickSize = TickSize;
                PrefillCheckReturnValues prefillCheck = OrderFillAdapter.CheckCompleteFill(orderFillView);
                if (prefillCheck != PrefillCheckReturnValues.OK)
                {
                    IsCheckDone = true;
                    if ((prefillCheck & PrefillCheckReturnValues.Warning) == PrefillCheckReturnValues.Warning)
                        DisplayError(orderFillView.Warning);
                    if ((prefillCheck & PrefillCheckReturnValues.AskCompleteFill) == PrefillCheckReturnValues.AskCompleteFill)
                        CompleteFillText = orderFillView.DisplayFillPercentage;
                    e.Cancel = true;
                    return;
                }
            }

            e.NewValues["Price"] = Price;
            e.NewValues["Size"] = Size;
            e.NewValues["Amount"] = Amount;
            e.NewValues["ExchangeRate"] = ExRate;

            if (IsServiceChargeVisible)
            {
                e.NewValues["ServiceChargePercentage"] = ServiceChargePercentage;
                e.NewValues["ServiceChargeAmount"] = ServiceChargeAmount;
            }
            if (isAccruedInterestVisible)
            {
                e.NewValues["AccruedInterestAmount"] = AccruedInterest;
            }
            e.NewValues["TransactionDate"] = TransactionDate;
            if (IsTransactionTimeVisible)
                e.NewValues["TransactionTime"] = TransactionTime;
            if (IsSettlementDateVisible)
                e.NewValues["SettlementDate"] = SettlementDate;
            if (IsCounterpartyVisible)
                e.NewValues["CounterpartyAccountId"] = CounterpartyAccountId;
            if (IsExchangeVisible)
                e.NewValues["ExchangeId"] = ExchangeId;
            e.NewValues["IsCompleteFill"] = IsCompleteFill;
        }
        catch (Exception ex)
        {
            DisplayError(ex);
            e.Cancel = true;
        }
    }
示例#9
0
    protected void dpTransactionDate_SelectionChanged(object sender, EventArgs e)
    {
        try
        {
            if (IsTransactionDateValid)
            {
                OrderFillView orderFillView =
                    new OrderFillView(OrderId, Size, Amount, Price, ExRate, IsSizeBased, TransactionDate, CounterpartyAccountId, ExchangeId);

                if (IsSettlementDateVisible)
                {
                    OnTransactionDateChanged(orderFillView);
                    SettlementDate = orderFillView.SettlementDate;
                }

                if (!dbPrice.IsEmpty && IsPriceValid)
                {
                    OnPriceChanged(orderFillView);
                    DisplayError(orderFillView.Warning);
                }

                CalculateAccruedInterestAmount();
            }
        }
        catch (Exception ex)
        {
            DisplayError(ex);
        }
    }
示例#10
0
    protected void dbPrice_ValueChanged(object sender, EventArgs e)
    {
        try
        {
            if (IsPriceValid)
            {
                OrderFillView orderFillView = new OrderFillView(OrderId, Size, Amount, Price, ExRate, IsSizeBased);
                orderFillView.TransactionDate = (IsTransactionDateValid ? TransactionDate : DateTime.Today);
                OnPriceChanged(orderFillView);

                DisplayError(orderFillView.Warning);

                Price = orderFillView.Price;
                Size = orderFillView.Size;
                Amount = orderFillView.Amount;

                if (IsServiceChargeVisible && IsSizeBased)
                {
                    ServiceChargePercentage = InitialServiceChargePercentage;
                    CalculateServiceChargeAmount();
                }

                IsCheckDone = false;
            }
        }
        catch (Exception ex)
        {
            DisplayError(ex);
        }
    }