今天看了一下quantdigger,pyalgotrade的回测,瞬间陷入痛苦之中,对于不熟悉的人,估计需要花很长时间来学习。我决定自己建造一个回测系统,参考quantdigger,pyalgotrade的优点。这是写的一个简单的均线回测平台,但是效率太低,也没有考虑手续费等问题,还有很遥远的距离需要走。
# -*- coding: utf-8 -*- """ Created on Tue Aug 22 15:57:36 2017 @author: Administrator """ import pandas as pd import numpy as np import matplotlib.pyplot as plt ########################################获取数据 data=pd.read_csv('C:/Users/Administrator/Desktop/rb00030.csv',encoding='gbk') data=data.iloc[::,:7] data.columns=['time','oepn','high','low','close','amt','opi'] data.head() ########################################确定交易信号 data['ma5']=data['close'].rolling(5).mean() data['ma20']=data['close'].rolling(20).mean() for i in range(len(data)): if i>1: if data.ix[i-1,'ma5']>data.ix[i-1,'ma20'] and data.ix[i,'ma5']<=data.ix[i,'ma20']: data.ix[i,'sk']=-1 if data.ix[i-1,'ma5']<data.ix[i-1,'ma20'] and data.ix[i,'ma5']>=data.ix[i,'ma20']: data.ix[i,'sk']=1 ''' for i in range(len(data)): if i>0: if data.ix[i-1,'sk']==-1 and data.ix[i,'sk']!=1: data.ix[i,'position']=-1 if data.ix[i-1,'sk']==-1 and data.ix[i,'sk']==1: data.ix[i,'position']=1 if data.ix[i-1,'sk']==1 and data.ix[i,'sk']!=-1: data.ix[i,'position']=1 if data.ix[i-1,'sk']==1 and data.ix[i,'sk']==-1: data.ix[i,'position']=-1 ''' data['position']='' for i in range(len(data)): if i>0: if data.ix[i,'sk']==1: data.ix[i,'position']=1 if data.ix[i,'sk']==-1: data.ix[i,'position']=-1 if data.ix[i,'sk']!=1 and data.ix[i,'sk']!=-1: data.ix[i,'position']=data.ix[i-1,'position'] for i in range(len(data)): if i>0: if data.ix[i-1,'position']==1: data.ix[i,'profit']=data.ix[i,'close']-data.ix[i-1,'close'] if data.ix[i-1,'position']==-1: data.ix[i,'profit']=-data.ix[i,'close']+data.ix[i-1,'close'] data.profit.cumsum().plot()