Package: pricehist
pricehistA command-line tool for fetching and formatting historical price data, with
support for multiple data sources and output formats.InstallationInstall via pip orpipx.pipx install pricehistSourcesalphavantage:Alpha Vantagebankofcanada:Bank of Canada daily exchange ratescoinbasepro:Coinbase Procoindesk:CoinDesk Bitcoin Price Indexcoinmarketcap:CoinMarketCapecb:European Central Bank Euro foreign exchange reference ratesyahoo:Yahoo! FinanceOutput formatsbeancount:Beancountcsv:Comma-separated valuesjson:JSONjsonl:JSON linesgnucash-sql:GnuCashSQLledger:LedgerandhledgerReactionsThis is my new favourite price fetcher, by far.--Simon Michael, creator ofhledger(ref)This is great!--Martin Blais, creator ofBeancount(ref)How toFetch pricesFetch prices by choosing a source, a pair and, optionally, a time interval.pricehist fetch ecb EUR/AUD -s 2021-01-04 -e 2021-01-08date,base,quote,amount,source,type
2021-01-04,EUR,AUD,1.5928,ecb,reference
2021-01-05,EUR,AUD,1.5927,ecb,reference
2021-01-06,EUR,AUD,1.5824,ecb,reference
2021-01-07,EUR,AUD,1.5836,ecb,reference
2021-01-08,EUR,AUD,1.5758,ecb,referenceThe default output format is CSV, which is suitable for use in spreadsheets and
with other tools. For example, you can generate a price chart from the command
line as follows (or usingan alias).pricehist fetch coindesk BTC/USD -s 2021-01-01 | \
  sed 1d | \
  cut -d, -f1,4 | \
  gnuplot -p -e '
    set datafile separator ",";
    set xdata time;
    set timefmt "%Y-%m-%d";
    set format x "%b\n%Y";
    plot "/dev/stdin" using 1:2 with lines title "BTC/USD"
  'Show usage informationAdd-hto any command to see usage information.pricehist fetch -husage: pricehist fetch SOURCE PAIR [-h] [-vvv] [-t TYPE] [-s DATE | -sx DATE] [-e DATE | -ex DATE]
[-o beancount|csv|json|jsonl|gnucash-sql|ledger] [--invert] [--quantize INT]
[--fmt-base SYM] [--fmt-quote SYM] [--fmt-time TIME] [--fmt-decimal CHAR] [--fmt-thousands CHAR]
[--fmt-symbol rightspace|right|leftspace|left] [--fmt-datesep CHAR]
[--fmt-csvdelim CHAR] [--fmt-jsonnums]

positional arguments:
  SOURCE                   the source identifier
  PAIR                     pair, usually BASE/QUOTE, e.g. BTC/USD

optional arguments:
  -h, --help               show this help message and exit
  -vvv, --verbose          show all log messages
  -t TYPE, --type TYPE     price type, e.g. close
  -s DATE, --start DATE    start date, inclusive (default: source start)
  -sx DATE, --startx DATE  start date, exclusive
  -e DATE, --end DATE      end date, inclusive (default: today)
  -ex DATE, --endx DATE    end date, exclusive
  -o FMT, --output FMT     output format (default: csv)
  --invert                 invert the price, swapping base and quote
  --quantize INT           round to the given number of decimal places
  --fmt-base SYM           rename the base symbol in output
  --fmt-quote SYM          rename the quote symbol in output
  --fmt-time TIME          set a particular time of day in output (default: 00:00:00)
  --fmt-decimal CHAR       decimal point in output (default: '.')
  --fmt-thousands CHAR     thousands separator in output (default: '')
  --fmt-symbol LOCATION    commodity symbol placement in output (default: rightspace)
  --fmt-datesep CHAR       date separator in output (default: '-')
  --fmt-csvdelim CHAR      field delimiter for CSV output (default: ',')
  --fmt-jsonnums           numbers not strings for JSON output (default: False)Choose and customize the output formatAs the output format you can choose one ofbeancount,csv,json,jsonl,ledgerorgnucash-sql.pricehist fetch ecb EUR/AUD -s 2021-01-04 -e 2021-01-08 -o ledgerP 2021-01-04 00:00:00 EUR 1.5928 AUD
P 2021-01-05 00:00:00 EUR 1.5927 AUD
P 2021-01-06 00:00:00 EUR 1.5824 AUD
P 2021-01-07 00:00:00 EUR 1.5836 AUD
P 2021-01-08 00:00:00 EUR 1.5758 AUDFormatting options let you control certain details of the output.pricehist fetch ecb EUR/AUD -s 2021-01-04 -e 2021-01-08 -o ledger \
  --fmt-time '' --fmt-datesep / --fmt-base € --fmt-quote $ --fmt-symbol leftP 2021/01/04 € $1.5928
