-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitcoin.py
34 lines (24 loc) · 1.08 KB
/
bitcoin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
#from mplfinance import candlestick2_ohlcs
cg = CoinGeckoAPI()
bitcoin_data = cg.get_coin_market_chart_by_id(id = 'bitcoin', vs_currency= 'usd', days= 30)
bitcoin_price_data = bitcoin_data['prices']
bitcoin_price_data[0:5]
data = pd.DataFrame(bitcoin_price_data, columns=['TimeStamp', 'Price'])
data['date'] = data['TimeStamp'].apply(lambda d: datetime.date.fromtimestamp(d/1000.0))
#.agg is used to apply function in series
candlestick_data = data.groupby(data.date, as_index=False).agg({"Price": ['min', 'max', 'first', 'last']})
fig = go.Figure(data=[go.Candlestick(x=candlestick_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()