Created
October 15, 2023 06:49
-
-
Save moneebullah25/6d7b23c4d31f97179d096917af7e288c to your computer and use it in GitHub Desktop.
stock_prediction_new_open_nb.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "provenance": [], | |
| "authorship_tag": "ABX9TyNk9F1qGdaCSwMvGkyh2b/q", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/moneebullah25/6d7b23c4d31f97179d096917af7e288c/stock_prediction_new_open_nb.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "id": "L4plaxiKBQ9P" | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "!pip install yfinance numpy pandas matplotlib seaborn truedata_ws" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "import numpy as np # linear algebra\n", | |
| "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n", | |
| "import yfinance as yf\n", | |
| "from sklearn.preprocessing import StandardScaler\n", | |
| "import torch\n", | |
| "import torch.nn as nn\n", | |
| "import time\n", | |
| "import math, time\n", | |
| "from sklearn.metrics import mean_squared_error\n", | |
| "from dateutil.relativedelta import relativedelta\n", | |
| "from datetime import datetime\n", | |
| "from sklearn.preprocessing import MinMaxScaler\n", | |
| "from truedata_ws.websocket.TD import TD\n", | |
| "\n", | |
| "import matplotlib.pyplot as plt\n", | |
| "import seaborn as sns\n", | |
| "import plotly.express as px\n", | |
| "import plotly.graph_objects as go" | |
| ], | |
| "metadata": { | |
| "id": "SrzULvEmBbON" | |
| }, | |
| "execution_count": 2, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "td_obj = TD('--', '--', live_port=8086)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "TsCSNYQvBbMV", | |
| "outputId": "23431bff-37fe-425b-8800-267ec4459fcd" | |
| }, | |
| "execution_count": 3, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stderr", | |
| "text": [ | |
| "(2023-10-15 06:32:44,243) WARNING :: Connected successfully to TrueData Real Time Data Service... (PID:224 Thread:132302802130496)\n", | |
| "WARNING:truedata_ws.websocket.TD:\u001b[22m\u001b[34mConnected successfully to TrueData Real Time Data Service... \u001b[0m\n", | |
| "(2023-10-15 06:32:44,334) WARNING :: Connected successfully to TrueData Historical Data Service... (PID:224 Thread:132306869886976)\n", | |
| "WARNING:truedata_ws.websocket.TD:\u001b[22m\u001b[34mConnected successfully to TrueData Historical Data Service... \u001b[0m\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def prepare_model_input(data, lookback):\n", | |
| " data_raw = data.to_numpy() # convert to numpy array\n", | |
| " data_sequences = []\n", | |
| "\n", | |
| " # create all possible sequences of length lookback\n", | |
| " for index in range(len(data_raw) - lookback):\n", | |
| " data_sequences.append(data_raw[index: index + lookback])\n", | |
| "\n", | |
| " data_sequences = np.array(data_sequences)\n", | |
| "\n", | |
| " return data_sequences" | |
| ], | |
| "metadata": { | |
| "id": "M1UA28n_BbKV" | |
| }, | |
| "execution_count": 4, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "lookback = 20\n", | |
| "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")" | |
| ], | |
| "metadata": { | |
| "id": "13PV80jjBbFN" | |
| }, | |
| "execution_count": 6, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "from google.colab import drive\n", | |
| "drive.mount('/content/drive')" | |
| ], | |
| "metadata": { | |
| "id": "83-5pX4F8c5C", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "outputId": "3a7f77ff-e495-4dde-d280-adb80609f20f" | |
| }, | |
| "execution_count": 7, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "Mounted at /content/drive\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "metadata": { | |
| "trusted": true, | |
| "id": "jXZYNVoK-V6c" | |
| }, | |
| "cell_type": "code", | |
| "source": [ | |
| "class LSTM(nn.Module):\n", | |
| " def __init__(self, input_dim, hidden_dim, num_layers, output_dim):\n", | |
| " super(LSTM, self).__init__()\n", | |
| " self.hidden_dim = hidden_dim\n", | |
| " self.num_layers = num_layers\n", | |
| "\n", | |
| " # Use torch.double here\n", | |
| " self.lstm = nn.LSTM(input_dim, hidden_dim, num_layers, batch_first=True).to(torch.double)\n", | |
| " self.fc = nn.Linear(hidden_dim, output_dim).to(torch.double)\n", | |
| "\n", | |
| " def forward(self, x):\n", | |
| " # Explicitly set the data type for initial hidden and cell states\n", | |
| " h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim, dtype=torch.double).requires_grad_().to(device)\n", | |
| " c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim, dtype=torch.double).requires_grad_().to(device)\n", | |
| " out, (hn, cn) = self.lstm(x, (h0.detach(), c0.detach()))\n", | |
| " out = self.fc(out[:, -1, :])\n", | |
| " return out\n" | |
| ], | |
| "execution_count": 9, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "model = torch.load('./drive/MyDrive/Pretrained_Models/nsebank_new_no.pt', map_location=torch.device('cpu'))\n", | |
| "model.eval()" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "UYwpYdxVjF9k", | |
| "outputId": "19bbfff6-c035-48f3-b33c-c2fd0fc6c64a" | |
| }, | |
| "execution_count": 28, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "LSTM(\n", | |
| " (lstm): LSTM(6, 128, num_layers=4, batch_first=True)\n", | |
| " (fc): Linear(in_features=128, out_features=1, bias=True)\n", | |
| ")" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 28 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "td_hist_data = td_obj.get_historic_data('NIFTY BANK', duration='60 D', bar_size='eod') # NIFTY 50 # NIFTY BANK # NIFTY FIN SERVICE" | |
| ], | |
| "metadata": { | |
| "id": "a90lcGZUclEQ" | |
| }, | |
| "execution_count": 29, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "td_df = pd.DataFrame(td_hist_data)\n", | |
| "td_df.rename(columns={'time': 'Date', 'o': 'Open', 'h': 'High', 'l': 'Low', 'v': 'Volume', 'c': 'Close'}, inplace=True)\n", | |
| "td_df.drop(columns=['oi'], inplace=True)\n", | |
| "td_df['NextOpen'] = td_df['Open'].shift(-1)" | |
| ], | |
| "metadata": { | |
| "id": "7PPTr---ILPT" | |
| }, | |
| "execution_count": 30, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "td_df.tail()" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 206 | |
| }, | |
| "id": "8Z4JUunaXBDy", | |
| "outputId": "25ac5483-2a62-4c59-9f76-ae9d3bd3ed13" | |
| }, | |
| "execution_count": 31, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| " Date Open High Low Close Volume NextOpen\n", | |
| "35 2023-10-09 44057.80 44113.20 43796.75 43886.50 0 44027.55\n", | |
| "36 2023-10-10 44027.55 44487.25 44004.55 44360.15 0 44554.90\n", | |
| "37 2023-10-11 44554.90 44710.55 44411.40 44516.90 0 44571.55\n", | |
| "38 2023-10-12 44571.55 44693.05 44530.05 44599.20 0 44322.05\n", | |
| "39 2023-10-13 44322.05 44563.05 44203.70 44287.95 0 NaN" | |
| ], | |
| "text/html": [ | |
| "\n", | |
| " <div id=\"df-d9e3aaa9-f376-4b50-99ba-cee101dd9de1\" class=\"colab-df-container\">\n", | |
| " <div>\n", | |
| "<style scoped>\n", | |
| " .dataframe tbody tr th:only-of-type {\n", | |
| " vertical-align: middle;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>Date</th>\n", | |
| " <th>Open</th>\n", | |
| " <th>High</th>\n", | |
| " <th>Low</th>\n", | |
| " <th>Close</th>\n", | |
| " <th>Volume</th>\n", | |
| " <th>NextOpen</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>35</th>\n", | |
| " <td>2023-10-09</td>\n", | |
| " <td>44057.80</td>\n", | |
| " <td>44113.20</td>\n", | |
| " <td>43796.75</td>\n", | |
| " <td>43886.50</td>\n", | |
| " <td>0</td>\n", | |
| " <td>44027.55</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>36</th>\n", | |
| " <td>2023-10-10</td>\n", | |
| " <td>44027.55</td>\n", | |
| " <td>44487.25</td>\n", | |
| " <td>44004.55</td>\n", | |
| " <td>44360.15</td>\n", | |
| " <td>0</td>\n", | |
| " <td>44554.90</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>37</th>\n", | |
| " <td>2023-10-11</td>\n", | |
| " <td>44554.90</td>\n", | |
| " <td>44710.55</td>\n", | |
| " <td>44411.40</td>\n", | |
| " <td>44516.90</td>\n", | |
| " <td>0</td>\n", | |
| " <td>44571.55</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>38</th>\n", | |
| " <td>2023-10-12</td>\n", | |
| " <td>44571.55</td>\n", | |
| " <td>44693.05</td>\n", | |
| " <td>44530.05</td>\n", | |
| " <td>44599.20</td>\n", | |
| " <td>0</td>\n", | |
| " <td>44322.05</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>39</th>\n", | |
| " <td>2023-10-13</td>\n", | |
| " <td>44322.05</td>\n", | |
| " <td>44563.05</td>\n", | |
| " <td>44203.70</td>\n", | |
| " <td>44287.95</td>\n", | |
| " <td>0</td>\n", | |
| " <td>NaN</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>\n", | |
| " <div class=\"colab-df-buttons\">\n", | |
| "\n", | |
| " <div class=\"colab-df-container\">\n", | |
| " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-d9e3aaa9-f376-4b50-99ba-cee101dd9de1')\"\n", | |
| " title=\"Convert this dataframe to an interactive table.\"\n", | |
| " style=\"display:none;\">\n", | |
| "\n", | |
| " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n", | |
| " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n", | |
| " </svg>\n", | |
| " </button>\n", | |
| "\n", | |
| " <style>\n", | |
| " .colab-df-container {\n", | |
| " display:flex;\n", | |
| " gap: 12px;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-convert {\n", | |
| " background-color: #E8F0FE;\n", | |
| " border: none;\n", | |
| " border-radius: 50%;\n", | |
| " cursor: pointer;\n", | |
| " display: none;\n", | |
| " fill: #1967D2;\n", | |
| " height: 32px;\n", | |
| " padding: 0 0 0 0;\n", | |
| " width: 32px;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-convert:hover {\n", | |
| " background-color: #E2EBFA;\n", | |
| " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n", | |
| " fill: #174EA6;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-buttons div {\n", | |
| " margin-bottom: 4px;\n", | |
| " }\n", | |
| "\n", | |
| " [theme=dark] .colab-df-convert {\n", | |
| " background-color: #3B4455;\n", | |
| " fill: #D2E3FC;\n", | |
| " }\n", | |
| "\n", | |
| " [theme=dark] .colab-df-convert:hover {\n", | |
| " background-color: #434B5C;\n", | |
| " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n", | |
| " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n", | |
| " fill: #FFFFFF;\n", | |
| " }\n", | |
| " </style>\n", | |
| "\n", | |
| " <script>\n", | |
| " const buttonEl =\n", | |
| " document.querySelector('#df-d9e3aaa9-f376-4b50-99ba-cee101dd9de1 button.colab-df-convert');\n", | |
| " buttonEl.style.display =\n", | |
| " google.colab.kernel.accessAllowed ? 'block' : 'none';\n", | |
| "\n", | |
| " async function convertToInteractive(key) {\n", | |
| " const element = document.querySelector('#df-d9e3aaa9-f376-4b50-99ba-cee101dd9de1');\n", | |
| " const dataTable =\n", | |
| " await google.colab.kernel.invokeFunction('convertToInteractive',\n", | |
| " [key], {});\n", | |
| " if (!dataTable) return;\n", | |
| "\n", | |
| " const docLinkHtml = 'Like what you see? Visit the ' +\n", | |
| " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n", | |
| " + ' to learn more about interactive tables.';\n", | |
| " element.innerHTML = '';\n", | |
| " dataTable['output_type'] = 'display_data';\n", | |
| " await google.colab.output.renderOutput(dataTable, element);\n", | |
| " const docLink = document.createElement('div');\n", | |
| " docLink.innerHTML = docLinkHtml;\n", | |
| " element.appendChild(docLink);\n", | |
| " }\n", | |
| " </script>\n", | |
| " </div>\n", | |
| "\n", | |
| "\n", | |
| "<div id=\"df-31ecc5d1-9de8-4f52-8490-a1a73e62696a\">\n", | |
| " <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-31ecc5d1-9de8-4f52-8490-a1a73e62696a')\"\n", | |
| " title=\"Suggest charts.\"\n", | |
| " style=\"display:none;\">\n", | |
| "\n", | |
| "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n", | |
| " width=\"24px\">\n", | |
| " <g>\n", | |
| " <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n", | |
| " </g>\n", | |
| "</svg>\n", | |
| " </button>\n", | |
| "\n", | |
| "<style>\n", | |
| " .colab-df-quickchart {\n", | |
| " --bg-color: #E8F0FE;\n", | |
| " --fill-color: #1967D2;\n", | |
| " --hover-bg-color: #E2EBFA;\n", | |
| " --hover-fill-color: #174EA6;\n", | |
| " --disabled-fill-color: #AAA;\n", | |
| " --disabled-bg-color: #DDD;\n", | |
| " }\n", | |
| "\n", | |
| " [theme=dark] .colab-df-quickchart {\n", | |
| " --bg-color: #3B4455;\n", | |
| " --fill-color: #D2E3FC;\n", | |
| " --hover-bg-color: #434B5C;\n", | |
| " --hover-fill-color: #FFFFFF;\n", | |
| " --disabled-bg-color: #3B4455;\n", | |
| " --disabled-fill-color: #666;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-quickchart {\n", | |
| " background-color: var(--bg-color);\n", | |
| " border: none;\n", | |
| " border-radius: 50%;\n", | |
| " cursor: pointer;\n", | |
| " display: none;\n", | |
| " fill: var(--fill-color);\n", | |
| " height: 32px;\n", | |
| " padding: 0;\n", | |
| " width: 32px;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-quickchart:hover {\n", | |
| " background-color: var(--hover-bg-color);\n", | |
| " box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n", | |
| " fill: var(--button-hover-fill-color);\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-quickchart-complete:disabled,\n", | |
| " .colab-df-quickchart-complete:disabled:hover {\n", | |
| " background-color: var(--disabled-bg-color);\n", | |
| " fill: var(--disabled-fill-color);\n", | |
| " box-shadow: none;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-spinner {\n", | |
| " border: 2px solid var(--fill-color);\n", | |
| " border-color: transparent;\n", | |
| " border-bottom-color: var(--fill-color);\n", | |
| " animation:\n", | |
| " spin 1s steps(1) infinite;\n", | |
| " }\n", | |
| "\n", | |
| " @keyframes spin {\n", | |
| " 0% {\n", | |
| " border-color: transparent;\n", | |
| " border-bottom-color: var(--fill-color);\n", | |
| " border-left-color: var(--fill-color);\n", | |
| " }\n", | |
| " 20% {\n", | |
| " border-color: transparent;\n", | |
| " border-left-color: var(--fill-color);\n", | |
| " border-top-color: var(--fill-color);\n", | |
| " }\n", | |
| " 30% {\n", | |
| " border-color: transparent;\n", | |
| " border-left-color: var(--fill-color);\n", | |
| " border-top-color: var(--fill-color);\n", | |
| " border-right-color: var(--fill-color);\n", | |
| " }\n", | |
| " 40% {\n", | |
| " border-color: transparent;\n", | |
| " border-right-color: var(--fill-color);\n", | |
| " border-top-color: var(--fill-color);\n", | |
| " }\n", | |
| " 60% {\n", | |
| " border-color: transparent;\n", | |
| " border-right-color: var(--fill-color);\n", | |
| " }\n", | |
| " 80% {\n", | |
| " border-color: transparent;\n", | |
| " border-right-color: var(--fill-color);\n", | |
| " border-bottom-color: var(--fill-color);\n", | |
| " }\n", | |
| " 90% {\n", | |
| " border-color: transparent;\n", | |
| " border-bottom-color: var(--fill-color);\n", | |
| " }\n", | |
| " }\n", | |
| "</style>\n", | |
| "\n", | |
| " <script>\n", | |
| " async function quickchart(key) {\n", | |
| " const quickchartButtonEl =\n", | |
| " document.querySelector('#' + key + ' button');\n", | |
| " quickchartButtonEl.disabled = true; // To prevent multiple clicks.\n", | |
| " quickchartButtonEl.classList.add('colab-df-spinner');\n", | |
| " try {\n", | |
| " const charts = await google.colab.kernel.invokeFunction(\n", | |
| " 'suggestCharts', [key], {});\n", | |
| " } catch (error) {\n", | |
| " console.error('Error during call to suggestCharts:', error);\n", | |
| " }\n", | |
| " quickchartButtonEl.classList.remove('colab-df-spinner');\n", | |
| " quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n", | |
| " }\n", | |
| " (() => {\n", | |
| " let quickchartButtonEl =\n", | |
| " document.querySelector('#df-31ecc5d1-9de8-4f52-8490-a1a73e62696a button');\n", | |
| " quickchartButtonEl.style.display =\n", | |
| " google.colab.kernel.accessAllowed ? 'block' : 'none';\n", | |
| " })();\n", | |
| " </script>\n", | |
| "</div>\n", | |
| " </div>\n", | |
| " </div>\n" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 31 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "scaler_train = StandardScaler()\n", | |
| "scaler_test = StandardScaler()\n", | |
| "\n", | |
| "features_td = td_df[['High', 'Low', 'Open', 'Volume', 'NextOpen', 'Close']]\n", | |
| "scaler_test.fit_transform(td_df[['Close']])\n", | |
| "features_td_scaled = scaler_train.fit_transform(features_td)" | |
| ], | |
| "metadata": { | |
| "id": "VA3YyHb80lcW" | |
| }, | |
| "execution_count": 41, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "# Combine normalized features_td with other columns\n", | |
| "data_scaled_td = pd.DataFrame(features_td_scaled, columns=['High', 'Low', 'Open', 'Close', 'NextOpen', 'Volume'])\n", | |
| "data_scaled_td['Date'] = td_df['Date']\n", | |
| "data_scaled_td = data_scaled_td.set_index('Date')\n", | |
| "data_scaled_td = data_scaled_td.sort_values('Date')" | |
| ], | |
| "metadata": { | |
| "id": "7XVXzIuO0wew" | |
| }, | |
| "execution_count": 42, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "print(data_scaled_td.shape)\n", | |
| "data_scaled_td.tail()" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 255 | |
| }, | |
| "id": "_V2S7q3Y1TMv", | |
| "outputId": "b8514bbb-b520-46a4-a56d-78b03e1c5e97" | |
| }, | |
| "execution_count": 43, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "(40, 6)\n" | |
| ] | |
| }, | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| " High Low Open Close NextOpen Volume\n", | |
| "Date \n", | |
| "2023-10-09 -1.184121 -1.079462 -0.961895 0.0 -1.050157 -1.224945\n", | |
| "2023-10-10 -0.576613 -0.726399 -1.011465 0.0 -0.179975 -0.450382\n", | |
| "2023-10-11 -0.213943 -0.035141 -0.147318 0.0 -0.152501 -0.194048\n", | |
| "2023-10-12 -0.242366 0.166452 -0.120034 0.0 -0.564202 -0.059462\n", | |
| "2023-10-13 -0.453503 -0.388033 -0.528879 0.0 NaN -0.568451" | |
| ], | |
| "text/html": [ | |
| "\n", | |
| " <div id=\"df-59eca1b3-e3df-4d5a-ac31-82917404259a\" class=\"colab-df-container\">\n", | |
| " <div>\n", | |
| "<style scoped>\n", | |
| " .dataframe tbody tr th:only-of-type {\n", | |
| " vertical-align: middle;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>High</th>\n", | |
| " <th>Low</th>\n", | |
| " <th>Open</th>\n", | |
| " <th>Close</th>\n", | |
| " <th>NextOpen</th>\n", | |
| " <th>Volume</th>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>Date</th>\n", | |
| " <th></th>\n", | |
| " <th></th>\n", | |
| " <th></th>\n", | |
| " <th></th>\n", | |
| " <th></th>\n", | |
| " <th></th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>2023-10-09</th>\n", | |
| " <td>-1.184121</td>\n", | |
| " <td>-1.079462</td>\n", | |
| " <td>-0.961895</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>-1.050157</td>\n", | |
| " <td>-1.224945</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2023-10-10</th>\n", | |
| " <td>-0.576613</td>\n", | |
| " <td>-0.726399</td>\n", | |
| " <td>-1.011465</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>-0.179975</td>\n", | |
| " <td>-0.450382</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2023-10-11</th>\n", | |
| " <td>-0.213943</td>\n", | |
| " <td>-0.035141</td>\n", | |
| " <td>-0.147318</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>-0.152501</td>\n", | |
| " <td>-0.194048</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2023-10-12</th>\n", | |
| " <td>-0.242366</td>\n", | |
| " <td>0.166452</td>\n", | |
| " <td>-0.120034</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>-0.564202</td>\n", | |
| " <td>-0.059462</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2023-10-13</th>\n", | |
| " <td>-0.453503</td>\n", | |
| " <td>-0.388033</td>\n", | |
| " <td>-0.528879</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>NaN</td>\n", | |
| " <td>-0.568451</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>\n", | |
| " <div class=\"colab-df-buttons\">\n", | |
| "\n", | |
| " <div class=\"colab-df-container\">\n", | |
| " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-59eca1b3-e3df-4d5a-ac31-82917404259a')\"\n", | |
| " title=\"Convert this dataframe to an interactive table.\"\n", | |
| " style=\"display:none;\">\n", | |
| "\n", | |
| " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n", | |
| " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n", | |
| " </svg>\n", | |
| " </button>\n", | |
| "\n", | |
| " <style>\n", | |
| " .colab-df-container {\n", | |
| " display:flex;\n", | |
| " gap: 12px;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-convert {\n", | |
| " background-color: #E8F0FE;\n", | |
| " border: none;\n", | |
| " border-radius: 50%;\n", | |
| " cursor: pointer;\n", | |
| " display: none;\n", | |
| " fill: #1967D2;\n", | |
| " height: 32px;\n", | |
| " padding: 0 0 0 0;\n", | |
| " width: 32px;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-convert:hover {\n", | |
| " background-color: #E2EBFA;\n", | |
| " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n", | |
| " fill: #174EA6;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-buttons div {\n", | |
| " margin-bottom: 4px;\n", | |
| " }\n", | |
| "\n", | |
| " [theme=dark] .colab-df-convert {\n", | |
| " background-color: #3B4455;\n", | |
| " fill: #D2E3FC;\n", | |
| " }\n", | |
| "\n", | |
| " [theme=dark] .colab-df-convert:hover {\n", | |
| " background-color: #434B5C;\n", | |
| " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n", | |
| " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n", | |
| " fill: #FFFFFF;\n", | |
| " }\n", | |
| " </style>\n", | |
| "\n", | |
| " <script>\n", | |
| " const buttonEl =\n", | |
| " document.querySelector('#df-59eca1b3-e3df-4d5a-ac31-82917404259a button.colab-df-convert');\n", | |
| " buttonEl.style.display =\n", | |
| " google.colab.kernel.accessAllowed ? 'block' : 'none';\n", | |
| "\n", | |
| " async function convertToInteractive(key) {\n", | |
| " const element = document.querySelector('#df-59eca1b3-e3df-4d5a-ac31-82917404259a');\n", | |
| " const dataTable =\n", | |
| " await google.colab.kernel.invokeFunction('convertToInteractive',\n", | |
| " [key], {});\n", | |
| " if (!dataTable) return;\n", | |
| "\n", | |
| " const docLinkHtml = 'Like what you see? Visit the ' +\n", | |
| " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n", | |
| " + ' to learn more about interactive tables.';\n", | |
| " element.innerHTML = '';\n", | |
| " dataTable['output_type'] = 'display_data';\n", | |
| " await google.colab.output.renderOutput(dataTable, element);\n", | |
| " const docLink = document.createElement('div');\n", | |
| " docLink.innerHTML = docLinkHtml;\n", | |
| " element.appendChild(docLink);\n", | |
| " }\n", | |
| " </script>\n", | |
| " </div>\n", | |
| "\n", | |
| "\n", | |
| "<div id=\"df-b5bca3fa-4122-4e82-b282-6b09e794ed36\">\n", | |
| " <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-b5bca3fa-4122-4e82-b282-6b09e794ed36')\"\n", | |
| " title=\"Suggest charts.\"\n", | |
| " style=\"display:none;\">\n", | |
| "\n", | |
| "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n", | |
| " width=\"24px\">\n", | |
| " <g>\n", | |
| " <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n", | |
| " </g>\n", | |
| "</svg>\n", | |
| " </button>\n", | |
| "\n", | |
| "<style>\n", | |
| " .colab-df-quickchart {\n", | |
| " --bg-color: #E8F0FE;\n", | |
| " --fill-color: #1967D2;\n", | |
| " --hover-bg-color: #E2EBFA;\n", | |
| " --hover-fill-color: #174EA6;\n", | |
| " --disabled-fill-color: #AAA;\n", | |
| " --disabled-bg-color: #DDD;\n", | |
| " }\n", | |
| "\n", | |
| " [theme=dark] .colab-df-quickchart {\n", | |
| " --bg-color: #3B4455;\n", | |
| " --fill-color: #D2E3FC;\n", | |
| " --hover-bg-color: #434B5C;\n", | |
| " --hover-fill-color: #FFFFFF;\n", | |
| " --disabled-bg-color: #3B4455;\n", | |
| " --disabled-fill-color: #666;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-quickchart {\n", | |
| " background-color: var(--bg-color);\n", | |
| " border: none;\n", | |
| " border-radius: 50%;\n", | |
| " cursor: pointer;\n", | |
| " display: none;\n", | |
| " fill: var(--fill-color);\n", | |
| " height: 32px;\n", | |
| " padding: 0;\n", | |
| " width: 32px;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-quickchart:hover {\n", | |
| " background-color: var(--hover-bg-color);\n", | |
| " box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n", | |
| " fill: var(--button-hover-fill-color);\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-quickchart-complete:disabled,\n", | |
| " .colab-df-quickchart-complete:disabled:hover {\n", | |
| " background-color: var(--disabled-bg-color);\n", | |
| " fill: var(--disabled-fill-color);\n", | |
| " box-shadow: none;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-spinner {\n", | |
| " border: 2px solid var(--fill-color);\n", | |
| " border-color: transparent;\n", | |
| " border-bottom-color: var(--fill-color);\n", | |
| " animation:\n", | |
| " spin 1s steps(1) infinite;\n", | |
| " }\n", | |
| "\n", | |
| " @keyframes spin {\n", | |
| " 0% {\n", | |
| " border-color: transparent;\n", | |
| " border-bottom-color: var(--fill-color);\n", | |
| " border-left-color: var(--fill-color);\n", | |
| " }\n", | |
| " 20% {\n", | |
| " border-color: transparent;\n", | |
| " border-left-color: var(--fill-color);\n", | |
| " border-top-color: var(--fill-color);\n", | |
| " }\n", | |
| " 30% {\n", | |
| " border-color: transparent;\n", | |
| " border-left-color: var(--fill-color);\n", | |
| " border-top-color: var(--fill-color);\n", | |
| " border-right-color: var(--fill-color);\n", | |
| " }\n", | |
| " 40% {\n", | |
| " border-color: transparent;\n", | |
| " border-right-color: var(--fill-color);\n", | |
| " border-top-color: var(--fill-color);\n", | |
| " }\n", | |
| " 60% {\n", | |
| " border-color: transparent;\n", | |
| " border-right-color: var(--fill-color);\n", | |
| " }\n", | |
| " 80% {\n", | |
| " border-color: transparent;\n", | |
| " border-right-color: var(--fill-color);\n", | |
| " border-bottom-color: var(--fill-color);\n", | |
| " }\n", | |
| " 90% {\n", | |
| " border-color: transparent;\n", | |
| " border-bottom-color: var(--fill-color);\n", | |
| " }\n", | |
| " }\n", | |
| "</style>\n", | |
| "\n", | |
| " <script>\n", | |
| " async function quickchart(key) {\n", | |
| " const quickchartButtonEl =\n", | |
| " document.querySelector('#' + key + ' button');\n", | |
| " quickchartButtonEl.disabled = true; // To prevent multiple clicks.\n", | |
| " quickchartButtonEl.classList.add('colab-df-spinner');\n", | |
| " try {\n", | |
| " const charts = await google.colab.kernel.invokeFunction(\n", | |
| " 'suggestCharts', [key], {});\n", | |
| " } catch (error) {\n", | |
| " console.error('Error during call to suggestCharts:', error);\n", | |
| " }\n", | |
| " quickchartButtonEl.classList.remove('colab-df-spinner');\n", | |
| " quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n", | |
| " }\n", | |
| " (() => {\n", | |
| " let quickchartButtonEl =\n", | |
| " document.querySelector('#df-b5bca3fa-4122-4e82-b282-6b09e794ed36 button');\n", | |
| " quickchartButtonEl.style.display =\n", | |
| " google.colab.kernel.accessAllowed ? 'block' : 'none';\n", | |
| " })();\n", | |
| " </script>\n", | |
| "</div>\n", | |
| " </div>\n", | |
| " </div>\n" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 43 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "data_scaled_td = data_scaled_td.iloc[:-1 , :]" | |
| ], | |
| "metadata": { | |
| "id": "cABkkPh-1TIP" | |
| }, | |
| "execution_count": 44, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "print(data_scaled_td.shape)\n", | |
| "data_scaled_td.tail()" | |
| ], | |
| "metadata": { | |
| "id": "iwpQz72mKODf", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 255 | |
| }, | |
| "outputId": "37e3a89c-316b-47cc-a75c-259bd8942be4" | |
| }, | |
| "execution_count": 45, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "(39, 6)\n" | |
| ] | |
| }, | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| " High Low Open Close NextOpen Volume\n", | |
| "Date \n", | |
| "2023-10-06 -0.554768 -0.321346 -0.408684 0.0 -1.000242 -0.449646\n", | |
| "2023-10-09 -1.184121 -1.079462 -0.961895 0.0 -1.050157 -1.224945\n", | |
| "2023-10-10 -0.576613 -0.726399 -1.011465 0.0 -0.179975 -0.450382\n", | |
| "2023-10-11 -0.213943 -0.035141 -0.147318 0.0 -0.152501 -0.194048\n", | |
| "2023-10-12 -0.242366 0.166452 -0.120034 0.0 -0.564202 -0.059462" | |
| ], | |
| "text/html": [ | |
| "\n", | |
| " <div id=\"df-0300802c-d535-411a-a83a-80b793a2bd91\" class=\"colab-df-container\">\n", | |
| " <div>\n", | |
| "<style scoped>\n", | |
| " .dataframe tbody tr th:only-of-type {\n", | |
| " vertical-align: middle;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>High</th>\n", | |
| " <th>Low</th>\n", | |
| " <th>Open</th>\n", | |
| " <th>Close</th>\n", | |
| " <th>NextOpen</th>\n", | |
| " <th>Volume</th>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>Date</th>\n", | |
| " <th></th>\n", | |
| " <th></th>\n", | |
| " <th></th>\n", | |
| " <th></th>\n", | |
| " <th></th>\n", | |
| " <th></th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>2023-10-06</th>\n", | |
| " <td>-0.554768</td>\n", | |
| " <td>-0.321346</td>\n", | |
| " <td>-0.408684</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>-1.000242</td>\n", | |
| " <td>-0.449646</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2023-10-09</th>\n", | |
| " <td>-1.184121</td>\n", | |
| " <td>-1.079462</td>\n", | |
| " <td>-0.961895</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>-1.050157</td>\n", | |
| " <td>-1.224945</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2023-10-10</th>\n", | |
| " <td>-0.576613</td>\n", | |
| " <td>-0.726399</td>\n", | |
| " <td>-1.011465</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>-0.179975</td>\n", | |
| " <td>-0.450382</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2023-10-11</th>\n", | |
| " <td>-0.213943</td>\n", | |
| " <td>-0.035141</td>\n", | |
| " <td>-0.147318</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>-0.152501</td>\n", | |
| " <td>-0.194048</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2023-10-12</th>\n", | |
| " <td>-0.242366</td>\n", | |
| " <td>0.166452</td>\n", | |
| " <td>-0.120034</td>\n", | |
| " <td>0.0</td>\n", | |
| " <td>-0.564202</td>\n", | |
| " <td>-0.059462</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>\n", | |
| " <div class=\"colab-df-buttons\">\n", | |
| "\n", | |
| " <div class=\"colab-df-container\">\n", | |
| " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-0300802c-d535-411a-a83a-80b793a2bd91')\"\n", | |
| " title=\"Convert this dataframe to an interactive table.\"\n", | |
| " style=\"display:none;\">\n", | |
| "\n", | |
| " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n", | |
| " <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n", | |
| " </svg>\n", | |
| " </button>\n", | |
| "\n", | |
| " <style>\n", | |
| " .colab-df-container {\n", | |
| " display:flex;\n", | |
| " gap: 12px;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-convert {\n", | |
| " background-color: #E8F0FE;\n", | |
| " border: none;\n", | |
| " border-radius: 50%;\n", | |
| " cursor: pointer;\n", | |
| " display: none;\n", | |
| " fill: #1967D2;\n", | |
| " height: 32px;\n", | |
| " padding: 0 0 0 0;\n", | |
| " width: 32px;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-convert:hover {\n", | |
| " background-color: #E2EBFA;\n", | |
| " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n", | |
| " fill: #174EA6;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-buttons div {\n", | |
| " margin-bottom: 4px;\n", | |
| " }\n", | |
| "\n", | |
| " [theme=dark] .colab-df-convert {\n", | |
| " background-color: #3B4455;\n", | |
| " fill: #D2E3FC;\n", | |
| " }\n", | |
| "\n", | |
| " [theme=dark] .colab-df-convert:hover {\n", | |
| " background-color: #434B5C;\n", | |
| " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n", | |
| " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n", | |
| " fill: #FFFFFF;\n", | |
| " }\n", | |
| " </style>\n", | |
| "\n", | |
| " <script>\n", | |
| " const buttonEl =\n", | |
| " document.querySelector('#df-0300802c-d535-411a-a83a-80b793a2bd91 button.colab-df-convert');\n", | |
| " buttonEl.style.display =\n", | |
| " google.colab.kernel.accessAllowed ? 'block' : 'none';\n", | |
| "\n", | |
| " async function convertToInteractive(key) {\n", | |
| " const element = document.querySelector('#df-0300802c-d535-411a-a83a-80b793a2bd91');\n", | |
| " const dataTable =\n", | |
| " await google.colab.kernel.invokeFunction('convertToInteractive',\n", | |
| " [key], {});\n", | |
| " if (!dataTable) return;\n", | |
| "\n", | |
| " const docLinkHtml = 'Like what you see? Visit the ' +\n", | |
| " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n", | |
| " + ' to learn more about interactive tables.';\n", | |
| " element.innerHTML = '';\n", | |
| " dataTable['output_type'] = 'display_data';\n", | |
| " await google.colab.output.renderOutput(dataTable, element);\n", | |
| " const docLink = document.createElement('div');\n", | |
| " docLink.innerHTML = docLinkHtml;\n", | |
| " element.appendChild(docLink);\n", | |
| " }\n", | |
| " </script>\n", | |
| " </div>\n", | |
| "\n", | |
| "\n", | |
| "<div id=\"df-1c025464-8902-45f7-8da2-bb4195494820\">\n", | |
| " <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-1c025464-8902-45f7-8da2-bb4195494820')\"\n", | |
| " title=\"Suggest charts.\"\n", | |
| " style=\"display:none;\">\n", | |
| "\n", | |
| "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n", | |
| " width=\"24px\">\n", | |
| " <g>\n", | |
| " <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n", | |
| " </g>\n", | |
| "</svg>\n", | |
| " </button>\n", | |
| "\n", | |
| "<style>\n", | |
| " .colab-df-quickchart {\n", | |
| " --bg-color: #E8F0FE;\n", | |
| " --fill-color: #1967D2;\n", | |
| " --hover-bg-color: #E2EBFA;\n", | |
| " --hover-fill-color: #174EA6;\n", | |
| " --disabled-fill-color: #AAA;\n", | |
| " --disabled-bg-color: #DDD;\n", | |
| " }\n", | |
| "\n", | |
| " [theme=dark] .colab-df-quickchart {\n", | |
| " --bg-color: #3B4455;\n", | |
| " --fill-color: #D2E3FC;\n", | |
| " --hover-bg-color: #434B5C;\n", | |
| " --hover-fill-color: #FFFFFF;\n", | |
| " --disabled-bg-color: #3B4455;\n", | |
| " --disabled-fill-color: #666;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-quickchart {\n", | |
| " background-color: var(--bg-color);\n", | |
| " border: none;\n", | |
| " border-radius: 50%;\n", | |
| " cursor: pointer;\n", | |
| " display: none;\n", | |
| " fill: var(--fill-color);\n", | |
| " height: 32px;\n", | |
| " padding: 0;\n", | |
| " width: 32px;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-quickchart:hover {\n", | |
| " background-color: var(--hover-bg-color);\n", | |
| " box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n", | |
| " fill: var(--button-hover-fill-color);\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-quickchart-complete:disabled,\n", | |
| " .colab-df-quickchart-complete:disabled:hover {\n", | |
| " background-color: var(--disabled-bg-color);\n", | |
| " fill: var(--disabled-fill-color);\n", | |
| " box-shadow: none;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-spinner {\n", | |
| " border: 2px solid var(--fill-color);\n", | |
| " border-color: transparent;\n", | |
| " border-bottom-color: var(--fill-color);\n", | |
| " animation:\n", | |
| " spin 1s steps(1) infinite;\n", | |
| " }\n", | |
| "\n", | |
| " @keyframes spin {\n", | |
| " 0% {\n", | |
| " border-color: transparent;\n", | |
| " border-bottom-color: var(--fill-color);\n", | |
| " border-left-color: var(--fill-color);\n", | |
| " }\n", | |
| " 20% {\n", | |
| " border-color: transparent;\n", | |
| " border-left-color: var(--fill-color);\n", | |
| " border-top-color: var(--fill-color);\n", | |
| " }\n", | |
| " 30% {\n", | |
| " border-color: transparent;\n", | |
| " border-left-color: var(--fill-color);\n", | |
| " border-top-color: var(--fill-color);\n", | |
| " border-right-color: var(--fill-color);\n", | |
| " }\n", | |
| " 40% {\n", | |
| " border-color: transparent;\n", | |
| " border-right-color: var(--fill-color);\n", | |
| " border-top-color: var(--fill-color);\n", | |
| " }\n", | |
| " 60% {\n", | |
| " border-color: transparent;\n", | |
| " border-right-color: var(--fill-color);\n", | |
| " }\n", | |
| " 80% {\n", | |
| " border-color: transparent;\n", | |
| " border-right-color: var(--fill-color);\n", | |
| " border-bottom-color: var(--fill-color);\n", | |
| " }\n", | |
| " 90% {\n", | |
| " border-color: transparent;\n", | |
| " border-bottom-color: var(--fill-color);\n", | |
| " }\n", | |
| " }\n", | |
| "</style>\n", | |
| "\n", | |
| " <script>\n", | |
| " async function quickchart(key) {\n", | |
| " const quickchartButtonEl =\n", | |
| " document.querySelector('#' + key + ' button');\n", | |
| " quickchartButtonEl.disabled = true; // To prevent multiple clicks.\n", | |
| " quickchartButtonEl.classList.add('colab-df-spinner');\n", | |
| " try {\n", | |
| " const charts = await google.colab.kernel.invokeFunction(\n", | |
| " 'suggestCharts', [key], {});\n", | |
| " } catch (error) {\n", | |
| " console.error('Error during call to suggestCharts:', error);\n", | |
| " }\n", | |
| " quickchartButtonEl.classList.remove('colab-df-spinner');\n", | |
| " quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n", | |
| " }\n", | |
| " (() => {\n", | |
| " let quickchartButtonEl =\n", | |
| " document.querySelector('#df-1c025464-8902-45f7-8da2-bb4195494820 button');\n", | |
| " quickchartButtonEl.style.display =\n", | |
| " google.colab.kernel.accessAllowed ? 'block' : 'none';\n", | |
| " })();\n", | |
| " </script>\n", | |
| "</div>\n", | |
| " </div>\n", | |
| " </div>\n" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 45 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "x_td = prepare_model_input(data_scaled_td, lookback)" | |
| ], | |
| "metadata": { | |
| "id": "ykcKq52qW8Om" | |
| }, | |
| "execution_count": 46, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "print(x_td.shape)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "Bg5JBUFu8XXs", | |
| "outputId": "daeaaf38-c7dc-4c31-d92a-682a718f4557" | |
| }, | |
| "execution_count": 47, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "(19, 20, 6)\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "x_td = torch.from_numpy(x_td).type(torch.double).to(device)\n", | |
| "x_td = x_td[-1, :, :].unsqueeze(0)" | |
| ], | |
| "metadata": { | |
| "id": "L98hxgk48ZAI" | |
| }, | |
| "execution_count": 48, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "# Make predictions on TrueData API data\n", | |
| "y_td_pred = model(x_td)\n", | |
| "\n", | |
| "# Invert predictions\n", | |
| "y_td_pred = scaler_test.inverse_transform(y_td_pred.detach().cpu().numpy())\n", | |
| "\n", | |
| "# Print or use predictions\n", | |
| "print('Predictions for TrueData API:')\n", | |
| "print(y_td_pred)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "bR5ZdYQU9A33", | |
| "outputId": "871b8dec-bb65-4a8f-9011-4ee4c26594c0" | |
| }, | |
| "execution_count": 49, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "Predictions for TrueData API:\n", | |
| "[[44341.87956096]]\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [], | |
| "metadata": { | |
| "id": "F_WbX8nLDvAg" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment