-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
222 lines (186 loc) · 6.42 KB
/
Copy pathmain.py
File metadata and controls
222 lines (186 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
"""
BackTestTools - 回测工具入口
支持策略:
1. ma_trend - 均线趋势策略
2. dual_ma - 双均线交叉策略
3. bollinger - 布林带策略
4. rsi - RSI超买超卖策略
5. macd - MACD策略
6. atr - ATR追踪止损策略
用法:python main.py <策略名>
示例:python main.py dual_ma
"""
import sys
from decimal import Decimal
from backtest_runner import create_provider, create_config, create_account, print_backtest_report
from engine import BackTestEngine
from strategies import (
MovingAverageTrendStrategy,
DualMACrossStrategy,
BollingerBandsStrategy,
RSIStrategy,
MACDStrategy,
ATRTrailingStopStrategy,
)
STRATEGIES = [
"ma_trend", "dual_ma", "bollinger", "rsi", "macd", "atr"
]
def run_ma_trend():
"""均线趋势策略"""
provider = create_provider()
config = create_config()
initial_quote = Decimal("10000")
account = create_account(initial_quote)
engine = BackTestEngine(provider, config, account)
strategy = MovingAverageTrendStrategy(
context=engine.broker,
fast_ma_period=10,
slow_ma_period=50,
trend_strength=Decimal("0.001"),
drawdown_rate=Decimal("0.05"),
take_profit_rate=Decimal("0.10")
)
engine.add_strategy(strategy)
engine.run()
print_backtest_report(account, strategy, config, initial_quote, {
"快均线周期": strategy._fast_ma_period,
"慢均线周期": strategy._slow_ma_period,
"趋势强度": f"{float(strategy._trend_strength) * 100:.2f}%",
"止损率": f"{float(strategy._drawdown_rate) * 100:.2f}%",
"止盈率": f"{float(strategy._take_profit_rate) * 100:.2f}%",
})
def run_dual_ma():
"""双均线交叉策略"""
provider = create_provider()
config = create_config()
initial_quote = Decimal("10000")
account = create_account(initial_quote)
engine = BackTestEngine(provider, config, account)
strategy = DualMACrossStrategy(
context=engine.broker,
fast_period=10,
slow_period=30,
drawdown_rate=Decimal("0.05"),
take_profit_rate=Decimal("0.10")
)
engine.add_strategy(strategy)
engine.run()
print_backtest_report(account, strategy, config, initial_quote, {
"快线周期": strategy._fast_period,
"慢线周期": strategy._slow_period,
"止损率": f"{float(strategy._drawdown_rate) * 100:.2f}%",
"止盈率": f"{float(strategy._take_profit_rate) * 100:.2f}%",
})
def run_bollinger():
"""布林带策略"""
provider = create_provider()
config = create_config()
initial_quote = Decimal("10000")
account = create_account(initial_quote)
engine = BackTestEngine(provider, config, account)
strategy = BollingerBandsStrategy(
context=engine.broker,
period=20,
num_std=Decimal("2"),
drawdown_rate=Decimal("0.05"),
take_profit_rate=Decimal("0.10")
)
engine.add_strategy(strategy)
engine.run()
print_backtest_report(account, strategy, config, initial_quote, {
"周期": strategy._period,
"标准差倍数": float(strategy._num_std),
"止损率": f"{float(strategy._drawdown_rate) * 100:.2f}%",
"止盈率": f"{float(strategy._take_profit_rate) * 100:.2f}%",
})
def run_rsi():
"""RSI超买超卖策略"""
provider = create_provider()
config = create_config()
initial_quote = Decimal("10000")
account = create_account(initial_quote)
engine = BackTestEngine(provider, config, account)
strategy = RSIStrategy(
context=engine.broker,
period=14,
oversold_threshold=Decimal("30"),
overbought_threshold=Decimal("70"),
drawdown_rate=Decimal("0.05"),
take_profit_rate=Decimal("0.10")
)
engine.add_strategy(strategy)
engine.run()
print_backtest_report(account, strategy, config, initial_quote, {
"RSI周期": strategy._period,
"超卖阈值": float(strategy._oversold_threshold),
"超买阈值": float(strategy._overbought_threshold),
"止损率": f"{float(strategy._drawdown_rate) * 100:.2f}%",
"止盈率": f"{float(strategy._take_profit_rate) * 100:.2f}%",
})
def run_macd():
"""MACD策略"""
provider = create_provider()
config = create_config()
initial_quote = Decimal("10000")
account = create_account(initial_quote)
engine = BackTestEngine(provider, config, account)
strategy = MACDStrategy(
context=engine.broker,
fast_period=12,
slow_period=26,
signal_period=9,
drawdown_rate=Decimal("0.05"),
take_profit_rate=Decimal("0.10")
)
engine.add_strategy(strategy)
engine.run()
print_backtest_report(account, strategy, config, initial_quote, {
"快线周期": strategy._fast_period,
"慢线周期": strategy._slow_period,
"信号线周期": strategy._signal_period,
"止损率": f"{float(strategy._drawdown_rate) * 100:.2f}%",
"止盈率": f"{float(strategy._take_profit_rate) * 100:.2f}%",
})
def run_atr():
"""ATR追踪止损策略"""
provider = create_provider()
config = create_config()
initial_quote = Decimal("10000")
account = create_account(initial_quote)
engine = BackTestEngine(provider, config, account)
strategy = ATRTrailingStopStrategy(
context=engine.broker,
atr_period=14,
atr_multiplier=Decimal("2"),
breakout_period=20,
take_profit_rate=Decimal("0.10")
)
engine.add_strategy(strategy)
engine.run()
print_backtest_report(account, strategy, config, initial_quote, {
"ATR周期": strategy._atr_period,
"ATR倍数": float(strategy._atr_multiplier),
"突破周期": strategy._breakout_period,
"止盈率": f"{float(strategy._take_profit_rate) * 100:.2f}%",
})
STRATEGY_MAP = {
"ma_trend": run_ma_trend,
"dual_ma": run_dual_ma,
"bollinger": run_bollinger,
"rsi": run_rsi,
"macd": run_macd,
"atr": run_atr,
}
def main():
if len(sys.argv) < 2:
print(f"用法: python main.py <策略名>")
print(f"可选策略: {', '.join(STRATEGIES)}")
sys.exit(1)
name = sys.argv[1]
if name not in STRATEGY_MAP:
print(f"未知策略: {name}")
print(f"可选策略: {', '.join(STRATEGIES)}")
sys.exit(1)
STRATEGY_MAP[name]()
if __name__ == "__main__":
main()