这是handle_bar的代码,其他部分我都没动。我就是读取csv文件里面的股票代码进行交易
def handle_bar(context):
# 开始编写你的主要的算法逻辑。
#使用buy_open、sell_close等方法下单
#下单示例:
#buy_open(context.s1, "Market", volume = 100) # 市价开多
#buy_open(context.s1, "Limit", 25.45, 100) # 限价开多
now_date=context.now.strftime('%Y%m%d')
#print(now_date)
#print(context.now.strftime('%H%M')=='0931')
if context.now.strftime('%H%M')=='0931':
#print('yes')
code=[]
a=read_csv('C://Users//ray//OneDrive//shouyi_5.csv',index_col='买入时间')
for i in range(1,6):
singlecode=a.loc[int(now_date)][str(i)]
code.append(singlecode[7:9]+singlecode[0:6])
for i in range(5):
#print('yes')
aa=buy_open(code[i], "Market",0, volume = 100,serial_id = 1)
print(code[i])
if context.now.strftime('%H%M')=='1458':
code=[]
a=read_csv('C://Users//ray//OneDrive//shouyi_5.csv',index_col='卖出时间')
for i in range(1,6):
singlecode=a.loc[int(now_date)][str(i)]
code.append(singlecode[7:9]+singlecode[0:6])
for i in range(5):
sell_close (code[i], "Market",0, volume = 100)
pass