{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "# Google Search Trend and Stock Price Tracker\n", "\n", "> The following [Jupyter notebook](https://jupyter.org) supports use of the [pyrends API](https://pypi.org/project/pytrends/) for querying changes in Google search trends and the [Polygon.io API](https://polygon.io/) for querying changes in a given public stock price. The notebook will generate a simple graph to show how these changes may corrolate over a selected time period. When running the notebook locally, be sure to add your Polygon.io API key to the stocks.py file (may be found in the src/ directory)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 1: Import Libraries\n", "Import pytrends, pandas, datetime, matplotlib and seaborn goodies, and homemade classes Stocks and Google." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "from pytrends.request import TrendReq\n", "import pandas as pd\n", "import datetime as dt\n", "from datetime import timedelta\n", "\n", "from pytrends_support_hvk import google_trends\n", "from stock_api_call.stocks import Stocks\n", "\n", "import matplotlib.pyplot as plt\n", "import matplotlib.dates as mdates\n", "from pandas.plotting import register_matplotlib_converters\n", "import seaborn as sns\n", "register_matplotlib_converters()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 2: Set Variables\n", "Declare Google search terms, days for analysis (max = 250), and stock ticker." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "kw_list = \"vaccine\"\n", "days = 250\n", "stock_ticker = \"JNJ\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 3: Run APIs\n", "Query Google Trends and Closing Stock Prices with selected variables. Tweak results (add index to Google dataframe and change dates to datetime objects in Stocks dataframe)." ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "google_trends = google_trends.Google (kw_list, days)\n", "stock_trends = Stocks (stock_ticker, days)\n", "gtf = google_trends.trends()\n", "gtf = gtf.reset_index()\n", "stf = stock_trends.stocks()\n", "stf['Dates'] = pd.to_datetime(stf['Dates'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 4: Merge results into one Trends table\n", "Save as .csv for playing around with" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | date | \n", "vaccine | \n", "isPartial | \n", "Dates | \n", "Closing Price | \n", "
---|---|---|---|---|---|
0 | \n", "2020-09-08 | \n", "5 | \n", "False | \n", "2020-09-08 | \n", "147.26 | \n", "
1 | \n", "2020-09-09 | \n", "6 | \n", "False | \n", "2020-09-09 | \n", "149.70 | \n", "
2 | \n", "2020-09-10 | \n", "5 | \n", "False | \n", "2020-09-10 | \n", "146.91 | \n", "
3 | \n", "2020-09-11 | \n", "4 | \n", "False | \n", "2020-09-11 | \n", "147.78 | \n", "
4 | \n", "2020-09-14 | \n", "4 | \n", "False | \n", "2020-09-14 | \n", "148.35 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
167 | \n", "2021-05-07 | \n", "30 | \n", "False | \n", "2021-05-07 | \n", "168.50 | \n", "
168 | \n", "2021-05-10 | \n", "29 | \n", "False | \n", "2021-05-10 | \n", "170.27 | \n", "
169 | \n", "2021-05-11 | \n", "33 | \n", "False | \n", "2021-05-11 | \n", "168.88 | \n", "
170 | \n", "2021-05-12 | \n", "31 | \n", "False | \n", "2021-05-12 | \n", "168.20 | \n", "
171 | \n", "2021-05-13 | \n", "40 | \n", "False | \n", "2021-05-13 | \n", "169.96 | \n", "
172 rows × 5 columns
\n", "