Exemple #1
0
        public static DateTime[] GenerateDateSchedule(this DatePeriodType period, DateTime start, DateTime end)
        {
            var o = new List <DateTime>();
            var d = start;

            while (d < end)
            {
                o.Add(d);
                d = d.AddPeriod(RollType.F, null, new Frequency(1, period));
            }
            o.Add(end);
            return(o.ToArray());
        }
Exemple #2
0
        public async Task <PagedListResult <Core.Entities.Financial.Order.Order> > GetOrdersAsPagedList(
            Expression <Func <Core.Entities.Financial.Order.Order, bool> > predicate,
            string orderNumber,
            int pageNumber = 0,
            int pageSize   = ApplicationSettings.Pagination.PageSize,
            OrderStatusType?orderStatus = null,
            DatePeriodType periodType   = DatePeriodType.All
            )
        {
            #region Pagination Set

            var skipCount = (pageNumber - 1) * pageSize;

            #endregion

            #region set time period filter

            DateTime periodDateTime;
            switch (periodType)
            {
            case DatePeriodType.Day:
                periodDateTime = DateTime.Now.Date;
                break;

            case DatePeriodType.Week:
                var dayOfWeek = PersianDateTime.Now.PersianDayOfWeek;
                periodDateTime = DateTime.Now.Date.AddDays(-(int)dayOfWeek);
                break;

            case DatePeriodType.Month:
                periodDateTime = new PersianDateTime(PersianDateTime.Now.Year, PersianDateTime.Now.Month, 1).ToDateTime();
                break;

            case DatePeriodType.Year:
                periodDateTime = new PersianDateTime(PersianDateTime.Now.Year, 1, 1).ToDateTime();
                break;

            case DatePeriodType.All:
                periodDateTime = default(DateTime);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(periodType), periodType, null);
            }

            #endregion set time period filter

            var resultCount = await(from order in GetQueryable()
                                    join tariff in _tariffRepository.GetQueryable() on order.TariffId equals tariff.Id
                                    join tariffPrice in _tariffPriceRepository.GetQueryable() on tariff.Id equals tariffPrice.TariffId
                                    join tariffItem in _tariffItemRepository.GetQueryable() on tariff.Id equals tariffItem.TariffId
                                    where orderStatus == null || order.OrderStatus == orderStatus
                                    where periodDateTime == default(DateTime) || order.CreateDateTime > periodDateTime
                                    where orderNumber == null || orderNumber == "" || order.OrderNumber.Contains(orderNumber)
                                    orderby order.CreateDateTime descending
                                    select order
                                    ).Distinct().CountAsync();

            var result = await(from order in GetQueryable()
                               join tariff in _tariffRepository.GetQueryable() on order.TariffId equals tariff.Id
                               join tariffPrice in _tariffPriceRepository.GetQueryable() on tariff.Id equals tariffPrice.TariffId
                               join tariffItem in _tariffItemRepository.GetQueryable() on tariff.Id equals tariffItem.TariffId
                               where orderStatus == null || order.OrderStatus == orderStatus
                               where periodDateTime == default(DateTime) || order.CreateDateTime > periodDateTime
                               where orderNumber == null || orderNumber == "" || order.OrderNumber.Contains(orderNumber)
                               orderby order.CreateDateTime descending
                               select new { order, tariff, tariffItem, tariffPrice }
                               ).ToListAsync();

            foreach (var item in result)
            {
                var firstOrDefault = result.Select(x => x.tariffPrice)
                                     .FirstOrDefault(x => x.TariffId == item.order.TariffId && x.IsValid);
                if (firstOrDefault != null)
                {
                    item.order.Price = firstOrDefault.Price;
                }

                item.order.TariffName = item.tariff.TariffName;
                var quantityExists = result.Select(x => x.tariffItem)
                                     .FirstOrDefault(x => x.Type == TariffItemType.Quantity);
                if (quantityExists != null)
                {
                    item.order.Quantity = Int64.Parse(quantityExists.Name) * item.order.Count;
                }
                else
                {
                    item.order.Quantity = 1;
                }

                item.order.FinalPrice = item.order.Price * item.order.Count + item.order.DesignPrice;
            }

            var finalResult = result.Select(x => x.order).AsQueryable();

            if (predicate != null)
            {
                finalResult = finalResult.Where(predicate);
            }

            var listItems = finalResult.Distinct().Skip(skipCount).Take(pageSize).ToList();
            return(new PagedListResult <Core.Entities.Financial.Order.Order>()
            {
                PageNumber = pageNumber,
                Result = listItems,
                PageSize = pageSize,
                TotalCount = resultCount
            });
        }
