pandas 回测股票数据出错

2020-09-08 10:26:03 +08:00
 yellowtail
money = 0
stock=0
pos = 0
df_tst['pnl']=0
df_tst['trade_times']=0
df_tst['pos']=0
df_tst['stock']=0
df_tst['money']=0
pnl=0
tt=0
for i,n in df_tst.iterrows():
if n.deal>0:
tt = tt + 1
if pos>0:
pnl = pos*n.open - stock
if pos<0:
pnl = pos*n.open - stock
money = money + pos*n.open#平仓
stock = 0
pos = 0
pos = pos + 100
money = money - 100*n.open
df_tst.pos.loc[i] = pos
df_tst.pnl.loc[i] = pnl
df_tst.money.loc[i] = money
df_tst.stock.loc[i] = pos*n.open
df_tst.trade_times.loc[i] = tt
if n.deal<0:
tt = tt + 1
if pos<0:
pnl = pos*n.open - stock
if pos>0:
pnl = pos*n.open - stock
money = money + pos*n.open#平仓
stock = 0
pos = 0
pos = pos - 100
money = money + 100*n.open
df_tst.pos.loc[i] = pos
df_tst.pnl.loc[i] = pnl
df_tst.money.loc[i] = money
df_tst.stock.loc[i] = pos*n.open
df_tst.trade_times.loc[i] = tt
else:
if pos>0:
pnl = pos*n.open - stock
stock = pos*n.open
if pos<0:
pnl = pos*n.open - stock
stock = pos*n.open
df_tst.pos.loc[i] = pos
df_tst.pnl.loc[i] = pnl
df_tst.money.loc[i] = money
df_tst.stock.loc[i] = stock
df_tst.trade_times.loc[i] = tt
709 次点击
所在节点    问与答
2 条回复
yellowtail
2020-09-08 10:31:42 +08:00
咋发出有格式的代码,捉急
m939594960
2020-09-08 11:31:07 +08:00
gists

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/705090

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX