LunaticMuch / msdownloader

Morningstar downloader for Funds and other securities data
16 stars 0 forks source link

Consider auto-populating securities.yaml file #3

Open the-solipsist opened 3 years ago

the-solipsist commented 3 years ago

You could use ledger commodities or hledger stats to get a list of commodities used in the file. By grepping for ISIN numbers, you can use that to build the skeleton of the YAML file.

Something like:

#!/usr/bin/env bash
ledger_dir=$HOME/accounts
all_file=$ledger_dir/all.journal
rates_file=$ledger_dir/rates.journal

if type ledger &>/dev/null
   then
   ledger -f $all_file commodities | \
   grep -Eo '\b([A-Z]{2})([A-Z0-9]{9})([0-9]{1})\w*' > /tmp/ledger-stocks
elif type hledger &>/dev/null
   then
   hledger -f $all_file stats | grep -E '^Commodities' | \
   grep -Eo '\b([A-Z]{2})([A-Z0-9]{9})([0-9]{1})\w*' > /tmp/ledger-stocks
else
   echo "Neither ledger nor hledger is present"
fi

(Note: the above is adapted from bash scripts I use, but I haven't tested it myself yet, as I don't have stocks in my journal.)

I use this method for extracting mutual funds and India's national pension scheme units from my hledger journal.

LunaticMuch commented 3 years ago

It's doable, but would be incomplete. I see the following problems:

  1. The name of the commodity you use in hledger might not be the ISIN so you need to touch manually the file
  2. The ISIN you use requires a universe which is not stored/available in your hledger setup
  3. If you do not use the ISIN you must the the name you use in hledger in the output (this option is on the list but I've not had time work on it yet)

So what we can get is, like your script (apologies I've not tested it) is a list of commodities, but you still need a manual job to making it workable on the YAML.

the-solipsist commented 3 years ago

I agree, a script can only get you a "skeleton" of the YAML file, as I'd noted. For instance, it wouldn't distinguish between funds and shares, which apparently is necessary. The resultant file will need to be manually edited. But I feel it would be easier to edit a file, rather than create a file anew.

  1. The name of the commodity you use in hledger might not be the ISIN so you need to touch manually the file

I'm a bit confused: if the name of the commodity in the journal is not the ISIN, then how will hledger be able to use the ISIN provided in the price db by msdownloader? (Wouldn't "GB00B907VX32" need to be a commodity name for the line P 2021-01-08 "GB00B907VX32" 214.18 GBX to make sense to hledger?)

  1. The ISIN you use requires a universe which is not stored/available in your hledger setup

The "universe", if I understand it correctly, refers to the Morningstar code for the exchange on which the security is traded. Can that be found through a web lookup? (Alphavantage, e.g., provides a SYMBOL_SEARCH function, for instance, for such a purpose, which can be queried using the ISIN as a keyword, and which returns the exchange as well in the json payload. Perhaps Morningstar provides something similar?) The name of the fund/share can also be found through such a query if the ISIN is available.

LunaticMuch commented 3 years ago

Can't believe I missed this.

I'm a bit confused ...

Today everything must be done using only the ISIN. So in hledger you need to call the commodity/funds/whatever with the ISIN. In the future, I envisaged already the usage of a name parameter for each ISIN to allow more flexibility.

The "universe", if I understand it correctly, refers to the Morningstar code for the exchange on which the security is traded.

More or less, it's correct. The universe is a Morningstar proprietary definition based on the way they internally categorize data. Most of times it's not needed, particularly on funds you find on major markets like LSE. If something is traded locally, say Frankfurt, and not widely available, you need to provide the universe as the ISIN is not enough.

You might need to inspect the page for it. If you cannot find what you need, ping here a message or write directly to me and I can help you figure it out. It would be good for including additional info the README at some point.

I am not expert with Alphavantage, but regardless that, from a purely financial standpoint, the same financial instrument can be traded on multiple markets and under different currencies. ISIN is not unique, so unless the ISIN points, by default, to what we need, we must add an additional info.