Exemple #3
0
 public Frequency(int periodCount, DatePeriodType periodType)
 {
     PeriodCount = periodCount;
     PeriodType  = periodType;
 }
Exemple #4
0
        public static ICube Calculate(IAssetFxModel model, Portfolio portfolio, double confidenceInterval, Currency reportingCcy, ICurrencyProvider currencyProvider,
                                      ICalendarProvider calendarProvider, DateTime[] exposureDates = null, DatePeriodType sampleFreq = DatePeriodType.Month, bool correlationCorrection = true)
        {
            var types = portfolio.Instruments.Select(x => x.GetType()).Distinct();

            if (!types.All(t => AllowedTypes.Contains(t)))
            {
                throw new Exception("Unsupported instrument types detected");
            }

            var pf       = portfolio.UnStripStrips();
            var assetIns = pf.Instruments.Select(x => ((IAssetInstrument)x));
            //check everything facting in same direction
            var pos = assetIns.Select(x => x.IsLongDeltaPosition()).Distinct();

            if (pos.Count() > 1)
            {
                throw new Exception("All trades must have same delta direction");
            }

            //check everything same asset/currency
            var keys = assetIns.Select(x => $"{x.Currency}~{string.Join("-",x.AssetIds)}").Distinct();

            if (keys.Count() > 1)
            {
                throw new Exception("All trades must have same underlying and currency");
            }

            //flip tail of distribution if we are short
            var ci = pos.Single() ? confidenceInterval : 1.0 - confidenceInterval;

            if (exposureDates == null)
            {
                exposureDates = pf.ComputeSimDates(model.BuildDate, sampleFreq);
            }

            var m = model.TrimModel(portfolio);

            var day0Pv = 0.0;

            if (exposureDates.Contains(model.BuildDate))
            {
                day0Pv = Max(0, pf.PV(m, reportingCcy, true).SumOfAllRows);
            }
            //adjust fx surface for correlation if needed
            var pairs = assetIns.Select(x => x.FxPair(model)).Where(x => !string.IsNullOrEmpty(x));

            if (correlationCorrection && pairs.Any())
            {
                var pd = pairs.Distinct();
                if (pd.Count() != 1)
                {
                    throw new Exception("Expecting a single Fx pair, if any");
                }
                var pair    = pd.Single();
                var assetId = assetIns.SelectMany(x => x.AssetIds).Distinct().Single();
                var(fxSurface, assetSurface)     = model.ImplySurfaceToCorrelation(assetId, pair, assetIns.First().Currency, currencyProvider);
                m.FundingModel.VolSurfaces[pair] = fxSurface;
                m.AddVolSurface(assetId, assetSurface);
            }

            var fxPairsToRoll = assetIns.SelectMany(x => x.AssetIds).Where(x => x.Length == 7 && x.Substring(3, 1) == "/")
                                .Concat(assetIns.Select(x => x.FxPair(model)))
                                .Where(x => !string.IsNullOrWhiteSpace(x))
                                .Distinct().ToList();

            var o = new double[exposureDates.Length];

            for (var i = 0; i < exposureDates.Length; i++)
            {
                var mm = m.RollModelPfe(exposureDates[i], ci, currencyProvider, calendarProvider, fxPairsToRoll);
                o[i] = exposureDates[i] == model.BuildDate ? day0Pv : Max(0, pf.PV(mm, reportingCcy, true).SumOfAllRows);
            }

            return(PackToCube(exposureDates, o, "PFE"));
        }