Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Agustin Alexander committed May 26, 2023
2 parents 66582b7 + 8e0cbac commit d54b915
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/manual_actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
steps:
- uses: actions/checkout@master
- name: Publish to Registry
uses: elgohr/Publish-Docker-Github-Action@master
uses: elgohr/Publish-Docker-Github-Action@v5
with:
name: agusalex/ghostfolio-sync
username: ${{ secrets.DOCKERHUB_USERNAME }}
Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# Ghostfolio-Sync

[![Docker Hub package][dockerhub-badge]][dockerhub-link]

[dockerhub-badge]: https://img.shields.io/badge/images%20on-Docker%20Hub-blue.svg
[dockerhub-link]: https://hub.docker.com/repository/docker/agusalex/ghostfolio-sync "Docker Hub Image"

Sync your Ghostfolio with IBKR ( more to come? )

## Setup
Expand All @@ -19,7 +25,7 @@ When you configure your Flex Query give it, cash statement permissions as well a

## Run in Docker

``` docker run -e GHOST_TOKEN=YOUR_GHOST_TOKEN -e IBKR_TOKEN=YOUR-IBKR-TOKEN -e IBKR_QUERY=YOUR-IBKR-QUERY```
```docker run -e GHOST_TOKEN=YOUR_GHOST_TOKEN -e IBKR-TOKEN=YOUR-IBKR-TOKEN -e IBKR-QUERY=YOUR-IBKR-QUERY agusalex/ghostfolio-sync```

### More Options
| Envs |Description |
Expand All @@ -28,5 +34,13 @@ When you configure your Flex Query give it, cash statement permissions as well a
|**IBKR_QUERY** | Your Query ID |
|**GHOST_TOKEN** | The token for your ghostfolio account |
|**GHOST_HOST** | (optional) Ghostfolio Host, only add if using custom ghostfolio |
|**GHOST_CURRENCY** | (optional) Ghostfolio Account Currency, only applied if account doesn't exist |
|**CRON** | (optional) To run on a [Cron Schedule](https://crontab.guru/) |
|**OPERATION** | (optional) SYNCIBKR (default) or DELETEALL (will erase all operations of all accounts) |

## Contributing

* Feel free to submit any issue or PR's you think necessary
* If you like the work and want to buy me a coffee you are more than welcome :)

<a href="https://www.buymeacoffee.com/YiQkYsghUQ" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"></a>
30 changes: 22 additions & 8 deletions SyncIBKR.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ def get_diff(old_acts, new_acts):
class SyncIBKR:
IBKRCATEGORY = "9da3a8a7-4795-43e3-a6db-ccb914189737"

def __init__(self, ghost_host, ibkrtoken, ibkrquery, ghost_token):
def __init__(self, ghost_host, ibkrtoken, ibkrquery, ghost_token, ghost_currency):
self.ghost_token = ghost_token
self.ghost_host = ghost_host
self.ghost_currency = ghost_currency
self.ibkrtoken = ibkrtoken
self.ibkrquery = ibkrquery

Expand All @@ -76,7 +77,9 @@ def sync_ibkr(self):
return
self.set_cash_to_account(account_id, get_cash_amount_from_flex(query))
for trade in query.FlexStatements[0].Trades:
if trade.openCloseIndicator.CLOSE:
if trade.openCloseIndicator is None:
print("trade is not open or close (ignoring): %s", trade)
elif trade.openCloseIndicator.CLOSE:
date = datetime.strptime(str(trade.tradeDate), date_format)
iso_format = date.isoformat()
symbol = trade.symbol
Expand Down Expand Up @@ -110,10 +113,15 @@ def set_cash_to_account(self, account_id, cash):
if cash == 0:
print("No cash set, no cash retrieved")
return False
account = {"accountType": "SECURITIES", "balance": float(cash), "id": account_id, "currency": "USD",
"isExcluded": False,
"name": "IBKR",
"platformId": self.IBKRCATEGORY}
account = {
"accountType": "SECURITIES",
"balance": float(cash),
"id": account_id,
"currency": self.ghost_currency,
"isExcluded": False,
"name": "IBKR",
"platformId": self.IBKRCATEGORY
}

url = f"{self.ghost_host}/api/v1/account/{account_id}"

Expand Down Expand Up @@ -193,8 +201,14 @@ def addAct(self, act):
return response.status_code == 201

def create_ibkr_account(self):
account = {"accountType": "SECURITIES", "balance": 0, "currency": "USD", "isExcluded": False, "name": "IBKR",
"platformId": "9da3a8a7-4795-43e3-a6db-ccb914189737"}
account = {
"accountType": "SECURITIES",
"balance": 0,
"currency": self.ghost_currency,
"isExcluded": False,
"name": "IBKR",
"platformId": "9da3a8a7-4795-43e3-a6db-ccb914189737"
}

url = f"{self.ghost_host}/api/v1/account"

Expand Down
3 changes: 2 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
ibkr_tokens = os.environ.get("IBKR_TOKEN").split(",")
ibkr_queries = os.environ.get("IBKR_QUERY").split(",")
ghost_hosts = os.environ.get("GHOST_HOST", "https://ghostfol.io").split(",")
ghost_currency = os.environ.get("GHOST_CURRENCY", "USD").split(",")
operations = os.environ.get("OPERATION", SYNCIBKR).split(",")

if __name__ == '__main__':
for i in range(len(operations)):
ghost = SyncIBKR(ghost_hosts[i], ibkr_tokens[i], ibkr_queries[i], ghost_tokens[i])
ghost = SyncIBKR(ghost_hosts[i], ibkr_tokens[i], ibkr_queries[i], ghost_tokens[i], ghost_currency[i])
if operations[i] == SYNCIBKR:
print("Starting sync")
ghost.sync_ibkr()
Expand Down

0 comments on commit d54b915

Please sign in to comment.