Message Boards Message Boards

Obtain a Candlestickchart for BTC

Posted 4 years ago

Hello there, I am new to this platform. I am facing the following problem.

How to create a candlestick chart or trading chart for bitcoin? I think it does not assume BTC as a part of financial data. The usual code does not work :-

data = FinancialData["BTC", {{2015, 5, 1}, {2020, 2, 15}}];
CandlestickChart[data]

It returns the following statement -

CandlestickChart::ldata: Missing[NotAvailable] is not a valid dataset or list of datasets.
CandlestickChart::dtvals: Unable to automatically determine horizontal coordinates for the given data and DataRange.

Also can any data (not registered as financial data), be represented as candlestick chart data?

If anyone knows how to go about this problem, please do reply. Thanks.

POSTED BY: Sheeba Pandey
9 Replies
Posted 4 years ago

Hello there ! Thanks a lot for replying. The above code gives a clear view of how to use API and interact with it. I would like to know, can I use the above code with some other trading exchange site say, "xyz.com", that is, can I replace "coinmarketcap.com" with "xyz.com" and run the above code ? Or will the code have to modified a little bit depending on some other trading site ?

Can I replace "coinmarketcap.com" with "xyz.com" in the following lines :- https://pro-api.coinmarketcap.com/v1/cryptocurrency/map?symbol=BTC and https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?id=1

Or do the above lines have to be constructed differently with different trading sites ?

Thanks Regards

POSTED BY: Sheeba Pandey
Posted 4 years ago

That will not work. Each sites API is different. The endpoints are different, query parameters different, required headers different, authentication mechanism different..... You will have to consult the API documentation.

If you need to use the API for many different sites then it might be worth the effort to write generic code that can deal with many sites using a site specific configuration file / options.

POSTED BY: Rohit Namjoshi
Posted 4 years ago

Thanks for replying again. Webexecute method is good for obtaining data. But I would also like to know how to access data using API. Say from coinmarket.com, I want to access Ethereum's/Bitcoin's information using their website's API. How would the code look like ?

In general, in Wolfram Language can we interact with API's of other websites to obtain data ? If so, can you give a basic outline and some general steps of how to call other websites API and obtain data ?

Thanks

POSTED BY: Sheeba Pandey
Posted 4 years ago

API's differ, so you have to consult the documentation to use it correctly. I signed up for the free plan to get an API key https://coinmarketcap.com/api/. The free plan does not allow access to historical data so you will have to pay for that. Here is an example that gets the latest quote for BTC.

apiKey = "your API key"; (* Replace with your key *)
params = <|"Headers" -> {"X-CMC_PRO_API_KEY" -> apiKey, "Accept" -> "application/json"}|>;

The coinmarketcap.com API uses id's to identify symbols, so get the id for BTC

idBTC = URLExecute[
  HTTPRequest[
   "https://pro-api.coinmarketcap.com/v1/cryptocurrency/map?symbol=BTC", params]]

The id for BTC is 1

