1
0

修复卖出逻辑的实现,不允许卖空

This commit is contained in:
2026-01-28 10:30:28 +08:00
parent 64bfd031b3
commit 5afb8ddcd1
3 changed files with 4 additions and 6 deletions

View File

@@ -12,7 +12,6 @@ SMA 双均线交叉策略
- SMA120: 120 日简单移动平均线
"""
import pandas as pd
from backtesting import Strategy
from backtesting.lib import crossover
@@ -84,13 +83,11 @@ class SmaCross(Strategy):
"""
# 金叉:短期均线上穿长期均线
if crossover(self.data.sma10, self.data.sma30):
self.position.close() # 先平掉现有仓位
self.buy() # 开多仓
# 死叉:短期均线下穿长期均线
elif crossover(self.data.sma30, self.data.sma10):
self.position.close() # 先平掉现有仓位
self.sell() # 开空仓
elif self.position.size > 0 and crossover(self.data.sma30, self.data.sma10):
self.position.close() # 开空仓
# 导入 talib (必须在文件末尾,因为 calculate_indicators 函数中使用了 talib)