%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn')
import matplotlib as mpl
mpl.rcParams['font.family'] = 'serif'
import warnings; warnings.simplefilter('ignore') #忽略警告信息;
import numpy as np
import pandas as pd
import tushare as ts
1. 数据准备 & 回测准备
data = ts.get_k_data('hs300', start = '2010-01-01', end='2017-06-30')[['date','close']]
data.rename(columns={'close': 'price'}, inplace=True)
data.set_index('date', inplace = True)
2. 策略开发思路
data['returns'] = np.log(data['price'] / data['price'].shift(1))
data['position'] = np.sign(data['returns'])
#关键语句,np.sign()很多地方用到;向量化;
data['s