38 lines
997 B
Python
38 lines
997 B
Python
import pandas as pd
|
|
import numpy as np
|
|
|
|
def run():
|
|
"""
|
|
简单动量策略示例:
|
|
- 买入条件:过去 3 日涨幅 > 2%
|
|
- 卖出条件:过去 3 日涨幅 < -2%
|
|
"""
|
|
# 模拟历史行情数据
|
|
dates = pd.date_range(start='2026-01-01', periods=20)
|
|
prices = pd.Series(np.random.uniform(50, 150, size=20), index=dates)
|
|
|
|
# 计算过去 3 日涨幅
|
|
returns = prices.pct_change(3).fillna(0)
|
|
|
|
# 策略信号
|
|
signal = (returns > 0.02).astype(int) - (returns < -0.02).astype(int)
|
|
|
|
# 简单净值计算
|
|
capital = 10000
|
|
position = 0
|
|
nav = []
|
|
for i in range(len(prices)):
|
|
if signal[i] == 1:
|
|
position = capital / prices[i]
|
|
elif signal[i] == -1:
|
|
capital = position * prices[i]
|
|
position = 0
|
|
nav.append(capital + position * prices[i])
|
|
|
|
result = pd.DataFrame({
|
|
'date': dates,
|
|
'price': prices,
|
|
'signal': signal,
|
|
'nav': nav
|
|
})
|
|
return result |