{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Predicting the next day stock value using LSTM and quantdata\n",
    "\n",
    "In this model the input value is opening value of day and target value is the closing value of that same day."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "945 945\n",
      "945\n"
     ]
    }
   ],
   "source": [
    "#open the file which you want predict the stock price for...\n",
    "#as we have minutely data with us we need firt create a dataframe which has data for day only..\n",
    "#we did this because for minutely data, huge processing is required...\n",
    "\n",
    "\n",
    "import pandas as pd\n",
    "df = pd.read_csv(\"Infosys.csv\",encoding = 'ISO-8859-1', index_col = 0)\n",
    "df1 = df[df.Time.str.contains('09:28')]\n",
    "df2 = df[df.Time.str.contains('15:28')]\n",
    "print(len(df1),len(df2))\n",
    "\n",
    "frame1 = [x for x in df1.Open]\n",
    "frame2 = [x for x in df2.Close]\n",
    "frame3 = [x for x in df1.Time]\n",
    "frame4 = [x for x in df2.High]\n",
    "frame5 = [x for x in df2.Low]\n",
    "df = pd.DataFrame({\n",
    "    \"Open\":frame1,\n",
    "    \"Close\":frame2,\n",
    "    \"Time\":frame3,\n",
    "    \"High\":frame4,\n",
    "    \"Low\":frame5\n",
    "})\n",
    "\n",
    "print(len(df))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import math\n",
    "import sklearn\n",
    "import sklearn.preprocessing\n",
    "import datetime\n",
    "import os\n",
    "import matplotlib.pyplot as plt\n",
    "import tensorflow as tf\n",
    "\n",
    "# split data in 80%/10%/10% train/validation/test sets\n",
    "valid_set_size_percentage = 10 \n",
    "test_set_size_percentage = 10 \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<bound method NDFrame.describe of        Close     High      Low     Open                      Time\n",
      "0     474.89   475.00   474.75   479.02  2014-03-03T09:28:00+0530\n",
      "1     474.88   475.12   474.71   475.12  2014-03-04T09:28:00+0530\n",
      "2     478.00   478.00   477.69   477.75  2014-03-05T09:28:00+0530\n",
      "3     479.51   479.71   479.14   480.47  2014-03-06T09:28:00+0530\n",
      "4     466.71   467.00   466.44   478.44  2014-03-07T09:28:00+0530\n",
      "5     459.63   459.88   459.45   461.10  2014-03-10T09:28:00+0530\n",
      "6     459.66   459.88   459.64   456.88  2014-03-11T09:28:00+0530\n",
      "7     459.40   459.50   459.04   461.29  2014-03-12T09:28:00+0530\n",
      "8     419.82   419.84   419.55   423.67  2014-03-13T09:28:00+0530\n",
      "9     423.99   424.00   423.78   423.70  2014-03-14T09:28:00+0530\n",
      "10    418.29   418.75   418.25   423.73  2014-03-18T09:28:00+0530\n",
      "11    409.05   409.09   409.02   410.04  2014-03-19T09:28:00+0530\n",
      "12    412.89   413.10   412.88   411.48  2014-03-20T09:28:00+0530\n",
      "13    414.02   414.58   413.75   413.75  2014-03-21T09:28:00+0530\n",
      "14    409.38   409.63   409.13   413.99  2014-03-24T09:28:00+0530\n",
      "15    406.49   406.50   406.25   407.73  2014-03-25T09:28:00+0530\n",
      "16    405.38   406.14   405.00   405.42  2014-03-26T09:28:00+0530\n",
      "17    403.75   403.75   402.63   405.17  2014-03-27T09:28:00+0530\n",
      "18    407.17   408.59   407.14   405.57  2014-03-28T09:28:00+0530\n",
      "19    414.25   414.25   413.77   413.84  2014-04-01T09:28:00+0530\n",
      "20    415.75   415.87   415.39   416.89  2014-04-02T09:28:00+0530\n",
      "21    417.58   417.70   417.31   416.44  2014-04-03T09:28:00+0530\n",
      "22    413.94   414.25   413.89   419.12  2014-04-04T09:28:00+0530\n",
      "23    410.75   411.88   410.00   413.25  2014-04-07T09:28:00+0530\n",
      "24    406.69   407.05   406.68   410.72  2014-04-09T09:28:00+0530\n",
      "25    399.63   399.97   399.44   405.17  2014-04-10T09:28:00+0530\n",
      "26    404.60   405.38   404.30   400.58  2014-04-11T09:28:00+0530\n",
      "27    408.76   408.88   408.75   416.98  2014-04-15T09:28:00+0530\n",
      "28    395.13   395.13   394.38   402.37  2014-04-16T09:28:00+0530\n",
      "29    398.53   399.00   398.27   396.65  2014-04-17T09:28:00+0530\n",
      "..       ...      ...      ...      ...                       ...\n",
      "915  1014.55  1015.00  1014.35  1007.75  2017-12-18T09:28:00+0530\n",
      "916  1000.00  1000.85   999.90  1015.30  2017-12-19T09:28:00+0530\n",
      "917  1016.30  1016.30  1015.25  1004.05  2017-12-20T09:28:00+0530\n",
      "918  1021.85  1022.40  1021.30  1003.95  2017-12-21T09:28:00+0530\n",
      "919  1037.50  1038.65  1037.20  1017.65  2017-12-22T09:28:00+0530\n",
      "920  1035.85  1036.30  1033.35  1024.00  2017-12-26T09:28:00+0530\n",
      "921  1034.75  1035.50  1034.15  1036.60  2017-12-27T09:28:00+0530\n",
      "922  1028.95  1033.25  1027.70  1035.60  2017-12-28T09:28:00+0530\n",
      "923  1042.20  1042.20  1041.00  1035.95  2017-12-29T09:28:00+0530\n",
      "924  1031.40  1031.85  1030.00  1041.25  2018-01-01T09:28:00+0530\n",
      "925  1029.20  1030.75  1029.10  1037.95  2018-01-02T09:28:00+0530\n",
      "926  1022.50  1022.50  1022.00  1025.00  2018-01-03T09:28:00+0530\n",
      "927  1015.05  1016.90  1014.80  1017.95  2018-01-04T09:28:00+0530\n",
      "928  1014.05  1014.95  1014.00  1015.65  2018-01-05T09:28:00+0530\n",
      "929  1033.95  1034.00  1033.10  1021.90  2018-01-08T09:28:00+0530\n",
      "930  1040.20  1040.40  1040.05  1039.00  2018-01-09T09:28:00+0530\n",
      "931  1053.95  1054.80  1053.50  1042.80  2018-01-10T09:28:00+0530\n",
      "932  1076.20  1076.50  1076.00  1062.85  2018-01-11T09:28:00+0530\n",
      "933  1078.70  1080.10  1078.05  1075.35  2018-01-12T09:28:00+0530\n",
      "934  1081.40  1082.40  1081.35  1081.45  2018-01-15T09:28:00+0530\n",
      "935  1123.65  1123.90  1122.30  1098.40  2018-01-16T09:28:00+0530\n",
      "936  1149.75  1151.05  1149.00  1153.80  2018-01-17T09:28:00+0530\n",
      "937  1150.95  1150.95  1149.20  1143.35  2018-01-18T09:28:00+0530\n",
      "938  1145.50  1145.50  1144.80  1140.20  2018-01-19T09:28:00+0530\n",
      "939  1152.00  1156.55  1151.95  1140.05  2018-01-22T09:28:00+0530\n",
      "940  1177.50  1178.45  1177.20  1170.00  2018-01-23T09:28:00+0530\n",
      "941  1182.85  1184.75  1182.85  1169.50  2018-01-24T09:28:00+0530\n",
      "942  1180.05  1182.60  1180.00  1168.50  2018-01-25T09:28:00+0530\n",
      "943  1181.75  1182.40  1180.00  1167.00  2018-01-29T09:28:00+0530\n",
      "944  1174.90  1175.05  1167.90  1178.90  2018-01-30T09:28:00+0530\n",
      "\n",
      "[945 rows x 5 columns]>\n"
     ]
    }
   ],
   "source": [
    "# import all stock prices \n",
    "\n",
    "print (df.describe)\n",
    "\n",
    "## number of different stocks\n",
    "# print('\\nnumber of different stocks: ', len(list(set(df.symbol))))\n",
    "# print(list(set(df.symbol))[:10])\n",
    "\n",
    "# df.tail()\n",
    "# df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x121bef710>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 26612 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x121b797b8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(15, 5));\n",
    "plt.subplot(1,2,1);\n",
    "plt.plot(df.Open.values, color='red', label='open')\n",
    "plt.plot(df.Close.values, color='green', label='close')\n",
    "# plt.plot(df.Low.values, color='blue', label='low')\n",
    "# plt.plot(df.High.values, color='black', label='high')\n",
    "plt.title('stock price')\n",
    "plt.xlabel('time [minutes]')\n",
    "plt.ylabel('price')\n",
    "plt.legend(loc='best')\n",
    "#plt.show()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "df_stock.columns.values =  ['Close', 'High', 'Low', 'Open']\n",
      "92 92\n",
      "925\n",
      "x_train.shape =  (741, 19, 4)\n",
      "y_train.shape =  (741, 4)\n",
      "x_valid.shape =  (92, 19, 4)\n",
      "y_valid.shape =  (92, 4)\n",
      "x_test.shape =  (92, 19, 4)\n",
      "y_test.shape =  (92, 4)\n",
      "741 741\n",
      "92 92\n",
      "92 92\n"
     ]
    }
   ],
   "source": [
    "# function for min-max normalization of stock\n",
    "def normalize_data(df):\n",
    "    min_max_scaler = sklearn.preprocessing.MinMaxScaler()\n",
    "    df['Open'] = min_max_scaler.fit_transform(df.Open.values.reshape(-1,1))\n",
    "    df['Close'] = min_max_scaler.fit_transform(df['Close'].values.reshape(-1,1))\n",
    "    df['High'] = min_max_scaler.fit_transform(df.High.values.reshape(-1,1))\n",
    "    df['Low'] = min_max_scaler.fit_transform(df['Low'].values.reshape(-1,1))\n",
    "    return df\n",
    "\n",
    "# function to create train, validation, test data given stock data and sequence length\n",
    "def load_data(stock, seq_len):\n",
    "    data_raw = stock.as_matrix() # convert to numpy array\n",
    "    data = []\n",
    "    \n",
    "    # create all possible sequences of length seq_len\n",
    "    for index in range(len(data_raw) - seq_len): \n",
    "        data.append(data_raw[index: index + seq_len])\n",
    "    \n",
    "    data = np.array(data);\n",
    "    valid_set_size = int(np.round(valid_set_size_percentage/100*data.shape[0]));  \n",
    "    test_set_size = int(np.round(test_set_size_percentage/100*data.shape[0]))\n",
    "    train_set_size = data.shape[0] - (valid_set_size + test_set_size)\n",
    "    print (valid_set_size,test_set_size)\n",
    "    print (data.shape[0])\n",
    "    \n",
    "    x_train = data[:train_set_size,:-1,:]\n",
    "    y_train = data[:train_set_size,-1,:]\n",
    "    \n",
    "    x_valid = data[train_set_size:train_set_size+valid_set_size,:-1,:]\n",
    "    y_valid = data[train_set_size:train_set_size+valid_set_size,-1,:]\n",
    "    \n",
    "    x_test = data[train_set_size+valid_set_size:,:-1,:]\n",
    "    y_test = data[train_set_size+valid_set_size:,-1,:]\n",
    "    \n",
    "    return [x_train, y_train, x_valid, y_valid, x_test, y_test]\n",
    "\n",
    "# choose one stock\n",
    "df_stock = df.copy()\n",
    "df_stock.drop(['Time'],1,inplace=True)\n",
    "\n",
    "cols = list(df_stock.columns.values)\n",
    "print('df_stock.columns.values = ', cols)\n",
    "\n",
    "# normalize stock\n",
    "df_stock_norm = df_stock.copy()\n",
    "df_stock_norm = normalize_data(df_stock_norm)\n",
    "\n",
    "# create train, test data\n",
    "seq_len = 20 # choose sequence length\n",
    "x_train, y_train, x_valid, y_valid, x_test, y_test = load_data(df_stock_norm, seq_len)\n",
    "print('x_train.shape = ',x_train.shape)\n",
    "print('y_train.shape = ', y_train.shape)\n",
    "print('x_valid.shape = ',x_valid.shape)\n",
    "print('y_valid.shape = ', y_valid.shape)\n",
    "print('x_test.shape = ', x_test.shape)\n",
    "print('y_test.shape = ',y_test.shape)\n",
    "\n",
    "print(len(x_train),len(y_train))\n",
    "print(len(x_test),len(y_test))\n",
    "print(len(x_valid),len(y_valid))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 43516 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x121b79080>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(15, 5));\n",
    "plt.plot(df_stock_norm.Open.values, color='red', label='open')\n",
    "plt.plot(df_stock_norm.Close.values, color='green', label='Close')\n",
    "# plt.plot(df_stock_norm.Low.values, color='blue', label='low')\n",
    "# plt.plot(df_stock_norm.High.values, color='black', label='high')\n",
    "#plt.plot(df_stock_norm.volume.values, color='gray', label='volume')\n",
    "plt.title('stock')\n",
    "plt.xlabel('time [Days]')\n",
    "plt.ylabel('normalized price')\n",
    "plt.legend(loc='best')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7410\n",
      "0.00 epochs: MSE train/valid = 0.135947/0.116027\n",
      "4.99 epochs: MSE train/valid = 0.001688/0.000274\n",
      "9.99 epochs: MSE train/valid = 0.001181/0.000775\n",
      "14.98 epochs: MSE train/valid = 0.000987/0.000298\n",
      "19.97 epochs: MSE train/valid = 0.001305/0.000969\n",
      "24.97 epochs: MSE train/valid = 0.000787/0.000321\n",
      "29.96 epochs: MSE train/valid = 0.000688/0.000236\n",
      "34.95 epochs: MSE train/valid = 0.000821/0.000318\n",
      "39.95 epochs: MSE train/valid = 0.000660/0.000131\n",
      "44.94 epochs: MSE train/valid = 0.000623/0.000116\n",
      "49.93 epochs: MSE train/valid = 0.000710/0.000443\n",
      "54.93 epochs: MSE train/valid = 0.000568/0.000120\n",
      "59.92 epochs: MSE train/valid = 0.000545/0.000155\n",
      "64.91 epochs: MSE train/valid = 0.000525/0.000167\n",
      "69.91 epochs: MSE train/valid = 0.000533/0.000191\n",
      "74.90 epochs: MSE train/valid = 0.001023/0.000749\n",
      "79.89 epochs: MSE train/valid = 0.000515/0.000102\n",
      "84.89 epochs: MSE train/valid = 0.000509/0.000118\n",
      "89.88 epochs: MSE train/valid = 0.000501/0.000102\n",
      "94.87 epochs: MSE train/valid = 0.000558/0.000246\n",
      "99.87 epochs: MSE train/valid = 0.000571/0.000291\n"
     ]
    }
   ],
   "source": [
    "## Basic Cell RNN in tensorflow\n",
    "\n",
    "index_in_epoch = 0;\n",
    "perm_array  = np.arange(x_train.shape[0])\n",
    "np.random.shuffle(perm_array)\n",
    "\n",
    "# function to get the next batch\n",
    "def get_next_batch(batch_size):\n",
    "    global index_in_epoch, x_train, perm_array   \n",
    "    start = index_in_epoch\n",
    "    index_in_epoch += batch_size\n",
    "    \n",
    "    if index_in_epoch > x_train.shape[0]:\n",
    "        np.random.shuffle(perm_array) # shuffle permutation array\n",
    "        start = 0 # start next epoch\n",
    "        index_in_epoch = batch_size\n",
    "        \n",
    "    end = index_in_epoch\n",
    "    return x_train[perm_array[start:end]], y_train[perm_array[start:end]]\n",
    "\n",
    "# parameters\n",
    "n_steps = seq_len-1 \n",
    "n_inputs = 4\n",
    "n_neurons = 200 \n",
    "n_outputs = 4\n",
    "n_layers = 2\n",
    "learning_rate = 0.001\n",
    "batch_size = 10\n",
    "n_epochs = 100 \n",
    "train_set_size = x_train.shape[0]\n",
    "test_set_size = x_test.shape[0]\n",
    "\n",
    "tf.reset_default_graph()\n",
    "\n",
    "X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])\n",
    "y = tf.placeholder(tf.float32, [None, n_outputs])\n",
    "\n",
    "# # use Basic RNN Cell\n",
    "# layers = [tf.contrib.rnn.BasicRNNCell(num_units=n_neurons, activation=tf.nn.elu)\n",
    "#           for layer in range(n_layers)]\n",
    "\n",
    "# use Basic LSTM Cell \n",
    "#layers = [tf.contrib.rnn.BasicLSTMCell(num_units=n_neurons, activation=tf.nn.elu)\n",
    "#          for layer in range(n_layers)]\n",
    "\n",
    "#use LSTM Cell with peephole connections\n",
    "layers = [tf.contrib.rnn.LSTMCell(num_units=n_neurons, \n",
    "                                 activation=tf.nn.leaky_relu, use_peepholes = True)\n",
    "         for layer in range(n_layers)]\n",
    "\n",
    "# use GRU cell\n",
    "#layers = [tf.contrib.rnn.GRUCell(num_units=n_neurons, activation=tf.nn.leaky_relu)\n",
    "#          for layer in range(n_layers)]\n",
    "                                                                     \n",
    "multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers)\n",
    "rnn_outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32)\n",
    "\n",
    "stacked_rnn_outputs = tf.reshape(rnn_outputs, [-1, n_neurons]) \n",
    "stacked_outputs = tf.layers.dense(stacked_rnn_outputs, n_outputs)\n",
    "outputs = tf.reshape(stacked_outputs, [-1, n_steps, n_outputs])\n",
    "outputs = outputs[:,n_steps-1,:] # keep only last output of sequence\n",
    "                                              \n",
    "loss = tf.reduce_mean(tf.square(outputs - y)) # loss function = mean squared error \n",
    "optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) \n",
    "training_op = optimizer.minimize(loss)\n",
    "                                              \n",
    "# run graph\n",
    "with tf.Session() as sess: \n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    print (int(n_epochs*train_set_size/batch_size))\n",
    "    for iteration in range(int(n_epochs*train_set_size/batch_size)):\n",
    "        x_batch, y_batch = get_next_batch(batch_size) # fetch the next training batch \n",
    "        sess.run(training_op, feed_dict={X: x_batch, y: y_batch}) \n",
    "        if iteration % int(5*train_set_size/batch_size) == 0:\n",
    "            mse_train = loss.eval(feed_dict={X: x_train, y: y_train}) \n",
    "            mse_valid = loss.eval(feed_dict={X: x_valid, y: y_valid}) \n",
    "            print('%.2f epochs: MSE train/valid = %.6f/%.6f'%(\n",
    "                iteration*batch_size/train_set_size, mse_train, mse_valid))\n",
    "\n",
    "    y_train_pred = sess.run(outputs, feed_dict={X: x_train})\n",
    "    y_valid_pred = sess.run(outputs, feed_dict={X: x_valid})\n",
    "    y_test_pred = sess.run(outputs, feed_dict={X: x_test})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(741, 4)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "correct sign prediction for close - open price for train/valid/test: 0.51/0.55/0.66\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 66500 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x126648f98>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ft = 0 \n",
    "# 0 = open, 1 = close, 2 = highest, 3 = lowest\n",
    "## show predictions\n",
    "plt.figure(figsize=(15, 5));\n",
    "plt.subplot(1,2,1);\n",
    "\n",
    "plt.plot(np.arange(y_train.shape[0]), y_train[:,ft], color='blue', label='train target')\n",
    "\n",
    "plt.plot(np.arange(y_train.shape[0], y_train.shape[0]+y_valid.shape[0]), y_valid[:,ft],\n",
    "         color='gray', label='valid target')\n",
    "\n",
    "plt.plot(np.arange(y_train.shape[0]+y_valid.shape[0],\n",
    "                   y_train.shape[0]+y_test.shape[0]+y_test.shape[0]),\n",
    "         y_test[:,ft], color='black', label='test target')\n",
    "\n",
    "plt.plot(np.arange(y_train_pred.shape[0]),y_train_pred[:,ft], color='red',\n",
    "         label='train prediction')\n",
    "\n",
    "plt.plot(np.arange(y_train_pred.shape[0], y_train_pred.shape[0]+y_valid_pred.shape[0]),\n",
    "         y_valid_pred[:,ft], color='orange', label='valid prediction')\n",
    "\n",
    "plt.plot(np.arange(y_train_pred.shape[0]+y_valid_pred.shape[0],\n",
    "                   y_train_pred.shape[0]+y_valid_pred.shape[0]+y_test_pred.shape[0]),\n",
    "         y_test_pred[:,ft], color='green', label='test prediction')\n",
    "\n",
    "plt.title('past and future stock prices')\n",
    "plt.xlabel('time [day]') \n",
    "plt.ylabel('normalized price')\n",
    "plt.legend(loc='best');\n",
    "\n",
    "plt.subplot(1,2,2);\n",
    "\n",
    "plt.plot(np.arange(y_train.shape[0], y_train.shape[0]+y_test.shape[0]),\n",
    "         y_test[:,ft], color='black', label='test target')\n",
    "\n",
    "plt.plot(np.arange(y_train_pred.shape[0], y_train_pred.shape[0]+y_test_pred.shape[0]),\n",
    "         y_test_pred[:,ft], color='green', label='test prediction')\n",
    "\n",
    "plt.title('future stock prices')\n",
    "plt.xlabel('time [day]')\n",
    "plt.ylabel('normalized price')\n",
    "plt.legend(loc='best');\n",
    "\n",
    "corr_price_development_train = np.sum(np.equal(np.sign(y_train[:,1]-y_train[:,0]),\n",
    "            np.sign(y_train_pred[:,1]-y_train_pred[:,0])).astype(int)) / y_train.shape[0]\n",
    "corr_price_development_valid = np.sum(np.equal(np.sign(y_valid[:,1]-y_valid[:,0]),\n",
    "            np.sign(y_valid_pred[:,1]-y_valid_pred[:,0])).astype(int)) / y_valid.shape[0]\n",
    "corr_price_development_test = np.sum(np.equal(np.sign(y_test[:,1]-y_test[:,0]),\n",
    "            np.sign(y_test_pred[:,1]-y_test_pred[:,0])).astype(int)) / y_test.shape[0]\n",
    "\n",
    "print('correct sign prediction for close - open price for train/valid/test: %.2f/%.2f/%.2f'%(\n",
    "    corr_price_development_train, corr_price_development_valid, corr_price_development_test))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The green line tells us the prediction of the input data while dark line is the target value..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
