KikeM / degiro-wrapper

From raw Degiro files to clean dataframes for portfolio analysis and valuation.
9 stars 1 forks source link

ENH: Compute with multicurrency positions #54

Closed KikeM closed 2 years ago

KikeM commented 2 years ago

Change valuation from local currency to portfolio currency.

KikeM commented 2 years ago

Hi @mmngreco,

When I was dealing with your dataset I found an odd thing about your transaction dates: they have the year first instead of the day first. This was causing the code to be unable to process correctly your file (in fact, a silent bug took place, the file is processed and a report can be created, but transactions have no date assigned 😆).

Could you please regenerate this file (download raw + create DB) with the latest version of the library? Please check for the content of "Fecha" column in the transactions file, which format does it show?

KikeM commented 2 years ago

Hi again @mmngreco,

As soon as you confirm the date pattern in your transaction files, this PR is ready. I have updated the valuation currency so that it computes correctly TnA and CFs in the same currency.

mmngreco commented 2 years ago

Yay! Thank you for taking care about this. Sure, I will check it out this afternoon ;-)

mmngreco commented 2 years ago

It raises an KeyError:

$ degiro create-db-transactions -p transactions_2019-01-01_2022-06-25.csv
Welcome to the degiro-wrapper CLI!
Cleaning raw transactions
From : /home/mgreco/github/mmngreco/degiro-wrapper/transactions_2019-01-01_2022-06-25.csv
To   : /home/mgreco/github/mmngreco/degiro-wrapper/db_transactions.csv
Traceback (most recent call last):
  File "/home/mgreco/github/mmngreco/degiro-wrapper/venv/lib/python3.10/site-packages/pandas/core/indexes/base.py", line 3621, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas/_libs/index.pyx", line 136, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 163, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'date'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/mgreco/github/mmngreco/degiro-wrapper/venv/bin/degiro", line 33, in <module>
    sys.exit(load_entry_point('degiro-wrapper', 'console_scripts', 'degiro')())
  File "/home/mgreco/github/mmngreco/degiro-wrapper/venv/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/home/mgreco/github/mmngreco/degiro-wrapper/venv/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/home/mgreco/github/mmngreco/degiro-wrapper/venv/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/mgreco/github/mmngreco/degiro-wrapper/venv/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/mgreco/github/mmngreco/degiro-wrapper/venv/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/home/mgreco/github/mmngreco/degiro-wrapper/src/degiro_wrapper/cli/cli.py", line 344, in create_db_transactions
    transactions = clean_transactions(raw)
  File "/home/mgreco/github/mmngreco/degiro-wrapper/src/degiro_wrapper/core/preprocess.py", line 267, in clean_transactions
    clean[Transactions.DATE],
  File "/home/mgreco/github/mmngreco/degiro-wrapper/venv/lib/python3.10/site-packages/pandas/core/frame.py", line 3505, in __getitem__
    indexer = self.columns.get_loc(key)
  File "/home/mgreco/github/mmngreco/degiro-wrapper/venv/lib/python3.10/site-packages/pandas/core/indexes/base.py", line 3623, in get_loc
    raise KeyError(key) from err
KeyError: 'date'

debug

 265         # Convert to date                                                                                                                                                                                                                
 266         __import__("pdb").set_trace()                                                                                                                                                                                                    
 267  ->     clean[Transactions.DATE] = pd.to_datetime(                                                                                                                                                                                       
 268             clean[Transactions.DATE],                                                                                                                                                                                                    
 269             dayfirst=True,                                                                                                                                                                                                               
 270             # yearfirst=True,                                                                                                                                                                                                            
 271             errors="coerce",                                                                                                                                                                                                             
 272             # format="%d-%m-%Y",                                                                                                                                                                                                         
 273         )                                                                                                                                                                                                                                
 274                                                                                                                                                                                                                                          
 275         return clean                                                                                                                                                                                                                     
>>> Transactions.DATE   # it seems that this should be "Fecha" instead of "date"
'date'
>>> clean.head().iloc[:, [0]]   # at this point this is the format of the column "Fecha" 
        Fecha
0  2022-06-14
1  2022-06-14
2  2022-05-04
3  2022-04-19
4  2022-03-16
KikeM commented 2 years ago

Mmm, did you download the raw transaction file from scratch with the latest version of the library (or this branch)?

That is,

degiro download-transactions -s 20190101 

and then execute the creation of the database,

degiro create-db-transactions -p transactions_2019-01-01_2022-07-24.csv

When I download the transactions, the date has the day first, but yours seem to have the year first. I would like to understand why, and I am not sure if it is because you created that file with an older version of the library.

Thx!

mmngreco commented 2 years ago

yes I did, should I delete the previous one before download it?

KikeM commented 2 years ago

Ah, sorry, the fact that the end date of your raw transactions is from the end of June made me think that you had not updated the raw file, transactions_2019-01-01_2022-06-25.csv.

yes I did, should I delete the previous one before download it?

You don't need to delete it, but I would like to know what happens with an updated file. I am not sure if our differences in the date format is due to the library or Degiro internal configurations of our accounts.

mmngreco commented 2 years ago

I've tried you command and it worked, sorry for the noise. Here're the answers:

KikeM commented 2 years ago

No worries :)

Sweet, then it seems that there was some incompatibility problem with a previous version of the library.

It seems that before we were saving the raw file with format yearfirst=True, but now we are doing it with the format dayfirst=True.

In that case, I will clean up the PR and merge it.

Thank you Max 😉

mmngreco commented 2 years ago

I would like to let you note that year first follows the international standard date ISO 8601 : https://fits.gsfc.nasa.gov/iso-time.html

KikeM commented 2 years ago

I would like to let you note that year first follows the international standard date ISO 8601 : https://fits.gsfc.nasa.gov/iso-time.html

Oh, I didn't know that; good to know 👍🏽

The matter is that the date on the raw file is out of our range, but we do convert it to a format that follows the ISO convention (year first).

Across the library we always use that format too, for instance when we interact with the CLI in the definition of the dates.