Пример #1
0
        public void AddQuote(DateTime date, XChangeRate quote)
        {
            if (quote.IsIdentity)
            {
                return;
            }
            AddCcyPair(quote.CcyPair);
            DateTime AdjustedDate = Freq.Adjust(date);
            FXMarket FX           = GetRealFXMarketSimple(AdjustedDate);

            UpdateLastReadDate_NoLive(AdjustedDate);
            if (FX != null)
            {
                FX.AddQuote(quote);
            }
            else
            {
                RealFXMarkets[AdjustedDate] = new FXMarket(AdjustedDate, quote);
            }
        }
Пример #2
0
        private FXMarket _CreateArtificialFXMarket(DateTime date, List <CurrencyPair> cpList)
        {
            if (date != Freq.Adjust(date))
            {
                throw new Exception($"Your date is not Freq adjusted {date} != {Freq.Adjust(date)}");
            }
            if (cpList.Count == 0)
            {
                cpList = CpList;
            }
            FXMarket res = GetRealFXMarket(date, isExactDate: true);

            if (res == null)
            {
                res = GetArtificialFXMarketSimple(date);
            }
            if (!res.FXContains(cpList))
            {
                foreach (CurrencyPair cp in cpList)
                {
                    if (res.FXContains(cp))
                    {
                        continue;
                    }
                    FXMarket beforeFX = RealFXMarkets
                                        .Where(x => x.Key <= date && x.Value.FXContains(cp))
                                        .LastOrDefault().Value;
                    FXMarket afterFX = RealFXMarkets
                                       .Where(x => x.Key >= date && x.Value.FXContains(cp))
                                       .FirstOrDefault().Value;
                    double beforeRate = 0, afterRate = 0, rate = 0, w = 0.5;
                    bool   useBefore = beforeFX != null;
                    bool   useAfter  = afterFX != null;
                    if (useBefore)
                    {
                        beforeRate = beforeFX.GetQuote(cp).Rate;
                    }
                    if (useAfter)
                    {
                        afterRate = afterFX.GetQuote(cp).Rate;
                    }
                    if (!(useBefore || useAfter))
                    {
                        continue;
                    }
                    if (useAfter && useBefore)
                    {
                        if (afterFX.Date.AddSeconds(1) > beforeFX.Date)
                        {
                            w = (date - beforeFX.Date).TotalSeconds / (double)(afterFX.Date - beforeFX.Date).TotalSeconds;
                        }
                        else
                        {
                            throw new Exception($"The afterFX Market {afterFX.Date} comes before the beforeFX Market {beforeFX.Date}");
                        }
                    }
                    else
                    {
                        if (useBefore)
                        {
                            w = 0;
                        }
                        else
                        {
                            w = 1;
                        }
                    }
                    rate = (1 - w) * beforeRate + w * afterRate;
                    XChangeRate xRateCp = new XChangeRate(rate, cp);
                    res.AddQuote(xRateCp);
                    res.DefineAsArtificial();
                }
            }
            if (res.FXContains(cpList))
            {
                ArtificialFXMarkets[date] = res;
            }
            else
            {
                res = _CreateArtificialFXMarket(date, new List <CurrencyPair> {
                });
            }
            return(res);
        }