原创内容第789篇,专注量化投资、个人成长与财富自由。
昨天我们复现了策略:
年化36.93%基于趋势(年化收益*R2拟合度)评分的核心资产轮动策略复现(代码+数据)
通过低代码平台,我们可以直接配置出来:
回测结果一样:
这是deepseek写的因子:
@calc_by_symbol
def trend_score(close, period=25):
"""
向量化计算趋势评分:年化收益率 × R平方
:param close: 收盘价序列(np.array或pd.Series)
:param period: 计算窗口长度,默认25天
:return: 趋势评分数组,长度与输入相同,前period-1位为NaN
"""
if len(close) < period:
return np.full_like(close, np.nan)
y = np.log(close)
windows = np.lib.stride_tricks.sliding_window_view(y, window_shape=period)
x = np.arange(period)
# 预计算固定值
n = period
sum_x = x.sum()
sum_x2 = (x ** 2).sum()
denominator = n * sum_x2 - sum_x ** 2
# 滑动窗口统计量
sum_y = windows.sum(axis=1)
sum_xy = (windows * x).sum(axis=1)
# 回归系数
slope = (n * sum_xy - sum_x * sum_y) / denominator
intercept = (sum_y - slope * sum_x) / n
# 年化收益率
annualized_returns = np.exp(slope * 250) - 1
# R平方计算
y_pred = slope[:, None] * x + intercept[:, None]
residuals = windows - y_pred
ss_res = np.sum(residuals ** 2, axis=1)
sum_y2 = np.sum(windows ** 2, axis=1)
ss_tot = sum_y2 - (sum_y ** 2) / n
r_squared = 1 - (ss_res / ss_tot)
r_squared = np.nan_to_num(r_squared, nan=0.0) # 处理零方差情况
# 综合评分
score = annualized_returns * r_squared
# 对齐原始序列长度
full_score = np.full_like(y, np.nan)
full_score = pd.Series(index=close.index)
full_score[period - 1:] = score
return full_score
celery的定时任务也比较简单,比apscheduler还轻:
cel.conf.timezone = 'Asia/Shanghai'
cel.conf.beat_schedule = {
'run-my-task-every-day-at-18-00': {
'task': 'common.celery_tasks.task_checker', # 任务路径
'schedule': crontab(hour=18, minute=0), # 每天18:00运行
#'schedule': timedelta(seconds=10), # 每