@
Livid 搞到 15m 和 1h 的 BTC 日内数据了,2021 年到现在,大概几十 MB ,我测试了下你的策略效果都是负的。
https://imgur.com/a/dOfRibG无论设置到 2021 年开始,还是 2022 年牛市开始,这种高频策略最终的收益都是亏损的。策略代码如下:
```python
class RsiStrategy(bt.Strategy):
params = (
('rsi_period', 14),
('rsi_overbought', 70),
('rsi_oversold', 30),
)
def __init__(self):
# 为两个不同的时间框架数据分别计算 RSI
self.rsi_15m = bt.indicators.RSI(self.datas[0].close, period=self.params.rsi_period)
self.rsi_1h = bt.indicators.RSI(self.datas[1].close, period=self.params.rsi_period)
def next(self):
# 获取当前时间戳
timestamp = self.datas[0].datetime.datetime(0)
# 买入条件:当 15 分钟和 1 小时的 RSI 都低于 30 时
if not self.position:
if self.rsi_15m[0] < self.params.rsi_oversold and self.rsi_1h[0] < self.params.rsi_oversold:
self.buy(data=self.datas[0]) # 使用 15 分钟数据源的价格买入
logging.info(f"{timestamp} BUY EXECUTED, Price: {self.datas[0].close[0]}, RSI 15m: {self.rsi_15m[0]}, RSI 1h: {self.rsi_1h[0]}")
# 卖出条件:当 15 分钟和 1 小时的 RSI 都高于 70 时
elif self.rsi_15m[0] > self.params.rsi_overbought and self.rsi_1h[0] > self.params.rsi_overbought:
self.close(data=self.datas[0]) # 使用 15 分钟数据源的价格卖出
logging.info(f"{timestamp} SELL EXECUTED, Price: {self.datas[0].close[0]}, RSI 15m: {self.rsi_15m[0]}, RSI 1h: {self.rsi_1h[0]}")
```