Computer Science/Concepts

REST API example - Coingecko API

metamong 2022. 3. 30.

๐Ÿ‘‹ ์ €๋ฒˆ ์‹œ๊ฐ„์— <tools for DS> section์—์„œ REST API์— ๋Œ€ํ•ด ๋ฐฐ์šด ์ ์ด ์žˆ๋‹ค..!

 

 

Tools for Data Science (from Coursera)

1. Data Scientist's Toolkit [1] Languages of Data Science # Python → by far the most popular programming language for data science → it uses clear, readable syntax. You can do many of the thi..

sh-avid-learner.tistory.com

 

 

๐Ÿ’ช ์š”์•ฝํ•˜์ž๋ฉด, API์ค‘ HTTP ๋ฐฉ์‹์œผ๋กœ client์™€ web service ๊ฐ„ ๋ฉ”์„ธ์ง€๋ฅผ ์ฃผ๊ณ  ๋ฐ›๋Š” ๋ฐฉ์‹์„ REST API๋ผ๊ณ  ํ•จ.

 

'* Rest API’s function by sending a request, the request is communicated via HTTP message. The HTTP message usually contains a JSON file. This contains instructions for what operation we would like the service or resource to perform. In a similar manner, API returns a response, via an HTTP message, this response is usually contained within a JSON. In cryptocurrency a popular method to display the movements of the price of a currency.'

 

'* we will be using the CoinGecko API to create one of these candlestick graphs for Bitcoin. We will use the API to get the price data for 30 days with 24 observation per day, 1 per hour. We will find the max, min, open, and close price per day meaning we will have 30 candlesticks and use that to generate the candlestick graph. Although we are using the CoinGecko API we will use a Python client/wrapper for the API called PyCoinGecko. PyCoinGecko will make performing the requests easy and it will deal with the endpoint targeting.

 

๐Ÿ’ช RESTAPI์˜ ์˜ˆ์‹œ ์ค‘ ํ•˜๋‚˜์ธ CoinGeckoAPI๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ„๋‹จํ•œ ์‹œ๊ฐํ™”๋ฅผ ํ•ด๋ณด๋ ค ํ•œ๋‹ค. ๐Ÿ’ช

 

1> ์ค€๋น„

 

!pip install pycoingecko
!pip install plotly
!pip install mplfinance

import pandas as pd
import numpy as np
import plotly.graph_objects as go
from plotly.offline import plot
import matplotlib.pyplot as plt
import datetime
from pycoingecko import CoinGeckoAPI #REST API example!
from mplfinance.original_flavor import candlestick2_ohlc

 

2> get_coin_market_chart_by_id(id, vs_currency, days) ์‚ฌ์šฉ

(* id is the name of the coin you want & vs_currency is the currency you want the price in & days is how many days back from today you want)

 

cg = CoinGeckoAPI()

bitcoin_data = cg.get_coin_market_chart_by_id(id='bitcoin', vs_currency='usd', days=30)

type(bitcoin_data ) #prints 'dict'

 

> return ๊ฒฐ๊ณผ → * in the form of a JSON which includes the price, market caps, and total volumes along with timestamps for each observation. We are focused on the prices so we will select that data. (๊ฐ€๊ฒฉ์— ์ดˆ์ )

 

bitcoin_price_data = bitcoin_data['prices']

bitcoin_price_data[0:5]

"""

[[1614305260769, 46542.2532873948],
 [1614308746201, 47671.0461272975],
 [1614312224381, 47629.322477694484],
 [1614315749395, 47074.58598303568],
 [1614319450318, 45823.394963459454]]
 
 """

 

3> pandas df๋กœ ๋ฐ”๊พธ๊ณ  datetimeํ˜•ํƒœ๋กœ ํ•œ column data type ๋ณ€๊ฒฝ - date๋ณ„ group by ์ดํ›„ min, max, open, close ์ค€๋น„

 

data = pd.DataFrame(bitcoin_price_data, columns=['TimeStamp', 'Price'])
data['Date'] = pd.to_datetime(data['TimeStamp'], unit='ms')

candlestick_data = data.groupby(data.Date.dt.date, as_index=False).agg({"Price": ['min', 'max', 'first', 'last']})

 

4> ์‹œ๊ฐํ™”

 

fig = go.Figure(data=[go.Candlestick(x=data['Date'],
                open=candlestick_data['Price']['first'], 
                high=candlestick_data['Price']['max'],
                low=candlestick_data['Price']['min'], 
                close=candlestick_data['Price']['last'])
                ])

fig.update_layout(xaxis_rangeslider_visible=True)

fig.show()

 


* ์ฝ”๋“œ & ๋‚ด์šฉ ์ถœ์ฒ˜) IBM Data Science course (from Coursera)

* ์ธ๋„ค์ผ ์ถœ์ฒ˜) https://www.coingecko.com/

'Computer Science > Concepts' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Tidy Data  (0) 2022.04.13
Tabular Data ๐Ÿ—„๏ธ  (0) 2022.03.26
Data Preprocessing  (0) 2022.03.25
FE - Feature Engineering  (0) 2022.03.22
EDA - Exploratory Data Analysis  (0) 2022.03.22

๋Œ“๊ธ€