最近在写论文,研究股票技术分析,需要做回测分析,所以我需要通过设置买入和卖出条件来形成交易策略,在网上搞了一个金叉死叉的策略代码,但是用不了,原因是金叉死叉只需要对比截面数据就行,而我设定的条件是:今天的值>昨天的>前天的,属于时间序列,想问下怎么来写这个代码
#以下是原代码
def get_stock(zip_file,code):
with zipfile.ZipFile(zip_file) as f:
fname = [f1.filename for f1 in f.filelist if code in f1.filename]
if not fname:
return None
data = f.read(fname[0]).decode('gbk')
data = data.split('\n')
if len(data) < 2:
return None
data0 = data[0].split(',')
if data0[0] != '代码':
return None
stock = {
'code':code
,'name':data[1].split(',')[1]
,'detail':{
'date':[]
,'close':[]
,'macd_dif':[]
,'macd_dea':[]
,'macd_macd':[]
}
}
date_index = data0.index('日期')
close_index = data0.index('收盘')
dif_index = data0.index('MACD_DIF')
dea_index = data0.index('MACD_DEA')
macd_index = data0.index('MACD_MACD')
for r in data[1:]:
r = r.split(',')
if len(r) < 10:
continue
try:
date1 = r[date_index]
close1 = float(r[close_index])
dif = float(r[dif_index])
dea = float(r[dea_index])
macd = float(r[macd_index])
except:
continue
stock['detail']['date'].append(date1)
stock['detail']['close'].append(close1)
stock['detail']['macd_dif'].append(dif)
stock['detail']['macd_dea'].append(dea)
stock['detail']['macd_macd'].append(macd)
return stock
def buy_check(detail,cur_index):
# 判断是否金叉
if detail['macd_dif'][cur_index] > detail['macd_dea'][cur_index] and detail['macd_dif'][cur_index - 1] < detail['macd_dea'][cur_index - 1]:
return True
else:
return False
if detail['macd_dif'][cur_index] > detail['macd_dea'][cur_index] and detail['macd_dif'][cur_index - 1] < detail['macd_dea'][cur_index - 1]:
return True
else:
return False
#后面就略了,主要是最后这一个,我想改变成时间序列的条件,应该用什么样的语法完成呢?
if 今天数据>昨天数据>前天数据
return True
else:
return False