通过sql创建的SCORE为什么无法传输到M4模块
由bqbkm8ac创建,最终由bq7zuymm 被浏览 8 用户
from bigmodule import M
import dai
# <aistudiograph>
# 初始化
# @param(id="m1", name="initialize")
def m1_initialize_bigquant_run(context):
from bigtrader.finance.commission import PerOrder
# 设置交易费率
context.set_commission(PerOrder(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
# 数据处理模块
# @param(id="m1", name="handle_data")
def m1_handle_data_bigquant_run(context, data):
import pandas as pd
# 判断是否为调仓周期
if not context.rebalance_period.is_signal_date(data.current_dt.date()):
return
# 读取当前日期数据
today_df = context.data[context.data["date"] == data.current_dt.strftime("%Y-%m-%d")]
target_instruments = set(today_df["instrument"])
# 获取当前持有股票
holding_instruments = set(context.get_account_positions().keys())
# 卖出不在目标持有列表中的股票
for instrument in holding_instruments - target_instruments:
context.order_target_percent(instrument, 0)
# 买入目标持有列表中的股票
for i, x in today_df.iterrows():
position = 0.0 if pd.isnull(x.position) else float(x.position)
context.order_target_percent(x.instrument, position)
# 股票池筛选模块
# @module(comment="使用基本信息对股票池过滤")
m2 = M.cn_stock_basic_selector.v8(
exchanges=["上交所", "深交所"],
list_sectors=["主板", "科创板", "创业板"],
indexes=["中证500", "上证指数", "创业板指", "深证成指", "上证50", "科创50"],
st_statuses=["正常"],
drop_suspended=True,
m_name="m2"
)
# 因子特征模块
# @module(comment="因子特征")
m3 = M.input_features_dai.v30(
input_1=m2.data,
mode="表达式",
expr="""
""", # Ensure triple quotes are correctly closed here.
expr_filters="",
expr_tables="cn_stock_prefactors",
extra_fields="date, instrument",
order_by="date, instrument",
sql = """
SELECT
date,
instrument,
0.4 * (
c_zscore(roe_avg_ttm) * 0.3 +
(1/c_pct_rank(pe_ttm)) * 0.3 +
(1/c_pct_rank(pb)) * 0.2 +
c_zscore(cce_ps_ttm) * 0.2
) + 0.6 * (
m_ta_sma(turn, 10) * 0.7 +
c_pct_rank(volume/m_avg(volume, 5)) * 0.3
) AS SCORE
FROM
cn_stock_prefactors
WHERE
st_status = 0
QUALIFY
COLUMNS(*) IS NOT NULL
ORDER BY
date,
instrument,
SCORE
""",
extract_data=False,
m_name="m3"
)
# 仓位分配模块
# @module(comment="持股数量、打分到仓位")
m4 = M.score_to_position.v4(
input_1=m3.data,
score_field="sorce",
hold_count=5,
position_expr="1 AS position",
total_position=1,
extract_data=False,
m_name="m4"
)
# 数据抽取模块
# @module(comment="抽取预测数据")
m5 = M.extract_data_dai.v18(
sql=m4.data,
start_date="2022-01-01",
start_date_bound_to_trading_date=True,
end_date="2024-12-31",
end_date_bound_to_trading_date=True,
before_start_days=90,
keep_before=False,
debug=False,
m_name="m5"
)
# 交易模块
# @module(comment="交易,日线,设置初始化函数和K线处理函数,以及初始资金、基准等")
m6 = M.bigtrader.v35(
data=m5.data,
start_date="",
end_date="",
initialize=m1_initialize_bigquant_run,
handle_data=m1_handle_data_bigquant_run,
capital_base=500000,
frequency="daily",
product_type="股票",
rebalance_period_type="交易日",
rebalance_period_days="20",
rebalance_period_roll_forward=True,
backtest_engine_mode="标准模式",
before_start_days=0,
volume_limit=1,
order_price_field_buy="open",
order_price_field_sell="open",
benchmark="沪深300指数",
plot_charts=True,
debug=False,
backtest_only=False,
m_name="m6"
)
# </aistudiograph>
\