forked from rahulmistry33/StockOracle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock.py
70 lines (60 loc) · 2.24 KB
/
stock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import numpy as np
from datetime import datetime
import smtplib
import time
import os
from selenium import webdriver
#For Prediction
from sklearn.linear_model import LinearRegression
from sklearn import preprocessing,svm
from sklearn.model_selection import train_test_split
#For Stock Data
from iexfinance.stocks import Stock
from iexfinance.stocks import get_historical_data
import pandas_datareader
import pandas as pd
from pandas_datareader import data
def getStocks(n):
#Navigating to the Yahoo stock screener
# chrome_driver_path = './chromedriver'
# driver = webdriver.Chrome(chrome_driver_path)
# url = 'https://finance.yahoo.com/screener/predefined/aggressive_small_caps?offset=0&count=202'
# driver.get(url)
# stock_list = []
# n += 1
# for i in range(1, n):
# ticker = driver.find_element_by_xpath('//*[@id="scr-res-table"]/div[1]/table/tbody/tr[ {} ]/td[1]/a'.format(i))
# stock_list.append(ticker.text)
# driver.quit()
#Using the stock list to predict the future price of the stock a specificed amount of days
# for i in stock_list:
# predictData(i, 2)
predictData('AAPL',2)
def predictData(stock,days):
start = datetime(2016, 1, 1)
end = datetime.now()
#Outputting the Historical data into a .csv for later use
#df = get_historical_data(stock, start,output_format='pandas')
df = data.get_data_yahoo(stock, start, end)
print(stock)
print("before",df.head(1))
# csv_name = ('Exports/' + stock + '_Export.csv')
# df.to_csv(csv_name)
df['prediction'] = df['Close'].shift(-1)
print("after",df.head(1))
df.dropna(inplace=True)
forecast_time = int(days)
X = np.array(df.drop(['prediction'], 1))
Y = np.array(df['prediction'])
X = preprocessing.scale(X)
X_prediction = X[-forecast_time:]
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.5)
print(X_train)
#Performing the Regression on the training data
clf = LinearRegression()
clf.fit(X_train, Y_train)
prediction = (clf.predict(X_prediction))
print("Prediction",prediction)
print("hejfhiodhviodjivd")
print(X_prediction)
getStocks(5)