111 lines
3.4 KiB
Python
111 lines
3.4 KiB
Python
"""
|
||
使用示例:时间序列因子挖掘流程
|
||
"""
|
||
from pipeline import FactorPipeline
|
||
from factors import FactorMiner, create_default_factors
|
||
|
||
# 方式1:使用默认流程(最简单)
|
||
def example_simple():
|
||
"""简单示例"""
|
||
pipeline = FactorPipeline(
|
||
ret_horizon=1, # 未来1期收益率
|
||
ic_window=30, # IC计算窗口
|
||
commission=0.001, # 手续费0.1%
|
||
slippage=0.0005 # 滑点0.05%
|
||
)
|
||
|
||
# 运行完整流程
|
||
results = pipeline.run_full_pipeline(
|
||
file_path="ETH_USDT-1h.feather",
|
||
min_ic=0.01, # 最小IC阈值
|
||
min_tstat=1.5, # 最小t统计量
|
||
weight_method='risk_parity', # 权重方法:risk_parity, regression, equal
|
||
buy_threshold=0.8, # 买入阈值(标准差倍数)
|
||
sell_threshold=-0.8 # 卖出阈值(标准差倍数)
|
||
)
|
||
|
||
return results
|
||
|
||
|
||
# 方式2:分步骤执行(更灵活)
|
||
def example_step_by_step():
|
||
"""分步骤示例"""
|
||
pipeline = FactorPipeline(ret_horizon=1, ic_window=30)
|
||
|
||
# 步骤1:加载和预处理数据
|
||
pipeline.load_and_preprocess("ETH_USDT-1h.feather")
|
||
|
||
# 步骤2:因子挖掘(可以使用自定义因子)
|
||
custom_miner = create_default_factors()
|
||
# 可以在这里添加自定义因子
|
||
# custom_miner.register_rule_factor('CUSTOM', your_custom_function)
|
||
pipeline.mine_factors(custom_miner)
|
||
|
||
# 步骤3:因子检验
|
||
pipeline.validate_factors(min_ic=0.01, min_tstat=1.5)
|
||
|
||
# 步骤4:因子组合
|
||
pipeline.combine_factors(weight_method='risk_parity')
|
||
|
||
# 步骤5:生成信号
|
||
signals = pipeline.generate_signals(buy_threshold=0.8, sell_threshold=-0.8)
|
||
|
||
# 步骤6:回测
|
||
backtest_results = pipeline.backtest(signals)
|
||
|
||
return {
|
||
'factors': pipeline.factors,
|
||
'score': pipeline.score,
|
||
'signals': signals,
|
||
'backtest': backtest_results
|
||
}
|
||
|
||
|
||
# 方式3:自定义因子
|
||
def example_custom_factors():
|
||
"""自定义因子示例"""
|
||
from factors import RuleFactor
|
||
import pandas as pd
|
||
import numpy as np
|
||
|
||
# 定义自定义因子函数
|
||
def my_custom_factor(data: pd.DataFrame) -> pd.Series:
|
||
"""自定义因子:价格与均线的距离"""
|
||
return (data['close'] - data['ema8']) / data['ema8']
|
||
|
||
# 创建因子挖掘器
|
||
miner = create_default_factors()
|
||
|
||
# 注册自定义因子
|
||
miner.register_rule_factor('CUSTOM_DISTANCE', my_custom_factor)
|
||
|
||
# 使用自定义因子挖掘器
|
||
pipeline = FactorPipeline()
|
||
pipeline.load_and_preprocess("ETH_USDT-1h.feather")
|
||
pipeline.mine_factors(custom_miner=miner)
|
||
pipeline.validate_factors()
|
||
pipeline.combine_factors()
|
||
pipeline.backtest()
|
||
|
||
return pipeline
|
||
|
||
|
||
if __name__ == "__main__":
|
||
# 运行简单示例
|
||
print("运行简单示例...")
|
||
results = example_simple()
|
||
|
||
# 保存结果
|
||
if results['factors'] is not None:
|
||
results['factors'].to_csv("factors_output.csv")
|
||
print("\n因子数据已保存到 factors_output.csv")
|
||
|
||
if results['score'] is not None:
|
||
results['score'].to_csv("score_output.csv")
|
||
print("综合得分已保存到 score_output.csv")
|
||
|
||
if results['backtest'] is not None and 'equity' in results['backtest']:
|
||
results['backtest']['equity'].to_csv("equity_curve.csv")
|
||
print("权益曲线已保存到 equity_curve.csv")
|
||
|