if high[-1]>=X周期高点 and portfolio.buy_quantity==0 and portfolio.sell_quantity==0:
buy_open(context.s, "Market",0 ,unit,serial_id = 1)
context.entry = atr[-2]
context.num = 1
context.enterprice = close[-1]
if low[-1]<=X周期低点 and portfolio.sell_quantity==0 and portfolio.buy_quantity==0:
sell_open(context.s, "Market",0 ,unit,serial_id = 2)
context.entry = atr[-2]
context.num = 1
context.enterprice = close[-1]
#加仓,最高价比上次开仓价多0.5个atr(盈利加仓)
if portfolio.sell_quantity ==0 and portfolio.buy_quantity>0 and high[-1]>context.enterprice + 0.5*context.entry and context.num<4:
buy_open(context.s, "Market",0 ,unit,serial_id = 3)
context.num+=1
context.enterprice = close[-1]
if portfolio.buy_quantity==0 and portfolio.sell_quantity>0 and low[-1]<context.enterprice - 0.5*context.entry and context.num<4:
sell_open(context.s, "Market",0 ,unit,serial_id = 4)
context.num+=1
context.enterprice = close[-1]
#出场,跌破短周期低点平多
Y周期高点 = high[-context.Y-1:-1].max()
Y周期低点 = low[-context.Y-1:-1].min()
if portfolio.buy_quantity>0 and low[-1] < Y周期低点:
sell_close(context.s,"Market",0,portfolio.buy_quantity,serial_id = 5)
if portfolio.sell_quantity>0 and high[-1] > Y周期高点:
buy_close(context.s,"Market",0,portfolio.sell_quantity,serial_id = 6)
#止损,亏损幅度超过开仓2个atr幅度止损
if portfolio.buy_quantity>0 and low[-1] < context.enterprice - 2*context.entry:
sell_close(context.s,"Market",0,portfolio.buy_quantity,serial_id = 7)
if portfolio.sell_quantity>0 and high[-1] > context.enterprice + 2*context.entry:
buy_close(context.s,"Market",0,portfolio.sell_quantity,serial_id = 8)作者: admin 时间: 2023-3-6 16:46
get_portfolio返回是个对象。你对象名是portfolio,你应该对获取的对象进行再次处理。