3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
公募 REITs(基础设施公募基金)兼具股性和债性。由于其在二级市场的交易价格由市场供求关系决定,而其基金份额净值(NAV)由底层资产评估决定,因此二级市场交易价格经常会偏离基金净值,产生折价(Discount)或溢价(Premium)。
当折价率过大时(二级市场价格显著低于净值),REITs 具备较高的安全边际与套利空间,投资者可以买入等待价值回归;反之,当溢价率过高时,则可以卖出避险。在 PTrade 平台中,我们可以通过 get_reits_list() 接口动态获取全市场 REITs 标的,并结合行情数据实现自动化套利。
initialize 阶段,使用 get_reits_list() 动态获取沪深两市所有的公募 REITs 代码,并使用 set_universe() 设定股票池。handle_data 阶段,通过 get_history() 获取历史价格,计算均值作为价值中枢。以下是基于 PTrade 规范编写的公募 REITs 均值回归(折溢价模拟)套利策略源码:
# -*- coding: utf-8 -*-
def initialize(context):
# 1. 获取沪深市场所有的公募 REITs 基金代码列表
g.reits_pool = get_reits_list()
log.info("获取到全市场公募 REITs 数量: %d" % len(g.reits_pool))
# 2. 设置策略操作的股票池
set_universe(g.reits_pool)
# 设定套利阈值
g.buy_threshold = 0.97 # 低于均价 3% 买入
g.sell_threshold = 1.03 # 高于均价 3% 卖出
def handle_data(context, data):
# 遍历 REITs 标的池
for reit in g.reits_pool:
# 确保标的在当前行情数据中
if reit not in data:
continue
# 3. 获取过去 10 个交易日的历史收盘价
df = get_history(10, '1d', 'close', reit, fq=None, include=False)
if df is None or len(df) < 10:
continue
# 计算 10 日价格均值作为价值中枢
mean_price = df['close'].mean()
# 获取当前最新价格
current_price = data[reit]['close']
# 获取当前持仓信息
position = get_position(reit)
# 4. 交易逻辑判断
# 情况 A:无持仓,且价格显著低于均值(折价),买入
if position.amount == 0 and current_price < mean_price * g.buy_threshold:
cash = context.portfolio.cash
# 每次分配可用资金的 10% 买入单只 REIT
invest_value = cash * 0.1
if invest_value > 1000:
order_value(reit, invest_value)
log.info("REITs [%s] 出现折价机会,当前价: %.3f, 均价: %.3f,执行买入。" % (reit, current_price, mean_price))
# 情况 B:持有仓位,且价格显著高于均值(溢价),卖出平仓
elif position.amount > 0 and current_price > mean_price * g.sell_threshold:
order_target(reit, 0)
log.info("REITs [%s] 出现溢价高估,当前价: %.3f, 均价: %.3f,执行卖出平仓。" % (reit, current_price, mean_price))
set_volume_ratio() 限制成交比例,避免冲击成本过大。limit_price 时需注意精度处理,避免委托失败。