专栏名称: 七年实现财富自由
七年,经过十万小时刻意练习,足矣在任何领域成为专家。 七年,成为自己的财富管理专家。 七年,实现财富自由。
目录
相关文章推荐
高校人才网V  ·  本硕博有岗,中国农科院华东中心科研团队诚聘 ·  3 天前  
山东省交通运输厅  ·  鲁B货车畅行国际物流大通道 ·  22 小时前  
51好读  ›  专栏  ›  七年实现财富自由

结合deepseek因子开发,“零代码”复现年化36.93%的策略,只需要5秒

七年实现财富自由  · 公众号  ·  · 2025-02-07 10:26

正文

原创内容第789篇,专注量化投资、个人成长与财富自由。

昨天我们复现了策略: 年化36.93%基于趋势(年化收益*R2拟合度)评分的核心资产轮动策略复现(代码+数据)

通过低代码平台,我们可以直接配置出来:

回测结果一样:

这是deepseek写的因子:

@calc_by_symbol
def trend_score(close, period=25):
"""
向量化计算趋势评分:年化收益率 × R平方
:param close: 收盘价序列(np.arraypd.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), #






请到「今天看啥」查看全文