Skip to content

Instantly share code, notes, and snippets.

@moneebullah25
Created October 15, 2023 06:49
Show Gist options
  • Select an option

  • Save moneebullah25/8a03f2e09c11b2763e7f9daa58f6e678 to your computer and use it in GitHub Desktop.

Select an option

Save moneebullah25/8a03f2e09c11b2763e7f9daa58f6e678 to your computer and use it in GitHub Desktop.
stock_prediction_new_open_n50.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyOxNluMECuXMZfliHPMKx/v",
"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/8a03f2e09c11b2763e7f9daa58f6e678/stock_prediction_new_open_n50.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "L4plaxiKBQ9P",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "c05cd281-d544-4558-ca48-e6c60482f72a"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Requirement already satisfied: yfinance in /usr/local/lib/python3.10/dist-packages (0.2.31)\n",
"Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (1.23.5)\n",
"Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (1.5.3)\n",
"Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (3.7.1)\n",
"Requirement already satisfied: seaborn in /usr/local/lib/python3.10/dist-packages (0.12.2)\n",
"Collecting truedata_ws\n",
" Downloading truedata_ws-5.0.7-py3-none-any.whl (48 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m48.6/48.6 kB\u001b[0m \u001b[31m1.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: requests>=2.31 in /usr/local/lib/python3.10/dist-packages (from yfinance) (2.31.0)\n",
"Requirement already satisfied: multitasking>=0.0.7 in /usr/local/lib/python3.10/dist-packages (from yfinance) (0.0.11)\n",
"Requirement already satisfied: lxml>=4.9.1 in /usr/local/lib/python3.10/dist-packages (from yfinance) (4.9.3)\n",
"Requirement already satisfied: appdirs>=1.4.4 in /usr/local/lib/python3.10/dist-packages (from yfinance) (1.4.4)\n",
"Requirement already satisfied: pytz>=2022.5 in /usr/local/lib/python3.10/dist-packages (from yfinance) (2023.3.post1)\n",
"Requirement already satisfied: frozendict>=2.3.4 in /usr/local/lib/python3.10/dist-packages (from yfinance) (2.3.8)\n",
"Requirement already satisfied: peewee>=3.16.2 in /usr/local/lib/python3.10/dist-packages (from yfinance) (3.16.3)\n",
"Requirement already satisfied: beautifulsoup4>=4.11.1 in /usr/local/lib/python3.10/dist-packages (from yfinance) (4.11.2)\n",
"Requirement already satisfied: html5lib>=1.1 in /usr/local/lib/python3.10/dist-packages (from yfinance) (1.1)\n",
"Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.10/dist-packages (from pandas) (2.8.2)\n",
"Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (1.1.1)\n",
"Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (0.12.1)\n",
"Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (4.43.1)\n",
"Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (1.4.5)\n",
"Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (23.2)\n",
"Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (9.4.0)\n",
"Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (3.1.1)\n",
"Requirement already satisfied: websocket-client>=0.58.0 in /usr/local/lib/python3.10/dist-packages (from truedata_ws) (1.6.4)\n",
"Collecting colorama>=0.4.3 (from truedata_ws)\n",
" Downloading colorama-0.4.6-py2.py3-none-any.whl (25 kB)\n",
"Collecting lz4==3.1.3 (from truedata_ws)\n",
" Downloading lz4-3.1.3.tar.gz (159 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m159.0/159.0 kB\u001b[0m \u001b[31m7.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
"Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.10/dist-packages (from beautifulsoup4>=4.11.1->yfinance) (2.5)\n",
"Requirement already satisfied: six>=1.9 in /usr/local/lib/python3.10/dist-packages (from html5lib>=1.1->yfinance) (1.16.0)\n",
"Requirement already satisfied: webencodings in /usr/local/lib/python3.10/dist-packages (from html5lib>=1.1->yfinance) (0.5.1)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.31->yfinance) (3.3.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.31->yfinance) (3.4)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.31->yfinance) (2.0.6)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.31->yfinance) (2023.7.22)\n",
"Building wheels for collected packages: lz4\n",
" Building wheel for lz4 (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Created wheel for lz4: filename=lz4-3.1.3-cp310-cp310-linux_x86_64.whl size=1625844 sha256=f2ff48c9865682480cb5fa0e6012169d9f8c2d8f7d9dd4a7d7c53f1c56339462\n",
" Stored in directory: /root/.cache/pip/wheels/35/b3/74/142efee0b4a3cf0d6fc5f843654657354407256c19b71cd1a6\n",
"Successfully built lz4\n",
"Installing collected packages: lz4, colorama, truedata_ws\n",
"Successfully installed colorama-0.4.6 lz4-3.1.3 truedata_ws-5.0.7\n"
]
}
],
"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": "a4b4f366-f94c-45ad-a5b3-bd00de06e19b"
},
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"(2023-10-15 06:44:59,002) WARNING :: Connected successfully to TrueData Real Time Data Service... (PID:214 Thread:137299229021760)\n",
"WARNING:truedata_ws.websocket.TD:\u001b[22m\u001b[34mConnected successfully to TrueData Real Time Data Service... \u001b[0m\n",
"(2023-10-15 06:44:59,226) WARNING :: Connected successfully to TrueData Historical Data Service... (PID:214 Thread:137303218540544)\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": 5,
"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": "a20669d7-961f-460d-ace6-ecbdaa686d2f"
},
"execution_count": 8,
"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/nsei_new_no.pt', map_location=torch.device('cpu'))\n",
"model.eval()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "UYwpYdxVjF9k",
"outputId": "38368d2b-cb98-4400-9a48-800856e252ee"
},
"execution_count": 10,
"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": 10
}
]
},
{
"cell_type": "code",
"source": [
"td_hist_data = td_obj.get_historic_data('NIFTY 50', duration='60 D', bar_size='eod') # NIFTY 50 # NIFTY BANK # NIFTY FIN SERVICE"
],
"metadata": {
"id": "a90lcGZUclEQ"
},
"execution_count": 15,
"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": 16,
"outputs": []
},
{
"cell_type": "code",
"source": [
"td_df.tail()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 206
},
"id": "8Z4JUunaXBDy",
"outputId": "897f019f-115d-471a-e5f4-afd666bb8db2"
},
"execution_count": 17,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" Date Open High Low Close Volume NextOpen\n",
"35 2023-10-09 19539.45 19588.95 19480.50 19512.35 0 19565.60\n",
"36 2023-10-10 19565.60 19717.80 19565.45 19689.85 0 19767.00\n",
"37 2023-10-11 19767.00 19839.20 19756.95 19811.35 0 19822.70\n",
"38 2023-10-12 19822.70 19843.30 19772.65 19794.00 0 19654.55\n",
"39 2023-10-13 19654.55 19805.40 19635.30 19751.05 0 NaN"
],
"text/html": [
"\n",
" <div id=\"df-a1fca74e-915d-4784-8854-07507bec3bdd\" 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>19539.45</td>\n",
" <td>19588.95</td>\n",
" <td>19480.50</td>\n",
" <td>19512.35</td>\n",
" <td>0</td>\n",
" <td>19565.60</td>\n",
" </tr>\n",
" <tr>\n",
" <th>36</th>\n",
" <td>2023-10-10</td>\n",
" <td>19565.60</td>\n",
" <td>19717.80</td>\n",
" <td>19565.45</td>\n",
" <td>19689.85</td>\n",
" <td>0</td>\n",
" <td>19767.00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>37</th>\n",
" <td>2023-10-11</td>\n",
" <td>19767.00</td>\n",
" <td>19839.20</td>\n",
" <td>19756.95</td>\n",
" <td>19811.35</td>\n",
" <td>0</td>\n",
" <td>19822.70</td>\n",
" </tr>\n",
" <tr>\n",
" <th>38</th>\n",
" <td>2023-10-12</td>\n",
" <td>19822.70</td>\n",
" <td>19843.30</td>\n",
" <td>19772.65</td>\n",
" <td>19794.00</td>\n",
" <td>0</td>\n",
" <td>19654.55</td>\n",
" </tr>\n",
" <tr>\n",
" <th>39</th>\n",
" <td>2023-10-13</td>\n",
" <td>19654.55</td>\n",
" <td>19805.40</td>\n",
" <td>19635.30</td>\n",
" <td>19751.05</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-a1fca74e-915d-4784-8854-07507bec3bdd')\"\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-a1fca74e-915d-4784-8854-07507bec3bdd 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-a1fca74e-915d-4784-8854-07507bec3bdd');\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-a0de5ad3-d92b-4303-a5f8-24b8a3a5a063\">\n",
" <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-a0de5ad3-d92b-4303-a5f8-24b8a3a5a063')\"\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-a0de5ad3-d92b-4303-a5f8-24b8a3a5a063 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": 17
}
]
},
{
"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": 18,
"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": 19,
"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": "cc7ad97c-f0ef-47e2-859c-b91091530f9a"
},
"execution_count": 20,
"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 -0.427539 -0.334074 -0.391331 0.0 -0.303101 -0.473706\n",
"2023-10-10 0.091636 0.010020 -0.285472 0.0 0.507832 0.232650\n",
"2023-10-11 0.580793 0.785700 0.529829 0.0 0.732107 0.716155\n",
"2023-10-12 0.597313 0.849293 0.755312 0.0 0.055054 0.647112\n",
"2023-10-13 0.444603 0.292951 0.074613 0.0 NaN 0.476193"
],
"text/html": [
"\n",
" <div id=\"df-754213d8-ba0d-41b0-b8c5-2d095ddcf3b1\" 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>-0.427539</td>\n",
" <td>-0.334074</td>\n",
" <td>-0.391331</td>\n",
" <td>0.0</td>\n",
" <td>-0.303101</td>\n",
" <td>-0.473706</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-10-10</th>\n",
" <td>0.091636</td>\n",
" <td>0.010020</td>\n",
" <td>-0.285472</td>\n",
" <td>0.0</td>\n",
" <td>0.507832</td>\n",
" <td>0.232650</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-10-11</th>\n",
" <td>0.580793</td>\n",
" <td>0.785700</td>\n",
" <td>0.529829</td>\n",
" <td>0.0</td>\n",
" <td>0.732107</td>\n",
" <td>0.716155</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-10-12</th>\n",
" <td>0.597313</td>\n",
" <td>0.849293</td>\n",
" <td>0.755312</td>\n",
" <td>0.0</td>\n",
" <td>0.055054</td>\n",
" <td>0.647112</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-10-13</th>\n",
" <td>0.444603</td>\n",
" <td>0.292951</td>\n",
" <td>0.074613</td>\n",
" <td>0.0</td>\n",
" <td>NaN</td>\n",
" <td>0.476193</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-754213d8-ba0d-41b0-b8c5-2d095ddcf3b1')\"\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-754213d8-ba0d-41b0-b8c5-2d095ddcf3b1 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-754213d8-ba0d-41b0-b8c5-2d095ddcf3b1');\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-c7c62d6b-97f5-4dd9-90c6-2a4ae99578a0\">\n",
" <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-c7c62d6b-97f5-4dd9-90c6-2a4ae99578a0')\"\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-c7c62d6b-97f5-4dd9-90c6-2a4ae99578a0 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": 20
}
]
},
{
"cell_type": "code",
"source": [
"data_scaled_td = data_scaled_td.iloc[:-1 , :]"
],
"metadata": {
"id": "cABkkPh-1TIP"
},
"execution_count": 21,
"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": "a18e9f3b-72b7-4d0a-e855-d88d5dabc447"
},
"execution_count": 22,
"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.077796 0.107031 -0.060394 0.0 -0.408394 0.087996\n",
"2023-10-09 -0.427539 -0.334074 -0.391331 0.0 -0.303101 -0.473706\n",
"2023-10-10 0.091636 0.010020 -0.285472 0.0 0.507832 0.232650\n",
"2023-10-11 0.580793 0.785700 0.529829 0.0 0.732107 0.716155\n",
"2023-10-12 0.597313 0.849293 0.755312 0.0 0.055054 0.647112"
],
"text/html": [
"\n",
" <div id=\"df-8402536a-878b-4f57-81a9-db33b44ad9e9\" 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.077796</td>\n",
" <td>0.107031</td>\n",
" <td>-0.060394</td>\n",
" <td>0.0</td>\n",
" <td>-0.408394</td>\n",
" <td>0.087996</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-10-09</th>\n",
" <td>-0.427539</td>\n",
" <td>-0.334074</td>\n",
" <td>-0.391331</td>\n",
" <td>0.0</td>\n",
" <td>-0.303101</td>\n",
" <td>-0.473706</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-10-10</th>\n",
" <td>0.091636</td>\n",
" <td>0.010020</td>\n",
" <td>-0.285472</td>\n",
" <td>0.0</td>\n",
" <td>0.507832</td>\n",
" <td>0.232650</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-10-11</th>\n",
" <td>0.580793</td>\n",
" <td>0.785700</td>\n",
" <td>0.529829</td>\n",
" <td>0.0</td>\n",
" <td>0.732107</td>\n",
" <td>0.716155</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-10-12</th>\n",
" <td>0.597313</td>\n",
" <td>0.849293</td>\n",
" <td>0.755312</td>\n",
" <td>0.0</td>\n",
" <td>0.055054</td>\n",
" <td>0.647112</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-8402536a-878b-4f57-81a9-db33b44ad9e9')\"\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-8402536a-878b-4f57-81a9-db33b44ad9e9 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-8402536a-878b-4f57-81a9-db33b44ad9e9');\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-d3cfc6b7-678a-4ec5-ab69-092672d097b9\">\n",
" <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-d3cfc6b7-678a-4ec5-ab69-092672d097b9')\"\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-d3cfc6b7-678a-4ec5-ab69-092672d097b9 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": 22
}
]
},
{
"cell_type": "code",
"source": [
"x_td = prepare_model_input(data_scaled_td, lookback)"
],
"metadata": {
"id": "ykcKq52qW8Om"
},
"execution_count": 23,
"outputs": []
},
{
"cell_type": "code",
"source": [
"print(x_td.shape)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Bg5JBUFu8XXs",
"outputId": "820e93be-2d63-493f-b659-b4eafade5e9d"
},
"execution_count": 24,
"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": 25,
"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": "8f08658f-4f99-4bf2-ab43-00cc69816d9f"
},
"execution_count": 26,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Predictions for TrueData API:\n",
"[[19636.19590559]]\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