SlideShare a Scribd company logo
1 of 25
Download to read offline
In [11]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.arima_model import ARIMA
import statsmodels.api as sm
from statsmodels.tsa.seasonal import seasonal_decompose
import warnings
warnings.filterwarnings('ignore')
from sklearn.metrics import accuracy_score
import seaborn as sns
In [2]:
######################################## Import Data From Quandl API
##########################################################
#import quandl
#import pandas as pd
#import xlsxwriter
##mydata = quandl.get_table('ZACKS/FC', ticker='AAPL')
#quandl.ApiConfig.api_key = "XXXXXXXXXXXXXX" ########## Register and
generate a key ##############
#mydata = quandl.get("FRED/GDP")
#excelfile= r'dailyd2.xlsx'
#workbook = xlsxwriter.Workbook(excelfile)
#worksheet1 = workbook.add_worksheet('daily')
#bold = workbook.add_format({'bold': True})
#row = 0
#worksheet1.write(row,0,'Date')
#mydata = quandl.get('WIKI/AAPL', start_date="2017-01-01", end_date="2017-0
8-25")
#daterange = mydata.index ############# Save the date range ##############
#
#for i in daterange:
# row = row+1
# worksheet1.write(row,0,i)
#data = pd.read_csv(r'0825_quandl_ticks2.csv') ########### Load the list of
tickers #############
#tlist = data['Ticker']
#col = 0
#for tl in tlist:
# try:
# mydata = quandl.get('WIKI/'+tl, start_date="2017-01-01", end_date=
"2017-08-25")
# if len(mydata)-1 == len(daterange):
# row = 0
# col = col+1
# worksheet1.write(row,col,tl,bold)
# for dr in daterange:
# row = row+1
# for i,j in mydata['Adj. Close'].iteritems():
# if dr==i:
# if dr==i:
# worksheet1.write(row,col,j)
# print(tl,j)
# except Exception as e:
# print(e)
#workbook.close()
In [3]:
######## Load Data #########
data = pd.read_excel(r'C:ModelDatadailyd2.xlsx', index_col='Date', parse_
dates=True)
In [4]:
################ Calculate and plot volatility #####################
df_stat = pd.DataFrame(columns = ['std','mean','normalized_std'])
df_stat[['std','mean','normalized_std']] = pd.DataFrame([data.std(),data.m
ean(),data.std()/data.mean()]).T
df_stat.sort_values('normalized_std')
plt.title('Normalized Standard Deviation')
plt.hist(df_stat['normalized_std'])
fig = plt.gcf()
fig.set_size_inches(14.5, 5.5)
plt.show()
In [5]:
######### Based on the graph above filter out stocks with high variance
and low returns#############
df_fltr = df_stat[df_stat['normalized_std']<.15]
df = data[df_fltr.index]
tkl = [x for x in df if (df[x][-1]-df[x][0])/df[x][0]>=.25 ]
df = df[tkl]
fltr_tk = []
for tk in df:
Y = df[tk].as_matrix()
X = range(len(df.index))
X = sm.add_constant(X)
model = sm.OLS(Y,X)
results = model.fit()
if results.params[1] > .01: ########## Filter on slope coefficient from
Regression Model #########
Regression Model #########
fltr_tk.append(tk)
df.plot(y=[tk])
plt.title('Daily '+tk+' Stock Prices 2017-01-01 to 2017-07-31')
plt.legend(loc='upper left')
plt.axvspan('2017-08-01','2017-08-25', color='green', alpha=0.25)
fig = plt.gcf()
fig.set_size_inches(16.5, 4.5)
plt.show()
In [6]:
################ PLot Trend and Seasonality #########################
for tk in fltr_tk:
df_d = df[tk]
adj_index = pd.date_range(df_d.index[0], periods=len(df), freq='D')
df_d.index = adj_index
decomposition = seasonal_decompose(df_d)
trend = decomposition.trend
seasonal = decomposition.seasonal
seasonal.index = df.index
trend.index = df.index
df_d.index = df.index
plt.subplot(1,2,1)
plt.plot(trend,label='Trend')
plt.title(tk)
plt.xticks(rotation=90)
plt.legend(loc='best')
plt.subplot(1,2,2)
plt.plot(seasonal,label='Seasonality')
plt.title(tk)
plt.xticks(rotation=90)
plt.legend(loc='best')
fig = plt.gcf()
fig.set_size_inches(16.5, 4.5)
plt.show()
In [7]:
############### Run ARIMA Model ###################
store = {}
for tk in fltr_tk:
train = df[tk][0:-20]
test = df[tk][len(train):]
ap = 99
ad = 99
aq = 99
amape = 99
af = []
for p in range(10):
for q in range(10):
for d in range(2):
try:
model = ARIMA(train, order=(p, d, q)).fit()
predict = model.forecast(len(test))
fcst=predict[0]
mapelist = []
for i in range(len(fcst)):
mapelist.insert(i, (np.absolute(test[i] - fcst[i])) /
test[i])
mape = np.mean(mapelist) * 100
mape = round(mape,2)
except:
mape = 9999
pass
if amape > mape:
amape = mape
ap = p
ad = d
aq = q
af= fcst
store[tk] = af
plt.plot(train)
plt.plot(test,label='Actual')
plt.plot(test.index,af,label='Predicted')
fig = plt.gcf()
fig.set_size_inches(16.5, 4.5)
plt.title(str(tk)+"_"+"MAPE"+"_"+str(amape)+"_"+"Order"+"_"+"("+str(ap)+
str(ad)+str(aq)+")")
plt.legend(loc='best')
plt.show()
In [8]:
###### Correlation HeatMap #######
corr = df[fltr_tk].corr(method='pearson', min_periods=1).abs()
ax = sns.heatmap(corr);
fig = plt.gcf()
fig.set_size_inches(18.5, 10.5)
ax.set_title('PortFolio Diversity HeatMap');
plt.show()
In [9]:
##### Test model accuracy #####
eval_metrcs = pd.DataFrame(columns = ['Actual','Predicted','Actual_Growth',
'Predicted_Growth'],index = fltr_tk)
for tk in fltr_tk:
train = df[tk][0:-20]
test = df[tk][len(train):]
if (train[-1] >= test[-1] and train[-1] >= store[tk][-1]):
eval_metrcs['Actual'].loc[eval_metrcs.index == tk] = 0
eval_metrcs['Predicted'].loc[eval_metrcs.index == tk] = 0
eval_metrcs['Actual_Growth'].loc[eval_metrcs.index == tk] = (test[-1
] - train[-1])/train[-1]
eval_metrcs['Predicted_Growth'].loc[eval_metrcs.index == tk] = (stor
e[tk][-1] - train[-1])/train[-1]
elif (train[-1] < test[-1] and train[-1] < store[tk][-1]):
eval_metrcs['Actual'].loc[eval_metrcs.index == tk] = 1
eval_metrcs['Predicted'].loc[eval_metrcs.index == tk] = 1
eval_metrcs['Actual_Growth'].loc[eval_metrcs.index == tk] = (test[-1
] - train[-1])/train[-1]
eval_metrcs['Predicted_Growth'].loc[eval_metrcs.index == tk] = (stor
e[tk][-1] - train[-1])/train[-1]
elif (train[-1] >= test[-1] and train[-1] < store[tk][-1]):
eval_metrcs['Actual'].loc[eval_metrcs.index == tk] = 0
eval_metrcs['Predicted'].loc[eval_metrcs.index == tk] = 1
eval_metrcs['Actual_Growth'].loc[eval_metrcs.index == tk] = (test[-1
] - train[-1])/train[-1]
eval_metrcs['Predicted_Growth'].loc[eval_metrcs.index == tk] = (stor
e[tk][-1] - train[-1])/train[-1]
elif (train[-1] < test[-1] and train[-1] >= store[tk][-1]):
eval_metrcs['Actual'].loc[eval_metrcs.index == tk] = 1
eval_metrcs['Predicted'].loc[eval_metrcs.index == tk] = 0
eval_metrcs['Actual_Growth'].loc[eval_metrcs.index == tk] = (test[-1
] - train[-1])/train[-1]
eval_metrcs['Predicted_Growth'].loc[eval_metrcs.index == tk] = (stor
e[tk][-1] - train[-1])/train[-1]
eval_metrcs
In [10]:
Out[9]:
Actual Predicted Actual_Growth Predicted_Growth
HPQ 1 0 0.00520562 -0.017592
BGCP 1 1 0.0344432 0.0274154
BLDR 0 1 -0.0562743 0.0407847
CZR 0 0 -0.113725 -0.0310849
CAMP 0 0 -0.0433884 -0.0357415
CPN 1 1 0.0288124 0.0241479
CWST 0 0 -0.0268692 -0.0257216
CENX 1 0 0.00892359 -0.21757
COHU 0 0 -0.0932919 -0.131128
DAR 1 0 0.0654545 -0.0141167
FOE 1 1 0.0202128 0.00680254
GSM 1 1 0.0460829 0.0259386
GTN 0 0 -0.0562914 -0.100722
IXYS 0 0 -0.0859599 -0.0192517
MDCA 0 0 -0.00980392 -0.00131282
MTOR 1 1 0.0772311 0.0496162
MGI 0 0 -0.0295567 -0.0605303
MYE 1 1 0.0314286 0.031333
NYT 0 0 -0.085213 -0.023136
ORBC 0 0 -0.0545145 -0.0124334
PNK 1 1 0.00455005 0.0167042
RDNT 1 1 0.251613 0.0372675
SEM 1 1 0.0617284 0.0590196
TGH 1 0 0.0125786 -0.0381664
USAP 0 0 -0.0281081 -0.0258438
NLY 1 1 0.0403361 0.0491873
FOR 0 1 0 0.000157287
FORM 1 1 0.0792453 0.0899497
RSO 1 1 0.0264188 0.0305759
GAIA 0 0 -0.0214592 -0.0252127
In [10]:
model_accuracy =
accuracy_score(eval_metrcs['Actual'].astype(int),eval_metrcs['Predicted'].a
stype(int))
model_accuracy
Out[10]:
0.80000000000000004

