#设定回撤值
withdraw = 0.03
#设定突破值
breakthrough =0.03
#设定账户资金
account = 10000
#持有仓位手数
position = 0
def buy(bar):
global account,position
print("{}: buy{}".format(bar.date,bar.close))
#一手价格
one = bar.close*100
position = account//one
account = account - (position*one)
def sell(bar):
global account,position
#一手价格
print("{}:sell{}".format(bar.date,bar.close))
one =bar.close*100
account+=position*one
position = 0
print("开始时间投资时间:",df.iloc[0]).apply(lambda x : x.days)
for date in df.index:
bar = df.loc[date]
if bar.pchange and bar.pchange > breakthrough and position ==0:
buy(bar)
elif bar.pchange and bar.pchange <withdraw and position > 0:
sell(bar)
print("最终可有现金:",account)
print("最终持有市值:",position*df.iloc[-1].close*100)
print("开始时间投资时间:",df.iloc[0]).apply(lambda x : x.days)
改成
print("开始时间投资时间:",df.iloc[0].apply(lambda x : x.days))
.iloc[0] 之后的)移到后面