pandas 回测股票数据出错

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

用列表列表遮盖两列(Pandas df) - python

我有一个列表,其中包含恰好两个元素的列表。我想将DataFrame过滤到列表列表中[column_a, column_b]所在的行。例:输入:l = [ [0,1], [1,2] ] df = pd.DataFrame({'column_a' : [0, 1, 2], 'column_b' : [1, 3, 0]}) …

Pandas df根据整数索引列表对行和列进行重新排序 - python

我的数据框具有以下结构: col1 col2 col3 myindex apple A B C pear Ab Bb Cb turtle A1 B1 C1 现在,我得到两个列表,一个列表具有重新排序的列索引,一个列表具有重新排序的行索引,但是它们是整数,例如rowindices = [3,1,2]和colindices = [1,3,2]。现在应该根据这些索…

在 Pandas 中放置多列 - python

我正在尝试使用以下代码在熊猫数据框中按索引号删除多列(数据集中的第2列和第70列,分别索引为1和69):df.drop([df.columns[[1, 69]]], axis=1, inplace=True) 我收到以下错误:TypeError: unhashable type: 'Index' 在我的代码中,[1,69]突出显示并说:E…

如何更改索引和转置 Pandas - python

我是 Pandas 的新手,正在尝试在日期框架上进行一些转换,但到达了封闭路径。我的数据框是: entity_name request_status dcount 0 entity1 0 1 1 entity1 1 6 2 entity1 2 13 3 entity2 1 4 4 entity2 2 7 我需要此数据框如下所示:index 0 1 2 ent…

df.head()有时在 Pandas ,Python中不起作用 - python

我是Python和Pandas库的初学者,我对DataFrame的一些基本功能感到困惑。我有一个熊猫DataFrame,如下所示:>>>df.head() X Y unixtime 0 652f5e69fcb3 1 1346689910622 1 400292 1 1346614723542 2 1c9d02e4f14e 1 1346862…