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(); } }
/// <summary> /// Get the educated guess of the size of a instrument for a amount of money /// </summary> /// <param name="inputAmount"></param> /// <returns></returns> public override PredictedSize PredictSize(Money inputAmount) { PredictedSize retVal = new PredictedSize(PredictedSizeReturnValue.NoRate); Money amount; if (CurrentPrice != null) { retVal.RateDate = CurrentPrice.Date; if (inputAmount.Underlying.Equals(CurrentPrice.Price.Underlying)) retVal.Size = inputAmount.CalculateSize(CurrentPrice.Price); else { amount = inputAmount.Convert(CurrentPrice.Price.Underlying); retVal.Size = amount.CalculateSize(CurrentPrice.Price); } retVal.Rate = currentPrice.Price.ToString(); } return retVal; }
public InstrumentSize CalculateSizeBackwards(Money amount, Price price, DateTime settlementDate) { if (price == null) throw new ApplicationException(string.Format("It is not possible to calculate the size of {0} without a price", Name)); if (Util.IsNullDate(settlementDate)) throw new ApplicationException(string.Format("It is not possible to calculate the size of {0} without a valid settlement date", Name)); DateTime lastCouponPaymentDate; DateTime nextCouponPaymentDate; decimal factor = ai_Factor(settlementDate, null, out lastCouponPaymentDate, out nextCouponPaymentDate); // Calculate backwards the number of bonds return amount.CalculateSize((price + price.Clone(factor * GetCouponRate(lastCouponPaymentDate, settlementDate)))); }