Ejemplo n.º 1
0
        public object Clone()
        {
            List <XChangeRate> xcr = new List <XChangeRate>();

            foreach (XChangeRate item in FX)
            {
                xcr.Add((XChangeRate)item.Clone());
            }
            FXMarket res = new FXMarket(Date, xcr);

            return(res);
        }
Ejemplo n.º 2
0
 public bool IsEquivalentTo(FXMarket fx, int precision = 8)
 {
     foreach (CurrencyPair cp in GetCurrencyPairs())
     {
         double rate  = GetQuote(cp, true).Rate;
         double rate2 = fx.GetQuote(cp, true).Rate;
         if (Math.Abs(rate - rate2) * Math.Pow(10, precision) > 1)
         {
             return(false);
         }
     }
     return(true);
 }
Ejemplo n.º 3
0
 private FXMarket GetArtificialFXMarketSimple(DateTime date)
 {
     if (date != Freq.Adjust(date))
     {
         throw new Exception($"Your date is not Freq adjusted {date} != {Freq.Adjust(date)}");
     }
     try { return(ArtificialFXMarkets[date]); }
     catch
     {
         FXMarket FX = new FXMarket(date);
         ArtificialFXMarkets[date] = FX;
         return(FX);
     }
 }
Ejemplo n.º 4
0
        public Tuple <DateTime, XChangeRate> GetQuote(DateTime date, CurrencyPair currencyPair,
                                                      bool isArtificial = false, bool isExactDate = false)
        {
            if (!isArtificial)
            {
                FXMarket fxMkt = GetRealFXMarket(date, isExactDate: isExactDate);
                if (fxMkt != null)
                {
                    return(new Tuple <DateTime, XChangeRate>(fxMkt.Date, fxMkt.GetQuote(currencyPair, true)));
                }
                else
                {
                    return(new Tuple <DateTime, XChangeRate>(date, null));
                }
            }
            FXMarket artFxMkt = GetArtificialFXMarket(date, currencyPair);

            return(new Tuple <DateTime, XChangeRate> (date, artFxMkt.GetQuote(currencyPair)));
        }
Ejemplo n.º 5
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);
            }
        }
Ejemplo n.º 6
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);
        }