
get_history和get_price两个接口,就能轻松获取指数的核心行情数据,不管是沪深 300、上证指数,还是行业指数,都能精准查询,今天就把具体用法讲透~definitialize(context):# 定义要查询的指数:沪深300指数(代码000300.XBHS,指数尾缀为XBHS) g.index_code = '000300.XBHS'defhandle_data(context, data):# 示例1:获取沪深300指数过去10天的每日收盘价 index_close = get_history(10, frequency='1d', field='close', security_list=g.index_code) log.info("沪深300近10天收盘价:") log.info(index_close)# 示例2:获取沪深300指数过去5周的开盘价和收盘价 index_ohlc = get_history(5, frequency='1w', field=['open', 'close'], security_list=g.index_code) log.info("沪深300近5周开收盘价:") log.info(index_ohlc)definitialize(context): g.stock_code = '600570.SS'# 恒生电子 g.index_code = '000300.XBHS'# 沪深300指数 set_universe(g.stock_code)defhandle_data(context, data):# 获取沪深300指数近5天收盘价,计算平均值 index_data = get_history(5, '1d', 'close', g.index_code) index_avg = index_data['close'].mean()# 获取当前指数最新价 current_index = data[g.index_code]['close']# 逻辑:指数突破5日均值,买入股票if current_index > index_avg and get_position(g.stock_code).amount == 0: order_value(g.stock_code, context.portfolio.cash) log.info(f"指数突破均值{index_avg},买入{g.stock_code}")definitialize(context): g.stock_code = '000001.SZ'# 平安银行 set_universe(g.stock_code)# 将上证指数(000001.SS)设为策略基准,替代默认的沪深300 set_benchmark('000001.SS')defhandle_data(context, data):# 策略逻辑(示例:简单买入持有)if get_position(g.stock_code).amount == 0: order_value(g.stock_code, context.portfolio.cash)
往期推荐









欢迎扫码咨询
股票、两融、量化、基金等业务

惊喜佣金等你来



点个在看你最好看
