728x90
In [41]:
import matplotlib.pyplot as plt
import pandas as pd
import datetime
In [43]:
# download market data from Yahoo finance
# !pip install yfinance
import yfinance as yf
In [44]:
# Ticker Symbols
investments_list = ['BTC-USD']
# Specify the start and end dates
start = datetime.datetime(2010,1,1)
end = datetime.datetime(2021, 5, 16)
# Download the data from Yahoo Finance, make sure to reset index!
df = yf.download(investments_list, start = start, end = end)
df
[*********************100%***********************] 1 of 1 completed
Out[44]:
Open | High | Low | Close | Adj Close | Volume | |
---|---|---|---|---|---|---|
Date | ||||||
2014-09-17 00:00:00+00:00 | 465.864014 | 468.174011 | 452.421997 | 457.334015 | 457.334015 | 21056800 |
2014-09-18 00:00:00+00:00 | 456.859985 | 456.859985 | 413.104004 | 424.440002 | 424.440002 | 34483200 |
2014-09-19 00:00:00+00:00 | 424.102997 | 427.834991 | 384.532013 | 394.795990 | 394.795990 | 37919700 |
2014-09-20 00:00:00+00:00 | 394.673004 | 423.295990 | 389.882996 | 408.903992 | 408.903992 | 36863600 |
2014-09-21 00:00:00+00:00 | 408.084991 | 412.425995 | 393.181000 | 398.821014 | 398.821014 | 26580100 |
... | ... | ... | ... | ... | ... | ... |
2021-05-11 00:00:00+00:00 | 55847.242188 | 56872.542969 | 54608.652344 | 56704.574219 | 56704.574219 | 61308396325 |
2021-05-12 00:00:00+00:00 | 56714.531250 | 57939.363281 | 49150.535156 | 49150.535156 | 49150.535156 | 75215403907 |
2021-05-13 00:00:00+00:00 | 49735.433594 | 51330.843750 | 46980.019531 | 49716.191406 | 49716.191406 | 96721152926 |
2021-05-14 00:00:00+00:00 | 49682.980469 | 51438.117188 | 48868.578125 | 49880.535156 | 49880.535156 | 55737497453 |
2021-05-15 00:00:00+00:00 | 49855.496094 | 50639.664062 | 46664.140625 | 46760.187500 | 46760.187500 | 59161047474 |
2433 rows × 6 columns
In [46]:
# !pip install yahoofinancials
from yahoofinancials import YahooFinancials
In [47]:
yahoo_financials = YahooFinancials('AAPL')
data = yahoo_financials.get_historical_price_data(start_date='2019-01-01',
end_date='2019-12-31',
time_interval='weekly')
aapl_df = pd.DataFrame(data['AAPL']['prices'])
aapl_df = aapl_df.drop('date', axis=1).set_index('formatted_date')
aapl_df.head()
Out[47]:
high | low | open | close | volume | adjclose | |
---|---|---|---|---|---|---|
formatted_date | ||||||
2019-01-01 | 39.712502 | 35.500000 | 38.722500 | 36.982498 | 966947200 | 35.694611 |
2019-01-08 | 38.632500 | 37.130001 | 37.389999 | 37.500000 | 725470000 | 36.194096 |
2019-01-15 | 39.470001 | 37.512501 | 37.567501 | 39.205002 | 491411200 | 37.839706 |
2019-01-22 | 39.532501 | 37.924999 | 39.102501 | 39.075001 | 554774800 | 37.714245 |
2019-01-29 | 42.915001 | 38.527500 | 39.062500 | 42.812500 | 830400800 | 41.321583 |
In [48]:
# reset index
df.reset_index(inplace = True)
df
Out[48]:
Date | Open | High | Low | Close | Adj Close | Volume | |
---|---|---|---|---|---|---|---|
0 | 2014-09-17 00:00:00+00:00 | 465.864014 | 468.174011 | 452.421997 | 457.334015 | 457.334015 | 21056800 |
1 | 2014-09-18 00:00:00+00:00 | 456.859985 | 456.859985 | 413.104004 | 424.440002 | 424.440002 | 34483200 |
2 | 2014-09-19 00:00:00+00:00 | 424.102997 | 427.834991 | 384.532013 | 394.795990 | 394.795990 | 37919700 |
3 | 2014-09-20 00:00:00+00:00 | 394.673004 | 423.295990 | 389.882996 | 408.903992 | 408.903992 | 36863600 |
4 | 2014-09-21 00:00:00+00:00 | 408.084991 | 412.425995 | 393.181000 | 398.821014 | 398.821014 | 26580100 |
... | ... | ... | ... | ... | ... | ... | ... |
2428 | 2021-05-11 00:00:00+00:00 | 55847.242188 | 56872.542969 | 54608.652344 | 56704.574219 | 56704.574219 | 61308396325 |
2429 | 2021-05-12 00:00:00+00:00 | 56714.531250 | 57939.363281 | 49150.535156 | 49150.535156 | 49150.535156 | 75215403907 |
2430 | 2021-05-13 00:00:00+00:00 | 49735.433594 | 51330.843750 | 46980.019531 | 49716.191406 | 49716.191406 | 96721152926 |
2431 | 2021-05-14 00:00:00+00:00 | 49682.980469 | 51438.117188 | 48868.578125 | 49880.535156 | 49880.535156 | 55737497453 |
2432 | 2021-05-15 00:00:00+00:00 | 49855.496094 | 50639.664062 | 46664.140625 | 46760.187500 | 46760.187500 | 59161047474 |
2433 rows × 7 columns
728x90
'Data Analytics with python > [Data Analysis]' 카테고리의 다른 글
[matplotlib]S5_04_Sub_plots (0) | 2023.01.21 |
---|---|
[matplotlib]S5_03_Multiple_plots (0) | 2023.01.21 |
[matplotlib]S5_01_single_line_plot (0) | 2023.01.21 |
[Pandas][DataFrame][MultiIndex]S4_05_Multi_indexing_operations2 (0) | 2023.01.21 |
[Pandas][DataFrame][MultiIndex]S4_04_Multi_indexing_operations1 (0) | 2023.01.21 |
댓글