修复卖出逻辑的实现,不允许卖空
This commit is contained in:
1
.idea/vcs.xml
generated
1
.idea/vcs.xml
generated
@@ -2,5 +2,6 @@
|
|||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="VcsDirectoryMappings">
|
<component name="VcsDirectoryMappings">
|
||||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
<mapping directory="$PROJECT_DIR$/backtestingpy" vcs="Git" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -117,7 +117,7 @@ class MacdTrendStrategy(Strategy):
|
|||||||
self.buy() # 开多仓
|
self.buy() # 开多仓
|
||||||
|
|
||||||
# 卖出条件: MACD 死叉 OR 价格 < EMA
|
# 卖出条件: MACD 死叉 OR 价格 < EMA
|
||||||
elif crossover(self.signal, self.macd) or self.data.Close[-1] < self.ema[-1]:
|
elif self.position.size > 0 and (crossover(self.signal, self.macd) or self.data.Close[-1] < self.ema[-1]):
|
||||||
self.position.close() # 平掉多仓
|
self.position.close() # 平掉多仓
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ SMA 双均线交叉策略
|
|||||||
- SMA120: 120 日简单移动平均线
|
- SMA120: 120 日简单移动平均线
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import pandas as pd
|
|
||||||
from backtesting import Strategy
|
from backtesting import Strategy
|
||||||
from backtesting.lib import crossover
|
from backtesting.lib import crossover
|
||||||
|
|
||||||
@@ -84,13 +83,11 @@ class SmaCross(Strategy):
|
|||||||
"""
|
"""
|
||||||
# 金叉:短期均线上穿长期均线
|
# 金叉:短期均线上穿长期均线
|
||||||
if crossover(self.data.sma10, self.data.sma30):
|
if crossover(self.data.sma10, self.data.sma30):
|
||||||
self.position.close() # 先平掉现有仓位
|
|
||||||
self.buy() # 开多仓
|
self.buy() # 开多仓
|
||||||
|
|
||||||
# 死叉:短期均线下穿长期均线
|
# 死叉:短期均线下穿长期均线
|
||||||
elif crossover(self.data.sma30, self.data.sma10):
|
elif self.position.size > 0 and crossover(self.data.sma30, self.data.sma10):
|
||||||
self.position.close() # 先平掉现有仓位
|
self.position.close() # 开空仓
|
||||||
self.sell() # 开空仓
|
|
||||||
|
|
||||||
|
|
||||||
# 导入 talib (必须在文件末尾,因为 calculate_indicators 函数中使用了 talib)
|
# 导入 talib (必须在文件末尾,因为 calculate_indicators 函数中使用了 talib)
|
||||||
|
|||||||
Reference in New Issue
Block a user