Python for Finance: Analyze Big Financial Data

(Elle) #1
Figure 3-6. The S&P 500 index with 42d and 252d trend lines

Our basic data set is mainly complete, such that we now can devise a rule to generate


trading signals. The rule says the following:


Buy signal (go long)


the 42d trend is for the first time SD points above the 252d trend.


Wait (park in cash)


the 42d trend is within a range of +/– SD points around the 252d trend.


Sell signal (go short)


the 42d trend is for the first time SD points below the 252d trend.


To this end, we add a new column to the pandas DataFrame object for the differences


between the two trends. As you can see, numerical operations with pandas can in general


be implemented in a vectorized fashion, in that one can take the difference between two


whole columns:


In  [ 39 ]: sp500[‘42-252’] =   sp500[‘42d’]    -   sp500[‘252d’]
sp500[‘42-252’].tail()
Out[39]: Date
2014-04-08 125.22
2014-04-09 125.87
2014-04-10 125.72
2014-04-11 124.72
2014-04-14 123.89
Name: 42-252, dtype: float64

On the last available trading date the 42d trend lies well above the 252d trend. Although


the number of entries in the two trend columns is not equal, pandas takes care of this by


putting NaN values at the respective index positions:


In  [ 40 ]: sp500[‘42-252’].head()
Out[40]: Date
2000-01-03 NaN
2000-01-04 NaN
2000-01-05 NaN
2000-01-06 NaN
2000-01-07 NaN
Name: 42-252, dtype: float64

To make it more formal, we again generate a new column for what we call a regime. We

Free download pdf