application_vnd.jupyter.stderr_output_2.png text_plain_output_1.png
from sklearn.model_selection import train_test_split

import pandas as pd

import numpy as np
import pandas as pd
def load_stock(name):
    df = pd.read_csv('../input/stock-market-data/stock_market_data/nasdaq/csv/{}.csv'.format(name))
    df.set_index('Date', inplace=True)
    return df
names = ['AAL', 'AAPL']
stocks = [load_stock(n) for n in names]
df = stocks[0][-1500:]
df

df.isnull().sum()

ma_days = [7, 10, 14, 21, 50, 100]
maxi_days = [30, 365, 730]
def calculate_average(df, ma_days):
    for ma in ma_days:
        column_name = 'MA for %s days' % str(ma)
        df[column_name] = pd.DataFrame.rolling(df['Close'], ma).mean()
def calculate_maximum(df, ma_days):
    for ma in maxi_days:
        column_name = 'Maximum for %s days' % str(ma)
        df[column_name] = pd.DataFrame.rolling(df['Close'], ma).max()
def calculate_minimum(df, ma_days):
    for ma in maxi_days:
        column_name = 'Minimum for %s days' % str(ma)
        df[column_name] = pd.DataFrame.rolling(df['Close'], ma).min()
calculate_average(df, ma_days)
calculate_maximum(df, maxi_days)
calculate_minimum(df, maxi_days)
df['std for 7 days'] = pd.DataFrame.rolling(df['Close'], 7).std()
df['Diff High Low'] = df['High'] - df['Low']
df['Diff Open Close'] = df['Open'] - df['Close']
df['Daily Return'] = df['Close'].pct_change() * 100
df.columns
df.isnull().sum()

from sklearn.model_selection import train_test_split
y = df['Close']
df['Close previous'] = df['Close']
df = df.drop(['Close'], axis=1)
df = df.drop(['Adjusted Close'], axis=1)
df = df.shift(periods=1)
x_train, x_test, y_train, y_test = train_test_split(df, y, test_size=0.2, shuffle=False)
print(x_test.isnull().sum())
x_train.isnull().sum()
x_train.fillna(x_train.mean(), inplace=True)
x_train.isnull().sum()
df
df.fillna(x_train.mean(), inplace=True)