6
$\begingroup$

What's the easiest way to ingest time series data on cryptocurrencies into Mathematica?

I'm looking for up-to-date price, volume, and other properties for both major coins and altcoins.

$\endgroup$
1
  • 1
    $\begingroup$ I don't believe there's any functionality on crypto in WL directly related to trading crypto, although there is a lot of blockchain stuff in there (like BlockchainData). You can use CurrencyConvert for a couple of the popular coins as well. You are likely to need to build this functionality yourself from some available API. $\endgroup$ – Carl Lange May 14 at 18:11
5
$\begingroup$

There are multiple crypto-currencies data sources, but a small proportion of them give a convenient way of extracting crypto-currencies data automatically. I found the easiest to work with to be https://finance.yahoo.com/cryptocurrencies, [YF1].

(I also looked into using https://data.bitcoinity.org and https://www.coindesk.com/coindesk20. )

Remark: The code below is made with certain ad-hoc inductive reasoning that brought meaningful results. This means the code has to be changed if the underlying data organization in [YF1] is changed.


Getting cryptocurrencies symbols and summaries

In this section we get all crypto-currencies symbols and related metadata.

Get the data of all crypto-currencies in [YF1]:

AbsoluteTiming[
  lsData = Import["https://finance.yahoo.com/cryptocurrencies", "Data"]; 
 ]

(*{6.36272, Null}*)

Locate the data:

pos = First@Position[lsData, {"Symbol", "Name", "Price (Intraday)", "Change", "% Change", ___}];
dsCryptoCurrenciesColumnNames = lsData[[Sequence @@ pos]]
Length[dsCryptoCurrenciesColumnNames]

(*{"Symbol", "Name", "Price (Intraday)", "Change", "% Change", "Market Cap", "Volume in Currency (Since 0:00 UTC)", "Volume in Currency (24Hr)", "Total Volume All Currencies (24Hr)", "Circulating Supply", "52 Week Range", "1 Day Chart"}*)

(*12*)

Get the data:

dsCryptoCurrencies = lsData[[Sequence @@ Append[Most[pos], 2]]];
Dimensions[dsCryptoCurrencies]

(*{25, 10}*)

Make a dataset:

dsCryptoCurrencies = Dataset[dsCryptoCurrencies][All, AssociationThread[dsCryptoCurrenciesColumnNames[[1 ;; -3]], #] &]

enter image description here


Get all time series

In this section we get all the crypto-currencies time series from [YF1].

AbsoluteTiming[
 ccNow = Round@AbsoluteTime[Date[]] - 
   AbsoluteTime[{1970, 1, 1, 0, 0, 0}];
 aCryptoCurrenciesDataRaw =
  Association@
   Map[
    # -> ResourceFunction["ImportCSVToDataset"][
       "https://query1.finance.yahoo.com/v7/finance/download/" <> # <>
         "?period1=1410825600&period2=" <> ToString[ccNow] <> 
        "&interval=1d&events=history&includeAdjustedClose=true"] &, 
    Normal[dsCryptoCurrencies[All, "Symbol"]]
    ];
 ]

(*{6.21141, Null}*)

Check we got the data with dimensions retrieval:

Dimensions /@ aCryptoCurrenciesDataRaw

(*<|"BTC-USD" -> {2464, 7}, "ETH-USD" -> {2140, 7}, "USDT-USD" -> {2303, 7}, "BNB-USD" -> {1422, 7}, "ADA-USD" -> {1354, 7}, "DOGE-USD" -> {2464, 7}, "XRP-USD" -> {2464, 7}, "USDC-USD" -> {982, 7}, "DOT1-USD" -> {300, 7}, "HEX-USD" -> {547, 7}, "UNI3-USD" -> {77, 7},"BCH-USD" -> {1424, 7}, "LTC-USD" -> {2464, 7}, "SOL1-USD" -> {432, 7}, "LINK-USD" -> {1365, 7}, "MATIC-USD" -> {780, 7}, "THETA-USD" -> {1246, 7}, "XLM-USD" -> {2464, 7}, "ICP1-USD" -> {28, 7}, "VET-USD" -> {1048, 7}, "ETC-USD" -> {1788, 7}, "FIL-USD" -> {1281, 7}, "AMP1-USD" -> {76, 7}, "TRX-USD" -> {1372, 7}, "XMR-USD" -> {2464, 7}|>*)

Check we got the data with random sample:

RandomSample[#, 6] & /@ KeyTake[aCryptoCurrenciesDataRaw, RandomChoice[Keys@aCryptoCurrenciesDataRaw]]

enter image description here

Here we add the crypto-currencies symbols and convert date strings into date objects.

AbsoluteTiming[
  aCryptoCurrenciesData = Association@KeyValueMap[Function[{k, v}, k -> v[All, Join[<|"Symbol" -> k, "DateObject" -> DateObject[#Date]|>, #] &]], aCryptoCurrenciesDataRaw]; 
 ]

(*{8.58146, Null}*)

Summary

In this section we compute the summary over all datasets:

ResourceFunction["RecordsSummary"][Join @@ Values[aCryptoCurrenciesData], "MaxTallies" -> 30]

enter image description here


Plots

Here we plot the “Low” and “High” price time series for each crypto-currency for the last 120 days:

nDays = 120;
Map[
  Block[{dsTemp = #[Select[AbsoluteTime[#DateObject] > AbsoluteTime[DatePlus[Now, -Quantity[nDays, "Days"]]] &]]}, 
    DateListPlot[{
      Normal[dsTemp[All, {"DateObject", "Low"}][Values]], 
      Normal[dsTemp[All, {"DateObject", "High"}][Values]]}, 
     PlotLegends -> {"Low", "High"}, 
     PlotRange -> All] 
   ] &, 
  aCryptoCurrenciesData 
 ]

enter image description here

Here we plot the volume time series for each crypto-currency for the last 120 days:

nDays = 120;
Map[
  Block[{dsTemp = #[Select[AbsoluteTime[#DateObject] > AbsoluteTime[DatePlus[Now, -Quantity[nDays, "Days"]]] &]]}, 
    DateListPlot[{
      Normal[dsTemp[All, {"DateObject", "Volume"}][Values]]}, 
     PlotLabel -> "Volume", 
     PlotRange -> All] 
   ] &, 
  aCryptoCurrenciesData 
 ]

enter image description here


References

[YF1] Yahoo! Finance, Cryptocurrencies.

$\endgroup$
1
2
$\begingroup$

Is there a CryptocurrencyData alternative to FinancialData?

I implemented a resource function that can be used to obtain cryptocurrency data using the approach explained in my previous post. See CryptocurrencyData:

enter image description here


BTW, FinancialData is aware of 10 cryptocurrencies, but that is not documented (as far as I can tell) and only prices are provided. (For more details see the discussion in CryptocurrencyData.)

Try

DateListPlot@FinancialData["BTC", "Jan 1 2021"]
DateListPlot@FinancialData["ETH", "Jan 1 2021"]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.