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
df = yf.download(sp,period = "3d",interval="15m") |
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
df = pd.read_html("http://en.wikipedia.org/wiki/List_of_S%26P_500_companies")[0] |
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
# Create a column by name, SMA and assign the SMA calculation to it | |
df['SMA'] = df['Close'].shift(1).rolling(window=n).mean() | |
# Create a column by name, Corr and assign the calculation of correlation to it | |
df['Corr'] = df['Close'].shift(1).rolling(window=n).corr(df['SMA'].shift(1)) | |
# Create a column by name, SAR and assign the SAR calculation to it | |
df['SAR'] = ta.SAR(np.array(df['High'].shift(1)), np.array(df['Low'].shift(1)), | |
0.2, 0.2) |
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
# Create a variable split that stores 80% of the length of the dataframe | |
t = .8 | |
split = int(t*len(df)) | |
split |
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
# Change the value of 'Corr' to -1 if it is less than -1 | |
df.loc[df['Corr'] < -1, 'Corr'] = -1 | |
# Change the value of 'Corr' to 1 if it is greater than 1 | |
df.loc[df['Corr'] > 1, 'Corr'] = 1 | |
# Drop the NaN values | |
df = df.dropna() |
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
# Create a column 'Ret' with the calculation of returns | |
df['Ret'] = (df['Open'].shift(-1)-df['Open'])/df['Open'] | |
# Create n columns and assign | |
for i in range(1, n): | |
df['return%i' % i] = df['Ret'].shift(i) |
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
# Create columns 'OO' with the difference between the current minute's open and last minute's open | |
df['OO'] = df['Open']-df['Open'].shift(1) | |
# Create columns 'OC' with the difference between the current minute's open and last minute's close | |
df['OC'] = df['Open']-df['Prev_Close'] |
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
# Create columns high, low and close with previous minute's OHLC data | |
df['Prev_High'] = df['High'].shift(1) | |
df['Prev_Low'] = df['Low'].shift(1) | |
df['Prev_Close'] = df['Close'].shift(1) |
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
# Create a column by name, SMA and assign the SMA calculation to it | |
df['SMA'] = df['Close'].shift(1).rolling(window=n).mean() | |
# Create a column by name, Corr and assign the calculation of correlation to it | |
df['Corr'] = df['Close'].shift(1).rolling(window=n).corr(df['SMA'].shift(1)) | |
# Create a column by name, SAR and assign the SAR calculation to it | |
df['SAR'] = ta.SAR(np.array(df['High'].shift(1)), np.array(df['Low'].shift(1)), | |
0.2, 0.2) |
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
# Data manipulation libraries | |
import pandas as pd | |
import numpy as np | |
# Machine learning libraries | |
from sklearn.svm import SVC | |
from sklearn.preprocessing import StandardScaler | |
from sklearn.model_selection import RandomizedSearchCV | |
from sklearn.model_selection import TimeSeriesSplit | |
from sklearn.pipeline import Pipeline |