More Related Content

What's hot

What's hot (20)

Data Visualization — Le funzionalità matematiche di Sage per la visualizzazio...
Data Visualization — Le funzionalità matematiche di Sage per la visualizzazio...Data Visualization — Le funzionalità matematiche di Sage per la visualizzazio...
Data Visualization — Le funzionalità matematiche di Sage per la visualizzazio...
 
multiple linear regression
multiple linear regressionmultiple linear regression
multiple linear regression
 
SupportVectorRegression
SupportVectorRegressionSupportVectorRegression
SupportVectorRegression
 
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
NCCU: Statistics in the Criminal Justice System, R basics and Simulation - Pr...
 
Berlin meetup
Berlin meetupBerlin meetup
Berlin meetup
 
A quick introduction to R
A quick introduction to RA quick introduction to R
A quick introduction to R
 
R-Shiny Cheat sheet
R-Shiny Cheat sheetR-Shiny Cheat sheet
R-Shiny Cheat sheet
 
Pure Future
Pure FuturePure Future
Pure Future
 
Functional Programming Advanced
Functional Programming AdvancedFunctional Programming Advanced
Functional Programming Advanced
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
ZIO Queue
ZIO QueueZIO Queue
ZIO Queue
 
The num py_library_20200818
The num py_library_20200818The num py_library_20200818
The num py_library_20200818
 
