Skip to content

Commit ca2184a

Browse files
committed
modified value strategy to match video walkthrough
1 parent 36badf5 commit ca2184a

File tree

1 file changed

+57
-75
lines changed

1 file changed

+57
-75
lines changed

finished_files/003_quantitative_value_strategy.ipynb

Lines changed: 57 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,32 +1688,28 @@
16881688
],
16891689
"source": [
16901690
"rv_columns = [\n",
1691-
" 'Ticker', \n",
1692-
" 'Price', \n",
1693-
" 'Number of Shares to Buy', \n",
1694-
" 'Price-to-Earnings Ratio',\n",
1695-
" 'PE Percentile',\n",
1696-
" 'Price-to-Book Ratio',\n",
1697-
" 'PB Percentile',\n",
1698-
" 'EV/EBITDA',\n",
1699-
" 'EV/EBITDA Percentile',\n",
1700-
" 'EV/GP',\n",
1701-
" 'EV/GP Percentile',\n",
1702-
" 'RV Score'\n",
1703-
" ]\n",
1691+
" 'Ticker',\n",
1692+
" 'Price',\n",
1693+
" 'Number of Shares to Buy', \n",
1694+
" 'Price-to-Earnings Ratio',\n",
1695+
" 'PE Percentile',\n",
1696+
" 'Price-to-Book Ratio',\n",
1697+
" 'PB Percentile',\n",
1698+
" 'Price-to-Sales Ratio',\n",
1699+
" 'PS Percentile',\n",
1700+
" 'EV/EBITDA',\n",
1701+
" 'EV/EBITDA Percentile',\n",
1702+
" 'EV/GP',\n",
1703+
" 'EV/GP Percentile',\n",
1704+
" 'RV Score'\n",
1705+
"]\n",
17041706
"\n",
17051707
"rv_dataframe = pd.DataFrame(columns = rv_columns)\n",
17061708
"\n",
17071709
"for symbol_string in symbol_strings:\n",
1708-
"# print(symbol_strings)\n",
1709-
" batch_api_call_url = f'https://sandbox.iexapis.com/stable/stock/market/batch/?types=advanced-stats,quote&symbols={symbol_string}&token={IEX_CLOUD_API_TOKEN}'\n",
1710+
" batch_api_call_url = f'https://sandbox.iexapis.com/stable/stock/market/batch?symbols={symbol_string}&types=quote,advanced-stats&token={IEX_CLOUD_API_TOKEN}'\n",
17101711
" data = requests.get(batch_api_call_url).json()\n",
17111712
" for symbol in symbol_string.split(','):\n",
1712-
" \n",
1713-
" pe_ratio = data[symbol]['quote']['peRatio']\n",
1714-
" pb_ratio = data[symbol]['advanced-stats']['priceToBook']\n",
1715-
" ps_ratio = data[symbol]['advanced-stats']['priceToSales']\n",
1716-
" \n",
17171713
" enterprise_value = data[symbol]['advanced-stats']['enterpriseValue']\n",
17181714
" ebitda = data[symbol]['advanced-stats']['EBITDA']\n",
17191715
" gross_profit = data[symbol]['advanced-stats']['grossProfit']\n",
@@ -1722,49 +1718,32 @@
17221718
" ev_to_ebitda = enterprise_value/ebitda\n",
17231719
" except TypeError:\n",
17241720
" ev_to_ebitda = np.NaN\n",
1725-
" \n",
1721+
" \n",
17261722
" try:\n",
17271723
" ev_to_gross_profit = enterprise_value/gross_profit\n",
17281724
" except TypeError:\n",
17291725
" ev_to_gross_profit = np.NaN\n",
17301726
" \n",
17311727
" rv_dataframe = rv_dataframe.append(\n",
1732-
" pd.Series([\n",
1733-
" symbol, \n",
1734-
" data[symbol]['quote']['latestPrice'], \n",
1735-
" 'N/A', \n",
1736-
" pe_ratio,\n",
1737-
" 'N/A',\n",
1738-
" pb_ratio,\n",
1739-
" 'N/A',\n",
1740-
" ev_to_ebitda,\n",
1741-
" 'N/A',\n",
1742-
" ev_to_gross_profit,\n",
1743-
" 'N/A',\n",
1744-
" 'N/A'\n",
1745-
" ], \n",
1746-
" index = rv_columns), \n",
1747-
" ignore_index = True)\n",
1748-
" \n",
1749-
"\n",
1750-
"rv_dataframe"
1751-
]
1752-
},
1753-
{
1754-
"cell_type": "code",
1755-
"execution_count": 13,
1756-
"metadata": {},
1757-
"outputs": [
1758-
{
1759-
"name": "stdout",
1760-
"output_type": "stream",
1761-
"text": [
1762-
"{'week52change': 0.290414, 'week52high': 169.51, 'week52low': 90.41, 'marketcap': 75861250162, 'employees': 10724, 'day200MovingAvg': 134.17, 'day50MovingAvg': 148.64, 'float': 479955020, 'avg10Volume': 1641586.3, 'avg30Volume': 1479579.2, 'ttmEPS': 3.499, 'ttmDividendRate': 0.77, 'companyName': 'Zoetis, Inc.', 'sharesOutstanding': 494883276, 'maxChangePercent': 3.3525, 'year5ChangePercent': 1.69, 'year2ChangePercent': 0.608, 'year1ChangePercent': 0.281583, 'ytdChangePercent': 0.189876, 'month6ChangePercent': 0.10241, 'month3ChangePercent': 0.297048, 'month1ChangePercent': 0.159224, 'day30ChangePercent': 0.165634, 'day5ChangePercent': -0.012817, 'nextDividendDate': None, 'dividendYield': 0.00489706569361443, 'nextEarningsDate': '2020-10-13', 'exDividendDate': '2020-07-01', 'peRatio': 49.1, 'beta': 0.9584403132945124, 'totalCash': 1974321215, 'currentDebt': 549171552, 'revenue': 6518431250, 'grossProfit': 4133660573, 'totalRevenue': 6300683466, 'EBITDA': 2502772553, 'revenuePerShare': 13.28, 'revenuePerEmployee': 618960.04, 'debtToEquity': 2.45, 'profitMargin': 0.2407740535828538, 'enterpriseValue': 81297632588, 'enterpriseValueToRevenue': 13.46, 'priceToSales': 12.6, 'priceToBook': 29.222282216073907, 'forwardPERatio': 46.68, 'pegRatio': -42.88, 'peHigh': 46.10321684721315, 'peLow': 25.052455887152025, 'week52highDate': '2020-07-31', 'week52lowDate': '2020-03-22', 'putCallRatio': 1.5877629858076678}\n"
1763-
]
1764-
}
1765-
],
1766-
"source": [
1767-
"print(data[symbol]['advanced-stats'])"
1728+
" pd.Series([\n",
1729+
" symbol,\n",
1730+
" data[symbol]['quote']['latestPrice'],\n",
1731+
" 'N/A',\n",
1732+
" data[symbol]['quote']['peRatio'],\n",
1733+
" 'N/A',\n",
1734+
" data[symbol]['advanced-stats']['priceToBook'],\n",
1735+
" 'N/A',\n",
1736+
" data[symbol]['advanced-stats']['priceToSales'],\n",
1737+
" 'N/A',\n",
1738+
" ev_to_ebitda,\n",
1739+
" 'N/A',\n",
1740+
" ev_to_gross_profit,\n",
1741+
" 'N/A',\n",
1742+
" 'N/A'\n",
1743+
" ],\n",
1744+
" index = rv_columns),\n",
1745+
" ignore_index = True\n",
1746+
" )"
17681747
]
17691748
},
17701749
{
@@ -2852,7 +2831,7 @@
28522831
"metadata": {},
28532832
"outputs": [],
28542833
"source": [
2855-
"for column in ['Price-to-Earnings Ratio', 'EV/EBITDA', 'EV/GP']:\n",
2834+
"for column in ['Price-to-Earnings Ratio', 'Price-to-Book Ratio','Price-to-Sales Ratio', 'EV/EBITDA','EV/GP']:\n",
28562835
" rv_dataframe[column].fillna(rv_dataframe[column].mean(), inplace = True)"
28572836
]
28582837
},
@@ -3257,6 +3236,7 @@
32573236
"metrics = {\n",
32583237
" 'Price-to-Earnings Ratio': 'PE Percentile',\n",
32593238
" 'Price-to-Book Ratio':'PB Percentile',\n",
3239+
" 'Price-to-Sales Ratio': 'PS Percentile',\n",
32603240
" 'EV/EBITDA':'EV/EBITDA Percentile',\n",
32613241
" 'EV/GP':'EV/GP Percentile'\n",
32623242
"}\n",
@@ -3572,7 +3552,7 @@
35723552
"outputs": [],
35733553
"source": [
35743554
"rv_dataframe.sort_values(by = 'RV Score', inplace = True)\n",
3575-
"rv_dataframe = rv_dataframe[:51]\n",
3555+
"rv_dataframe = rv_dataframe[:50]\n",
35763556
"rv_dataframe.reset_index(drop = True, inplace = True)"
35773557
]
35783558
},
@@ -4681,24 +4661,26 @@
46814661
"metadata": {},
46824662
"outputs": [],
46834663
"source": [
4684-
"column_formats = { \n",
4685-
" 'A' : ['Ticker', string_template],\n",
4686-
" 'B' : ['Price', dollar_template],\n",
4687-
" 'C' : ['Number of Shares to Buy', integer_template],\n",
4688-
" 'D' : ['Price-to-Earnings Ratio', float_template],\n",
4689-
" 'E' : ['PE Percentile', percent_template],\n",
4690-
" 'F' : ['Price-to-Book Ratio', float_template],\n",
4691-
" 'G' : ['PB Percentile', percent_template],\n",
4692-
" 'H' : ['EV/EBITDA', float_template],\n",
4693-
" 'I' : ['EV/EBITDA Percentile', percent_template],\n",
4694-
" 'J' : ['EV/GP', float_template],\n",
4695-
" 'K' : ['EV/GP Percentile', percent_template],\n",
4696-
" 'L' : ['RV Score', percent_template]\n",
4697-
" }\n",
4664+
"column_formats = {\n",
4665+
" 'A': ['Ticker', string_template],\n",
4666+
" 'B': ['Price', dollar_template],\n",
4667+
" 'C': ['Number of Shares to Buy', integer_template],\n",
4668+
" 'D': ['Price-to-Earnings Ratio', float_template],\n",
4669+
" 'E': ['PE Percentile', percent_template],\n",
4670+
" 'F': ['Price-to-Book Ratio', float_template],\n",
4671+
" 'G': ['PB Percentile',percent_template],\n",
4672+
" 'H': ['Price-to-Sales Ratio', float_template],\n",
4673+
" 'I': ['PS Percentile', percent_template],\n",
4674+
" 'J': ['EV/EBITDA', float_template],\n",
4675+
" 'K': ['EV/EBITDA Percentile', percent_template],\n",
4676+
" 'L': ['EV/GP', float_template],\n",
4677+
" 'M': ['EV/GP Percentile', percent_template],\n",
4678+
" 'N': ['RV Score', percent_template]\n",
4679+
" }\n",
46984680
"\n",
46994681
"for column in column_formats.keys():\n",
4700-
" writer.sheets['Value Strategy'].set_column(f'{column}:{column}', 20, column_formats[column][1])\n",
4701-
" writer.sheets['Value Strategy'].write(f'{column}1', column_formats[column][0], string_template)"
4682+
" writer.sheets['Value Strategy'].set_column(f'{column}:{column}', 25, column_formats[column][1])\n",
4683+
" writer.sheets['Value Strategy'].write(f'{column}1', column_formats[column][0], column_formats[column][1])"
47024684
]
47034685
},
47044686
{

0 commit comments

Comments
 (0)