P 2021/01/05 € $1.5927
P 2021/01/06 € $1.5824
P 2021/01/07 € $1.5836
P 2021/01/08 € $1.5758Fetch new prices onlyYou can update an existing file without refetching the prices you already have.
First find the date of the last price, then fetch from there, drop the header
line if present and append the rest to the existing file.last=$(tail -1 prices-eur-usd.csv | cut -d, -f1)
pricehist fetch ecb EUR/USD -sx $last -o csv | sed 1d >> prices-eur-usd.csvLoad prices into GnuCashYou can generate SQL for a GnuCash database and apply it immediately with one
of the following commands.pricehist fetch ecb EUR/AUD -s 2021-01-01 -o gnucash-sql | sqlite3 Accounts.gnucash
pricehist fetch ecb EUR/AUD -s 2021-01-01 -o gnucash-sql | mysql -u username -p -D databasename
pricehist fetch ecb EUR/AUD -s 2021-01-01 -o gnucash-sql | psql -U username -d databasename -v ON_ERROR_STOP=1Beware that the GnuCash project itself does not support integration at the
database level, so there is a risk that the SQL generated bypricehistwill
be ineffective or even damaging for some version of GnuCash. In practice, this
strategy has been used successfully by other projects. Reading the SQL and
keeping regular database backups is recommended.The GnuCash database must already contain commodities with mnemonics matching
the base and quote of new prices, otherwise the SQL will fail without making
changes.Each price entry is given a GUID based on its content (date, base, quote,
source, type and amount) and existing GUIDs are skipped in the final insert, so
you can apply identical or overlapping SQL files multiple times without
creating duplicate entries in the database.Show source informationThesourcecommand shows information about a source.pricehist source alphavantageID          : alphavantage
Name        : Alpha Vantage
Description : Provider of market data for stocks, forex and cryptocurrencies
URL         : https://www.alphavantage.co/
Start       : 1995-01-01
Types       : close, open, high, low, adjclose, mid
Notes       : Alpha Vantage has data on...Available symbols can be listed for most sources, either as full pairs or as
separate base and quote symbols that will work in certain combinations.pricehist source ecb --symbolsEUR/AUD    Euro against Australian Dollar
EUR/BGN    Euro against Bulgarian Lev
EUR/BRL    Euro against Brazilian Real
EUR/CAD    Euro against Canadian Dollar
EUR/CHF    Euro against Swiss Franc
...It may also be possible to search for symbols.pricehist source alphavantage --search TeslaTL0.DEX       Tesla, Equity, XETRA, EUR
TL0.FRK       Tesla, Equity, Frankfurt, EUR
TSLA34.SAO    Tesla, Equity, Brazil/Sao Paolo, BRL
TSLA          Tesla Inc, Equity, United States, USD
TXLZF         Tesla Exploration Ltd, Equity, United States, USDInspect source interactionsYou can see extra information by adding the verbose option (--verboseor-vvv), includingcurlcommands that reproduce each request to a source.pricehist fetch coindesk BTC/USD -s 2021-01-01 -e 2021-01-05 -vvvDEBUG Began pricehist run at 2021-08-12 14:38:26.630357.
DEBUG Starting new HTTPS connection (1): api.coindesk.com:443
DEBUG https://api.coindesk.com:443 "GET /v1/bpi/historical/close.json?currency=USD&start=2021-01-01&end=2021-01-05 HTTP/1.1" 200 319
DEBUG curl -X GET -H 'Accept: */*' -H 'Accept-Encoding: gzip, deflate' -H 'Connection: keep-alive' -H 'User-Agent: python-requests/2.25.1' --compressed 'https://api.coindesk.com/v1/bpi/historical/close.json?currency=USD&start=2021-01-01&end=2021-01-05'
DEBUG Available data covers the interval [2021-01-01--2021-01-05], as requested.
date,base,quote,amount,source,type
2021-01-01,BTC,USD,29391.775,coindesk,close
2021-01-02,BTC,USD,32198.48,coindesk,close
2021-01-03,BTC,USD,33033.62,coindesk,close
2021-01-04,BTC,USD,32017.565,coindesk,close
2021-01-05,BTC,USD,34035.0067,coindesk,close
DEBUG Ended pricehist run at 2021-08-12 14:38:26.709428.Running a loggedcurlcommand shows exactly what data is returned by the
source.pricehist fetch coindesk BTC/USD -s 2021-01-01 -e 2021-01-05 -vvv 2>&1 \
  | grep '^DEBUG curl' | sed 's/^DEBUG //' | bash | jq .{"bpi":{"2021-01-01":29391.775,"2021-01-02":32198.48,"2021-01-03":33033.62,"2021-01-04":32017.565,"2021-01-05":34035.0067},"disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index. BPI value data returned as USD.","time":{"updated":"Jan 6, 2021 00:03:00 UTC","updatedISO":"2021-01-06T00:03:00+00:00"}}Use viabean-priceBeancount users may wish to usepricehistsources viabean-price. To do so,
ensure thepricehistpackage is installed in an accessible location.You can fetch the latest price directly from the command line.bean-price -e "USD:pricehist.beanprice.coindesk/BTC:USD"2021-08-18 price BTC:USD                          44725.12 USDYou can fetch a series of prices by providing a Beancount file as input.; input.beancount
2021-08-14 commodity BTC
  price: "USD:pricehist.beanprice.coindesk/BTC:USD:close"bean-price input.beancount --update --update-rate daily --inactive --clear-cache2021-08-14 price BTC                            47098.2633 USD