Array notes
Array notesArray notes
Array notes
 
F(3)
F(3)F(3)
F(3)
 
Code optimization
Code optimization Code optimization
Code optimization
 
Odoo - From v7 to v8: the new api
Odoo - From v7 to v8: the new apiOdoo - From v7 to v8: the new api
Odoo - From v7 to v8: the new api
 
Tweaking the interactive grid
Tweaking the interactive gridTweaking the interactive grid
Tweaking the interactive grid
 
Atomically { Delete Your Actors }
Atomically { Delete Your Actors }Atomically { Delete Your Actors }
Atomically { Delete Your Actors }
 
Flying Futures at the same sky can make the sun rise at midnight
Flying Futures at the same sky can make the sun rise at midnightFlying Futures at the same sky can make the sun rise at midnight
Flying Futures at the same sky can make the sun rise at midnight
 

Similar to Forecast stock prices python

Using the following code Install Packages pip install .pdf
Using the following code Install Packages   pip install .pdfUsing the following code Install Packages   pip install .pdf
Using the following code Install Packages pip install .pdfpicscamshoppe
 
Assignment 5.2.pdf
Assignment 5.2.pdfAssignment 5.2.pdf
Assignment 5.2.pdfdash41
 
Regression and Classification with R
Regression and Classification with RRegression and Classification with R
Regression and Classification with RYanchang Zhao
 
