【平台使用】新版盘前处理函数中无法找到zipline
由lookit创建,最终由small_q 被浏览 9 用户
1.0.0回测中盘前处理函数如下:
from zipline.finance.order import Order
#插入定单
def insert_order(context,date,instr,amount):
order = Order(
dt = pd.to_datetime(date+" 09:30:00"),
asset=context.symbol(instr),
amount=-amount,
stop=None,
limit=None,
price_field='open')
try:
context.blotter.open_orders\[order.asset\].append(order)
except Exception:
context.blotter.open_orders\[order.asset\] = \[order\]
context.blotter.orders\[order.id\] = order
context.blotter.new_orders.append(order)
def bigquant_run(context, data):
from datetime import timedelta
#timedeltaabst
today=data.current_dt.strftime('%Y-%m-%d')
bm_df=context.bm_df
last_trade_day=bm_df\[bm_df.date<today\].iloc\[-1\].date
#yesterday=data.previous_day.strftime('%Y-%m-%d')
today=data.current_dt.strftime('%Y-%m-%d')
now_bm = context.bm_df\[context.bm_df.date==last_trade_day\]
#这个有问题了,不可能提前得到当天的大盘收盘价,这个是未来数据
context.bm_risk = 0
#大盘风控判断
if(now_bm.bm_ret.iloc\[0\]<0):
context.bm_risk = 1
# 得到当前未完成订单
for orders in get_open_orders().values():
# 循环,撤销订单
for _order in orders:
ins=str(_order.sid.symbol)
if data.can_trade(_order.sid) and _order.amount>0:
#大盘风控取消买单
cancel_order(_order)
print(today,'大盘风控取消买单',ins)
if data.can_trade(_order.sid) and _order.amount<0:#卖单由后续统一处理,先取消
#大盘风控取消卖单
cancel_order(_order)
print(today,'大盘风控取消卖单',ins)
#====卖出股票
stock_hold_now = {e.symbol:p.amount for e, p in context.perf_tracker.position_tracker.positions.items()}
print(today,"=======风控卖出所有的股票:",stock_hold_now)
for instr,amount in stock_hold_now.items():
#插入定单
insert_order(context,today,instr,amount)
然而在3.0.0中无法正常运行,提示找不到zipline模块,请问后续如何修改?
\