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

Multiple enhancements including support for multiple accounts sync and better symbol mapping to YAHOO #25

Merged
merged 2 commits into from
Jan 29, 2025

Conversation

NautillusSs
Copy link
Contributor

@NautillusSs NautillusSs commented Jan 29, 2025

Multiple enhancements including support for multiple accounts sync and better symbol mapping to YAHOO

Hello I have 2 accounts on IBKR and had to make a few changes to the code to support both of them and sync both in one run. I also added some enhancements listed below. Apologies for the big amount of changes. I wanted to make it all work locally and then push it. Happy to discuss. :)

  • Using Ghostfolio API filtering to fetch all activities from an account instead of comparing all activities.
  • Using Ghostfolio API filtering to delete all activities from an account instead of comparing all activities.
  • Support for flex query output containing multiple IBKR accounts data to sync multiple accounts.
  • Use ISIN symbol type from IBKR to upload new activities since it provides better mapping to YAHOO data and no need for the mapping file.
  • Add IBKR trade id to the uploaded activity comment to enable precise comparison when computing the diff. Diff retrocompatibility with previous versions of symbols (mapping file etc).
  • Find the IBKR account base currency from the flex query.
  • Get trade fee from the Trade node instead of UnbundledCommissionDetail node.
  • Use hours, minutes and seconds in the activity date.
  • Use logging instead of print.
  • Add operation to print all activities from an account.
  • Add underscores to some operations to be more readable.
  • If an environment variable does not have enough comma separated values, use the last one (good for unique stuff like API keys).
  • Caching of ghostfolio account id in the class for all operations to avoid multiple API calls.
  • Updated dependencies to remove security issues.

Summary by Sourcery

New Features:

  • Support syncing multiple IBKR accounts in a single run.

…d better symbol mapping to YAHOO.

Using Ghostfolio API filtering to fetch all activities from an account instead of comparing all activities.
Using Ghostfolio API filtering to delete all activities from an account instead of comparing all activities.
Support for flex query output containing multiple IBKR accounts data to sync multiple accounts.
Use ISIN symbol type from IBKR to upload new activities since it provides better mapping to YAHOO data and no need for the mapping file.
Add IBKR trade id to the uploaded activity comment to enable precise comparison when computing the diff. Diff retrocompatibility with previous versions of symbols (mapping file etc).
Find the IBKR account base currency from the flex query.
Get trade fee from the Trade node instead of UnbundledCommissionDetail node.
Use hours, minutes and seconds in the activity date.
Use logging instead of print.
Add operation to print all activities from an account.
Add underscores to some operations to be more readable.
If an environment variable does not have enough comma separated values, use the last one (good for unique stuff like API keys).
Caching of ghostfolio account id in the class for all operations to avoid multiple API calls.
Updated dependencies to remove security issues.
Copy link
Contributor

sourcery-ai bot commented Jan 29, 2025

Reviewer's Guide by Sourcery

This pull request introduces multiple enhancements to the IBKR sync functionality, including support for multiple accounts, improved symbol mapping, and more robust activity tracking. The changes involve fetching and parsing data from IBKR Flex Queries, transforming it into a format compatible with the Ghostfolio API, and then synchronizing the data. The implementation includes filtering activities using the Ghostfolio API, using ISIN symbols for better mapping, and adding trade IDs to activity comments for precise comparisons.

Sequence diagram for enhanced IBKR sync process

sequenceDiagram
    participant IBKR
    participant SyncIBKR
    participant Ghostfolio

    SyncIBKR->>IBKR: Download Flex Query
    IBKR-->>SyncIBKR: Return Flex Query Response
    SyncIBKR->>SyncIBKR: Parse Query & Extract Account Statement
    SyncIBKR->>SyncIBKR: Get Account Currency
    SyncIBKR->>Ghostfolio: Create/Get IBKR Account ID
    Ghostfolio-->>SyncIBKR: Return Account ID
    SyncIBKR->>Ghostfolio: Set Cash Balance

    loop For Each Trade
        SyncIBKR->>SyncIBKR: Get Symbol (ISIN preferred)
        SyncIBKR->>SyncIBKR: Format Activity with Trade ID
    end

    SyncIBKR->>Ghostfolio: Get Existing Activities (filtered)
    Ghostfolio-->>SyncIBKR: Return Activities
    SyncIBKR->>SyncIBKR: Compare Activities using Trade IDs
    SyncIBKR->>Ghostfolio: Import New Activities
