184 lines
6.3 KiB
Plaintext
184 lines
6.3 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "78af696f-0a94-4067-8883-12737e63d111",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import requests\n",
|
|
"import urllib.parse\n",
|
|
"from pandas import DataFrame as df\n",
|
|
"import numpy as np"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "379014f5-f668-4991-acc8-4f46f38801ba",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"base_url = \"https://www.dolthub.com/api/v1alpha1/post-no-preference/earnings/master\"\n",
|
|
"\n",
|
|
"alpha_url = \"https://www.alphavantage.co/query\"\n",
|
|
"alpha_vantage_key = \"0ASTESBIVM48QLYV\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "6c9a6813-8b84-4bc5-9d44-b06d5ea91485",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"symbol = \"PHM\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "399ec136-ad3a-40b9-92d0-d020868d65f4",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def get_income_frame(symbol: str):\n",
|
|
" query = \"SELECT * FROM income_statement WHERE act_symbol = '%s' AND period = 'Year'\" % symbol\n",
|
|
" response = requests.get(\"%s?q=%s\" % (base_url, urllib.parse.quote_plus(query)))\n",
|
|
" return df.from_dict(response.json()[\"rows\"]).sort_values(\"date\", ascending=False)\n",
|
|
"\n",
|
|
"def get_quote(symbol: str):\n",
|
|
" query = \"?function=TIME_SERIES_DAILY&symbol=%s&apikey=%s\" % (symbol, alpha_vantage_key)\n",
|
|
" response = requests.get(\"%s%s\" % (alpha_url, query)).json()\n",
|
|
" aframe = df.from_dict(response['Time Series (Daily)']).T\n",
|
|
" return aframe.iloc[0,3]\n",
|
|
"\n",
|
|
"def ppere(income_frame, price):\n",
|
|
" eps = income_frame.iloc[0][\"diluted_net_eps\"]\n",
|
|
" return float(price) / float(eps)\n",
|
|
"\n",
|
|
"def mean_margin(income_frame):\n",
|
|
" return np.mean(income_frame.net_income.astype(float) / income_frame.sales.astype(float))\n",
|
|
"\n",
|
|
"def margin(income_frame):\n",
|
|
" return (income_frame.net_income.astype(float) / income_frame.sales.astype(float))[0]\n",
|
|
"\n",
|
|
"def eps3(income_frame):\n",
|
|
" return np.mean(income_frame.diluted_net_eps.astype(float)[0:3])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "1ee4bf5c-01d8-4f9c-929c-652b1b801683",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"ename": "KeyError",
|
|
"evalue": "'Time Series (Daily)'",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
|
|
"\u001b[31mKeyError\u001b[39m Traceback (most recent call last)",
|
|
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[5]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m 1\u001b[39m income_frame = get_income_frame(symbol)\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m price = \u001b[43mget_quote\u001b[49m\u001b[43m(\u001b[49m\u001b[43msymbol\u001b[49m\u001b[43m)\u001b[49m\n",
|
|
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[4]\u001b[39m\u001b[32m, line 9\u001b[39m, in \u001b[36mget_quote\u001b[39m\u001b[34m(symbol)\u001b[39m\n\u001b[32m 7\u001b[39m query = \u001b[33m\"\u001b[39m\u001b[33m?function=TIME_SERIES_DAILY&symbol=\u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[33m&apikey=\u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[33m\"\u001b[39m % (symbol, alpha_vantage_key)\n\u001b[32m 8\u001b[39m response = requests.get(\u001b[33m\"\u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[33m\"\u001b[39m % (alpha_url, query)).json()\n\u001b[32m----> \u001b[39m\u001b[32m9\u001b[39m aframe = df.from_dict(\u001b[43mresponse\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mTime Series (Daily)\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m]\u001b[49m).T\n\u001b[32m 10\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m aframe.iloc[\u001b[32m0\u001b[39m,\u001b[32m3\u001b[39m]\n",
|
|
"\u001b[31mKeyError\u001b[39m: 'Time Series (Daily)'"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"income_frame = get_income_frame(symbol)\n",
|
|
"price = get_quote(symbol)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "1afdc099-986b-47a5-bf29-b2af249207e9",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"pe = ppere(income_frame, price)\n",
|
|
"mm = mean_margin(income_frame)\n",
|
|
"m = margin(income_frame)\n",
|
|
"upside = ((float(eps3(income_frame) * 12) - float(price)) / float(price) * 100)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "f88f49b5-7c36-4790-83b5-2fbf3d1b8363",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"print(symbol)\n",
|
|
"print(\"P/E: %0.1f\" % pe)\n",
|
|
"print(\"Margin: %01.1f %%\" % ((m + mm) * 50))\n",
|
|
"print(\"Potential: %01.1f %%\" % upside)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"id": "7c0aa850-8c29-49ed-81d7-c96341041c46",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
" query = \"?function=TIME_SERIES_DAILY&symbol=%s&apikey=%s\" % (symbol, alpha_vantage_key)\n",
|
|
" response = requests.get(\"%s%s\" % (alpha_url, query)).json()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"id": "bf8b8ba0-9e97-4439-8b84-7bb7383435f5",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{'Information': 'We have detected your API key as 0ASTESBIVM48QLYV and our standard API rate limit is 25 requests per day. Please subscribe to any of the premium plans at https://www.alphavantage.co/premium/ to instantly remove all daily rate limits.'}"
|
|
]
|
|
},
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"response"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "6f633545-b5bc-444d-a2d8-60712c8a5b68",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.13.7"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|