lolokraus / DegiroAPI

An unofficial API for the trading platform Degiro, with the ability to get real time data and historical data
MIT License
216 stars 84 forks source link

Is there any way I can download the Account Statement as .csv? #38

Open matheusft opened 3 years ago

Jakub-CZ commented 3 years ago

With the enhancement contributed in #16 you can make your own:

import csv
from datetime import date

degiro = ...  # connect & login here
ao = degiro.account_overview(date(2021, 4, 1), date.today())
cash_movements = ao["cashMovements"]  # type: List[Dict]
fieldnames = ['date', 'valueDate', 'description', 'currency', 'change', 'type', 'exchangeRate', 'orderId']
# productId, balance: also available but they would require further processing
with open('report.csv', 'w', newline='', encoding='utf-8') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames, extrasaction="ignore")
    writer.writeheader()
    writer.writerows(cash_movements)
matheusft commented 3 years ago

Hummmm, I see, thanks @Jakub-CZ . I was hoping to get something in the same shape as the .csv file available for download.

Date, Time, Value date, Product, ISIN, Description, FX, Change_Currency,  Change_Value, Balance_Currency, Balance_Value, and Order Id

Is the ISIN represented by any of the fields from fieldnames ? What is type from fieldnames, by the way? fieldnames = ['date', 'valueDate', 'description', 'currency', 'change', 'type', 'exchangeRate', 'orderId']

Jakub-CZ commented 3 years ago

In my report types is either of these two: {'TRANSACTION', 'CASH_TRANSACTION'}. Personally, I don't use that attribute for anything.


The field productId gives you some kind of Degiro's internal number for the stock. You can translate it to something meaningful (e.g. the ISINs) using degiro.product_info(). See https://github.com/lolokraus/DegiroAPI#product_info

This is how you can build a handy-dandy dictionary mapping all the productIds that appear in cash_movements to ISINs:

from pprint import pprint
from degiroapi.product import Product

pid_map = {p: Product(degiro.product_info(p)).isin for p in filter(None, {e.get("productId") for e in cash_movements})}
pprint(pid_map)

# {322171: 'US4581401001',
#  331916: 'US94106L1098',
#  332062: 'US7170811035',
#  ...

How to put this all together in a single CSV is left as an exercise to the reader.