Loading

Updated class diagram for SyncIBKR

classDiagram
    class SyncIBKR {
        -account_id: Optional[str]
        -ghost_token: str
        -ghost_host: str
        -ibkr_account_id: str
        -ghost_account_name: str
        -ghost_currency: str
        -symbol_mapping: dict
        +sync_ibkr()
        +get_symbol_for_trade(trade: Trade, data_source: str)
        +set_cash_to_account(account_id: str, cash: dict)
        +get_all_acts_for_account(account_id: str, range: str, symbol: str)
        +get_account_flex_statement(query: FlexQueryResponse)
        -create_ghost_token(ghost_host: str, ghost_key: str)
        -create_or_get_IBKR_accountId()
    }

    note for SyncIBKR "Enhanced to support:
    - Multiple accounts
    - ISIN symbol mapping
    - Trade ID tracking"
Loading

File-Level Changes

Change Details Files
Support for multiple IBKR accounts.
  • Added logic to handle multiple IBKR accounts from the flex query.
  • Added logic to fetch the base currency from the flex query.
  • Added logic to use the IBKR account id to filter the flex query.
  • Added logic to use the IBKR account name to filter the Ghostfolio accounts.
SyncIBKR.py
main.py
Improved symbol mapping using ISIN from IBKR.
  • Modified the code to use ISIN symbol type from IBKR for better mapping to YAHOO data.
  • Removed the need for a mapping file for most symbols.
  • Added logic to use the symbol mapping file if the ISIN is not available.
SyncIBKR.py
Enhanced activity tracking with IBKR trade IDs.
  • Added IBKR trade ID to the uploaded activity comment.
  • Implemented precise comparison using the IBKR trade ID when computing the diff.
  • Maintained retrocompatibility with previous versions of symbols (mapping file etc).
SyncIBKR.py
Improved activity filtering and deletion using Ghostfolio API.
  • Utilized Ghostfolio API filtering to fetch all activities from an account.
  • Utilized Ghostfolio API filtering to delete all activities from an account.
SyncIBKR.py
Refactored code for better readability and maintainability.
  • Replaced print statements with logging.
  • Added underscores to some operations for better readability.
  • Implemented caching of the Ghostfolio account ID to avoid multiple API calls.
  • Updated dependencies to remove security issues.
  • Added a new operation to print all activities from an account.
  • Added a new file to pretty print tables.
SyncIBKR.py
main.py
Improved trade fee handling.
  • Modified the code to get trade fee from the Trade node instead of UnbundledCommissionDetail node.
SyncIBKR.py
Improved date handling.
  • Modified the code to use hours, minutes, and seconds in the activity date.
SyncIBKR.py
Improved environment variable handling.
  • Modified the code to use the last value if an environment variable does not have enough comma separated values.
main.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @NautillusSs - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider splitting future large changes into smaller, focused pull requests to make review easier. However, the current changes are well structured and documented.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Owner

@agusalex agusalex left a comment

Choose a reason for hiding this comment

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

Overall looking really good thank you so much! left some comments

from SyncIBKR import SyncIBKR
from pretty_print import pretty_print_table

template = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
Copy link
Owner

Choose a reason for hiding this comment

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

this is good thank you!


GETALLACTS = "GETALLACTS"
GET_ALL_ACTS = "GET_ALL_ACTS"
Copy link
Owner

Choose a reason for hiding this comment

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

noice

@@ -8,4 +8,5 @@ symbol_mapping:
EGLN: EGLN.L
IWDA: IWDA.AS
VWCE: VWCE.DE
ASML: ASML.AS
Copy link
Owner

Choose a reason for hiding this comment

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

awesome!

@agusalex
Copy link
Owner

agusalex commented Jan 29, 2025

Thank you so much for your contribution, this means a lot for the project! ❤️

@agusalex agusalex merged commit d62b25f into agusalex:main Jan 29, 2025
1 check passed
@NautillusSs
Copy link
Contributor Author

Thank you for reviewing so quickly and merging!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants