# Start writing code here...
import pandas as pd
import numpy as np
import seaborn as sns
#sns.get_dataset_names()
from pandas_datareader import data
from sklearn.model_selection import train_test_split
import time
import datetime
ticker = 'FB2A.BE'
period1 = int(time.mktime(datetime.datetime(2010, 1, 1, 23, 59).timetuple()))
period2 = int(time.mktime(datetime.datetime(2022, 2, 1, 23, 59).timetuple()))
interval = '1d'
query_string = f'https://query1.finance.yahoo.com/v7/finance/download/{ticker}?period1={period1}&period2={period2}&interval={interval}&events=history&includeAdjustedClose=true'
data = pd.read_csv(query_string)
data
data.info()
data['highest hight'] = data['High'].rolling(window=10).max()
data['lowest low'] = data['Low'].rolling(window=10).min()
data.info()
data
data['trigger'] = np.where(data['High']==data['highest hight'],1,np.nan)
data['trigger'] = np.where(data['Low']==data['lowest low'],0,data['trigger'])
data['position'] = data['trigger'].ffill().fillna(-1)
data
df = data.drop(['Date','Adj Close','Volume','highest hight', 'lowest low', 'trigger'],axis=1)
df
X = df.drop('position',axis=1)
X
y = df['position']
y
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=0)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
from sklearn import svm
svm_reg = svm.SVR()
regressor = svm_reg.fit(X_train, y_train)
regressor
y_pre = regressor.predict(X_test)
import pickle
pkl_filename = "Facebook.pkl"
with open(pkl_filename, 'wb') as file:
pickle.dump(regressor, file)
with open(pkl_filename, 'rb') as file:
model = pickle.load(file)
x_in = np.asarray([320.47,320.88,304.00,304.79]).reshape(1,-1)
predicts = regressor.predict(x_in)
predicts[0]