Interactive financial analytics with vix(cboe)
Interactive financial analytics with vix(cboe)Interactive financial analytics with vix(cboe)
Interactive financial analytics with vix(cboe)Aiden Wu, FRM
 
Naive application of Machine Learning to Software Development
Naive application of Machine Learning to Software DevelopmentNaive application of Machine Learning to Software Development
Naive application of Machine Learning to Software DevelopmentAndriy Khavryuchenko
 
Forecasting Revenue With Stationary Time Series Models
Forecasting Revenue With Stationary Time Series ModelsForecasting Revenue With Stationary Time Series Models
Forecasting Revenue With Stationary Time Series ModelsGeoffery Mullings
 
Efficient equity portfolios using mean variance optimisation in R
Efficient equity portfolios using mean variance optimisation in REfficient equity portfolios using mean variance optimisation in R
Efficient equity portfolios using mean variance optimisation in RGregg Barrett
 
you need to complete the r code and a singlepage document c.pdf
you need to complete the r code and a singlepage document c.pdfyou need to complete the r code and a singlepage document c.pdf
you need to complete the r code and a singlepage document c.pdfadnankhan605720
 
intro_to_python_20150825
intro_to_python_20150825intro_to_python_20150825
intro_to_python_20150825Shung-Hsi Yu
 
Literary Genre MatrixPart 1 Matrix FictionNon-fiction.docx
Literary Genre MatrixPart 1 Matrix FictionNon-fiction.docxLiterary Genre MatrixPart 1 Matrix FictionNon-fiction.docx
Literary Genre MatrixPart 1 Matrix FictionNon-fiction.docxjeremylockett77
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School ProgrammersSiva Arunachalam
 
Company segmentation - an approach with R
Company segmentation - an approach with RCompany segmentation - an approach with R
Company segmentation - an approach with RCasper Crause
 
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...Yao Yao
 

Similar to Forecast stock prices python (20)

Using the following code Install Packages pip install .pdf
Using the following code Install Packages   pip install .pdfUsing the following code Install Packages   pip install .pdf
Using the following code Install Packages pip install .pdf
 
Final project kijtorntham n
Final project kijtorntham nFinal project kijtorntham n
Final project kijtorntham n
 
Assignment 5.2.pdf
Assignment 5.2.pdfAssignment 5.2.pdf
Assignment 5.2.pdf
 
QMC: Undergraduate Workshop, Tutorial on 'R' Software - Yawen Guan, Feb 26, 2...
QMC: Undergraduate Workshop, Tutorial on 'R' Software - Yawen Guan, Feb 26, 2...QMC: Undergraduate Workshop, Tutorial on 'R' Software - Yawen Guan, Feb 26, 2...
QMC: Undergraduate Workshop, Tutorial on 'R' Software - Yawen Guan, Feb 26, 2...
 
Regression and Classification with R
Regression and Classification with RRegression and Classification with R
Regression and Classification with R
 
Interactive financial analytics with vix(cboe)
Interactive financial analytics with vix(cboe)Interactive financial analytics with vix(cboe)
Interactive financial analytics with vix(cboe)
 
Naive application of Machine Learning to Software Development
Naive application of Machine Learning to Software DevelopmentNaive application of Machine Learning to Software Development
Naive application of Machine Learning to Software Development
 
Forecasting Revenue With Stationary Time Series Models
Forecasting Revenue With Stationary Time Series ModelsForecasting Revenue With Stationary Time Series Models
Forecasting Revenue With Stationary Time Series Models
 
Efficient equity portfolios using mean variance optimisation in R
Efficient equity portfolios using mean variance optimisation in REfficient equity portfolios using mean variance optimisation in R
Efficient equity portfolios using mean variance optimisation in R
 
you need to complete the r code and a singlepage document c.pdf
you need to complete the r code and a singlepage document c.pdfyou need to complete the r code and a singlepage document c.pdf
you need to complete the r code and a singlepage document c.pdf
 
cluster(python)
cluster(python)cluster(python)
cluster(python)
 
Py lecture5 python plots
Py lecture5 python plotsPy lecture5 python plots
Py lecture5 python plots
 
intro_to_python_20150825
intro_to_python_20150825intro_to_python_20150825
intro_to_python_20150825
 
Literary Genre MatrixPart 1 Matrix FictionNon-fiction.docx
Literary Genre MatrixPart 1 Matrix FictionNon-fiction.docxLiterary Genre MatrixPart 1 Matrix FictionNon-fiction.docx
Literary Genre MatrixPart 1 Matrix FictionNon-fiction.docx
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School Programmers
 
Company segmentation - an approach with R
Company segmentation - an approach with RCompany segmentation - an approach with R
Company segmentation - an approach with R
 
R programming
R programmingR programming
R programming
 
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
 
Practicle 1.docx
Practicle 1.docxPracticle 1.docx
Practicle 1.docx
 
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
 

Recently uploaded

Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceSapana Sha
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAbdelrhman abooda
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxFurkanTasci3
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home ServiceSapana Sha
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfJohn Sterrett
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...ThinkInnovation
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 

Recently uploaded (20)

Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts Service
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptx
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdf
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 

Forecast stock prices python

  • 1. In [11]: import numpy as np import pandas as pd import matplotlib.pyplot as plt from statsmodels.tsa.arima_model import ARIMA import statsmodels.api as sm from statsmodels.tsa.seasonal import seasonal_decompose import warnings warnings.filterwarnings('ignore') from sklearn.metrics import accuracy_score import seaborn as sns In [2]: ######################################## Import Data From Quandl API ########################################################## #import quandl #import pandas as pd #import xlsxwriter ##mydata = quandl.get_table('ZACKS/FC', ticker='AAPL') #quandl.ApiConfig.api_key = "XXXXXXXXXXXXXX" ########## Register and generate a key ############## #mydata = quandl.get("FRED/GDP") #excelfile= r'dailyd2.xlsx' #workbook = xlsxwriter.Workbook(excelfile) #worksheet1 = workbook.add_worksheet('daily') #bold = workbook.add_format({'bold': True}) #row = 0 #worksheet1.write(row,0,'Date') #mydata = quandl.get('WIKI/AAPL', start_date="2017-01-01", end_date="2017-0 8-25") #daterange = mydata.index ############# Save the date range ############## # #for i in daterange: # row = row+1 # worksheet1.write(row,0,i) #data = pd.read_csv(r'0825_quandl_ticks2.csv') ########### Load the list of tickers ############# #tlist = data['Ticker'] #col = 0 #for tl in tlist: # try: # mydata = quandl.get('WIKI/'+tl, start_date="2017-01-01", end_date= "2017-08-25") # if len(mydata)-1 == len(daterange): # row = 0 # col = col+1 # worksheet1.write(row,col,tl,bold) # for dr in daterange: # row = row+1 # for i,j in mydata['Adj. Close'].iteritems(): # if dr==i:
  • 2. # if dr==i: # worksheet1.write(row,col,j) # print(tl,j) # except Exception as e: # print(e) #workbook.close() In [3]: ######## Load Data ######### data = pd.read_excel(r'C:ModelDatadailyd2.xlsx', index_col='Date', parse_ dates=True) In [4]: ################ Calculate and plot volatility ##################### df_stat = pd.DataFrame(columns = ['std','mean','normalized_std']) df_stat[['std','mean','normalized_std']] = pd.DataFrame([data.std(),data.m ean(),data.std()/data.mean()]).T df_stat.sort_values('normalized_std') plt.title('Normalized Standard Deviation') plt.hist(df_stat['normalized_std']) fig = plt.gcf() fig.set_size_inches(14.5, 5.5) plt.show() In [5]: ######### Based on the graph above filter out stocks with high variance and low returns############# df_fltr = df_stat[df_stat['normalized_std']<.15] df = data[df_fltr.index] tkl = [x for x in df if (df[x][-1]-df[x][0])/df[x][0]>=.25 ] df = df[tkl] fltr_tk = [] for tk in df: Y = df[tk].as_matrix() X = range(len(df.index)) X = sm.add_constant(X) model = sm.OLS(Y,X) results = model.fit() if results.params[1] > .01: ########## Filter on slope coefficient from Regression Model #########
  • 3. Regression Model ######### fltr_tk.append(tk) df.plot(y=[tk]) plt.title('Daily '+tk+' Stock Prices 2017-01-01 to 2017-07-31') plt.legend(loc='upper left') plt.axvspan('2017-08-01','2017-08-25', color='green', alpha=0.25) fig = plt.gcf() fig.set_size_inches(16.5, 4.5) plt.show()
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9. In [6]: ################ PLot Trend and Seasonality ######################### for tk in fltr_tk: df_d = df[tk] adj_index = pd.date_range(df_d.index[0], periods=len(df), freq='D') df_d.index = adj_index decomposition = seasonal_decompose(df_d) trend = decomposition.trend seasonal = decomposition.seasonal seasonal.index = df.index trend.index = df.index df_d.index = df.index plt.subplot(1,2,1) plt.plot(trend,label='Trend') plt.title(tk) plt.xticks(rotation=90) plt.legend(loc='best') plt.subplot(1,2,2) plt.plot(seasonal,label='Seasonality') plt.title(tk) plt.xticks(rotation=90) plt.legend(loc='best') fig = plt.gcf() fig.set_size_inches(16.5, 4.5) plt.show()
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16. In [7]: ############### Run ARIMA Model ################### store = {} for tk in fltr_tk: train = df[tk][0:-20] test = df[tk][len(train):] ap = 99 ad = 99 aq = 99 amape = 99 af = [] for p in range(10): for q in range(10): for d in range(2): try: model = ARIMA(train, order=(p, d, q)).fit() predict = model.forecast(len(test)) fcst=predict[0] mapelist = [] for i in range(len(fcst)): mapelist.insert(i, (np.absolute(test[i] - fcst[i])) / test[i]) mape = np.mean(mapelist) * 100 mape = round(mape,2) except: mape = 9999 pass if amape > mape: amape = mape ap = p ad = d aq = q af= fcst store[tk] = af plt.plot(train) plt.plot(test,label='Actual') plt.plot(test.index,af,label='Predicted') fig = plt.gcf() fig.set_size_inches(16.5, 4.5) plt.title(str(tk)+"_"+"MAPE"+"_"+str(amape)+"_"+"Order"+"_"+"("+str(ap)+ str(ad)+str(aq)+")") plt.legend(loc='best') plt.show()
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22. In [8]: ###### Correlation HeatMap ####### corr = df[fltr_tk].corr(method='pearson', min_periods=1).abs() ax = sns.heatmap(corr); fig = plt.gcf() fig.set_size_inches(18.5, 10.5) ax.set_title('PortFolio Diversity HeatMap'); plt.show()
  • 23. In [9]: ##### Test model accuracy ##### eval_metrcs = pd.DataFrame(columns = ['Actual','Predicted','Actual_Growth', 'Predicted_Growth'],index = fltr_tk) for tk in fltr_tk: train = df[tk][0:-20] test = df[tk][len(train):] if (train[-1] >= test[-1] and train[-1] >= store[tk][-1]): eval_metrcs['Actual'].loc[eval_metrcs.index == tk] = 0 eval_metrcs['Predicted'].loc[eval_metrcs.index == tk] = 0 eval_metrcs['Actual_Growth'].loc[eval_metrcs.index == tk] = (test[-1 ] - train[-1])/train[-1] eval_metrcs['Predicted_Growth'].loc[eval_metrcs.index == tk] = (stor e[tk][-1] - train[-1])/train[-1] elif (train[-1] < test[-1] and train[-1] < store[tk][-1]): eval_metrcs['Actual'].loc[eval_metrcs.index == tk] = 1 eval_metrcs['Predicted'].loc[eval_metrcs.index == tk] = 1 eval_metrcs['Actual_Growth'].loc[eval_metrcs.index == tk] = (test[-1 ] - train[-1])/train[-1] eval_metrcs['Predicted_Growth'].loc[eval_metrcs.index == tk] = (stor e[tk][-1] - train[-1])/train[-1] elif (train[-1] >= test[-1] and train[-1] < store[tk][-1]): eval_metrcs['Actual'].loc[eval_metrcs.index == tk] = 0 eval_metrcs['Predicted'].loc[eval_metrcs.index == tk] = 1 eval_metrcs['Actual_Growth'].loc[eval_metrcs.index == tk] = (test[-1 ] - train[-1])/train[-1] eval_metrcs['Predicted_Growth'].loc[eval_metrcs.index == tk] = (stor e[tk][-1] - train[-1])/train[-1] elif (train[-1] < test[-1] and train[-1] >= store[tk][-1]): eval_metrcs['Actual'].loc[eval_metrcs.index == tk] = 1 eval_metrcs['Predicted'].loc[eval_metrcs.index == tk] = 0 eval_metrcs['Actual_Growth'].loc[eval_metrcs.index == tk] = (test[-1 ] - train[-1])/train[-1] eval_metrcs['Predicted_Growth'].loc[eval_metrcs.index == tk] = (stor e[tk][-1] - train[-1])/train[-1]
  • 24. eval_metrcs In [10]: Out[9]: Actual Predicted Actual_Growth Predicted_Growth HPQ 1 0 0.00520562 -0.017592 BGCP 1 1 0.0344432 0.0274154 BLDR 0 1 -0.0562743 0.0407847 CZR 0 0 -0.113725 -0.0310849 CAMP 0 0 -0.0433884 -0.0357415 CPN 1 1 0.0288124 0.0241479 CWST 0 0 -0.0268692 -0.0257216 CENX 1 0 0.00892359 -0.21757 COHU 0 0 -0.0932919 -0.131128 DAR 1 0 0.0654545 -0.0141167 FOE 1 1 0.0202128 0.00680254 GSM 1 1 0.0460829 0.0259386 GTN 0 0 -0.0562914 -0.100722 IXYS 0 0 -0.0859599 -0.0192517 MDCA 0 0 -0.00980392 -0.00131282 MTOR 1 1 0.0772311 0.0496162 MGI 0 0 -0.0295567 -0.0605303 MYE 1 1 0.0314286 0.031333 NYT 0 0 -0.085213 -0.023136 ORBC 0 0 -0.0545145 -0.0124334 PNK 1 1 0.00455005 0.0167042 RDNT 1 1 0.251613 0.0372675 SEM 1 1 0.0617284 0.0590196 TGH 1 0 0.0125786 -0.0381664 USAP 0 0 -0.0281081 -0.0258438 NLY 1 1 0.0403361 0.0491873 FOR 0 1 0 0.000157287 FORM 1 1 0.0792453 0.0899497 RSO 1 1 0.0264188 0.0305759 GAIA 0 0 -0.0214592 -0.0252127