After some time play around and writing some code around portfolio selection, following Markowitz principles and backtesting this with data taken from Yahoo finance ...it is time to retake it in a more systematic approach.
I had a running code that was doing reasonable fine...and just to keep the focus made a couple of stock investments following the algorithm outcome
Then I lost some momentum last April as Yahoo finance API service stopped working...
but now it is time too start from scratch.
- Anaconda 64bit installed with python 3.6
- Pycharm installed
- Pandas, numpy
- Quantopian account active
Environment created with:
- pandas
- pandas-reader
- matplotlib
- quandl
Once everything is installed, I check out the hability to readout trading data..
I follow the example at
http://www.learndatasci.com/python-finance-part-yahoo-finance-api-pandas-matplotlib/
Jupyter QtConsole 4.3.1
Python 3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 12:30:02) [MSC v.1900 64 bit
In [1]: from pandas_datareader import data
...: import matplotlib.pyplot as plt
...: import pandas as pd
...:
In [2]: # Define the instruments to download. We would like to see Apple, Microsoft and the S&P500 index.
...: tickers = ['AAPL', 'MSFT', '^GSPC']
...:
...: # Define which online source one should use
...: data_source = 'yahoo'
...:
...: # We would like all available data from 01/01/2000 until 12/31/2016.
...: start_date = '2000-01-01'
...: end_date = '2016-12-31'
...:
...: # User pandas_reader.data.DataReader to load the desired data. As simple as that.
...: panel_data = data.DataReader(tickers, data_source, start_date, end_date)
...:
In [3]: panel_data
Out[3]:
<class 'pandas.core.panel.Panel'>
Dimensions: 6 (items) x 4278 (major_axis) x 3 (minor_axis)
Items axis: Adj Close to Volume
Major_axis axis: 2016-12-30 00:00:00 to 1999-12-31 00:00:00
Minor_axis axis: AAPL to ^GSPC
In [4]: panel_data
Out[4]:
<class 'pandas.core.panel.Panel'>
Dimensions: 6 (items) x 4278 (major_axis) x 3 (minor_axis)
Items axis: Adj Close to Volume
Major_axis axis: 2016-12-30 00:00:00 to 1999-12-31 00:00:00
Minor_axis axis: AAPL to ^GSPC
In [5]: # Getting just the adjusted closing prices. This will return a Pandas DataFrame
...: # The index in this DataFrame is the major index of the panel_data.
...: adj_close = panel_data.ix['Adj Close']
...:
...: # Getting all weekdays between 01/01/2000 and 12/31/2016
...: all_weekdays = pd.date_range(start=start_date, end=end_date, freq='B')
...:
...: # How do we align the existing prices in adj_close with our new set of dates?
...: # All we need to do is reindex adj_close using all_weekdays as the new index
...: adj_close = adj_close.reindex(all_weekdays)
...:
...: # Reindexing will insert missing values (NaN) for the dates that were not present
...: # in the original set. To cope with this, we can fill the missing by replacing them
...: # with the latest available price for each instrument.
...: adj_close = adj_close.fillna(method='ffill')
...:
C:\Users\pedro\Anaconda3\envs\qfinance\lib\site-packages\ipykernel_launcher.py:3: DeprecationWarning:
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing
See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated
This is separate from the ipykernel package so we can avoid doing imports until
In [6]: # Get the MSFT time series. This now returns a Pandas Series object indexed by date.
...: msft = adj_close.ix[:, 'MSFT']
...: # Calculate the 20 and 100 days moving averages of the closing prices
...: short_rolling_msft = msft.rolling(window=20).mean()
...: long_rolling_msft = msft.rolling(window=100).mean()
...:
...: # Plot everything by leveraging the very powerful matplotlib package
...: fig = plt.figure()
...: ax = fig.add_subplot(1,1,1)
...: ax.plot(msft.index, msft, label='MSFT')
...: ax.plot(short_rolling_msft.index, short_rolling_msft, label='20 days rolling')
...: ax.plot(long_rolling_msft.index, long_rolling_msft, label='100 days rolling')
...: ax.set_xlabel('Date')
...: ax.set_ylabel('Adjusted closing price ($)')
...: ax.legend()
...:
C:\Users\pedro\Anaconda3\envs\qfinance\lib\site-packages\ipykernel_launcher.py:2: DeprecationWarning:
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing
See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated
Out[6]: <matplotlib.legend.Legend at 0x1fbcbf89cc0>
In [7]: plt.show()
-----------------------------------------------------------------------------------------------------------------

No comments:
Post a Comment