{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: alpha_vantage in c:\\users\\nax\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (2.3.1)\n",
"Requirement already satisfied: requests in c:\\users\\nax\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from alpha_vantage) (2.21.0)\n",
"Requirement already satisfied: aiohttp in c:\\users\\nax\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from alpha_vantage) (3.8.0)\n",
"Requirement already satisfied: attrs>=17.3.0 in c:\\users\\nax\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from aiohttp->alpha_vantage) (20.3.0)\n",
"Requirement already satisfied: frozenlist>=1.1.1 in c:\\users\\nax\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from aiohttp->alpha_vantage) (1.2.0)\n",
"Requirement already satisfied: aiosignal>=1.1.2 in c:\\users\\nax\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from aiohttp->alpha_vantage) (1.2.0)\n",
"Requirement already satisfied: yarl<2.0,>=1.0 in c:\\users\\nax\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from aiohttp->alpha_vantage) (1.7.2)\n",
"Requirement already satisfied: charset-normalize
3.0,>=2.0 in c:\\users\\nax\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from aiohttp->alpha_vantage) (2.0.7)\n",
"Requirement already satisfied: multidict<7.0,>=4.5 in c:\\users\\nax\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from aiohttp->alpha_vantage) (5.2.0)\n",
"Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in c:\\users\\nax\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from aiohttp->alpha_vantage) (4.0.0)\n",
"Requirement already satisfied: typing-extensions>=3.6.5 in c:\\users\\nax\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from async-timeout<5.0,>=4.0.0a3->aiohttp->alpha_vantage) (3.7.4.3)\n",
"Requirement already satisfied: idna>=2.0 in c:\\users\\nax\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from yarl<2.0,>=1.0->aiohttp->alpha_vantage) (2.8)\n",
"Requirement already satisfied: urllib3<1.25,>=1.21.1 in c:\\users\\nax\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from requests->alpha_vantage) (1.24.3)\n",
"Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\nax\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from requests->alpha_vantage) (2020.12.5)\n",
"Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\\users\\nax\\appdata\\local\\programs\\python\\python39\\lib\\site-packages (from requests->alpha_vantage) (3.0.4)\n"
]
},
{
"name": "stde
",
"output_type": "stream",
"text": [
"WARNING: You are using pip version 21.0.1; however, version 21.3.1 is available.\n",
"You should consider upgrading via the 'c:\\users\\nax\\appdata\\local\\programs\\python\\python39\\python.exe -m pip install --upgrade pip' command.\n"
]
}
],
"source": [
"!pip install alpha_vantage\n",
"\n",
"import alpha_vantage\n",
"import requests\n",
"import numpy as np\n",
"import pandas as pd\n",
"import json as js\n",
"import matplotlib.pyplot as plt\n",
"from matplotlib.ticker import MaxNLocator"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def fetch_symbol(stock):\n",
" return requests.get(api_root + api_series + stock + api, stream=False).json()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def company_symbol():\n",
" try:\n",
" stc = ''\n",
" while not stc:\n",
" stc = input(\"What stock would you like to view? \")\n",
" stc = stc.upper()\n",
" stock = fetch_symbol(stc)\n",
" if len(stc) == 0:\n",
" print(\"Please enter a symbol.\")\n",
" except requests.exceptions.ConnectionE
or:\n",
" print(\"Couldn't connect to server! Please check the network?\")\n",
" return stock, stc"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def series():\n",
" \"\"\"This function promts the user for the desired\n",
" time series. Day, week, month, etc\"\"\"\n",
" while True:\n",
" t_type = input('Please enter time series you would like to see. ' \\\n",
" 'Daily[1], Weekly[2], Monthly[3]: ')\n",
" if t_type == '1':\n",
" t_type = 'Time Series (Daily)'\n",
" return t_type\n",
" elif t_type == '2':\n",
" t_type = 'Weekly Time Series'\n",
" return t_type\n",
" elif t_type == '3':\n",
" t_type = 'Monthly Time Series'\n",
" return t_type\n",
" else:\n",
" print('E
or')\n",
" print(t_type)\n",
" continue\n",
" return t_type"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def time_series(t_series):\n",
" \"\"\"The time_series function takes the selected time series and return\n",
" the co
ect format for the url to retrive the symbol time series\"\"\"\n",
" #try except block here \n",
" if t_series == 'Time Series (Daily)':\n",
" api_series = '/query?function=TIME_SERIES_DAILY&symbol='\n",
" return api_series\n",
" elif t_series == 'Weekly Time Series':\n",
" api_series = '/query?function=TIME_SERIES_WEEKLY&symbol='\n",
" return api_series\n",
" elif t_series == 'Monthly Time Series':\n",
" api_series = '/query?function=TIME_SERIES_MONTHLY&symbol='\n",
" return api_series\n",
" else:\n",
" print('E
or')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"t_series = series()\n",
"api_root = 'https:
www.alphavantage.co'\n",
"api_series = time_series(t_series)\n",
"api = '&apikey=QZBPQQGVJDILUMS4'\n",
"\n",
"#api = '&apikey=EKFVA2O5LEO3WL88'\n",
"fhand, stc = company_symbol()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def createDframe(fhand):\n",
" \"\"\"This function finds the date and price for the selected stock\n",
" and creates and returns the dataframe\"\"\"\n",
" lst = []\n",
" for key, values in fhand.items():\n",
" if key == t_series:\n",
" for date, sinfo in values.items():\n",
" #Finds the date and values for stock\n",
" for cols, nums in sinfo.items():\n",
" #finds the closing price\n",
" if cols == '4. close':\n",
" #create list for date and price key values\n",
" lst.append([date,nums]) \n",
" df = pd.DataFrame(lst,columns=['Date','Price'])\n",
" df.sort_index(ascending=False, inplace=True)\n",
" return df"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"