Skip to content

Commit

Permalink
ML regression
Browse files Browse the repository at this point in the history
  • Loading branch information
ApurvShah007 committed Jul 31, 2020
1 parent a58b085 commit 2881b4b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
Binary file modified .DS_Store
Binary file not shown.
61 changes: 61 additions & 0 deletions Machine Learning Scripts /regression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import quandl, math
import numpy as np
import pandas as pd
from sklearn import preprocessing, svm
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from matplotlib import style
from datetime import datetime, timedelta
import pickle
import yfinance as yf

style.use('ggplot')

stock = yf.Ticker('FB')
df = stock.history(period="max")
df = df[['Open', 'High', 'Low', 'Close', 'Volume']]
df['HL_PCT'] = (df['High'] - df['Low']) / df['Close'] * 100.0
df['PCT_change'] = (df['Close'] - df['Open']) / df['Open'] * 100.0
arr = df['Close']
df = df[['Close', 'HL_PCT', 'PCT_change', 'Volume']]
forecast_col = 'Close'
df.fillna(value=-99999, inplace=True)
forecast_out = int(math.ceil(0.1 * len(df)))
df['label'] = df[forecast_col].shift(-forecast_out)


X = np.array(df.drop(['label','Close'], 1))
X = preprocessing.scale(X)
X_lately = X[-forecast_out:]
X = X[:-forecast_out]

df.dropna(inplace=True)
y = np.array(df['label'])

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

clf = LinearRegression(n_jobs=-1)
clf.fit(X_train, y_train)

confidence = clf.score(X_test, y_test)
print(confidence)
forecast_set = clf.predict(X_lately)
df['Forecast'] = np.nan

last_date = df.iloc[-1].name
last_unix = last_date
next_unix = last_unix + timedelta(days=1)

for i in forecast_set:
next_date = next_unix
next_unix += timedelta(days=1)
j= [np.nan for _ in range(len(df.columns)-1)]+[i]
df.loc[next_date]=j
#print(df.head())
arr.plot()
df['Forecast'].plot()
plt.legend(loc=4)
plt.xlabel('Date')
plt.ylabel('Price')
plt.show()
Binary file modified Portfolio Optimizing Scripts/.DS_Store
Binary file not shown.
13 changes: 8 additions & 5 deletions Portfolio Optimizing Scripts/sharpe_portfolio_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ def basicStats(df, weights):
print('Annual volatility/standard deviation/risk : ',percent_vols)
print('Annual variance : ',percent_var)
print("\n")
# def omptimizePortCLA():
def getDiscrete(df, weights):

def omptimizePortCLA():


def getDiscreteAllocations(df, weights):
latest_prices = get_latest_prices(df)
#plotting.plot_weights(weights)
total_portfolio_value = 15000
Expand Down Expand Up @@ -86,15 +89,15 @@ def optimizePortEfficient(port, weights, start, plot = False, short = False, pri
print("Weights of an optimal portfolio maximised on Sharpe Ratio:")
print(cleaned_weights)
ef.portfolio_performance(verbose = True)
getDiscrete(df, weights)
getDiscreteAllocations(df, weights)
if how == "Vol":
# Minimized on Volatility
efi = EfficientFrontier(mu, S, weight_bounds=(-1,1))
w = dict(efi.min_volatility())
print("\nWeights of an optimal portfolio minimized on Volatilty (Risk):")
print(w)
efi.portfolio_performance(verbose = True)
getDiscrete(df, w)
getDiscreteAllocations(df, w)

#Current best allocations

Expand All @@ -103,5 +106,5 @@ def optimizePortEfficient(port, weights, start, plot = False, short = False, pri
portfolio = ['FB', "AAPL", "AMZN", 'NFLX', 'GOOG']
weights = np.array([0.2,0.2,0.2,0.2,0.2])
start = '2013-01-01'
optimizePortEfficient(portfolio, weights, start)
optimizePortEfficient(portfolio, weights, start, how = "Vol")

0 comments on commit 2881b4b

Please sign in to comment.