Predicting Financial Time Series Movements
Dr. Yves J. Hilpisch | The Python Quants GmbH
http://tpq.io | @dyjh | training@tpq.io
This tutorial shows
pandas
, Plotly
and Cufflinks
andimport eikon as ek # the Eikon Python wrapper package
import numpy as np # NumPy
import pandas as pd # pandas
import cufflinks as cf # Cufflinks
from sklearn.svm import SVC # sckikit-learn
from sklearn.model_selection import train_test_split
import configparser as cp
The following Python and package versions are used.
import sys
print(sys.version)
ek.__version__
np.__version__
pd.__version__
cf.__version__
This code sets the app_id
to connect to the Eikon Data API Proxy which needs to be running locally.
cfg = cp.ConfigParser()
cfg.read('eikon.cfg')
ek.set_app_id(cfg['eikon']['app_id'])
We first define a small universe of RICS
for which to retrieve data.
rics = [
'SPY', # S&P 500 ETF
'AAPL.O', # Apple stock
'AMZN.O' # Amazon stock
]
Second, end-of-day (EOD) data is retrieved.
data = ek.get_timeseries(rics, # the RICs
fields='CLOSE', # the required fields
start_date='2018-02-12', # start date
end_date='2018-02-28', # end date
interval='minute') # bar length
data.info()
data.head() # first five rows
data.tail() # final five rows
Only complete data rows are selected.
data.dropna(inplace=True) # deletes tows with NaN values
data.info() # DataFrame meta information
We next calculate the log returns in vectorized fashion.
rets = np.log(data / data.shift(1)).dropna() # log returns in vectorized fashion
rets.head()
Using Cufflinks
, we can plot the normalized financial time series as line plots for comparison.
cf.set_config_file(offline=True) # set the plotting mode to offline
data.normalize().iplot(kind='lines')