Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calculate prices from csv #96

Merged
merged 47 commits into from
Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
43e1fe5
calculate prices from csv
scientes Dec 13, 2021
74077df
Merge branch 'main' into prices-from-csv
scientes Dec 13, 2021
f2729c7
fixed linting except line to long
scientes Dec 13, 2021
13fa82e
fixed for multiple coins
scientes Dec 19, 2021
9afb70a
added sql migration and extra function
scientes Dec 25, 2021
8e21d8a
bugfix (prices were inverted db) and migration fix
scientes Dec 25, 2021
9c52dd8
fixed missing inversion
scientes Dec 31, 2021
f74327d
formatting
scientes Dec 31, 2021
5112a14
Prices from csv (#3)
Griffsano Dec 31, 2021
45b6a39
FIX type annotation of group_by
provinzio Jan 2, 2022
7c0f924
REFACTOR add newline
provinzio Jan 2, 2022
dc852ee
REFACTOR get_price_from_csv
provinzio Jan 2, 2022
62b861c
ADD docstring to _sort_pair
provinzio Jan 2, 2022
5124ebe
RENAME reciprocal bool to inverted
provinzio Jan 2, 2022
3d10f3d
ADD comment in get_price when price is missing
provinzio Jan 2, 2022
30fe876
TODO ADD rudimentary patch functions
provinzio Jan 2, 2022
9ea7822
boilerplate
scientes Jan 7, 2022
e711b47
removed classes and replaced get_price occurrences
scientes Jan 9, 2022
0dd0132
refractored db functions
scientes Jan 9, 2022
33876f2
fixed patch function
scientes Jan 9, 2022
ab15eaa
fixed imports and flake added patch 001
scientes Jan 10, 2022
7265ee7
fix iosort
scientes Jan 10, 2022
7bb7bd8
Prices from csv (#5)
Griffsano Jan 28, 2022
179bb05
Refactor logging patching info
provinzio Feb 5, 2022
acc215b
CHANGE get_tablenames does not query §version table per default
provinzio Feb 5, 2022
1419746
UPDATE transfer platform parameter
provinzio Feb 5, 2022
d70559f
REFACTOR function docstrings
provinzio Feb 5, 2022
09a81e5
FIX mypy linting error
provinzio Feb 5, 2022
a06f735
Reorder preamble of set_price_db
provinzio Feb 5, 2022
e99a7aa
UPDATE assert db exists before setting new price
provinzio Feb 5, 2022
cd33058
ADD helper functions to get patch func names/versions
provinzio Feb 5, 2022
b54c366
REFACTOR patch databases
provinzio Feb 5, 2022
63b3959
ADD create database in book.py when missing
provinzio Feb 5, 2022
c400a4c
AUTOFORMAT database.py
provinzio Feb 5, 2022
78046f4
Avoid circular import
provinzio Feb 5, 2022
7b81aa9
CHANGE update_version create §version table when missing
provinzio Feb 5, 2022
700ea26
Merge remote-tracking branch 'origin/main' into prices-from-csv
provinzio Feb 5, 2022
0f2e7fb
UPDATE format of warning message
provinzio Feb 5, 2022
a6fc0eb
UPDATE reword debug message
provinzio Feb 5, 2022
7cae5c3
ADD debug message when updating db version
provinzio Feb 5, 2022
f932e0c
CHANGE Create db not in book, but on set_price when db is missing
provinzio Feb 5, 2022
0b93b3b
UPDATE duplicate price warning, add db price for comparison
provinzio Feb 5, 2022
81b4e30
AUTOFORMAT price_data
provinzio Feb 5, 2022
53753d6
ADD comment to price_data when querying new price
provinzio Feb 5, 2022
16dabab
FIX database type for price
provinzio Feb 5, 2022
c1538cf
ADD set_price_db parameter to overwrite already existing prices
provinzio Feb 5, 2022
9f906b1
FIX linting errors
provinzio Feb 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,35 @@ def detect_exchange(self, file_path: Path) -> Optional[str]:

return None

def get_price_from_csv(self):
print("hi")
scientes marked this conversation as resolved.
Show resolved Hide resolved
for platform, operations_a in misc.group_by(
self.operations, "platform"
).items():
for timestamp, operations_b in misc.group_by(
operations_a, "utc_time"
).items():
if len(operations_b) > 1:
buytr = selltr = None
for operation in operations_b:
if isinstance(operation, tr.Buy):
buytr = operation
elif isinstance(operation, tr.Sell):
selltr = operation
scientes marked this conversation as resolved.
Show resolved Hide resolved
if buytr is not None and selltr is not None:
price = decimal.Decimal(selltr.change / buytr.change)
logging.debug(
f"Added price from csv: {selltr.coin}/{buytr.coin} price: {price}"
)
self.price_data.set_price_db(
platform,
selltr.coin,
buytr.coin,
timestamp,
price,
)
break

def read_file(self, file_path: Path) -> None:
"""Import transactions form an account statement.

Expand Down
2 changes: 1 addition & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def main() -> None:
if not status:
log.warning("Stopping CoinTaxman.")
return

book.get_price_from_csv()
taxman.evaluate_taxation()
taxman.export_evaluation_as_csv()
taxman.print_evaluation()
Expand Down
28 changes: 22 additions & 6 deletions src/price_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ def __set_price_db(
create_query = (
f"CREATE TABLE `{tablename}`"
"(utc_time DATETIME PRIMARY KEY, "
"price FLOAT NOT NULL);"
"price STR NOT NULL);"
scientes marked this conversation as resolved.
Show resolved Hide resolved
)
cur.execute(create_query)
cur.execute(query, (utc_time, str(price)))
Expand Down Expand Up @@ -580,13 +580,19 @@ def set_price_db(
price (decimal.Decimal): [description]
"""
assert coin != reference_coin
if coin < reference_coin:
coin_a = coin
coin_b = reference_coin
else:
coin_a = reference_coin
coin_b = coin
scientes marked this conversation as resolved.
Show resolved Hide resolved
db_path = self.get_db_path(platform)
tablename = self.get_tablename(coin, reference_coin)
tablename = self.get_tablename(coin_a, coin_b)
try:
self.__set_price_db(db_path, tablename, utc_time, price)
except sqlite3.IntegrityError as e:
if str(e) == f"UNIQUE constraint failed: {tablename}.utc_time":
price_db = self.get_price(platform, coin, utc_time, reference_coin)
price_db = self.get_price(platform, coin_a, utc_time, coin_b)
if price != price_db:
log.warning(
"Tried to write price to database, "
Expand Down Expand Up @@ -627,15 +633,22 @@ def get_price(
return decimal.Decimal("1")

db_path = self.get_db_path(platform)
tablename = self.get_tablename(coin, reference_coin)
if coin < reference_coin:
coin_a = coin
coin_b = reference_coin
inverted = False
else:
coin_a = reference_coin
coin_b = coin
inverted = True
tablename = self.get_tablename(coin_a, coin_b)
scientes marked this conversation as resolved.
Show resolved Hide resolved

# Check if price exists already in our database.
if (price := self.__get_price_db(db_path, tablename, utc_time)) is None:
try:
get_price = getattr(self, f"_get_price_{platform}")
except AttributeError:
raise NotImplementedError("Unable to read data from %s", platform)

price = get_price(coin, utc_time, reference_coin, **kwargs)
assert isinstance(price, decimal.Decimal)
self.__set_price_db(db_path, tablename, utc_time, price)
scientes marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -646,7 +659,10 @@ def get_price(
# Do not save price in database.
price = self.__mean_price_db(db_path, tablename, utc_time)

return price
if inverted:
return decimal.Decimal(1 / price)
scientes marked this conversation as resolved.
Show resolved Hide resolved
else:
return price

def get_cost(
self,
Expand Down