Source code for babao.inputs.ledger.krakenLedgerInput
"""
Handle logging in database all our real transactions on kraken api (wet-run)
Also fetch from kraken api the history of your past transactions
"""
import sys
import pandas as pd
import babao.utils.date as du
from babao.inputs.krakenInputBase import ABCKrakenInput
from babao.inputs.ledger.ledgerInputBase import ABCLedgerInput
from babao.utils.enum import CryptoEnum, QuoteEnum, ActionEnum
[docs]class ABCKrakenLedgerInput(ABCLedgerInput, ABCKrakenInput):
"""Base class for any kraken ledger"""
def __init__(self, log_to_file=True, unused_temp=False):
super().__init__()
self.log_to_file = log_to_file # TODO: move that to ABCLedgerInput
self.balance = 0 # TODO
[docs] def fetch(self):
if self.current_row is None:
since = "0"
else:
since = str(du.nanoToSec(self.current_row.name))
res = self._doRequest("Ledgers", {
"start": since, "asset": self.asset.name
})
if res is None:
self.up_to_date = False
return None
if res["count"] == 0:
self.up_to_date = True
return None
if res["count"] > 50 and self.current_row is None:
# kraken api is *STOOPID*: if we don't have the exact date of the
# first transaction, we can't fetch the ledger data starting from
# the begining... so we'll need a couple extra requests, sorry!
res = self._doRequest("Ledgers", {
"ofs": res["count"] - 1, "asset": self.asset.name
}) # first
raw_ledger = pd.DataFrame(res["ledger"]).T
self.up_to_date = len(raw_ledger) != 50
if raw_ledger.empty:
return raw_ledger
raw_ledger.index = du.secToNano(raw_ledger["time"])
del raw_ledger["time"]
del raw_ledger["aclass"]
del raw_ledger["asset"]
raw_ledger = raw_ledger.sort_index()
raw_ledger.rename(columns={"amount": "volume"}, inplace=True)
raw_ledger["volume"] = raw_ledger["volume"].astype(float)
raw_ledger["fee"] = raw_ledger["fee"].astype(float)
raw_ledger["balance"] = raw_ledger["balance"].astype(float)
raw_ledger["product"] = 0 # TODO: enum?
raw_ledger.loc[
(raw_ledger["volume"] > 0) & (raw_ledger["type"] == "trade"), "type"
] = "buy"
raw_ledger["type"] = raw_ledger["type"].replace({
"withdrawal": ActionEnum.WITHDRAW.value,
"deposit": ActionEnum.DEPOSIT.value,
"buy": ActionEnum.BUY.value,
"trade": ActionEnum.SELL.value,
}).astype(int)
return raw_ledger
[docs] def buy(self, ledger, volume_spent, price, timestamp=None):
raise NotImplementedError("Sorry, this is not implement yet :/")
[docs] def sell(self, ledger, volume_spent, price, timestamp=None):
raise NotImplementedError("Sorry, this is not implement yet :/")
[docs] def deposit(self, ledger, volume, timestamp=None):
raise NotImplementedError("Sorry, this is not implement yet :/")
[docs] def withdraw(self, ledger, volume, timestamp=None):
raise NotImplementedError("Sorry, this is not implement yet :/")
# Dynamically generate these inkd of classes for all assets available:
# class KrakenLedgerEURInput(ABCKrakenLedgerInput):
# asset = QuoteEnum.EUR
for asset in list(QuoteEnum) + list(CryptoEnum):
cls = "KrakenLedger" + asset.name + "Input"
setattr(
sys.modules[__name__],
cls,
type(cls, (ABCKrakenLedgerInput,), {"asset": asset})
)