Spaces:
Runtime error
Runtime error
Trying to use Polygon to retrieve hourly bars
Browse files- stock_predictor.ipynb +66 -138
stock_predictor.ipynb
CHANGED
|
@@ -4,7 +4,7 @@
|
|
| 4 |
"metadata": {
|
| 5 |
"colab": {
|
| 6 |
"provenance": [],
|
| 7 |
-
"authorship_tag": "
|
| 8 |
"include_colab_link": true
|
| 9 |
},
|
| 10 |
"kernelspec": {
|
|
@@ -40,9 +40,9 @@
|
|
| 40 |
"base_uri": "https://localhost:8080/"
|
| 41 |
},
|
| 42 |
"id": "Xr3Qozgfktoc",
|
| 43 |
-
"outputId": "
|
| 44 |
},
|
| 45 |
-
"execution_count":
|
| 46 |
"outputs": [
|
| 47 |
{
|
| 48 |
"output_type": "stream",
|
|
@@ -65,9 +65,9 @@
|
|
| 65 |
"base_uri": "https://localhost:8080/"
|
| 66 |
},
|
| 67 |
"id": "E0itUkoVeKYn",
|
| 68 |
-
"outputId": "
|
| 69 |
},
|
| 70 |
-
"execution_count":
|
| 71 |
"outputs": [
|
| 72 |
{
|
| 73 |
"output_type": "stream",
|
|
@@ -93,9 +93,9 @@
|
|
| 93 |
"base_uri": "https://localhost:8080/"
|
| 94 |
},
|
| 95 |
"id": "2bylenpXc1oB",
|
| 96 |
-
"outputId": "
|
| 97 |
},
|
| 98 |
-
"execution_count":
|
| 99 |
"outputs": [
|
| 100 |
{
|
| 101 |
"output_type": "stream",
|
|
@@ -104,11 +104,11 @@
|
|
| 104 |
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
|
| 105 |
"Collecting polygon-api-client\n",
|
| 106 |
" Downloading polygon_api_client-1.8.5-py3-none-any.whl (38 kB)\n",
|
| 107 |
-
"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",
|
| 108 |
"Collecting websockets<11.0,>=10.3\n",
|
| 109 |
" 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",
|
| 110 |
-
"\u001b[2K \u001b[90m
|
| 111 |
-
"\u001b[?25hRequirement already satisfied:
|
|
|
|
| 112 |
"Installing collected packages: websockets, polygon-api-client\n",
|
| 113 |
"Successfully installed polygon-api-client-1.8.5 websockets-10.4\n"
|
| 114 |
]
|
|
@@ -117,7 +117,7 @@
|
|
| 117 |
},
|
| 118 |
{
|
| 119 |
"cell_type": "code",
|
| 120 |
-
"execution_count":
|
| 121 |
"metadata": {
|
| 122 |
"id": "e8SQqogMQYLh"
|
| 123 |
},
|
|
@@ -140,13 +140,14 @@
|
|
| 140 |
{
|
| 141 |
"cell_type": "code",
|
| 142 |
"source": [
|
|
|
|
| 143 |
"config = dotenv_values(\"env_stock_predictor\")\n",
|
| 144 |
-
"
|
| 145 |
],
|
| 146 |
"metadata": {
|
| 147 |
"id": "MwIQIS6GeSJr"
|
| 148 |
},
|
| 149 |
-
"execution_count":
|
| 150 |
"outputs": []
|
| 151 |
},
|
| 152 |
{
|
|
@@ -155,115 +156,73 @@
|
|
| 155 |
"# Select a company for now\n",
|
| 156 |
"ticker = 'AAPL'\n",
|
| 157 |
"\n",
|
| 158 |
-
"
|
| 159 |
-
"
|
| 160 |
-
"
|
| 161 |
-
"\n",
|
| 162 |
-
"# data = web.DataReader(ticker, 'yahoo', start, end) # This trows \"TypeError: string indices must be integers\"\n",
|
| 163 |
"\n",
|
| 164 |
-
"
|
| 165 |
-
"
|
| 166 |
-
"print(data.tail())"
|
| 167 |
-
],
|
| 168 |
-
"metadata": {
|
| 169 |
-
"colab": {
|
| 170 |
-
"base_uri": "https://localhost:8080/"
|
| 171 |
-
},
|
| 172 |
-
"id": "O6dtJpJwS5Eg",
|
| 173 |
-
"outputId": "8782cb37-06ce-47c0-b352-f1f82a6db7de"
|
| 174 |
-
},
|
| 175 |
-
"execution_count": null,
|
| 176 |
-
"outputs": [
|
| 177 |
-
{
|
| 178 |
-
"output_type": "stream",
|
| 179 |
-
"name": "stdout",
|
| 180 |
-
"text": [
|
| 181 |
-
"\r[*********************100%***********************] 1 of 1 completed\n",
|
| 182 |
-
" Open High Low Close Adj Close \\\n",
|
| 183 |
-
"Date \n",
|
| 184 |
-
"2023-03-29 159.369995 161.050003 159.350006 160.770004 160.770004 \n",
|
| 185 |
-
"2023-03-30 161.529999 162.470001 161.270004 162.360001 162.360001 \n",
|
| 186 |
-
"2023-03-31 162.440002 165.000000 161.910004 164.899994 164.899994 \n",
|
| 187 |
-
"2023-04-03 164.270004 166.289993 164.220001 166.169998 166.169998 \n",
|
| 188 |
-
"2023-04-04 166.600006 166.839996 165.110001 165.630005 165.630005 \n",
|
| 189 |
-
"\n",
|
| 190 |
-
" Volume \n",
|
| 191 |
-
"Date \n",
|
| 192 |
-
"2023-03-29 51305700 \n",
|
| 193 |
-
"2023-03-30 49501700 \n",
|
| 194 |
-
"2023-03-31 68694700 \n",
|
| 195 |
-
"2023-04-03 56976200 \n",
|
| 196 |
-
"2023-04-04 46237900 \n"
|
| 197 |
-
]
|
| 198 |
-
}
|
| 199 |
-
]
|
| 200 |
-
},
|
| 201 |
-
{
|
| 202 |
-
"cell_type": "code",
|
| 203 |
-
"source": [
|
| 204 |
-
"# using the poligon API\n",
|
| 205 |
-
"poligon_client = RESTClient(api_key=POLIGON_API_KEY)"
|
| 206 |
],
|
| 207 |
"metadata": {
|
| 208 |
-
"id": "
|
| 209 |
},
|
| 210 |
-
"execution_count":
|
| 211 |
"outputs": []
|
| 212 |
},
|
| 213 |
{
|
| 214 |
"cell_type": "code",
|
| 215 |
"source": [
|
| 216 |
-
"
|
| 217 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
],
|
| 219 |
"metadata": {
|
| 220 |
-
"id": "
|
| 221 |
},
|
| 222 |
-
"execution_count":
|
| 223 |
"outputs": []
|
| 224 |
},
|
| 225 |
{
|
| 226 |
"cell_type": "code",
|
| 227 |
-
"source": [
|
| 228 |
-
"for bar in bars[-5:]:\n",
|
| 229 |
-
" print(type(bar))\n",
|
| 230 |
-
" print(bar)\n",
|
| 231 |
-
" print(bar.timestamp)\n",
|
| 232 |
-
" print(dt.date.fromtimestamp(bar.timestamp/1000))"
|
| 233 |
-
],
|
| 234 |
"metadata": {
|
| 235 |
"colab": {
|
| 236 |
"base_uri": "https://localhost:8080/"
|
| 237 |
},
|
| 238 |
"id": "IX_o3NTggblq",
|
| 239 |
-
"outputId": "
|
| 240 |
},
|
| 241 |
-
"execution_count":
|
| 242 |
"outputs": [
|
| 243 |
{
|
| 244 |
"output_type": "stream",
|
| 245 |
"name": "stdout",
|
| 246 |
"text": [
|
|
|
|
| 247 |
"<class 'polygon.rest.models.aggs.Agg'>\n",
|
| 248 |
-
"Agg(open=
|
| 249 |
-
"
|
| 250 |
-
"2023-
|
| 251 |
-
"
|
| 252 |
-
"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",
|
| 253 |
-
"1678338000000\n",
|
| 254 |
-
"2023-03-09\n",
|
| 255 |
"<class 'polygon.rest.models.aggs.Agg'>\n",
|
| 256 |
-
"Agg(open=
|
| 257 |
-
"
|
| 258 |
-
"2023-
|
| 259 |
-
"
|
| 260 |
-
"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",
|
| 261 |
-
"1678680000000\n",
|
| 262 |
-
"2023-03-13\n",
|
| 263 |
-
"<class 'polygon.rest.models.aggs.Agg'>\n",
|
| 264 |
-
"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",
|
| 265 |
-
"1678766400000\n",
|
| 266 |
-
"2023-03-14\n"
|
| 267 |
]
|
| 268 |
}
|
| 269 |
]
|
|
@@ -276,33 +235,23 @@
|
|
| 276 |
],
|
| 277 |
"metadata": {
|
| 278 |
"colab": {
|
| 279 |
-
"base_uri": "https://localhost:8080/"
|
|
|
|
| 280 |
},
|
| 281 |
"id": "EMoXLT5vd8Ex",
|
| 282 |
-
"outputId": "
|
| 283 |
},
|
| 284 |
-
"execution_count":
|
| 285 |
"outputs": [
|
| 286 |
{
|
| 287 |
-
"output_type": "
|
| 288 |
-
"
|
| 289 |
-
"
|
| 290 |
-
|
| 291 |
-
"
|
| 292 |
-
"
|
| 293 |
-
"
|
| 294 |
-
"
|
| 295 |
-
"2022-10-26 381.619995 387.579987 381.350006 382.019989 378.827972 \n",
|
| 296 |
-
"2022-10-27 383.070007 385.000000 379.329987 379.980011 376.805023 \n",
|
| 297 |
-
"2022-10-28 379.869995 389.519989 379.679993 389.019989 385.769470 \n",
|
| 298 |
-
"\n",
|
| 299 |
-
" Volume \n",
|
| 300 |
-
"Date \n",
|
| 301 |
-
"2022-10-24 85436900 \n",
|
| 302 |
-
"2022-10-25 78846300 \n",
|
| 303 |
-
"2022-10-26 104087300 \n",
|
| 304 |
-
"2022-10-27 81971800 \n",
|
| 305 |
-
"2022-10-28 100302000 \n"
|
| 306 |
]
|
| 307 |
}
|
| 308 |
]
|
|
@@ -315,31 +264,10 @@
|
|
| 315 |
"print(end)"
|
| 316 |
],
|
| 317 |
"metadata": {
|
| 318 |
-
"
|
| 319 |
-
"base_uri": "https://localhost:8080/",
|
| 320 |
-
"height": 353
|
| 321 |
-
},
|
| 322 |
-
"id": "THGxnQbSUgvw",
|
| 323 |
-
"outputId": "82234614-328b-40b7-9024-fa32e20b2858"
|
| 324 |
},
|
| 325 |
"execution_count": null,
|
| 326 |
-
"outputs": [
|
| 327 |
-
{
|
| 328 |
-
"output_type": "error",
|
| 329 |
-
"ename": "TypeError",
|
| 330 |
-
"evalue": "ignored",
|
| 331 |
-
"traceback": [
|
| 332 |
-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
| 333 |
-
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
| 334 |
-
"\u001b[0;32m<ipython-input-17-078ffcb02a17>\u001b[0m in \u001b[0;36m<cell line: 1>\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",
|
| 335 |
-
"\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",
|
| 336 |
-
"\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",
|
| 337 |
-
"\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",
|
| 338 |
-
"\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",
|
| 339 |
-
"\u001b[0;31mTypeError\u001b[0m: string indices must be integers"
|
| 340 |
-
]
|
| 341 |
-
}
|
| 342 |
-
]
|
| 343 |
},
|
| 344 |
{
|
| 345 |
"cell_type": "code",
|
|
|
|
| 4 |
"metadata": {
|
| 5 |
"colab": {
|
| 6 |
"provenance": [],
|
| 7 |
+
"authorship_tag": "ABX9TyPcuRkmq64yTPWXIBG7lLf0",
|
| 8 |
"include_colab_link": true
|
| 9 |
},
|
| 10 |
"kernelspec": {
|
|
|
|
| 40 |
"base_uri": "https://localhost:8080/"
|
| 41 |
},
|
| 42 |
"id": "Xr3Qozgfktoc",
|
| 43 |
+
"outputId": "28119a16-7e41-437a-969b-3713f019548e"
|
| 44 |
},
|
| 45 |
+
"execution_count": 1,
|
| 46 |
"outputs": [
|
| 47 |
{
|
| 48 |
"output_type": "stream",
|
|
|
|
| 65 |
"base_uri": "https://localhost:8080/"
|
| 66 |
},
|
| 67 |
"id": "E0itUkoVeKYn",
|
| 68 |
+
"outputId": "bc2a7293-a9f0-4f4d-d42f-f7ecfab7e5c5"
|
| 69 |
},
|
| 70 |
+
"execution_count": 2,
|
| 71 |
"outputs": [
|
| 72 |
{
|
| 73 |
"output_type": "stream",
|
|
|
|
| 93 |
"base_uri": "https://localhost:8080/"
|
| 94 |
},
|
| 95 |
"id": "2bylenpXc1oB",
|
| 96 |
+
"outputId": "74b2587b-2b58-42a1-f5bf-c3866c13b8a1"
|
| 97 |
},
|
| 98 |
+
"execution_count": 3,
|
| 99 |
"outputs": [
|
| 100 |
{
|
| 101 |
"output_type": "stream",
|
|
|
|
| 104 |
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
|
| 105 |
"Collecting polygon-api-client\n",
|
| 106 |
" Downloading polygon_api_client-1.8.5-py3-none-any.whl (38 kB)\n",
|
|
|
|
| 107 |
"Collecting websockets<11.0,>=10.3\n",
|
| 108 |
" 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",
|
| 109 |
+
"\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",
|
| 110 |
+
"\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",
|
| 111 |
+
"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",
|
| 112 |
"Installing collected packages: websockets, polygon-api-client\n",
|
| 113 |
"Successfully installed polygon-api-client-1.8.5 websockets-10.4\n"
|
| 114 |
]
|
|
|
|
| 117 |
},
|
| 118 |
{
|
| 119 |
"cell_type": "code",
|
| 120 |
+
"execution_count": 4,
|
| 121 |
"metadata": {
|
| 122 |
"id": "e8SQqogMQYLh"
|
| 123 |
},
|
|
|
|
| 140 |
{
|
| 141 |
"cell_type": "code",
|
| 142 |
"source": [
|
| 143 |
+
"# geting poligon api key\n",
|
| 144 |
"config = dotenv_values(\"env_stock_predictor\")\n",
|
| 145 |
+
"POLYGON_API_KEY = config['POLYGON_API_KEY']"
|
| 146 |
],
|
| 147 |
"metadata": {
|
| 148 |
"id": "MwIQIS6GeSJr"
|
| 149 |
},
|
| 150 |
+
"execution_count": 18,
|
| 151 |
"outputs": []
|
| 152 |
},
|
| 153 |
{
|
|
|
|
| 156 |
"# Select a company for now\n",
|
| 157 |
"ticker = 'AAPL'\n",
|
| 158 |
"\n",
|
| 159 |
+
"data_sources = {'pandas': 'pandas-datareader',\n",
|
| 160 |
+
" 'polygon':'polygon'}\n",
|
| 161 |
+
"source = data_sources['polygon']\n",
|
| 162 |
+
"# source = data_sources['pandas']\n",
|
|
|
|
| 163 |
"\n",
|
| 164 |
+
"start = dt.datetime(2013,1,1)\n",
|
| 165 |
+
"end = dt.date.today()"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
],
|
| 167 |
"metadata": {
|
| 168 |
+
"id": "O6dtJpJwS5Eg"
|
| 169 |
},
|
| 170 |
+
"execution_count": 19,
|
| 171 |
"outputs": []
|
| 172 |
},
|
| 173 |
{
|
| 174 |
"cell_type": "code",
|
| 175 |
"source": [
|
| 176 |
+
"if source == data_sources['pandas']:\n",
|
| 177 |
+
" yfin.pdr_override()\n",
|
| 178 |
+
" data = web.data.get_data_yahoo(ticker, start, end)\n",
|
| 179 |
+
"elif source == data_sources['polygon']:\n",
|
| 180 |
+
" # using the poligon API\n",
|
| 181 |
+
" poligon_client = RESTClient(api_key=POLYGON_API_KEY)\n",
|
| 182 |
+
" # bars = poligon_client.get_aggs(ticker=ticker, multiplier=1, timespan=\"day\", from_=\"2023-01-09\", to=\"2023-01-15\")\n",
|
| 183 |
+
" # bars = poligon_client.get_aggs(ticker=ticker, multiplier=1, timespan=\"day\", from_=start, to=end)\n",
|
| 184 |
+
" bars = poligon_client.get_aggs(ticker=ticker, multiplier=1, timespan=\"hour\", from_=dt.datetime.now() - dt.timedelta(days=5), to=dt.datetime.now())\n",
|
| 185 |
+
" print(len(bars))\n",
|
| 186 |
+
" for bar in bars[-2:]:\n",
|
| 187 |
+
" print(type(bar))\n",
|
| 188 |
+
" print(bar)\n",
|
| 189 |
+
" print(bar.timestamp)\n",
|
| 190 |
+
" print(dt.date.fromtimestamp(bar.timestamp/1000))\n",
|
| 191 |
+
" print(dt.datetime.fromtimestamp(bar.timestamp/1000))"
|
| 192 |
],
|
| 193 |
"metadata": {
|
| 194 |
+
"id": "LwPyk8Uh-Zz_"
|
| 195 |
},
|
| 196 |
+
"execution_count": 36,
|
| 197 |
"outputs": []
|
| 198 |
},
|
| 199 |
{
|
| 200 |
"cell_type": "code",
|
| 201 |
+
"source": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
"metadata": {
|
| 203 |
"colab": {
|
| 204 |
"base_uri": "https://localhost:8080/"
|
| 205 |
},
|
| 206 |
"id": "IX_o3NTggblq",
|
| 207 |
+
"outputId": "27d4d43b-e063-4651-db16-f5ecf819860b"
|
| 208 |
},
|
| 209 |
+
"execution_count": 37,
|
| 210 |
"outputs": [
|
| 211 |
{
|
| 212 |
"output_type": "stream",
|
| 213 |
"name": "stdout",
|
| 214 |
"text": [
|
| 215 |
+
"41\n",
|
| 216 |
"<class 'polygon.rest.models.aggs.Agg'>\n",
|
| 217 |
+
"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",
|
| 218 |
+
"1680645600000\n",
|
| 219 |
+
"2023-04-04\n",
|
| 220 |
+
"2023-04-04 22:00:00\n",
|
|
|
|
|
|
|
|
|
|
| 221 |
"<class 'polygon.rest.models.aggs.Agg'>\n",
|
| 222 |
+
"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",
|
| 223 |
+
"1680649200000\n",
|
| 224 |
+
"2023-04-04\n",
|
| 225 |
+
"2023-04-04 23:00:00\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
]
|
| 227 |
}
|
| 228 |
]
|
|
|
|
| 235 |
],
|
| 236 |
"metadata": {
|
| 237 |
"colab": {
|
| 238 |
+
"base_uri": "https://localhost:8080/",
|
| 239 |
+
"height": 187
|
| 240 |
},
|
| 241 |
"id": "EMoXLT5vd8Ex",
|
| 242 |
+
"outputId": "74416af4-da65-4d12-ed3a-27806b8f0965"
|
| 243 |
},
|
| 244 |
+
"execution_count": 10,
|
| 245 |
"outputs": [
|
| 246 |
{
|
| 247 |
+
"output_type": "error",
|
| 248 |
+
"ename": "NameError",
|
| 249 |
+
"evalue": "ignored",
|
| 250 |
+
"traceback": [
|
| 251 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
| 252 |
+
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
|
| 253 |
+
"\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",
|
| 254 |
+
"\u001b[0;31mNameError\u001b[0m: name 'spy' is not defined"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
]
|
| 256 |
}
|
| 257 |
]
|
|
|
|
| 264 |
"print(end)"
|
| 265 |
],
|
| 266 |
"metadata": {
|
| 267 |
+
"id": "THGxnQbSUgvw"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
},
|
| 269 |
"execution_count": null,
|
| 270 |
+
"outputs": []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
},
|
| 272 |
{
|
| 273 |
"cell_type": "code",
|