Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion src/domain/stock/profit/per_stock_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ def _calculate_cost_for_sell(self, buy_queue: Queue, transaction: Transaction) -
try:
oldest_buy = buy_queue.head()
except IndexError:
raise ValueError("No buy transaction to match sell transaction. Try exporting whole history.")
raise ValueError(
f"No buy transaction to match sell transaction: {transaction}. "
"Try exporting whole history."
)
oldest_buy_stock_amount = oldest_buy.asset.amount

if oldest_buy_stock_amount <= stock_amount_to_account + self.EPSILON:
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/crypto/revolut/row_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ def _action(cls, row: dict) -> Action:

@classmethod
def _datetime(cls, row: dict) -> pendulum.DateTime:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, the whole _datetime function can look just like this:

parsed = dateutil_parser.parse(row["Date"])
return pendulum.instance(parsed)

with include from dateutil import parser as dateutil_parser at the top of the file

In that way we won't be dependent on any other future date format changes

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to give it a try - if it works it would be awesome! 👍

It's really tedious to work out those Revolut date formats...

@MarvinRucinski could you check it out?

date_str = row["Date"].replace("Sept", "Sep") # revolut uses Sept instead of Sep
date_str = re.sub(r"\s+", " ", row["Date"], flags=re.UNICODE) # Revolut sometimes uses unicode thin/non-breaking spaces in timestamps.
date_str = date_str.replace("Sept", "Sep").strip() # Revolut uses Sept instead of Sep
supported_formats = (
"DD MMM YYYY, HH:mm:ss",
"MMM DD, YYYY, hh:mm:ss A",
"MMM DD, YYYY, h:mm:ss A",
)

for fmt in supported_formats:
Expand Down
15 changes: 10 additions & 5 deletions src/plugins/stock/revolut/row_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

class RowParser:
OPERATIONS = {
"BUY - MARKET": OperationType.BUY,
"BUY - LIMIT": OperationType.BUY,
"SELL - MARKET": OperationType.SELL,
"SELL - LIMIT": OperationType.SELL,
"BUY": OperationType.BUY,
"SELL": OperationType.SELL,
"DIVIDEND": OperationType.DIVIDEND,
"CUSTODY FEE": OperationType.SERVICE_FEE,
"STOCK SPLIT": OperationType.STOCK_SPLIT,
Expand All @@ -27,6 +25,7 @@ def _fiat_value(cls, row: Dict) -> FiatValue:
currency = CurrencyBuilder.build(row['Currency'])
# e.g."-$1,003.01"
amount_row = row['Total Amount']
amount_row = amount_row.strip(str(currency))

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I handled it already in #36 :)

if amount_row.startswith("-"):
amount_row = amount_row[1:]
amount_row = amount_row[1:].replace(",", "")
Expand All @@ -43,4 +42,10 @@ def _date(cls, row: dict) -> pendulum.DateTime:

@classmethod
def _operation_type(cls, row: dict) -> OperationType:
return cls.OPERATIONS.get(row['Type'])
operation_type = cls.OPERATIONS.get(row['Type'])
if operation_type:
return operation_type
for operation_name, operation_type in cls.OPERATIONS.items():
if operation_name in row['Type']:
return operation_type
return None
Comment on lines +45 to +51

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather make this whole function a regex matching, instead of that.

'BUY\w+' is OperationType.BUY etc,
'DIVIDEND' is OperationType.DIVIDEND

Loading