from PythonApi import *
import pandas as pd
def init(context):
context.days = 0
pass
# before_trading此函数会在每天基准合约的策略交易开始前被调用,当天只会被调用一次。--(选择实现)
def before_trading(context):
pass
# 你选择的品种的数据更新将会触发此段逻辑,例如日或分钟历史数据切片或者是实时数据切片更新。--(必须实现)
def handle_bar(context):
# 平仓部分
for i in get_portfolio_book(2, ""):
portfolio = get_portfolio (i, 2)
# 开盘就平
if portfolio.buy_today_quantity>0:
sell_close(i, "Market",0 ,portfolio.buy_today_quantity,serial_id = 1)
#开仓部分
hour = context.now.hour
minute = context.now.minute
days = context.now.day
if hour >= 9 and minute >= 31 and context.days != days:
code_list = context.universe
dic = {}
for i in code_list:
close = history_bars(i,2,'1d','close')
close_1m = history_bars(i,2,'1m','close')
if len(close) < 2 or len(close_1m) < 2:
continue
zf = close_1m[-1]/close[-1]
if zf < 0.91:
continue
if zf > 0.93:
continue
portfolio = get_portfolio (i, 2)
if portfolio.buy_quantity==0 and context.days != days :
buy_open(i, "Market",0 ,amount=10*10000,serial_id = 2)
context.days = days
break
def after_trading(context):
pass