(*
{"status" -> {"timestamp" -> "2020-02-26T01:49:39.066Z", 
   "error_code" -> 0, "error_message" -> Null, "elapsed" -> 8, 
   "credit_count" -> 1, "notice" -> Null}, 
 "data" -> {{"id" -> 1, "name" -> "Bitcoin", "symbol" -> "BTC", 
    "slug" -> "bitcoin", "is_active" -> 1, "rank" -> 1, 
    "first_historical_data" -> "2013-04-28T18:47:21.000Z", 
    "last_historical_data" -> "2020-02-26T01:44:00.000Z", 
    "platform" -> Null}}
*)

Use that id to get the latest quote

quoteBTC = 
 URLExecute[
  HTTPRequest[
   "https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?id=1", params]]

(*
{"status" -> {"timestamp" -> "2020-02-26T01:50:08.193Z", 
   "error_code" -> 0, "error_message" -> Null, "elapsed" -> 19, 
   "credit_count" -> 1, "notice" -> Null}, 
 "data" -> {"1" -> {"id" -> 1, "name" -> "Bitcoin", "symbol" -> "BTC",
      "slug" -> "bitcoin", "num_market_pairs" -> 7786, 
     "date_added" -> "2013-04-28T00:00:00.000Z", 
     "tags" -> {"mineable"}, "max_supply" -> 21000000, 
     "circulating_supply" -> 18237562, "total_supply" -> 18237562, 
     "platform" -> Null, "cmc_rank" -> 1, 
     "last_updated" -> "2020-02-26T01:49:37.000Z", 
     "quote" -> {"USD" -> {"price" -> 9264.46, 
         "volume_24h" -> 4.27441*10^10, 
         "percent_change_1h" -> -0.762424, 
         "percent_change_24h" -> -3.76928, 
         "percent_change_7d" -> -8.34669, 
         "market_cap" -> 1.68961*10^11, 
         "last_updated" -> "2020-02-26T01:49:37.000Z"}}}}}
*)
POSTED BY: Rohit Namjoshi
Posted 4 years ago

Thanks for replying and showing the code for the problem. Very neat code. How can I import trading view data of Ethereum from : https://www.tradingview.com/chart/?symbol=NYSE%3AETH to wolfram notebook in the candlestick chart form as shown in the website ? Idea is to obtain/import the data in candlestick form as shown in the web page.

Do I require API for that? Yes/No. Or can I import the data in CSV format?

Can you share a code for the same? Also can you suggest some sources/resources from where I can learn to code in Wolfram language better? I like the way you have coded the above problem.

Thanks.

POSTED BY: Sheeba Pandey
Posted 4 years ago

Sheeba,

I don't think tradingview.com has an API. BTW, the URL in your post is for "Ethan Allen Interiors", not Etherium. You can use the code from my answer to get data for Etherium by using this URL https://coinmarketcap.com/currencies/ethereum/historical-data/?start=20130428&end=20200221.

Not sure what you mean by "in the candlestick chart form as shown in the website". Do you mean visual appearance? If you want to include volume data, change Map[Take[#, 5] &] to Map[Take[#, 6] &] and use TradingChart.

data // Take[#, 40] & // TradingChart

enter image description here

Take a look at this for an introduction to WL.

POSTED BY: Rohit Namjoshi
CandlestickChart[
 FinancialData["APPLE", "OHLC", {{2015, 5, 1}, {2020, 2, 15}}]]

BTC stocks pricing is not Available for the OHLC option.

POSTED BY: l van Veen
Posted 4 years ago

Can OHLC option be created for BTC? Can we request a developer for the same?

POSTED BY: Sheeba Pandey
Posted 4 years ago

Hi Sheeba,

You can get OHLC data from other sources and use it to generate the chart.

session = StartWebSession[];
WebExecute[session, 
  "OpenPage" -> 
   "https://coinmarketcap.com/currencies/bitcoin/historical-data/?start=20130428&end=20200220"];

table = WebExecute[session, 
   "LocateElements" -> {"CSSSelector", 
     "#__next > div > div.container.cmc-main-section > \
div.cmc-main-section__content > div.cmc-currencies.aiq2zi-0.eXmmQp > \
div.cmc-details-panel-tabs.col-xs-12.cuxb2c-0.gfuorn > div > \
ul.cmc-tabs__body > li.cmc-tab.cmc-tab--selected.sc-10kr9hg-0.ieBMYq \
> div > div > div.cmc-table.sc-1yv6u5n-0.jLXhLh > div:nth-child(3)"}] // First;

rawData = WebExecute[session, "ElementText" -> table] // StringSplit[#, "\n"] &;
DeleteObject[session];

data = rawData // Rest // Partition[#, 7] & //
        Map[Take[#, 5] &] //     
        Map[{DateObject@First@#, Rest@# // Map[StringReplace["," -> ""] /* ToExpression]} &];

ts = TimeSeries@data;

(* Select 2020 data *)
ts20 = TimeSeriesWindow[ts, {{2020, 1, 1}, {2020, 2, 20}}];
CandlestickChart@ts20

enter image description here

POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract