{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyNi+Ewkxp2IZ8viyYUSIC21", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" }, "gpuClass": "standard" }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "code", "source": [ "from google.colab import drive\n", "drive.mount('/content/drive')\n", "project_path = '/content/drive/MyDrive/projects/Stock_Predicter'\n", "%cd $project_path" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Xr3Qozgfktoc", "outputId": "78396a70-6eaa-462b-f7ca-75e282dab940" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Mounted at /content/drive\n", "/content/drive/MyDrive/projects/Stock_Predicter\n" ] } ] }, { "cell_type": "code", "source": [ "# install dotenv\n", "!pip install python-dotenv" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "E0itUkoVeKYn", "outputId": "a876789d-096c-4301-e316-023f87e2e5de" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Collecting python-dotenv\n", " Downloading python_dotenv-1.0.0-py3-none-any.whl (19 kB)\n", "Installing collected packages: python-dotenv\n", "Successfully installed python-dotenv-1.0.0\n" ] } ] }, { "cell_type": "code", "source": [ "# install polygon client\n", "!pip install polygon-api-client" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2bylenpXc1oB", "outputId": "c47ad32c-3c50-41d9-a6ce-c051fb6639b5" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Collecting polygon-api-client\n", " Downloading polygon_api_client-1.8.5-py3-none-any.whl (38 kB)\n", "Requirement already satisfied: urllib3<2.0.0,>=1.26.9 in /usr/local/lib/python3.9/dist-packages (from polygon-api-client) (1.26.15)\n", "Collecting websockets<11.0,>=10.3\n", " Downloading websockets-10.4-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (106 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m106.5/106.5 KB\u001b[0m \u001b[31m5.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: certifi<2023.0.0,>=2022.5.18 in /usr/local/lib/python3.9/dist-packages (from polygon-api-client) (2022.12.7)\n", "Installing collected packages: websockets, polygon-api-client\n", "Successfully installed polygon-api-client-1.8.5 websockets-10.4\n" ] } ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "e8SQqogMQYLh" }, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import pandas as pd\n", "import pandas_datareader as web\n", "import datetime as dt\n", "import yfinance as yfin\n", "\n", "from sklearn.preprocessing import MinMaxScaler\n", "from tensorflow.keras.models import Sequential\n", "from tensorflow.keras.layers import Dense, Dropout, LSTM\n", "from dotenv import dotenv_values\n", "from polygon import RESTClient\n" ] }, { "cell_type": "code", "source": [ "config = dotenv_values(\"env_stock_predictor\")\n", "POLIGON_API_KEY = config['POLIGON_API_KEY']" ], "metadata": { "id": "MwIQIS6GeSJr" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# Select a company for now\n", "ticker = 'AAPL'\n", "\n", "start = dt.datetime(2013,1,1)\n", "end = dt.date.today()\n", "# end = dt.datetime(2023,3,15)\n", "\n", "# data = web.DataReader(ticker, 'yahoo', start, end) # This trows \"TypeError: string indices must be integers\"\n", "\n", "yfin.pdr_override()\n", "data = web.data.get_data_yahoo(ticker, start, end)\n", "print(data.tail())" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "O6dtJpJwS5Eg", "outputId": "8782cb37-06ce-47c0-b352-f1f82a6db7de" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\r[*********************100%***********************] 1 of 1 completed\n", " Open High Low Close Adj Close \\\n", "Date \n", "2023-03-29 159.369995 161.050003 159.350006 160.770004 160.770004 \n", "2023-03-30 161.529999 162.470001 161.270004 162.360001 162.360001 \n", "2023-03-31 162.440002 165.000000 161.910004 164.899994 164.899994 \n", "2023-04-03 164.270004 166.289993 164.220001 166.169998 166.169998 \n", "2023-04-04 166.600006 166.839996 165.110001 165.630005 165.630005 \n", "\n", " Volume \n", "Date \n", "2023-03-29 51305700 \n", "2023-03-30 49501700 \n", "2023-03-31 68694700 \n", "2023-04-03 56976200 \n", "2023-04-04 46237900 \n" ] } ] }, { "cell_type": "code", "source": [ "# using the poligon API\n", "poligon_client = RESTClient(api_key=POLIGON_API_KEY)" ], "metadata": { "id": "LEfjQ4cZi0tn" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# bars = poligon_client.get_aggs(ticker=ticker, multiplier=1, timespan=\"day\", from_=\"2023-01-09\", to=\"2023-01-15\")\n", "bars = poligon_client.get_aggs(ticker=ticker, multiplier=1, timespan=\"day\", from_=start, to=end)\n" ], "metadata": { "id": "edWz4rdxdwqh" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "for bar in bars[-5:]:\n", " print(type(bar))\n", " print(bar)\n", " print(bar.timestamp)\n", " print(dt.date.fromtimestamp(bar.timestamp/1000))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "IX_o3NTggblq", "outputId": "7a974d77-952e-425b-c702-e9a60fbb89be" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "Agg(open=152.81, high=153.47, low=151.83, close=152.87, volume=47204791.0, vwap=152.6973, timestamp=1678251600000, transactions=405203, otc=None)\n", "1678251600000\n", "2023-03-08\n", "\n", "Agg(open=153.559, high=154.535, low=150.225, close=150.59, volume=53833122.0, vwap=152.4689, timestamp=1678338000000, transactions=480909, otc=None)\n", "1678338000000\n", "2023-03-09\n", "\n", "Agg(open=150.21, high=150.94, low=147.6096, close=148.5, volume=68559600.0, vwap=149.0716, timestamp=1678424400000, transactions=611457, otc=None)\n", "1678424400000\n", "2023-03-10\n", "\n", "Agg(open=147.805, high=153.14, low=147.7, close=150.47, volume=84457122.0, vwap=151.1835, timestamp=1678680000000, transactions=760660, otc=None)\n", "1678680000000\n", "2023-03-13\n", "\n", "Agg(open=151.28, high=153.4, low=150.1, close=152.59, volume=72045893.0, vwap=152.1061, timestamp=1678766400000, transactions=565196, otc=None)\n", "1678766400000\n", "2023-03-14\n" ] } ] }, { "cell_type": "code", "source": [ "print(type(spy))\n", "print(spy.head())" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "EMoXLT5vd8Ex", "outputId": "d3c00e06-bf0a-4384-a21d-643d72a6848c" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", " Open High Low Close Adj Close \\\n", "Date \n", "2022-10-24 375.890015 380.059998 373.109985 378.869995 375.704315 \n", "2022-10-25 378.790009 385.250000 378.670013 384.920013 381.703735 \n", "2022-10-26 381.619995 387.579987 381.350006 382.019989 378.827972 \n", "2022-10-27 383.070007 385.000000 379.329987 379.980011 376.805023 \n", "2022-10-28 379.869995 389.519989 379.679993 389.019989 385.769470 \n", "\n", " Volume \n", "Date \n", "2022-10-24 85436900 \n", "2022-10-25 78846300 \n", "2022-10-26 104087300 \n", "2022-10-27 81971800 \n", "2022-10-28 100302000 \n" ] } ] }, { "cell_type": "code", "source": [ "df = web.DataReader('GE', 'yahoo', start='2019-09-10', end='2019-10-09')\n", "print(start)\n", "print(end)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 353 }, "id": "THGxnQbSUgvw", "outputId": "82234614-328b-40b7-9024-fa32e20b2858" }, "execution_count": null, "outputs": [ { "output_type": "error", "ename": "TypeError", "evalue": "ignored", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mweb\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDataReader\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'GE'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'yahoo'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstart\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'2019-09-10'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'2019-10-09'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstart\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mend\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.9/dist-packages/pandas/util/_decorators.py\u001b[0m in \u001b[0;36mwrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 205\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 206\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mnew_arg_name\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnew_arg_value\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 207\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 208\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 209\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcast\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mF\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwrapper\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.9/dist-packages/pandas_datareader/data.py\u001b[0m in \u001b[0;36mDataReader\u001b[0;34m(name, data_source, start, end, retry_count, pause, session, api_key)\u001b[0m\n\u001b[1;32m 368\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 369\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mdata_source\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"yahoo\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 370\u001b[0;31m return YahooDailyReader(\n\u001b[0m\u001b[1;32m 371\u001b[0m \u001b[0msymbols\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 372\u001b[0m \u001b[0mstart\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mstart\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.9/dist-packages/pandas_datareader/base.py\u001b[0m in \u001b[0;36mread\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 251\u001b[0m \u001b[0;31m# If a single symbol, (e.g., 'GOOG')\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 252\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbols\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mstring_types\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 253\u001b[0;31m \u001b[0mdf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_read_one_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mparams\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_params\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbols\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 254\u001b[0m \u001b[0;31m# Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 255\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbols\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mDataFrame\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/usr/local/lib/python3.9/dist-packages/pandas_datareader/yahoo/daily.py\u001b[0m in \u001b[0;36m_read_one_data\u001b[0;34m(self, url, params)\u001b[0m\n\u001b[1;32m 151\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 152\u001b[0m \u001b[0mj\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mloads\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mre\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msearch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mptrn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtext\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mre\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDOTALL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgroup\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 153\u001b[0;31m \u001b[0mdata\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mj\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"context\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"dispatcher\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"stores\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"HistoricalPriceStore\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 154\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 155\u001b[0m \u001b[0mmsg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"No data fetched for symbol {} using {}\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: string indices must be integers" ] } ] }, { "cell_type": "code", "source": [ "scaler = MinMaxScaler(feature_range=(0,1))\n", "scaled_data = scaler.fit_transform(data['Close'].values.reshape(-1,1))\n", "prediction_days = 60\n", "\n", "x_train = []\n", "y_train = []\n", "\n", "for x in range()" ], "metadata": { "id": "ccV59ukvXaNF" }, "execution_count": null, "outputs": [] } ] }