V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
yellowtail
V2EX  ›  问与答

pandas 回测股票数据出错

  •  
  •   yellowtail · 2020-09-08 10:26:03 +08:00 · 707 次点击
    这是一个创建于 1538 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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
    yellowtail
        1
    yellowtail  
    OP
       2020-09-08 10:31:42 +08:00
    咋发出有格式的代码,捉急
    m939594960
        2
    m939594960  
       2020-09-08 11:31:07 +08:00
    gists
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2860 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:21 · PVG 17:21 · LAX 01:21 · JFK 04:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.