Package: cmc
CoinMarketCap Historical Data RetrievalObtainUSD price historydata forCoinMarketCap-listed cryptocurrencies.Use this library as acommand-line scriptto obtain historical cryptocurrency data on the fly, orimport thecmclibraryto obtain cryptocurrency data within your Python program.ContentsInstallationUsageCommand LineUsageExamplesModuleUsageExamplesLegacyUpdatesInstallationInstall coinmarketcap-history with pip:$pipinstallcmcUsageCommand LineThe command line tool is useful for US tax reporting, among other things.  If you wish to report the cost basis for a trade (or for coins acquired through mining), the IRS requires you to denominate that cost basis in USD.  In the case of token-for-token trades (e.g. purchasing ETH with BTC), that requires you know the USD:BTC exchange rate at the time of the trade.Rather than getting the exchange rate at the exact moment of your trade, which is generally not feasible, the IRS standard is to use the average of a stock's high and low price for the day. CoinMarketCap doesn't provide this figure, but this tool does.Command Line UsageTo gather cryptocurrency data, open a terminal and run:$coinmarketcap<currency1><start_date><end_date>where:currencyis the (case-insensitive) name of the currency / token as displayed on CoinMarketCap, with dashes in place of spaces (i.e. bitcoin).start_dateis the beginning of the range to fetch data for inyyyy-mm-ddformat (i.e. 2017-10-01 for 2017 October 10th).end_yearis the end of the range to fetch data for inyyyy-mm-ddformat.Data is returned in the following tabular format:BitcoinDateOpenHighLowCloseVolumeMarket CapAverage........................Data for multiple cryptocurrencies can be obtained with:$coinmarketcap<currency_1,currency_2,...,currency_n><start_date><end_date>Note:currencies must be comma-separated, with no spaces in between.Data for multiple cryptocurrencies is returned in the following tabular format:BitcoinRippleDateOpenHighLowCloseVolumeMarket CapAverageOpenHighLowCloseVolumeMarket CapAverage.............................................The above information can also be found by running:$coinmarketcap-hWrite outputs to a file by running:$coinmarketcap<currency><start_date><end_date>><output_filename>For faster retrieval, cryptocurrency data can be gathered asynchronously by supplying the--asynchroflag:$coinmarketcap<currency><start_date><end_date>--asynchroRunningcoinmarketcapasynchronously greatly reduces the amount of time required to obtain data for cryptocurrencies, especially when gathering data for multiple cryptocurrencies at a time:Note:Asynchronous runtimes may vary according to CPU architecture. Benchmark performed with a 64-bit 6-core AMD processor.Command Line ExamplesCollecting data for one cryptocurrency:$coinmarketcapbitcoin2017-01-012017-12-31Collecting data for multiple cryptocurrencies:$coinmarketcapbitcoin,ripple,ethereum2017-01-012017-12-31Collecting data for multiple cryptocurrencies asynchronously(faster):$coinmarketcapbitcoin,ripple,ethereum2017-01-012017-12-31--asynchroWriting output to a file:$coinmarketcapbitcoin2017-01-012017-12-31>bitcoin_history.csvWriting output for multiple cryptocurrencies to a file:$pythoncoinmarketcap.pybitcoin,ripple,ethereum2017-01-012017-12-31>bitcoin_ripple_ethereum_history.csvModuleIn addition to command-line functionality, coinmarketcap-history provides thecmclibrary, which allows users to obtain CoinMarketCap data from within a Python program. Historical is returned in the form of aPandas DataFrame, which allows for easy use.To get started with thecmclibrary, import it from within your program:fromcmcimportcoinmarketcapData for cryptocurrencies can be gathered using thegetDataFor()method:getDataFor()params:cryptocurrencies: string or listcrypto(s) to be scraped. supply a string for a single cryptocurrency, or supply a list of strings for multiple cryptocurrencies.start_date: datetime objectdatetime object for the beginning of the range to fetch data for. Must contain values for year, month, and day.end_date: datetime objectdatetime object for the ebd of the range to fetch data for. Must contain values for year, month, and day.fields(optional): listcolumns to obtain upon data retrieval. Options are:['Open','High','Low','Close','Volume','Market Cap','Average']iffieldsis not specified, all fields are returned.async(optional): booleanifTrue, data is gathered asynchronously. This is especially useful when gathering data for multiple cryptocurrencies at a time, which can be slow when gathered synchronously. Ifasyncis not specified, data is gathered synchronously.DOWNLOAD_DIR(optional): stringString of the relative path to save data to and load data from. IfDOWNLOAD_DIRis not specified, no data is saved.returns:out:Pandas DataFrameA DataFrame containing historical price information for the specified cryptocurrencies through the desired daterange.Library ExamplesGathering data for a single cryptocurrency:fromcmcimportcoinmarketcapfromdatetimeimportdatetimecrypto='bitcoin'start_date,end_date=datetime(2017,6,1),datetime(2018,6,1)df_bitcoin=coinmarketcap.getDataFor(crypto,start_date,end_date)Getting data for multiple cryptocurrencies:fromcmcimportcoinmarketcapfromdatetimeimportdatetimecryptos=['bitcoin','ripple','ethereum']start_date,end_date=datetime(2017,6,1),datetime(2018,6,1)df_cryptos=coinmarketcap.getDataFor(cryptos,start_date,end_date)To cache retrieved data, simply supply a string forDOWNLOAD_DIR. The string should be a relative path to the desired download directory. Data is stored in the lightweight.msgformat.Saving data and pulling cached data:fromcmcimportcoinmarketcapfromdatetimeimportdatetimecryptos=['bitcoin','ripple','ethereum']start_date,end_date=datetime(2017,6,1),datetime(2018,6,1)# retrieves data and stores .msg files in DOWNLOAD_DIRdf_cryptos=coinmarketcap.getDataFor(cryptos,start_date,end_date,DOWNLOAD_DIR='data/coinmarketcap')# does not retreive data. Instead, pulls cached data from DOWNLOAD_DIRdf_cryptos=coinmarketcap.getDataFor(cryptos,start_date,end_date,DOWNLOAD_DIR='data/coinmarketcap')Pulling specified columns only:fromcmcimportcoinmarketcapfromdatetimeimportdatetimecryptos=['bitcoin','ripple','ethereum']start_date,end_date=datetime(2017,6,1),datetime(2018,6,1)df_cryptos=coinmarketcap.getDataFor(cryptos,start_date,end_date,fields=['High','Low','Close'])LegacyLegacy code can be obtained from thecoinmarketcap-history-legacyrepository foundhere.Updates2.0.0 - July 6th, 2018as of version 2, coinmarketcap-history now offers support for Python 3. Additionally, thecmcartifact allows for global use of thecoinmarketcapcommand line tool, as well as dedicated support for in-program operations.2.0.2 - May 30th, 2019as of version 2.0.2, coinmarketcap-history can now be run asynchronously inside jupyter notebooks thanks tochanges in the way they handle event loops.