Created
March 5, 2022 18:27
-
-
Save stephenmm/8e7249b8ce580956c43e895f4b255db8 to your computer and use it in GitHub Desktop.
StockFundamentalAnalysis.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "StockFundamentalAnalysis.ipynb", | |
"provenance": [], | |
"authorship_tag": "ABX9TyOKJeLmeE68SF4M3UrDIeQr", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/stephenmm/8e7249b8ce580956c43e895f4b255db8/stockfundamentalanalysis.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"Just a playground to help me understand and analyze stocks." | |
], | |
"metadata": { | |
"id": "1cen60EkScD1" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "mE5-9bmhSaNj", | |
"outputId": "4b27894c-9892-4c91-8fb5-780b991a25db" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
" 10.000e-006\n", | |
" -10.000e-006\n", | |
" 123.456e+003\n", | |
"-100.000e-003\n" | |
] | |
} | |
], | |
"source": [ | |
"# Display numbers in engineering notation\n", | |
"from math import log10\n", | |
"def eng_str(x, significance=8, precision=3):\n", | |
" y = abs(x)\n", | |
" exponent = int(log10(y))\n", | |
" engr_exponent = exponent - exponent%3\n", | |
" z = y/10**engr_exponent\n", | |
" if x < 0:\n", | |
" z=z*-1\n", | |
" if engr_exponent > 0:\n", | |
" exp_str=f\"+{engr_exponent:03}\"\n", | |
" else:\n", | |
" exp_str=f\"{engr_exponent:04}\"\n", | |
" return f\"{z:{significance}.{precision}f}\"+f\"e{exp_str}\"\n", | |
"\n", | |
"print(eng_str(.00001))\n", | |
"print(eng_str(-.00001))\n", | |
"print(eng_str(123456.2342))\n", | |
"print(eng_str(-.1))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"pip install yfinance" | |
], | |
"metadata": { | |
"id": "nm_dWNWYTWvH" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import yfinance as yf\n", | |
"data = yf.download(\"SPY AAPL\", start=\"2017-01-01\", end=\"2017-04-30\", group_by=\"ticker\")\n" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "vdk0L94TTJKd", | |
"outputId": "5c96bc27-09ce-4c37-faf9-618b6e826393" | |
}, | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"[*********************100%***********************] 2 of 2 completed\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import pandas as pd\n", | |
"microsoft = yf.Ticker('MSFT')\n", | |
"dict = microsoft.info\n", | |
"df = pd.DataFrame.from_dict(dict,orient='index')\n", | |
"df = df.reset_index()\n", | |
"df" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 423 | |
}, | |
"id": "mrsU-yuNTocT", | |
"outputId": "bd8250ad-394a-4804-c842-a1aa380f87ef" | |
}, | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/html": [ | |
"\n", | |
" <div id=\"df-154ab8d5-f4c3-4bfc-8b58-02bd1423070d\">\n", | |
" <div class=\"colab-df-container\">\n", | |
" <div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>index</th>\n", | |
" <th>0</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>zip</td>\n", | |
" <td>98052-6399</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>sector</td>\n", | |
" <td>Technology</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>fullTimeEmployees</td>\n", | |
" <td>181000</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>longBusinessSummary</td>\n", | |
" <td>Microsoft Corporation develops, licenses, and ...</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>city</td>\n", | |
" <td>Redmond</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>...</th>\n", | |
" <td>...</td>\n", | |
" <td>...</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>148</th>\n", | |
" <td>dayHigh</td>\n", | |
" <td>295.64</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>149</th>\n", | |
" <td>regularMarketPrice</td>\n", | |
" <td>289.86</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>150</th>\n", | |
" <td>preMarketPrice</td>\n", | |
" <td>None</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>151</th>\n", | |
" <td>logo_url</td>\n", | |
" <td>https://logo.clearbit.com/microsoft.com</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>152</th>\n", | |
" <td>trailingPegRatio</td>\n", | |
" <td>2.4159</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"<p>153 rows × 2 columns</p>\n", | |
"</div>\n", | |
" <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-154ab8d5-f4c3-4bfc-8b58-02bd1423070d')\"\n", | |
" title=\"Convert this dataframe to an interactive table.\"\n", | |
" style=\"display:none;\">\n", | |
" \n", | |
" <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n", | |
" width=\"24px\">\n", | |
" <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n", | |
" <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n", | |
" </svg>\n", | |
" </button>\n", | |
" \n", | |
" <style>\n", | |
" .colab-df-container {\n", | |
" display:flex;\n", | |
" flex-wrap:wrap;\n", | |
" gap: 12px;\n", | |
" }\n", | |
"\n", | |
" .colab-df-convert {\n", | |
" background-color: #E8F0FE;\n", | |
" border: none;\n", | |
" border-radius: 50%;\n", | |
" cursor: pointer;\n", | |
" display: none;\n", | |
" fill: #1967D2;\n", | |
" height: 32px;\n", | |
" padding: 0 0 0 0;\n", | |
" width: 32px;\n", | |
" }\n", | |
"\n", | |
" .colab-df-convert:hover {\n", | |
" background-color: #E2EBFA;\n", | |
" box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n", | |
" fill: #174EA6;\n", | |
" }\n", | |
"\n", | |
" [theme=dark] .colab-df-convert {\n", | |
" background-color: #3B4455;\n", | |
" fill: #D2E3FC;\n", | |
" }\n", | |
"\n", | |
" [theme=dark] .colab-df-convert:hover {\n", | |
" background-color: #434B5C;\n", | |
" box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n", | |
" filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n", | |
" fill: #FFFFFF;\n", | |
" }\n", | |
" </style>\n", | |
"\n", | |
" <script>\n", | |
" const buttonEl =\n", | |
" document.querySelector('#df-154ab8d5-f4c3-4bfc-8b58-02bd1423070d button.colab-df-convert');\n", | |
" buttonEl.style.display =\n", | |
" google.colab.kernel.accessAllowed ? 'block' : 'none';\n", | |
"\n", | |
" async function convertToInteractive(key) {\n", | |
" const element = document.querySelector('#df-154ab8d5-f4c3-4bfc-8b58-02bd1423070d');\n", | |
" const dataTable =\n", | |
" await google.colab.kernel.invokeFunction('convertToInteractive',\n", | |
" [key], {});\n", | |
" if (!dataTable) return;\n", | |
"\n", | |
" const docLinkHtml = 'Like what you see? Visit the ' +\n", | |
" '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n", | |
" + ' to learn more about interactive tables.';\n", | |
" element.innerHTML = '';\n", | |
" dataTable['output_type'] = 'display_data';\n", | |
" await google.colab.output.renderOutput(dataTable, element);\n", | |
" const docLink = document.createElement('div');\n", | |
" docLink.innerHTML = docLinkHtml;\n", | |
" element.appendChild(docLink);\n", | |
" }\n", | |
" </script>\n", | |
" </div>\n", | |
" </div>\n", | |
" " | |
], | |
"text/plain": [ | |
" index 0\n", | |
"0 zip 98052-6399\n", | |
"1 sector Technology\n", | |
"2 fullTimeEmployees 181000\n", | |
"3 longBusinessSummary Microsoft Corporation develops, licenses, and ...\n", | |
"4 city Redmond\n", | |
".. ... ...\n", | |
"148 dayHigh 295.64\n", | |
"149 regularMarketPrice 289.86\n", | |
"150 preMarketPrice None\n", | |
"151 logo_url https://logo.clearbit.com/microsoft.com\n", | |
"152 trailingPegRatio 2.4159\n", | |
"\n", | |
"[153 rows x 2 columns]" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 5 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"dict['zip']\n", | |
"list(dict.keys())\n" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "aP9u3mOZUHPT", | |
"outputId": "96242c1c-74dd-4e5a-d033-692c44e12a3d" | |
}, | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"['zip',\n", | |
" 'sector',\n", | |
" 'fullTimeEmployees',\n", | |
" 'longBusinessSummary',\n", | |
" 'city',\n", | |
" 'phone',\n", | |
" 'state',\n", | |
" 'country',\n", | |
" 'companyOfficers',\n", | |
" 'website',\n", | |
" 'maxAge',\n", | |
" 'address1',\n", | |
" 'industry',\n", | |
" 'ebitdaMargins',\n", | |
" 'profitMargins',\n", | |
" 'grossMargins',\n", | |
" 'operatingCashflow',\n", | |
" 'revenueGrowth',\n", | |
" 'operatingMargins',\n", | |
" 'ebitda',\n", | |
" 'targetLowPrice',\n", | |
" 'recommendationKey',\n", | |
" 'grossProfits',\n", | |
" 'freeCashflow',\n", | |
" 'targetMedianPrice',\n", | |
" 'currentPrice',\n", | |
" 'earningsGrowth',\n", | |
" 'currentRatio',\n", | |
" 'returnOnAssets',\n", | |
" 'numberOfAnalystOpinions',\n", | |
" 'targetMeanPrice',\n", | |
" 'debtToEquity',\n", | |
" 'returnOnEquity',\n", | |
" 'targetHighPrice',\n", | |
" 'totalCash',\n", | |
" 'totalDebt',\n", | |
" 'totalRevenue',\n", | |
" 'totalCashPerShare',\n", | |
" 'financialCurrency',\n", | |
" 'revenuePerShare',\n", | |
" 'quickRatio',\n", | |
" 'recommendationMean',\n", | |
" 'exchange',\n", | |
" 'shortName',\n", | |
" 'longName',\n", | |
" 'exchangeTimezoneName',\n", | |
" 'exchangeTimezoneShortName',\n", | |
" 'isEsgPopulated',\n", | |
" 'gmtOffSetMilliseconds',\n", | |
" 'quoteType',\n", | |
" 'symbol',\n", | |
" 'messageBoardId',\n", | |
" 'market',\n", | |
" 'annualHoldingsTurnover',\n", | |
" 'enterpriseToRevenue',\n", | |
" 'beta3Year',\n", | |
" 'enterpriseToEbitda',\n", | |
" '52WeekChange',\n", | |
" 'morningStarRiskRating',\n", | |
" 'forwardEps',\n", | |
" 'revenueQuarterlyGrowth',\n", | |
" 'sharesOutstanding',\n", | |
" 'fundInceptionDate',\n", | |
" 'annualReportExpenseRatio',\n", | |
" 'totalAssets',\n", | |
" 'bookValue',\n", | |
" 'sharesShort',\n", | |
" 'sharesPercentSharesOut',\n", | |
" 'fundFamily',\n", | |
" 'lastFiscalYearEnd',\n", | |
" 'heldPercentInstitutions',\n", | |
" 'netIncomeToCommon',\n", | |
" 'trailingEps',\n", | |
" 'lastDividendValue',\n", | |
" 'SandP52WeekChange',\n", | |
" 'priceToBook',\n", | |
" 'heldPercentInsiders',\n", | |
" 'nextFiscalYearEnd',\n", | |
" 'yield',\n", | |
" 'mostRecentQuarter',\n", | |
" 'shortRatio',\n", | |
" 'sharesShortPreviousMonthDate',\n", | |
" 'floatShares',\n", | |
" 'beta',\n", | |
" 'enterpriseValue',\n", | |
" 'priceHint',\n", | |
" 'threeYearAverageReturn',\n", | |
" 'lastSplitDate',\n", | |
" 'lastSplitFactor',\n", | |
" 'legalType',\n", | |
" 'lastDividendDate',\n", | |
" 'morningStarOverallRating',\n", | |
" 'earningsQuarterlyGrowth',\n", | |
" 'priceToSalesTrailing12Months',\n", | |
" 'dateShortInterest',\n", | |
" 'pegRatio',\n", | |
" 'ytdReturn',\n", | |
" 'forwardPE',\n", | |
" 'lastCapGain',\n", | |
" 'shortPercentOfFloat',\n", | |
" 'sharesShortPriorMonth',\n", | |
" 'impliedSharesOutstanding',\n", | |
" 'category',\n", | |
" 'fiveYearAverageReturn',\n", | |
" 'previousClose',\n", | |
" 'regularMarketOpen',\n", | |
" 'twoHundredDayAverage',\n", | |
" 'trailingAnnualDividendYield',\n", | |
" 'payoutRatio',\n", | |
" 'volume24Hr',\n", | |
" 'regularMarketDayHigh',\n", | |
" 'navPrice',\n", | |
" 'averageDailyVolume10Day',\n", | |
" 'regularMarketPreviousClose',\n", | |
" 'fiftyDayAverage',\n", | |
" 'trailingAnnualDividendRate',\n", | |
" 'open',\n", | |
" 'toCurrency',\n", | |
" 'averageVolume10days',\n", | |
" 'expireDate',\n", | |
" 'algorithm',\n", | |
" 'dividendRate',\n", | |
" 'exDividendDate',\n", | |
" 'circulatingSupply',\n", | |
" 'startDate',\n", | |
" 'regularMarketDayLow',\n", | |
" 'currency',\n", | |
" 'trailingPE',\n", | |
" 'regularMarketVolume',\n", | |
" 'lastMarket',\n", | |
" 'maxSupply',\n", | |
" 'openInterest',\n", | |
" 'marketCap',\n", | |
" 'volumeAllCurrencies',\n", | |
" 'strikePrice',\n", | |
" 'averageVolume',\n", | |
" 'dayLow',\n", | |
" 'ask',\n", | |
" 'askSize',\n", | |
" 'volume',\n", | |
" 'fiftyTwoWeekHigh',\n", | |
" 'fromCurrency',\n", | |
" 'fiveYearAvgDividendYield',\n", | |
" 'fiftyTwoWeekLow',\n", | |
" 'bid',\n", | |
" 'tradeable',\n", | |
" 'dividendYield',\n", | |
" 'bidSize',\n", | |
" 'dayHigh',\n", | |
" 'regularMarketPrice',\n", | |
" 'preMarketPrice',\n", | |
" 'logo_url',\n", | |
" 'trailingPegRatio']" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 6 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"stks = ['crus','aapl']\n", | |
"prms = ['totalRevenue', 'trailingPE', 'sharesOutstanding']\n", | |
"for stk in stks:\n", | |
" info = yf.Ticker(stk).info\n", | |
" print(\"\\n%s rev/shr: %f \" % (stk, info['totalRevenue']/info['sharesOutstanding']) )\n", | |
" for prm in prms:\n", | |
" print(\" %s: %s |\" % (prm, eng_str(info[prm])), end='')\n", | |
" " | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "WYBWua9uUNil", | |
"outputId": "ed1a6103-fd73-4b75-9ead-64bc0392ab9d" | |
}, | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"\n", | |
"crus rev/shr: 27.670270 \n", | |
" totalRevenue: 1.585e+009 | trailingPE: 19.797e0000 | sharesOutstanding: 57.283e+006 |\n", | |
"aapl rev/shr: 23.182409 \n", | |
" totalRevenue: 378.323e+009 | trailingPE: 27.127e0000 | sharesOutstanding: 16.319e+009 |" | |
] | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment