def CalculateTotal(self, FX: FXMarket, curRef: Currency = Currency.EUR): total = Price(0, curRef) for cur in self.Dictionary.keys(): total = FX.Sum(total, self.Dictionary[cur]) total = FX.Sum(total, self.Fee.Price) self.Total = total return total
def __init__(self, dictionary: dict, fee=AllocationElement(0., 0., Currency.NONE)): #self.Date = date self.Dictionary = dictionary self.Fee = fee self.Total = Price(0, Currency.NONE)
def VaR(self, q: float = 0.05, returnSize: int = 400, n: int = 1000, scale: float = 4, printCorrelation: bool = True): C = Copula(list(self.Dictionary.keys()), returnSize) C.ComputeCorrelation() if (printCorrelation): print("Correlation Matrix (" + str(returnSize) + " returns):") C.PrintCorrelation() sim = C.Simulate(n) PnL = [] for i in range(n): reti = sim[i] PnLi = 0 j = 0 for cur in C.CurrencyList: PnLi += self.Dictionary[cur].Percentage * C.Densities[ cur].TransformFromStdNorm(reti[j]) j += 1 PnL += [PnLi] PnL.sort() return Price(round(PnL[int(n * q)] * self.Total.Amount * scale, 2), self.Total.Currency)
def Sum(self, price : Price, Delta : Price): if price.Currency == Currency.NONE and price.Currency.Amount == 0: return Delta else: if Delta.Currency == Currency.NONE and Delta.Amount ==0: return price else: return Price(price.Amount + self.ConvertPrice(Delta, price.Currency).Amount, price.Currency)
def Download(self, data: pd.DataFrame, curRef: Currency = Currency.EUR): self.CcyRef = curRef self.List = [] paidPrice = Price(0, curRef) receivedPrice = Price(0, curRef) fees = Price(0, curRef) for (index, row) in data.iterrows(): if row["type"] == "deposit": self.List += [ Transaction(TransactionType.Deposit, row["time"], Price(0, curRef), Price(row["amount"], row["asset"][1:]), Price(0, curRef)) ] elif row["type"] == "trade": asset = row["asset"] asset = asset[(len(asset) - 3):] if row["amount"] > 0: receivedPrice = Price(row["amount"], asset) elif row["amount"] < 0: paidPrice = Price(-row["amount"], asset) else: raise Exception("Amount free Price") if row["fee"] > 0: if fees.Amount == 0: fees = Price(row["fee"], row["asset"][1:]) else: raise Exception("Double fees Problem") if paidPrice.Amount > 0 and receivedPrice.Amount > 0: self.List += [ Transaction(TransactionType.Trade, row["time"], paidPrice, receivedPrice, fees) ] paidPrice = Price(0, curRef) receivedPrice = Price(0, curRef) fees = Price(0, curRef) elif row["type"] == "withdrawal": asset = row["asset"] curr = Currency[asset[(len(asset) - 3):]] paid = Price(-row["amount"], curr) self.List += [ Transaction(TransactionType.Withdrawals, row["time"], paid, Price(0, curr), Price(row["fee"], curr)) ] else: raise Exception("Trade type Unknown")
def ConvertPrice(self, input: Price, output: Currency): rate = self.GetFXRate(input.Currency, output) return Price(input.Amount * rate, output)
def Total(self, cur: Currency = Currency.EUR): usedCur = self.Dictionary.keys.First() if cur in self.Dictionary.keys(): usedCur = cur allocCur = self.Dictionary[cur] return Price(round(allocCur.Amount / allocCur.Percentage, 2), usedCur)
def __init__(self, percentage: float, amount: float, currency: Currency): if percentage <= 1.0: self.Percentage = percentage else: Exception("Allocation Element > 100%") self.Price = Price(amount, currency)
XCRateETH1 = XChangeRate(90, Currency("ETH"), Currency("EUR")) XCRateBTC2 = XChangeRate(1200, Currency("XBT"), Currency("EUR")) XCRateETH2 = XChangeRate(100, Currency("ETH"), Currency("EUR")) FXMH = FXMarketHistory() FXMH.AddXChangeRate(datetime(2017, 1, 1), XCRateBTC0) FXMH.AddXChangeRate(datetime(2017, 1, 1), XCRateETH0) FXMH.AddXChangeRate(datetime(2017, 3, 1), XCRateBTC1) FXMH.AddXChangeRate(datetime(2017, 3, 1), XCRateETH1) FXMH.AddXChangeRate(datetime(2017, 5, 1), XCRateBTC2) FXMH.AddXChangeRate(datetime(2017, 5, 1), XCRateETH2) print(FXMH.ToString) t1 = Transaction(TransactionType.Deposit, datetime(2017, 2, 1), Price(0, Currency.NONE), Price(1000, Currency.EUR), Price(0, Currency.NONE)) t2 = Transaction(TransactionType.Trade, datetime(2017, 2, 15), Price(500, Currency.EUR), Price(0.5, Currency.XBT), Price(0, Currency.EUR)) t3 = Transaction(TransactionType.Trade, datetime(2017, 4, 1), Price(0.25, Currency.XBT), Price(3.4375, Currency.ETH), Price(0, Currency.EUR)) TL = TransactionList() TL.SetList([t1, t2, t3]) AH = AllocationHistory(TL, FXMH) print(AH.ToString)