/// <summary> /// Get the FX Market history implied by the OHLC timeseries /// </summary> /// <param name="fiat"></param> /// <param name="cpList"></param> /// <param name="startDate"></param> /// <param name="freq"></param> /// <returns></returns> public void UpdateFXMarketHistory(FXMarketHistory fxmh, Currency fiat, DateTime startDate, Frequency freq = Frequency.Hour4) { ResetReadFiles(); // Need To Duplicate the market in order to have "clean" dates List <CurrencyPair> cpList = new List <CurrencyPair>(fxmh.CpList); List <Currency> fiatList = new List <Currency> { }; foreach (CurrencyPair cp in cpList) { CurrencyPairTimeSeries cpts = new CurrencyPairTimeSeries(cp, freq); CryptoFiatPair cfp = cp.GetCryptoFiatPair; if (!cfp.IsNone) { FillFXMarketHistory(fxmh, cpts, startDate); if (cfp.Fiat != fiat) { CurrencyPairTimeSeries cpts2 = new CurrencyPairTimeSeries(cfp.Crypto, fiat, freq); FillFXMarketHistory(fxmh, cpts2, startDate); if (!fiatList.Contains(cfp.Fiat)) { fiatList.Add(cfp.Fiat); } } } } foreach (Currency fiat_i in fiatList) { CurrencyPairTimeSeries cptsFiatPair = new CurrencyPairTimeSeries(fiat_i, fiat, freq); FillFXMarketHistory(fxmh, cptsFiatPair, startDate); } }
public void UpdateHistory(Currency ccyChek, FXMarketHistory fxmh) { if (!TxLoaded) { throw new Exception("Load Transactions First!"); } if (ccyChek != CcyRefLoaded) { throw new Exception("Load Transactions First with correct Fiat!"); } if (fxmh.Freq != FreqLoaded) { FreqLoaded = fxmh.Freq; this.PublishInfo($"Recalculating Chart Data {CcyRefLoaded} - {FreqLoaded} ..."); foreach (DateTime date in fxmh.GetAllDates()) { if (date >= StartDate) { FXMarket fx = fxmh.GetArtificialFXMarket(date); if (!History.Keys.Contains(date)) { AddAllocationToHistory(GetClosestAllocation(date), fx); } History[date].CalculateTotal(fx, CcyRefLoaded); } } } }
public void FXMktHist_CcyManagement() { FXMarketHistory fxmh = MarketTestTools.CreateMktHistory(); List <CurrencyPair> cpListRef = MarketTestTools.CreateMarket().GetCurrencyPairs(); TestTools <CurrencyPair> .ListComparisonTest(fxmh.CpList, cpListRef); }
public Dictionary <string, PnLElement> ToTable(FXMarketHistory fxmh, DateTime date) { int n = PnLs.Count + 1; Dictionary <string, PnLElement> res = new Dictionary <string, PnLElement> { }; PnLElement total = new PnLElement(); foreach (Currency ccy in PnLs.Keys) { CurrencyPair cpCcy = new CurrencyPair(ccy, CcyRef); XChangeRate xrCcy = fxmh.GetQuote(date, cpCcy, isArtificial: true).Item2; Tuple <string, PnLElement> item = PnLs[ccy].ToArray(xrCcy, date); res.Add(item.Item1, item.Item2); total.Position += item.Item2.Position * item.Item2.xChangeRate.Value; total.OnGoingPnL += item.Item2.OnGoingPnL; total.Fees += item.Item2.Fees; total.RealizedPnL += item.Item2.RealizedPnL; total.Deposit += item.Item2.Deposit; total.Withdrawal += item.Item2.Withdrawal; } total.Weight = 1.0; res.Add("Total", total); foreach (Currency key in PnLs.Keys) { PnLElement item = res[key.ToFullName()]; item.Weight = item.Position * item.xChangeRate / total.Position; res[key.ToFullName()] = item; } return(res); }
public void SetUpAllHistory(Frequency freq, bool useKraken = false) { SortedList <DateTime, Transaction> txList = DataProvider.GetTransactionList(useKraken: useKraken, forceReload: ChangeLedger); DateTime startDate = txList.First().Key; FXMH = DataProvider.GetFXMarketHistory(Fiat, DataProvider.GetCurrencyPairs(txList), startDate, freq); }
/// <summary> /// Get the FX Market history implied by the OHLC timeseries /// </summary> /// <param name="fiat"></param> /// <param name="cpList"></param> /// <param name="startDate"></param> /// <param name="freq"></param> /// <returns></returns> public FXMarketHistory GetFXMarketHistory(Currency fiat, List <CurrencyPair> cpList, DateTime startDate, Frequency freq = Frequency.Hour4) { // Need To Duplicate the market in order to have "clean" dates FXMarketHistory fxmh = new FXMarketHistory(freq); List <Currency> fiatList = new List <Currency> { }; foreach (CurrencyPair cp in cpList) { CurrencyPairTimeSeries cpts = new CurrencyPairTimeSeries(cp, freq); FillFXMarketHistory(fxmh, cpts, startDate); CryptoFiatPair cfp = cp.GetCryptoFiatPair; if (cfp.Fiat != fiat) { CurrencyPairTimeSeries cpts2 = new CurrencyPairTimeSeries(cfp.Crypto, fiat, freq); FillFXMarketHistory(fxmh, cpts2, startDate); if (!fiatList.Contains(cfp.Fiat)) { fiatList.Add(cfp.Fiat); } } } foreach (Currency fiat_i in fiatList) { CurrencyPairTimeSeries cptsFiatPair = new CurrencyPairTimeSeries(fiat_i, fiat, freq); FillFXMarketHistory(fxmh, cptsFiatPair, startDate); } return(fxmh); }
public static AllocationHistory GetAllocationHistory(FXMarketHistory fxmh, bool addCryptoWD = false, Currency Fiat = Currency.USD) { AllocationHistory AH = new AllocationHistory(); AH.AddTransactions(Fiat, GetTransactionList(addCryptoWD), fxmh); return(AH); }
private void CalculatePnLs(FXMarketHistory fxmh) { foreach (Currency ccy in TxDict.Keys) { PnLs[ccy] = new PnLItem(ccy, CcyRef); PnLs[ccy].AddTransactions(TxDict[ccy], fxmh); } }
public void FXMktHist_GetArtificialFXMarket() { FXMarketHistory fxMH = MarketTestTools.CreateMktHistory(); FXMarket fxArt = fxMH.GetArtificialFXMarket(MarketTestTools.dateArt, new CurrencyPair(Currency.EUR, Currency.USD)); XChangeRate xr = fxArt.GetQuote(Currency.EUR, Currency.USD, true); Assert.AreEqual(MarketTestTools.EurUsdArtRate.Rate, xr.Rate, Math.Pow(10, -6)); }
public void ChangeCcyRef(Currency ccy, FXMarketHistory fxmh) { if (ccy != CcyRef) { CcyRef = ccy; CalculatePnLs(fxmh); } }
public void FXMktHist_GetAllDates() { FXMarketHistory fxmh = MarketTestTools.CreateMktHistory(); FXMarket artMkt = fxmh.GetArtificialFXMarket(MarketTestTools.dateArt); TestTools <DateTime> .ListComparisonTest(fxmh.GetAllDates().ToList(), new List <DateTime> { MarketTestTools.date1, MarketTestTools.dateArt, MarketTestTools.date2 }); }
//TODO: To replace by Set up already defined Ccy Pairs public List <CurrencyPair> GetCurrencyPairs(SortedList <DateTime, Transaction> txList) { FXMarketHistory fxmh = new FXMarketHistory(); foreach (var item in txList) { fxmh.AddQuote(item.Value.Date, item.Value.XRate); } return(fxmh.CpList); }
public void AllocationHistory_Init() { FXMarketHistory fxmh = MarketTestTools.CreateMktHistory(); AllocationHistory allocH = AllocationsTools.GetAllocationHistory(fxmh); Allocation alloc = allocH.GetAllocation(MarketTestTools.date1); Allocation allocTest = AllocationsTools.GetAllocation(); allocTest.Update(MarketTestTools.CreateMarket()); Assert.IsTrue(alloc.Total.Equals(allocTest.Total)); }
public void AggPnL_AddTransaction() { FXMarketHistory fxmh = MarketTestTools.CreateMktHistory(true, true); SortedList <DateTime, Transaction> txL = AllocationsTools.GetTransactionList(); AggregatedPnL pnl = new AggregatedPnL(Currency.USD); pnl.AddTransactions(txL, fxmh); Dictionary <string, PnLElement> table = pnl.ToTable(fxmh); TestTools <PnLElement> .DictionaryTest(pnlref, table); }
public void AggPnL_Equation() { FXMarketHistory fxmh = MarketTestTools.CreateMktHistory(true, true); SortedList <DateTime, Transaction> txL = AllocationsTools.GetTransactionList(); AggregatedPnL pnl = new AggregatedPnL(Currency.USD); pnl.AddTransactions(txL, fxmh); PnLElement elmt = pnl.ToTable(fxmh)["Total"]; Assert.IsTrue(elmt.Position - elmt.Deposit + elmt.Withdrawal == elmt.TotalPnLWithFees); }
public void FXMktHist_GetRealFXMarket() { FXMarketHistory fxmh = MarketTestTools.CreateMktHistory(); FXMarket fxNull = fxmh.GetRealFXMarket(MarketTestTools.dateArt, isExactDate: true); FXMarket fXMarket = fxmh.GetRealFXMarket(MarketTestTools.dateArt); if (fxNull != null) { Assert.IsTrue(false); } Assert.IsTrue(fXMarket.IsEquivalentTo(MarketTestTools.CreateMarket())); }
public void AggPnL_EquationXCCY() { FXMarketHistory fxmh = MarketTestTools.CreateMktHistory(true, true); fxmh.ConstructQuotes(new CurrencyPair(Currency.EUR, Currency.USD)); SortedList <DateTime, Transaction> txL = AllocationsTools.GetTransactionList(); AggregatedPnL pnl = new AggregatedPnL(Currency.EUR); pnl.AddTransactions(txL, fxmh); PnLElement elmt = pnl.ToTable(fxmh)["Total"]; Assert.IsTrue(Math.Abs(elmt.Position - elmt.Deposit + elmt.Withdrawal - elmt.TotalPnLWithFees) < 0.00001); }
public void AllocationHistory_Update() { FXMarketHistory fxmh = MarketTestTools.CreateMktHistory(); AllocationHistory allocH = AllocationsTools.GetAllocationHistory(fxmh, Fiat: Currency.EUR); CurrencyPair testCP = new CurrencyPair(Currency.EUR, Currency.USD); //fxmh.ConstructQuotes(testCP); allocH.UpdateHistory(Currency.EUR, fxmh); Price total = allocH.GetLastAllocation().Total; Assert.IsTrue(total .Equals(new Price(932.687, Currency.EUR), precision: 2)); }
public static FXMarketHistory CreateMktHistory(bool addDate3 = false, bool addDate4 = false) { FXMarketHistory fxmh = new FXMarketHistory(FrequencyRef); fxmh.AddFXMarket(CreateMarket()); fxmh.AddFXMarket(CreateMarket2()); if (addDate3) { fxmh.AddFXMarket(CreateMarket2(MarketTestTools.date3)); } if (addDate4) { fxmh.AddFXMarket(CreateMarket2(MarketTestTools.date4)); } return(fxmh); }
public TimeSeriesManager(Currency fiat, Frequency freq = Frequency.Hour4, bool useKraken = false, bool useInternet = true, string path = null, IView view = null) { if (view != null) { AddLoggingLink(view.PublishLogMessage); } Fiat = fiat; if (path != null) { BasePath = path; } DataProvider = new DataProvider(BasePath, view, useInternet: useInternet); SortedList <DateTime, Transaction> txList = DataProvider.GetTransactionList(useKraken: useKraken); StartDate = txList.First().Key; FXMH = DataProvider.GetFXMarketHistory(Fiat, DataProvider.GetCurrencyPairs(txList), StartDate, freq); AH = new AllocationHistory(view); APnL = new AggregatedPnL(fiat); APnL.AddTransactions(txList, FXMH); }
public XChangeRate GetAdjustedFXRate(DateTime date, XChangeRate tx_xr, FXMarketHistory fxmh) { if (CpRef.IsFiatPair) { return(fxmh.GetQuote(date, CpRef, isArtificial: true).Item2); } CryptoFiatPair cfp = tx_xr.CcyPair.GetCryptoFiatPair; if (cfp.IsNone) { return(fxmh.GetQuote(date, tx_xr.CcyPair, isArtificial: true).Item2); } CurrencyPair cp = new CurrencyPair(cfp.Fiat, CcyRef); XChangeRate mkt_rate = fxmh.GetQuote(date, cp, isArtificial: true).Item2; double alpha = 1; if (Ccy != tx_xr.CcyPair.Ccy1) { alpha = -1; } return(new XChangeRate(mkt_rate.Rate * Math.Pow(tx_xr.Rate, alpha), cfp.Crypto, cfp.Fiat)); }
public void FXMktHistory_GetQuote() { FXMarketHistory fxMH = MarketTestTools.CreateMktHistory(); CurrencyPair cp = new CurrencyPair(Currency.XBT, Currency.USD); Tuple <DateTime, XChangeRate> xr1 = fxMH.GetQuote(MarketTestTools.dateArt, cp, isArtificial: true); Tuple <DateTime, XChangeRate> xr2 = fxMH.GetQuote(MarketTestTools.dateArt, cp, isArtificial: false, isExactDate: false); Tuple <DateTime, XChangeRate> xr3 = fxMH.GetQuote(MarketTestTools.dateArt, cp, isArtificial: false, isExactDate: true); bool test1 = xr1.Item1 == MarketTestTools.dateArt && xr1.Item2.Equals(MarketTestTools.XbtUsdArtRate); bool test2 = xr2.Item1 == MarketTestTools.date1 && xr2.Item2.Equals(fxMH.GetRealFXMarket(MarketTestTools.date1).GetQuote(cp)); bool test3 = xr3.Item1 == MarketTestTools.dateArt && xr3.Item2 == null; Assert.IsTrue(test1 && test2 && test3); }
/// <summary> /// Auxiliary function in order to fill in the FX Market History (including the increasing Frequency feature) /// </summary> /// <param name="fxmh"></param> /// <param name="cpts"></param> /// <param name="startDate"></param> private void FillFXMarketHistory(FXMarketHistory fxmh, CurrencyPairTimeSeries cpts, DateTime startDate) { List <Tuple <DateTime, double> > ts = GetTimeSeries(cpts, isIndex: false, startDate: startDate); if (ts.Count > 0) { foreach (Tuple <DateTime, double> item in ts) { fxmh.AddQuote(item.Item1, new XChangeRate(item.Item2, cpts.CurPair)); } } DateTime firstDate = fxmh.GetFirstDate(cpts.CurPair); if (firstDate > startDate) { CurrencyPairTimeSeries newCpts = (CurrencyPairTimeSeries)cpts.Clone(); newCpts.IncreaseFreq(); if (newCpts.Freq != Frequency.None) { FillFXMarketHistory(fxmh, newCpts, startDate); } } }
private void AddTransaction(Transaction tx, DateTime nextTxDate, FXMarketHistory fxmh) { Allocation alloc = GetClosestAllocation(tx.Date, true); FXMarket FX = fxmh.GetArtificialFXMarket(tx.Date); alloc = alloc.AddTransaction(tx, FX); alloc.CalculateTotal(FX); if (History.ContainsKey(tx.Date)) { tx.Date = tx.Date.AddSeconds(1); } History.Add(tx.Date, alloc); // update the same allocation for the following days List <DateTime> datesList = fxmh.ArtificialFXMarkets .Keys .Where(x => x > tx.Date && x < nextTxDate) .Select(x => x).ToList(); foreach (DateTime date in datesList) { AddAllocationToHistory(alloc, fxmh.GetArtificialFXMarket(tx.Date)); } }
public Dictionary <string, PnLElement> ToTable(FXMarketHistory fxmh) { return(ToTable(fxmh, fxmh.LastRealDate)); }
public void AddTransactions(SortedList <DateTime, Transaction> txList, FXMarketHistory fxmh, bool fullReload = false) { AddTransactions(txList, fullReload); CalculatePnLs(fxmh); }
public void AddTransactions(Currency FiatRef, SortedList <DateTime, Transaction> stxList, FXMarketHistory fxmh) { if (!TxLoaded || FiatRef != CcyRefLoaded) { TxLoaded = true; CcyRefLoaded = FiatRef; List <Transaction> txList = stxList.Select(x => x.Value).ToList(); if (txList.Count > 0) { for (int i = 0; i < txList.Count - 1; i++) { AddTransaction(txList[i], txList[i + 1].Date, fxmh); } AddTransaction(txList.Last(), fxmh); } } }
private void AddTransaction(Transaction tx, FXMarketHistory fxmh) { AddTransaction(tx, new DateTime(9999, 1, 1), fxmh); }
public void AddTransactions(SortedList <DateTime, Transaction> txList, FXMarketHistory fxmh) { PnLElements = new SortedDictionary <DateTime, PnLElement> { }; DateTime lastDate = new DateTime(2008, 1, 1); foreach (var item in txList) { Transaction tx = item.Value; if ((tx.Date.ToOADate() - lastDate.ToOADate()) * 24 * 60 * 60 < 1) { lastDate = lastDate.AddSeconds(1); } else { lastDate = tx.Date; } bool justFees = true; if (tx.Received.Ccy == Ccy) { justFees = false; switch (tx.Type) { case TransactionType.Deposit: if (Ccy.IsFiat()) { PnLElement pnlD = GetLastPnLElement(); PnLElement newPnLD = (PnLElement)pnlD.Clone(); CurrencyPair cpD = new CurrencyPair(tx.Received.Ccy, CcyRef); XChangeRate xrD = fxmh.GetQuote(lastDate, cpD, isArtificial: true).Item2; double newWeightD = 1 / (1 + pnlD.Position / tx.Received.Amount); newPnLD.AverageCost = (1 - newWeightD) * pnlD.AverageCost + newWeightD * xrD.Rate; newPnLD.Position += tx.Received.Amount; newPnLD.Deposit += tx.Received.Amount * xrD.Rate; PnLElements.Add(lastDate, newPnLD); } else { PnLElement pnlDCrypto = GetLastPnLElement(); PnLElement newPnLDCrypto = (PnLElement)pnlDCrypto.Clone(); newPnLDCrypto.Position -= tx.Fees.Amount; newPnLDCrypto.Fees += tx.Fees.Amount; PnLElements.Add(lastDate, newPnLDCrypto); } break; case TransactionType.Trade: PnLElement pnlT = GetLastPnLElement(); PnLElement newPnLT = (PnLElement)pnlT.Clone(); CurrencyPair cpT = new CurrencyPair(tx.Received.Ccy, CcyRef); //XChangeRate xrT = fxmh.GetQuote(lastDate, cpT, isArtificial: true).Item2; XChangeRate xrT = GetAdjustedFXRate(lastDate, tx.XRate, fxmh); double amount = tx.Received.Amount; if (tx.Fees.Ccy == Ccy) { amount -= tx.Fees.Amount; newPnLT.Fees += tx.Fees.Amount * xrT.Rate; } double newWeight = 1 / (1 + pnlT.Position / amount); newPnLT.AverageCost = (1 - newWeight) * pnlT.AverageCost + newWeight * xrT.Rate; newPnLT.Position += amount; PnLElements.Add(lastDate, newPnLT); break; case TransactionType.Transfer: PnLElement pnlTf = GetLastPnLElement(); PnLElement newPnLTf = (PnLElement)pnlTf.Clone(); newPnLTf.Position += tx.Received.Amount; double newWeightTf = 1 / (1 + pnlTf.Position / tx.Received.Amount); newPnLT.AverageCost = (1 - newWeightTf) * pnlTf.AverageCost + newWeightTf * 0; PnLElements.Add(lastDate, newPnLTf); break; default: break; } } if (tx.Paid.Ccy == Ccy) { justFees = false; switch (tx.Type) { case TransactionType.Trade: PnLElement pnlTrade = GetLastPnLElement(); PnLElement newPnLTrade = (PnLElement)pnlTrade.Clone(); newPnLTrade.Position -= tx.Paid.Amount; Price feesT = tx.Fees; if (tx.Fees.Ccy == Ccy) { CurrencyPair cpFT = new CurrencyPair(feesT.Ccy, CcyRef); XChangeRate xrFT = fxmh.GetQuote(lastDate, cpFT, isArtificial: true).Item2; newPnLTrade.Fees += xrFT.ConvertPrice(feesT).Amount; newPnLTrade.Position -= feesT.Amount; } CurrencyPair cpT2 = new CurrencyPair(tx.Paid.Ccy, CcyRef); //XChangeRate xrT2 = fxmh.GetQuote(lastDate, cpT2, isArtificial: true).Item2; XChangeRate xrT2 = GetAdjustedFXRate(lastDate, tx.XRate, fxmh); newPnLTrade.RealizedPnL += tx.Paid.Amount * (xrT2.Rate - newPnLTrade.AverageCost); PnLElements.Add(lastDate, newPnLTrade); break; case TransactionType.WithDrawal: PnLElement pnl = GetLastPnLElement(); PnLElement newPnLW = (PnLElement)pnl.Clone(); Price feesW = tx.Fees; CurrencyPair cpW = new CurrencyPair(feesW.Ccy, CcyRef); XChangeRate xW = fxmh.GetQuote(lastDate, cpW, isArtificial: true).Item2; newPnLW.Fees += xW.ConvertPrice(feesW).Amount; newPnLW.Position -= feesW.Amount; if (tx.Paid.Ccy.IsFiat()) { newPnLW.Position -= tx.Paid.Amount; newPnLW.Withdrawal += xW.ConvertPrice(tx.Paid).Amount; newPnLW.RealizedPnL += (tx.Paid.Amount + feesW.Amount) * (xW.Rate - newPnLW.AverageCost); } else { newPnLW.RealizedPnL += feesW.Amount * (xW.Rate - newPnLW.AverageCost); } PnLElements.Add(lastDate, newPnLW); break; default: break; } } if (tx.Fees.Ccy == Ccy && justFees) { throw new Exception("ERROR JUST FEES!"); } } }