B.append(0) else:B.append(1) elif B[-1]==-1: if dfi['High'][i]>dfi['rMAX8'][i]: B.append(0) else:B.append(-1) dfi['sig']=B B=[] for i in dfi.index: if dfi['sig'].ix[i]==0: B.append(0) elif dfi['sig'].ix[i]!=0: if B[-1]==0: B.append(100/(dfi['ATR'][i]*2)) else: B.append(B[-1]) else: B.append(B[-1]) dfi['invested']=B dfi['pch']=dfi['Adj Close'].pct_change(periods=1) dfi['simple']=(dfi['pch']*dfi['sig'])+1 dfi['lev']=(dfi['pch']*dfi['invested']*dfi['sig']*0.1)+1 dfi['simple_pnl']=dfi['simple'].cumprod() dfi['lev_pnl']=dfi['lev'].cumprod() print dfi.to_string() plt.subplot(111) plt.plot(dfi.index,dfi[['simple_pnl','lev_pnl']]) plt.show()
import numpy as np import talib from pandas.io.data import DataReader from datetime import datetime import pandas as pd import matplotlib.pyplot as plt f = lambda x: float(x) SPX = DataReader("SP500", "fred", datetime(2011,1,1), datetime(2012,1,11)).applymap(f).bfill() #SPX # SPX['ma']=SPX['SP500'].apply(abstract.Function('sma')) SPX['SMA'] = talib.MA(SPX.SP500, 15) print SPX.to_string() print talib.get_functions() #https://github.com/mrjbq7/ta-lib