mirror of
https://github.com/norandom/trading_analysis.git
synced 2026-09-23 17:39:30 +00:00
219 KiB
219 KiB
Yahoo Finance Data DL¶
- Download historical data for analysis (raw)
Install deps¶
In [32]:
%pip install "yfinance[optional]"=="0.2.37"
Requirement already satisfied: yfinance==0.2.37 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from yfinance[optional]==0.2.37) (0.2.37) Requirement already satisfied: pandas>=1.3.0 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from yfinance==0.2.37->yfinance[optional]==0.2.37) (2.2.1) Requirement already satisfied: numpy>=1.16.5 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from yfinance==0.2.37->yfinance[optional]==0.2.37) (1.26.4) Requirement already satisfied: requests>=2.31 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from yfinance==0.2.37->yfinance[optional]==0.2.37) (2.31.0) Requirement already satisfied: multitasking>=0.0.7 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from yfinance==0.2.37->yfinance[optional]==0.2.37) (0.0.11) Requirement already satisfied: lxml>=4.9.1 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from yfinance==0.2.37->yfinance[optional]==0.2.37) (5.1.0) Requirement already satisfied: appdirs>=1.4.4 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from yfinance==0.2.37->yfinance[optional]==0.2.37) (1.4.4) Requirement already satisfied: pytz>=2022.5 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from yfinance==0.2.37->yfinance[optional]==0.2.37) (2024.1) Requirement already satisfied: frozendict>=2.3.4 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from yfinance==0.2.37->yfinance[optional]==0.2.37) (2.4.0) Requirement already satisfied: peewee>=3.16.2 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from yfinance==0.2.37->yfinance[optional]==0.2.37) (3.17.1) Requirement already satisfied: beautifulsoup4>=4.11.1 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from yfinance==0.2.37->yfinance[optional]==0.2.37) (4.12.3) Requirement already satisfied: html5lib>=1.1 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from yfinance==0.2.37->yfinance[optional]==0.2.37) (1.1) WARNING: yfinance 0.2.37 does not provide the extra 'optional' Requirement already satisfied: soupsieve>1.2 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from beautifulsoup4>=4.11.1->yfinance==0.2.37->yfinance[optional]==0.2.37) (2.5) Requirement already satisfied: six>=1.9 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from html5lib>=1.1->yfinance==0.2.37->yfinance[optional]==0.2.37) (1.16.0) Requirement already satisfied: webencodings in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from html5lib>=1.1->yfinance==0.2.37->yfinance[optional]==0.2.37) (0.5.1) Requirement already satisfied: python-dateutil>=2.8.2 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from pandas>=1.3.0->yfinance==0.2.37->yfinance[optional]==0.2.37) (2.9.0) Requirement already satisfied: tzdata>=2022.7 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from pandas>=1.3.0->yfinance==0.2.37->yfinance[optional]==0.2.37) (2024.1) Requirement already satisfied: charset-normalizer<4,>=2 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from requests>=2.31->yfinance==0.2.37->yfinance[optional]==0.2.37) (3.3.2) Requirement already satisfied: idna<4,>=2.5 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from requests>=2.31->yfinance==0.2.37->yfinance[optional]==0.2.37) (3.6) Requirement already satisfied: urllib3<3,>=1.21.1 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from requests>=2.31->yfinance==0.2.37->yfinance[optional]==0.2.37) (2.2.1) Requirement already satisfied: certifi>=2017.4.17 in /home/marius/miniconda3/envs/lang_chain/lib/python3.11/site-packages (from requests>=2.31->yfinance==0.2.37->yfinance[optional]==0.2.37) (2024.2.2) Note: you may need to restart the kernel to use updated packages.
DL¶
- HG=F - Copper
In [33]:
import yfinance as yf hg_f = yf.Ticker("HG=F")
In [34]:
# get all stock info hg_f.info
Out[34]:
{'maxAge': 86400,
'priceHint': 4,
'previousClose': 3.926,
'open': 3.9255,
'dayLow': 3.8815,
'dayHigh': 3.9465,
'regularMarketPreviousClose': 3.926,
'regularMarketOpen': 3.9255,
'regularMarketDayLow': 3.8815,
'regularMarketDayHigh': 3.9465,
'volume': 80624,
'regularMarketVolume': 80624,
'averageVolume': 1088,
'averageVolume10days': 4041,
'averageDailyVolume10Day': 4041,
'bid': 3.8905,
'ask': 3.8915,
'bidSize': 800,
'askSize': 1200,
'expireDate': 1716940800,
'openInterest': 126184,
'fiftyTwoWeekLow': 3.53,
'fiftyTwoWeekHigh': 4.1895,
'fiftyDayAverage': 3.81898,
'twoHundredDayAverage': 3.7641976,
'currency': 'USD',
'exchange': 'CMX',
'quoteType': 'FUTURE',
'symbol': 'HGK24.CMX',
'underlyingSymbol': 'HG=F',
'shortName': 'Copper May 24',
'firstTradeDateEpochUtc': 967608000,
'timeZoneFullName': 'America/New_York',
'timeZoneShortName': 'EST',
'uuid': '02846b86-92eb-33ab-a801-f9b43107619f',
'gmtOffSetMilliseconds': -18000000,
'trailingPegRatio': None}
In [43]:
import pandas as pd import numpy as np # get historical market data hist = hg_f.history(period="1y") # Replace 0 values with NaN in specific columns hist.replace({'Volume': {0: pd.NA}, 'Open': {0: pd.NA}}, inplace=True) hist.replace(0, np.nan, inplace=True) # Replace 0 with NaN # Drop rows with NaN values in these specific columns hist.dropna(subset=['Volume', 'Open'], inplace=True) hist
Out[43]:
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
| Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|
| Date | |||||||
| 2023-03-09 00:00:00-05:00 | 4.0465 | 4.0565 | 4.0300 | 4.0545 | 584 | NaN | NaN |
| 2023-03-10 00:00:00-05:00 | 4.0120 | 4.0675 | 3.9900 | 4.0455 | 591 | NaN | NaN |
| 2023-03-13 00:00:00-04:00 | 4.0230 | 4.0800 | 3.9460 | 4.0695 | 842 | NaN | NaN |
| 2023-03-14 00:00:00-04:00 | 4.0395 | 4.0620 | 4.0010 | 4.0105 | 923 | NaN | NaN |
| 2023-03-15 00:00:00-04:00 | 4.0210 | 4.0210 | 3.8565 | 3.8565 | 875 | NaN | NaN |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 2024-03-04 00:00:00-05:00 | 3.8450 | 3.8815 | 3.8435 | 3.8520 | 1136 | NaN | NaN |
| 2024-03-05 00:00:00-05:00 | 3.8465 | 3.8630 | 3.8380 | 3.8440 | 929 | NaN | NaN |
| 2024-03-06 00:00:00-05:00 | 3.8400 | 3.8915 | 3.8375 | 3.8705 | 987 | NaN | NaN |
| 2024-03-07 00:00:00-05:00 | 3.8740 | 3.9310 | 3.8740 | 3.9195 | 1243 | NaN | NaN |
| 2024-03-08 00:00:00-05:00 | 3.9175 | 3.9320 | 3.8780 | 3.8850 | 1243 | NaN | NaN |
252 rows × 7 columns
In [45]:
import matplotlib.pyplot as plt # Assuming 'data' is your DataFrame plt.figure(figsize=(12, 5)) # Plotting Open plt.subplot(1, 2, 2) hist['Open'].hist(bins=20) plt.title('Open Price Histogram') plt.xlabel('Open Price') plt.ylabel('Frequency') plt.tight_layout() plt.show()
In [48]:
import pandas as pd import numpy as np import matplotlib.pyplot as plt # Plotting the 'Open' price as a line plot plt.figure(figsize=(10, 6)) hist['Open'].plot(kind='line', title='Open Price Over Time', xlabel='Date', ylabel='Open Price') plt.grid(True) plt.show()
In [50]:
import pandas as pd import numpy as np import matplotlib.pyplot as plt # Assuming 'hist' is your DataFrame with 'Date' set as the index and non-zero 'Open' values # Calculate daily change in 'Open' prices hist['OpenChange'] = hist['Open'].diff() # Find points where the change in 'Open' price shifts direction # This is a simple way to spot potential trend changes trend_changes = hist['OpenChange'] * hist['OpenChange'].shift(-1) < 0 # Plotting the 'Open' price as a line plot plt.figure(figsize=(10, 6)) hist['Open'].plot(kind='line', title='Open Price Over Time', xlabel='Date', ylabel='Open Price') plt.grid(True) # Marking trend changes for date, change in trend_changes.items(): if change: plt.plot(date, hist.loc[date, 'Open'], 'ro') # Mark with red circle plt.show()
In [30]:
# show meta information about the history (requires history() to be called first) hg_f.history_metadata
Out[30]:
{'currency': 'USD',
'symbol': 'HG=F',
'exchangeName': 'CMX',
'instrumentType': 'FUTURE',
'firstTradeDate': 967608000,
'regularMarketTime': 1709935198,
'hasPrePostMarketData': False,
'gmtoffset': -18000,
'timezone': 'EST',
'exchangeTimezoneName': 'America/New_York',
'regularMarketPrice': 3.891,
'chartPreviousClose': 4.042,
'priceHint': 4,
'currentTradingPeriod': {'pre': {'timezone': 'EST',
'start': 1709874000,
'end': 1709874000,
'gmtoffset': -18000},
'regular': {'timezone': 'EST',
'start': 1709874000,
'end': 1709960340,
'gmtoffset': -18000},
'post': {'timezone': 'EST',
'start': 1709960340,
'end': 1709960340,
'gmtoffset': -18000}},
'dataGranularity': '1d',
'range': '1y',
'validRanges': ['1d',
'5d',
'1mo',
'3mo',
'6mo',
'1y',
'2y',
'5y',
'10y',
'ytd',
'max']}
In [16]:
# show actions (dividends, splits, capital gains) hg_f.actions hg_f.dividends hg_f.splits hg_f.capital_gains # only for mutual funds & etfs
Out[16]:
Series([], dtype: object)
In [18]:
# show share count hg_f.get_shares_full(start="2022-01-01", end=None)
Out[18]:
yfinance.Ticker object <HG=F>
In [20]:
hg_f.income_stmt hg_f.quarterly_income_stmt
Out[20]:
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
In [21]:
# - balance sheet hg_f.balance_sheet hg_f.quarterly_balance_sheet
Out[21]:
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
In [22]:
# - cash flow statement hg_f.cashflow hg_f.quarterly_cashflow # see `Ticker.get_income_stmt()` for more options
Out[22]:
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
In [23]:
# show holders hg_f.major_holders hg_f.institutional_holders hg_f.mutualfund_holders hg_f.insider_transactions hg_f.insider_purchases hg_f.insider_roster_holders
404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/?modules=institutionOwnership%2CfundOwnership%2CmajorDirectHolders%2CmajorHoldersBreakdown%2CinsiderTransactions%2CinsiderHolders%2CnetSharePurchaseActivity&corsDomain=finance.yahoo.com&symbol=HG%3DF&formatted=false&crumb=kY9MYXZrrjV
Out[23]:
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
In [24]:
# show recommendations hg_f.recommendations hg_f.recommendations_summary hg_f.upgrades_downgrades
404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/HG=F?modules=recommendationTrend&corsDomain=finance.yahoo.com&formatted=false&symbol=HG%3DF&crumb=kY9MYXZrrjV 404 Client Error: Not Found for url: https://query2.finance.yahoo.com/v10/finance/quoteSummary/HG=F?modules=upgradeDowngradeHistory&corsDomain=finance.yahoo.com&formatted=false&symbol=HG%3DF&crumb=kY9MYXZrrjV
Out[24]:
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
In [25]:
# Show future and historic earnings dates, returns at most next 4 quarters and last 8 quarters by default. # Note: If more are needed use msft.get_earnings_dates(limit=XX) with increased limit argument. hg_f.earnings_dates
HG=F: No earnings dates found, symbol may be delisted
In [26]:
# show ISIN code - *experimental* # ISIN = International Securities Identification Number hg_f.isin
Out[26]:
'-'
In [27]:
# show options expirations hg_f.options
Out[27]:
()
In [28]:
# show news hg_f.news
Out[28]:
[{'uuid': 'a081de49-3834-35d7-9b1a-49ae61a24622',
'title': 'EMERGING MARKETS-Latam stocks advance, weak copper prices drag currencies of Chile, Peru',
'publisher': 'Reuters',
'link': 'https://finance.yahoo.com/news/emerging-markets-latam-stocks-advance-153434824.html',
'providerPublishTime': 1706283274,
'type': 'STORY',
'relatedTickers': ['HG=F', '^BVSP', '^MXX']},
{'uuid': '08c6cfc7-1146-374c-bf8f-8f02328cc4b1',
'title': 'CANADA STOCKS-TSX futures edge up on higher gold prices; US data in focus',
'publisher': 'Reuters',
'link': 'https://finance.yahoo.com/news/canada-stocks-tsx-futures-edge-123846632.html',
'providerPublishTime': 1706272726,
'type': 'STORY',
'relatedTickers': ['GC=F', 'ZG=F', 'HG=F', '^GSPC']},
{'uuid': '1666725e-a880-3220-be81-4b39be02d706',
'title': 'These Are the 4 Best Silver Coin Types To Buy in Order To Turn a Profit',
'publisher': 'GOBankingRates',
'link': 'https://finance.yahoo.com/news/4-best-silver-coin-types-123010528.html',
'providerPublishTime': 1706272210,
'type': 'STORY',
'thumbnail': {'resolutions': [{'url': 'https://s.yimg.com/uu/api/res/1.2/BhiemWjsPOiPzWVgmzWacQ--~B/aD0xMDgwO3c9MTkyMDthcHBpZD15dGFjaHlvbg--/https://media.zenfs.com/en/gobankingrates_644/397831e32274b07c29579b8e42a54267',
'width': 1920,
'height': 1080,
'tag': 'original'},
{'url': 'https://s.yimg.com/uu/api/res/1.2/__z5WbG1O.bg53xqLE74Dg--~B/Zmk9ZmlsbDtoPTE0MDtweW9mZj0wO3c9MTQwO2FwcGlkPXl0YWNoeW9u/https://media.zenfs.com/en/gobankingrates_644/397831e32274b07c29579b8e42a54267',
'width': 140,
'height': 140,
'tag': '140x140'}]},
'relatedTickers': ['GC=F', 'ZG=F', 'HG=F', 'SI=F', 'ZI=F']},
{'uuid': '0edc4734-fb05-3bb1-a95b-9e11c776fdc5',
'title': 'Gold prices struggle before inflation, Fed cues; copper set for strong week',
'publisher': 'Investing.com',
'link': 'https://finance.yahoo.com/news/gold-prices-struggle-inflation-fed-001606596.html',
'providerPublishTime': 1706228166,
'type': 'STORY',
'relatedTickers': ['HG=F', 'GC=F', 'ZG=F']},
{'uuid': '7b91db52-a3f1-335d-9176-6d61187cf0a7',
'title': 'Basic Materials Roundup: Market Talk',
'publisher': 'The Wall Street Journal',
'link': 'https://finance.yahoo.com/m/7b91db52-a3f1-335d-9176-6d61187cf0a7/basic-materials-roundup%3A.html',
'providerPublishTime': 1706219880,
'type': 'STORY',
'thumbnail': {'resolutions': [{'url': 'https://s.yimg.com/uu/api/res/1.2/hus7PFWagBBSuGWsuAHBxA--~B/aD02NDA7dz0xMjgwO2FwcGlkPXl0YWNoeW9u/https://media.zenfs.com/en/wsj.com/7e9c00d68c1ab82df50577310bb19679',
'width': 1280,
'height': 640,
'tag': 'original'},
{'url': 'https://s.yimg.com/uu/api/res/1.2/2AS8Z8b8P15BKyqk61NEWQ--~B/Zmk9ZmlsbDtoPTE0MDtweW9mZj0wO3c9MTQwO2FwcGlkPXl0YWNoeW9u/https://media.zenfs.com/en/wsj.com/7e9c00d68c1ab82df50577310bb19679',
'width': 140,
'height': 140,
'tag': '140x140'}]},
'relatedTickers': ['GVDBF', 'GVDNY', 'GC=F', 'ZG=F', 'HG=F']},
{'uuid': '2ce9d404-879c-3b5c-85c2-652c92639df1',
'title': 'Closing prices for crude oil, gold and other commodities',
'publisher': 'Associated Press Finance',
'link': 'https://finance.yahoo.com/news/closing-prices-crude-oil-gold-201521282.html',
'providerPublishTime': 1706213721,
'type': 'STORY',
'relatedTickers': ['CL=F',
'SI=F',
'ZI=F',
'NG=F',
'GC=F',
'ZG=F',
'HG=F',
'HO=F']},
{'uuid': 'e0748b77-83b7-34c3-b83e-f55508291594',
'title': 'Crypto for Advisors: Unlocking Crypto Custody',
'publisher': 'CoinDesk',
'link': 'https://finance.yahoo.com/m/e0748b77-83b7-34c3-b83e-f55508291594/crypto-for-advisors%3A.html',
'providerPublishTime': 1706199734,
'type': 'STORY',
'thumbnail': {'resolutions': [{'url': 'https://s.yimg.com/uu/api/res/1.2/YvjxRlrTdjUCMJlc1MT4tg--~B/aD0zMDAwO3c9NDUwMDthcHBpZD15dGFjaHlvbg--/https://media.zenfs.com/en/coindesk_75/c7a00a6fe2bc93bfbbac3e92d2fac0e6',
'width': 4500,
'height': 3000,
'tag': 'original'},
{'url': 'https://s.yimg.com/uu/api/res/1.2/q.NOd5AdTb2Y1wpOwX9L.w--~B/Zmk9ZmlsbDtoPTE0MDtweW9mZj0wO3c9MTQwO2FwcGlkPXl0YWNoeW9u/https://media.zenfs.com/en/coindesk_75/c7a00a6fe2bc93bfbbac3e92d2fac0e6',
'width': 140,
'height': 140,
'tag': '140x140'}]},
'relatedTickers': ['HG=F']},
{'uuid': '922264e0-c1d2-3af4-9d38-c318b8a7b523',
'title': "Blinken hails progress in construction of Angola's Lobito rail corridor",
'publisher': 'Reuters',
'link': 'https://finance.yahoo.com/news/blinken-hails-progress-construction-angolas-155740812.html',
'providerPublishTime': 1706198260,
'type': 'STORY',
'relatedTickers': ['HG=F']}]
In [29]:
# get option chain for specific expiration opt = hg_f.option_chain('2024-06-21') # data available via: opt.calls, opt.puts
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[29], line 2 1 # get option chain for specific expiration ----> 2 opt = hg_f.option_chain('2024-06-21') 3 # data available via: opt.calls, opt.puts File ~/miniconda3/envs/lang_chain/lib/python3.11/site-packages/yfinance/ticker.py:91, in Ticker.option_chain(self, date, tz) 89 self._download_options() 90 if date not in self._expirations: ---> 91 raise ValueError( 92 f"Expiration `{date}` cannot be found. " 93 f"Available expirations are: [{', '.join(self._expirations)}]") 94 date = self._expirations[date] 95 options = self._download_options(date) ValueError: Expiration `2024-06-21` cannot be found. Available expirations are: []
In [ ]: