File size: 10,136 Bytes
f90cb2c
 
 
 
 
 
59c1eb1
f90cb2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59c1eb1
f90cb2c
59c1eb1
f90cb2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59c1eb1
f90cb2c
59c1eb1
f90cb2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59c1eb1
f90cb2c
59c1eb1
f90cb2c
 
 
 
 
 
 
 
 
 
59c1eb1
 
 
f90cb2c
 
 
 
 
 
 
 
59c1eb1
f90cb2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59c1eb1
f90cb2c
59c1eb1
f90cb2c
 
 
 
59c1eb1
f90cb2c
 
 
 
 
 
 
 
59c1eb1
 
 
 
f90cb2c
59c1eb1
 
f90cb2c
 
59c1eb1
f90cb2c
59c1eb1
f90cb2c
 
 
 
 
59c1eb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f90cb2c
 
59c1eb1
f90cb2c
59c1eb1
f90cb2c
 
 
 
59c1eb1
f90cb2c
 
 
 
 
59c1eb1
f90cb2c
59c1eb1
f90cb2c
 
 
 
 
59c1eb1
f90cb2c
59c1eb1
 
 
 
f90cb2c
59c1eb1
 
 
 
f90cb2c
 
 
 
 
 
 
 
 
 
 
 
59c1eb1
 
f90cb2c
 
59c1eb1
f90cb2c
59c1eb1
f90cb2c
 
59c1eb1
 
 
 
 
 
 
 
f90cb2c
 
 
 
 
 
 
 
 
 
 
 
59c1eb1
f90cb2c
 
59c1eb1
f90cb2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyPcuRkmq64yTPWXIBG7lLf0",
      "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": [
        "<a href=\"https://colab.research.google.com/github/jsebdev/Stock_Predictor/blob/main/stock_predictor.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "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": "28119a16-7e41-437a-969b-3713f019548e"
      },
      "execution_count": 1,
      "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": "bc2a7293-a9f0-4f4d-d42f-f7ecfab7e5c5"
      },
      "execution_count": 2,
      "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": "74b2587b-2b58-42a1-f5bf-c3866c13b8a1"
      },
      "execution_count": 3,
      "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",
            "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[31m11.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hRequirement 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",
            "Requirement 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": 4,
      "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": [
        "# geting poligon api key\n",
        "config = dotenv_values(\"env_stock_predictor\")\n",
        "POLYGON_API_KEY = config['POLYGON_API_KEY']"
      ],
      "metadata": {
        "id": "MwIQIS6GeSJr"
      },
      "execution_count": 18,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Select a company for now\n",
        "ticker = 'AAPL'\n",
        "\n",
        "data_sources = {'pandas': 'pandas-datareader',\n",
        "                'polygon':'polygon'}\n",
        "source = data_sources['polygon']\n",
        "# source = data_sources['pandas']\n",
        "\n",
        "start = dt.datetime(2013,1,1)\n",
        "end = dt.date.today()"
      ],
      "metadata": {
        "id": "O6dtJpJwS5Eg"
      },
      "execution_count": 19,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "if source == data_sources['pandas']:\n",
        "  yfin.pdr_override()\n",
        "  data = web.data.get_data_yahoo(ticker, start, end)\n",
        "elif source == data_sources['polygon']:\n",
        "  # using the poligon API\n",
        "  poligon_client = RESTClient(api_key=POLYGON_API_KEY)\n",
        "  # 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",
        "  bars = poligon_client.get_aggs(ticker=ticker, multiplier=1, timespan=\"hour\", from_=dt.datetime.now() - dt.timedelta(days=5), to=dt.datetime.now())\n",
        "  print(len(bars))\n",
        "  for bar in bars[-2:]:\n",
        "    print(type(bar))\n",
        "    print(bar)\n",
        "    print(bar.timestamp)\n",
        "    print(dt.date.fromtimestamp(bar.timestamp/1000))\n",
        "    print(dt.datetime.fromtimestamp(bar.timestamp/1000))"
      ],
      "metadata": {
        "id": "LwPyk8Uh-Zz_"
      },
      "execution_count": 36,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "IX_o3NTggblq",
        "outputId": "27d4d43b-e063-4651-db16-f5ecf819860b"
      },
      "execution_count": 37,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "41\n",
            "<class 'polygon.rest.models.aggs.Agg'>\n",
            "Agg(open=165.57, high=165.68, low=165.53, close=165.64, volume=11712, vwap=165.6067, timestamp=1680645600000, transactions=258, otc=None)\n",
            "1680645600000\n",
            "2023-04-04\n",
            "2023-04-04 22:00:00\n",
            "<class 'polygon.rest.models.aggs.Agg'>\n",
            "Agg(open=165.6, high=165.85, low=165.6, close=165.79, volume=28951, vwap=165.7385, timestamp=1680649200000, transactions=533, otc=None)\n",
            "1680649200000\n",
            "2023-04-04\n",
            "2023-04-04 23:00:00\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "print(type(spy))\n",
        "print(spy.head())"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 187
        },
        "id": "EMoXLT5vd8Ex",
        "outputId": "74416af4-da65-4d12-ed3a-27806b8f0965"
      },
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "error",
          "ename": "NameError",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-10-dab045b648a5>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mspy\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      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mspy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhead\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;31mNameError\u001b[0m: name 'spy' is not defined"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "df = web.DataReader('GE', 'yahoo', start='2019-09-10', end='2019-10-09')\n",
        "print(start)\n",
        "print(end)"
      ],
      "metadata": {
        "id": "THGxnQbSUgvw"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "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": []
    }
  ]
}