Skip to content

Commit

Permalink
fix: fix outdated orders not being handled properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Augusto de Oliveira committed Dec 23, 2024
1 parent 1183050 commit 9f39e84
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 21 deletions.
77 changes: 56 additions & 21 deletions src/tastytrade_ghostfolio/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,46 @@ def split_shares(
return transactions


def extract_outdated_ghostfolio_orders(
activities: list[GhostfolioActivity], orders: list[GhostfolioActivity]
) -> list[GhostfolioActivity]:
outdated_orders: list[GhostfolioActivity] = []
for order in orders:
try:
next(
filter(
lambda x: x.date == order.date
and x.quantity == order.quantity
and x.symbol == order.symbol,
activities,
)
)
except StopIteration:
outdated_orders.append(order)

return outdated_orders


def extract_activities_for_exporting(
activities: list[GhostfolioActivity], orders: list[GhostfolioActivity]
) -> list[GhostfolioActivity]:
activities_for_exporting: list[GhostfolioActivity] = []
for activity in activities:
try:
next(
filter(
lambda x: x.date == activity.date
and x.quantity == activity.quantity
and x.symbol == activity.symbol,
orders,
)
)
except StopIteration:
activities_for_exporting.append(activity)

return activities_for_exporting


if __name__ == "__main__":
ghostfolio_service = GhostfolioService()
ghostfolio_accounts = ghostfolio_service.get_all_accounts()
Expand Down Expand Up @@ -173,30 +213,25 @@ def split_shares(
activity.account_id = tastytrade_account.account_id
activity.comment = "Activity created by Tastytrade-Ghostfolio."

orders = ghostfolio_service.get_all_orders(tastytrade_account.account_id)

print("Started exporting activities to Ghostfolio...")
activities_for_exporting: list[GhostfolioActivity] = []
for activity in activities:
try:
next(
filter(
lambda x: x.date == activity.date
and x.quantity == activity.quantity
and x.symbol == activity.symbol,
orders,
)
)
except StopIteration:
activities_for_exporting.append(activity)

try:
symbol_mappings = SymbolMappingRepository().get_symbol_mappings()
activities_for_exporting = adapt_symbols(
activities_for_exporting, symbol_mappings
)
activities = adapt_symbols(activities, symbol_mappings)
except SymbolMappingsNotFoundException:
print("Skipping symbol changes, as no mapping file was found.")

ghostfolio_service.export_activities_to_ghostfolio(activities_for_exporting)
print("Started exporting activities to Ghostfolio...")
orders = ghostfolio_service.get_all_orders(tastytrade_account.account_id)
outdated_orders = extract_outdated_ghostfolio_orders(activities, orders)
if outdated_orders:
print("Deleting outdated orders...")
for order in outdated_orders:
ghostfolio_service.delete_order(order)

activities = extract_activities_for_exporting(activities, orders)
if activities:
print("Exporting new activities...")
ghostfolio_service.export_activities_to_ghostfolio(activities)
else:
print("No new activities to import!")

print("Done!")
14 changes: 14 additions & 0 deletions src/tastytrade_ghostfolio/services/ghostfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,17 @@ def create_account(self, account: GhostfolioAccount) -> GhostfolioAccount:

except requests.exceptions.HTTPError as ex:
raise Exception(f"Error while requesting accounts to Ghostfolio: {ex}")

def delete_order(self, order: GhostfolioActivity):
response = requests.delete(
f"{GhostfolioSettings.BASE_URL}/order/{order.id}",
headers=self.AUTHORIZATION_HEADER,
)

try:
response.raise_for_status()

except requests.exceptions.HTTPError:
raise Exception(
f"Error while deleting order in Ghostfolio: {response.json()}"
)

0 comments on commit 9f39e84

Please sign in to comment.