ICIV股票预测竞赛SDK - 用于开发和测试交易策略
Project description
ICIV Stock Predictor SDK
股票预测竞赛SDK,用于开发和测试交易策略。
安装
pip install iciv-predictor
或从源码安装:
git clone https://github.com/your-repo/iciv-predictor-sdk.git
cd iciv-predictor-sdk
pip install -e .
快速开始
1. 创建预测器
from iciv_predictor import BasePredictor, PredictionOutput
import numpy as np
class MyPredictor(BasePredictor):
def __init__(self, df, predict_steps=48):
super().__init__(df, predict_steps)
# 初始化你的参数
self.short_ma = 12
self.long_ma = 48
def predict_step(self, current_idx):
"""
预测当前时间点
Args:
current_idx: 当前索引,只能使用历史数据 df[:current_idx+1]
Returns:
PredictionOutput 或 None
"""
if current_idx < self.long_ma:
return None
# 获取历史收盘价
closes = self.get_close_prices(current_idx)
# 计算均线
short_avg = closes[-self.short_ma:].mean()
long_avg = closes[-self.long_ma:].mean()
# 判断方向
if short_avg > long_avg:
direction = 1 # 看涨,买入
elif short_avg < long_avg:
direction = -1 # 看跌,卖出
else:
direction = 0 # 持有
return PredictionOutput(
predict_from_idx=current_idx,
predicted_prices=np.zeros(self.predict_steps),
predicted_returns=np.zeros(self.predict_steps),
confidence=0.6,
direction=direction
)
2. 本地测试
iciv-test my_predictor.py --data 000021_SZ.csv
或使用Python:
from iciv_predictor import Backtester
import pandas as pd
# 加载数据
df = pd.read_csv('000021_SZ.csv')
df['trade_time'] = pd.to_datetime(df['datetime'])
# 创建预测器
predictor = MyPredictor(df)
# 运行回测
backtester = Backtester()
result = backtester.run(df, predictor)
print(f"收益率: {result.total_return:.2f}%")
print(f"最大回撤: {result.max_drawdown:.2f}%")
print(f"交易次数: {result.total_trades}")
3. 提交到平台
测试通过后,将你的 .py 文件提交到 https://stock.w3drop.com
API 参考
BasePredictor
基类,所有预测器必须继承此类。
方法:
| 方法 | 说明 |
|---|---|
__init__(df, predict_steps=48) |
初始化,predict_steps为预测步长 |
predict_step(current_idx) |
必须实现,返回PredictionOutput |
get_historical_data(idx) |
获取历史DataFrame |
get_close_prices(idx) |
获取历史收盘价数组 |
get_returns(idx) |
获取历史收益率数组 |
PredictionOutput
预测输出结构。
字段:
| 字段 | 类型 | 说明 |
|---|---|---|
predict_from_idx |
int | 预测起始索引 |
predicted_prices |
np.ndarray | 预测价格序列 |
predicted_returns |
np.ndarray | 预测收益率序列 |
confidence |
float | 置信度 (0-1) |
direction |
int | 交易信号: 1=买入, -1=卖出, 0=持有 |
回测规则
- 预测频率: 每48步(约4小时)预测一次
- 测试集: 使用后20%数据
- 交易逻辑:
- direction=1 且无持仓 → 买入
- direction=-1 且有持仓 → 卖出
- 手续费: 买卖各万分之三
- 强制平仓: 测试期结束时平仓
注意事项
⚠️ 禁止使用未来数据!predict_step(idx) 只能访问 idx 及之前的数据。
# ✅ 正确
closes = self.get_close_prices(current_idx)
# ❌ 错误 - 访问了未来数据
future = self.df['close'].iloc[current_idx + 10]
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
iciv_predictor-0.1.0.tar.gz
(7.8 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file iciv_predictor-0.1.0.tar.gz.
File metadata
- Download URL: iciv_predictor-0.1.0.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95ae2baf6f6f1457eed6b5157d438380796284543785fd93ccd76cf6d8e81b5c
|
|
| MD5 |
1ddc9aff4fd2e78547fc3465ec1c2ec3
|
|
| BLAKE2b-256 |
d53912467e270710b825d6c1ba40b0a691377705223d3afd67b7794635b55d69
|
File details
Details for the file iciv_predictor-0.1.0-py3-none-any.whl.
File metadata
- Download URL: iciv_predictor-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
addacde695ba326ea94da72bb589ae4c57e70069d1839fc78860d3994f74337a
|
|
| MD5 |
c3c19b4a972f1f2ed04105d0efabdd6f
|
|
| BLAKE2b-256 |
5fa88a2f223870f533b264c35a48fccc7458939570643da606dc5845cd61146b
|