forked from yagami1997/TradeMind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenhanced_trading_advisor.py
186 lines (158 loc) · 6.5 KB
/
enhanced_trading_advisor.py
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
import yfinance as yf
import pandas as pd
import numpy as np
import logging
from pathlib import Path
from datetime import datetime
import pytz
from typing import Dict, Optional, List
class EnhancedTradingAdvisor:
def __init__(self, symbol: str):
self.symbol = symbol
self.data = None
self.setup_logging()
def setup_logging(self):
"""设置日志"""
log_dir = Path("logs")
log_dir.mkdir(exist_ok=True)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(f"logs/trader_{self.symbol}.log"),
logging.StreamHandler()
]
)
self.logger = logging.getLogger(f"trader_{self.symbol}")
def fetch_data(self) -> bool:
"""获取股票数据"""
try:
ticker = yf.Ticker(self.symbol)
self.data = ticker.history(period="1y")
if self.data.empty:
self.logger.error(f"没有获取到 {self.symbol} 的数据")
return False
return True
except Exception as e:
self.logger.error(f"获取 {self.symbol} 数据时出错: {str(e)}")
return False
def calculate_indicators(self) -> bool:
"""计算技术指标,重点关注布林带"""
try:
if self.data is None or self.data.empty:
return False
# 计算布林带(20日)
self.data['BB_middle'] = self.data['Close'].rolling(window=20).mean()
std = self.data['Close'].rolling(window=20).std()
self.data['BB_upper'] = self.data['BB_middle'] + 2 * std
self.data['BB_lower'] = self.data['BB_middle'] - 2 * std
# 计算布林带宽度和百分比B
self.data['BB_width'] = (self.data['BB_upper'] - self.data['BB_lower']) / self.data['BB_middle']
self.data['BB_percent'] = (self.data['Close'] - self.data['BB_lower']) / (self.data['BB_upper'] - self.data['BB_lower'])
# 计算RSI
delta = self.data['Close'].diff()
gain = (delta.where(delta > 0, 0)).rolling(window=14).mean()
loss = (-delta.where(delta < 0, 0)).rolling(window=14).mean()
rs = gain / loss
self.data['RSI'] = 100 - (100 / (1 + rs))
# 计算成交量变化
self.data['Volume_MA20'] = self.data['Volume'].rolling(window=20).mean()
self.data['Volume_Ratio'] = self.data['Volume'] / self.data['Volume_MA20']
return True
except Exception as e:
self.logger.error(f"计算指标时出错: {str(e)}")
return False
def generate_trading_signal(self) -> Optional[Dict]:
"""基于布林带生成交易信号"""
try:
if self.data is None or self.data.empty:
return None
latest = self.data.iloc[-1]
prev = self.data.iloc[-2]
# 布林带信号评分
bb_score = 0
if latest['Close'] < latest['BB_lower']:
bb_score = 2 # 超卖
elif latest['Close'] > latest['BB_upper']:
bb_score = -2 # 超买
# 布林带宽度变化信号
bb_width_change = (latest['BB_width'] - prev['BB_width']) / prev['BB_width']
if bb_width_change > 0.1:
bb_score += 1 # 波动性增加
elif bb_width_change < -0.1:
bb_score -= 1 # 波动性减少
# RSI信号
rsi_score = 0
if latest['RSI'] < 30:
rsi_score = 2
elif latest['RSI'] > 70:
rsi_score = -2
# 成交量信号
volume_score = 0
if latest['Volume_Ratio'] > 2:
volume_score = 1
elif latest['Volume_Ratio'] < 0.5:
volume_score = -1
# 计算总分
total_score = bb_score + rsi_score + volume_score
# 生成信号
signal = "观望"
if total_score >= 2:
signal = "买入"
elif total_score <= -2:
signal = "卖出"
return {
"symbol": self.symbol,
"current_price": latest['Close'],
"bb_upper": latest['BB_upper'],
"bb_lower": latest['BB_lower'],
"bb_percent": latest['BB_percent'],
"bb_width": latest['BB_width'],
"rsi": latest['RSI'],
"volume_ratio": latest['Volume_Ratio'],
"price_change": ((latest['Close'] / prev['Close']) - 1) * 100,
"score": total_score,
"signal": signal
}
except Exception as e:
self.logger.error(f"生成交易信号时出错: {str(e)}")
return None
def setup_watchlists(self) -> dict:
"""设置观察列表"""
return {
"主要指数": [
"^DJI", # 道琼斯工业指数
"^IXIC", # 纳斯达克综合指数
"^GSPC", # 标普500指数
"^VIX", # VIX波动率指数
"^NDX", # 纳斯达克100指数
"^RUT" # 罗素2000
],
"商品ETF": [
"GLD", # SPDR黄金ETF
"IAU", # iShares黄金ETF
"USO", # 原油ETF
"UCO", # 2倍做多原油ETF
"UNG", # 天然气ETF
"SLV", # iShares白银ETF
"PPLT", # 铂金ETF
"PALL", # 钯金ETF
"WEAT", # 小麦ETF
"CORN", # 玉米ETF
"SOYB", # 大豆ETF
"DBA" # 农产品ETF
],
# [之前提供的其他分组...]
}
def main():
# 示例使用
symbol = "AAPL" # 示例股票代码
advisor = EnhancedTradingAdvisor(symbol)
if advisor.fetch_data() and advisor.calculate_indicators():
signal = advisor.generate_trading_signal()
if signal:
print(f"分析结果: {signal}")
else:
print("分析失败")
if __name__ == "__main__":
main()