2021-08-15 price BTC                            47018.9017 USD
2021-08-16 price BTC                             45927.405 USD
2021-08-17 price BTC                            44686.3333 USD
2021-08-18 price BTC                              44725.12 USDAdding-vwill print progress information,-vvwill print debug information,
including that frompricehist.A source map specification forbean-pricehas the form<currency>:<module>/[^]<ticker>. Additional<module>/[^]<ticker>parts can
be appended, separated by commas.The module name will be of the formpricehist.beanprice.<source_id>.The ticker symbol will be of the formBASE:QUOTE:TYPE.Any non-alphanumeric characters except the equals sign (=), hyphen (-),
period (.), or parentheses ((or)) are special characters that need to
be encoded as their a two-digit hexadecimal code prefixed with an underscore,
becausebean-priceticker symbols don't allow all the characters used bypricehistpairs.This pagewill do it for you.For example, the Yahoo! Finance symbol for the Dow Jones Industrial Average is^DJI, and would have the source map specificationUSD:pricehist.beanprice.yahoo/_5eDJI, or for the daily high priceUSD:pricehist.beanprice.yahoo/_5eDJI::high.Use as a libraryYou may findpricehist's source classes useful in your own scripts.$ python
Python 3.9.6 (default, Jun 30 2021, 10:22:16)
[GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pricehist.series import Series
>>> from pricehist.sources.ecb import ECB
>>> series = ECB().fetch(Series("EUR", "AUD", "reference", "2021-01-04", "2021-01-08"))
>>> series.prices
[Price(date='2021-01-04', amount=Decimal('1.5928')), Price(date='2021-01-05', amount=Decimal('1.5927')), Price(date='2021-01-06', amount=Decimal('1.5824')), Price(date='2021-01-07', amount=Decimal('1.5836')), Price(date='2021-01-08', amount=Decimal('1.5758'))]A subclass ofpricehist.exceptions.SourceErrorwill be raised for any error.ContributeContributions are welcome! If you discover a bug or want to work on a
non-trivial change, please open aGitLab issueto discuss it.Runmake install-pre-commit-hookset up local pre-commit checks.
Set up your editor to runisort,BlackandFlake8,
or run them manually viamake format lint.TerminologyAsourceis an upstream service that can provide a series of prices.Eachseriesof prices is for one pair and price type.Thepairis made up of a
base and a quote, each given as a symbol. Sometimes you will give the base
only, and the quote will be determined with information from the source. The
available pairs, the symbols used in them and the available price types all
depend on the particular source used.Thebaseis the currency or commodity being valued. Each price expresses
the value of one unit of the base.Thequoteis the unit used to express the value of the base.Asymbolis a code or abbreviation for a currency or commodity.Thepricesin a series each have a date and an amount.Theamountis the number of units of the quote that are equal to one unit
of the base.Consider the following command.pricehist fetch coindesk BTC/USD --type closecoindeskis the ID of the CoinDesk Bitcoin Price Index source.BTCis the symbol for Bitcoin, used here as the base.USDis the symbol for the United States Dollar, used here as the quote.BTC/USDis the pair Bitcoin against United States Dollar.closeis the price type for the last price of each day.A BTC/USD price of the amount 29,391.775 can be written as
"BTC/USD = 29391.775" or "BTC 29391.775 USD", and means that one Bitcoin is
worth 29,391.775 United States Dollars.Initial design choicesTo keep things simple,pricehistprovides only univariate time series of
daily historical prices. It doesn't provide other types of market, financial or
economic data, real-time prices, or other temporal resolutions. Multiple or
multivariate series require multiple invocations.Potential featuresIn the future,pricehistmay be extended to cover some of the following
features:Time of day: Sources sometimes provide specific times for each day's
high/low prices and these could be preserved for output. This would require
changes to how dates are handled internally, clarification of time zone
handling and extension of the time formatting option.Alternate resolutions: Some sources can provide higher or lower
resolution data, such as hourly or weekly. These could be supported where
available. For other cases an option could be provided for downsampling data
before output.Real-time prices: These generally come from different source endpoints
than the historical data. Real-time prices will usually have a different
price type, such aslast,bidorask. Support for real-time prices
would allow adding sources that don't provide historical data. Start and end
times are irrelevant when requesting real-time prices. A "follow" option
could continuously poll for new prices.Related non-price data: Trading volume, spreads, split and dividend
events and other related data could be supported. The base/quote/type model
used for prices would work for some of this. Other things may require
extending the model.Multivariate series: Would allow, for example, fetching
high/low/open/close prices in a single invocation.formatcommand: A command for rewriting existing CSV data into one of
the other output formats.AlternativesBeancount'sbean-pricetool fetches
prices and addresses other workflow concerns in a Beancount-specific manner,
generally requiring a Beancount file as input.ThePiecashlibrary is a pythonic interface
to GnuCash files stored in SQL which has aCommodity.update_pricesmethod for fetching historical prices.
The GnuCash wiki documentswrapper scriptsfor theFinance::QuoteHistPerl
module.