Repository navigation
Expand file tree
/
Copy pathindicator_calculator.py
More file actions
399 lines (319 loc) · 13.9 KB
/
Copy pathindicator_calculator.py
File metadata and controls
399 lines (319 loc) · 13.9 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
"""
指标计算模块,负责计算各种技术指标
"""
import pandas as pd
import numpy as np
import sqlite3
from datetime import datetime
from MyTT import *
import config
from logger import get_logger
from data_manager import get_data_manager
# 获取logger
logger = get_logger("indicator_calculator")
class IndicatorCalculator:
"""指标计算类"""
def __init__(self):
"""初始化指标计算器"""
self.data_manager = get_data_manager()
self.conn = self.data_manager.conn
def calculate_all_indicators(self, stock_code, force_update=False):
"""
计算所有技术指标
参数:
stock_code (str): 股票代码
force_update (bool): 是否强制更新所有数据的指标
返回:
bool: 是否计算成功
"""
try:
# 获取历史数据
df = self.data_manager.get_history_data_from_db(stock_code)
if df.empty:
logger.warning(f"没有 {stock_code} 的历史数据,无法计算指标")
return False
# 按日期排序
df = df.sort_values('date')
# 如果不是强制更新,检查是否有新数据需要计算
if not force_update:
# 获取指标表中的最新日期
cursor = self.conn.cursor()
cursor.execute(
"SELECT MAX(date) FROM stock_indicators WHERE stock_code=?",
(stock_code,)
)
result = cursor.fetchone()
latest_indicator_date = result[0] if result and result[0] else None
# 如果没有新数据,不需要计算
if latest_indicator_date:
latest_df_date = df['date'].max()
if latest_indicator_date >= latest_df_date:
logger.debug(f"{stock_code} 的指标已是最新,不需要更新")
return True
# 只获取新数据部分
df = df[df['date'] > latest_indicator_date]
if df.empty:
logger.debug(f"{stock_code} 没有新的数据需要计算指标")
return True
# 计算各种指标
result_df = pd.DataFrame()
result_df['stock_code'] = df['stock_code']
result_df['date'] = df['date']
# 计算均线指标
for period in config.MA_PERIODS:
ma_col = f'ma{period}'
result_df[ma_col] = self._calculate_ma(df, period)
# 计算MACD指标
macd_df = self._calculate_macd(df)
for col in macd_df.columns:
result_df[col] = macd_df[col]
# 保存指标结果到数据库
self._save_indicators(result_df)
logger.info(f"成功计算 {stock_code} 的技术指标,共 {len(result_df)} 条记录")
return True
except Exception as e:
logger.error(f"计算 {stock_code} 的技术指标时出错: {str(e)}")
return False
def _calculate_ma(self, df, period):
"""
计算移动平均线
参数:
df (pandas.DataFrame): 历史数据
period (int): 周期
返回:
pandas.Series: 移动平均线数据
"""
try:
# 检查数据长度是否足够
if len(df) < period:
logger.warning(f"数据长度不足以计算MA{period},需要{period}条数据,实际{len(df)}条")
return pd.Series([None] * len(df))
# 使用MyTT计算MA,并检查结果
ma = SMA(df['close'].values.astype(float), N=period)
# 检查计算结果
if ma is None or len(ma) == 0:
logger.warning(f"MA{period}计算结果为空")
return pd.Series([None] * len(df))
# 转换为pandas Series并处理NaN
ma_series = pd.Series(ma)
ma_series = ma_series.replace([np.nan, np.inf, -np.inf], None)
return ma_series
except Exception as e:
logger.error(f"计算MA{period}指标时出错: {str(e)}")
return pd.Series([None] * len(df))
def _calculate_macd(self, df):
"""
计算MACD指标
参数:
df (pandas.DataFrame): 历史数据
返回:
pandas.DataFrame: MACD指标数据
"""
try:
# 检查数据长度是否足够
min_periods = max(config.MACD_FAST, config.MACD_SLOW, config.MACD_SIGNAL) + 10
if len(df) < min_periods:
logger.warning(f"数据长度不足以计算MACD,需要至少{min_periods}条数据,实际{len(df)}条")
return pd.DataFrame({
'macd': [None] * len(df),
'macd_signal': [None] * len(df),
'macd_hist': [None] * len(df)
})
# 使用MyTT计算MACD
macd, signal, hist = MACD(
df['close'].values.astype(float),
SHORT=config.MACD_FAST,
LONG=config.MACD_SLOW,
M=config.MACD_SIGNAL
)
# 检查计算结果
if any(x is None or len(x) == 0 for x in [macd, signal, hist]):
logger.warning("MACD计算结果包含空值")
return pd.DataFrame({
'macd': [None] * len(df),
'macd_signal': [None] * len(df),
'macd_hist': [None] * len(df)
})
# 创建结果DataFrame并处理异常值
result = pd.DataFrame({
'macd': pd.Series(macd).replace([np.nan, np.inf, -np.inf], None),
'macd_signal': pd.Series(signal).replace([np.nan, np.inf, -np.inf], None),
'macd_hist': pd.Series(hist).replace([np.nan, np.inf, -np.inf], None)
})
return result
except Exception as e:
logger.error(f"计算MACD指标时出错: {str(e)}")
# 返回包含None值的DataFrame
return pd.DataFrame({
'macd': [None] * len(df),
'macd_signal': [None] * len(df),
'macd_hist': [None] * len(df)
})
def _save_indicators(self, df):
"""
保存指标到数据库
参数:
df (pandas.DataFrame): 指标数据
"""
try:
# 处理NaN值
df = df.replace({np.nan: None})
# 保存到数据库
df.to_sql('stock_indicators', self.conn, if_exists='append', index=False, method='multi')
self.conn.commit()
except Exception as e:
logger.error(f"保存指标数据时出错: {str(e)}")
self.conn.rollback()
def get_latest_indicators(self, stock_code):
"""
获取最新的指标数据
参数:
stock_code (str): 股票代码
返回:
dict: 最新指标数据
"""
try:
cursor = self.conn.cursor()
cursor.execute("""
SELECT * FROM stock_indicators
WHERE stock_code=?
ORDER BY date DESC
LIMIT 1
""", (stock_code,))
row = cursor.fetchone()
if not row:
logger.warning(f"未找到 {stock_code} 的指标数据")
return None
# 获取列名
columns = [description[0] for description in cursor.description]
# 转换为字典
indicators = dict(zip(columns, row))
return indicators
except Exception as e:
logger.error(f"获取 {stock_code} 的最新指标数据时出错: {str(e)}")
return None
def get_indicators_history(self, stock_code, days=60):
"""
获取历史指标数据
参数:
stock_code (str): 股票代码
days (int): 获取最近的天数
返回:
pandas.DataFrame: 历史指标数据
"""
try:
query = f"""
SELECT * FROM stock_indicators
WHERE stock_code=?
ORDER BY date DESC
LIMIT {days}
"""
df = pd.read_sql_query(query, self.conn, params=(stock_code,))
# 按日期排序(从早到晚)
df = df.sort_values('date')
return df
except Exception as e:
logger.error(f"获取 {stock_code} 的历史指标数据时出错: {str(e)}")
return pd.DataFrame()
def check_buy_signal(self, stock_code):
"""
检查买入信号
参数:
stock_code (str): 股票代码
返回:
bool: 是否有买入信号
"""
try:
# 获取最近的指标数据
indicators_df = self.get_indicators_history(stock_code, days=10)
if indicators_df.empty:
logger.warning(f"没有足够的 {stock_code} 指标数据来检查买入信号")
return False
# 计算MACD金叉信号
if len(indicators_df) >= 2:
# 检查前一天MACD柱为负,当天MACD柱为正(MACD金叉)
prev_hist = indicators_df.iloc[-2]['macd_hist']
curr_hist = indicators_df.iloc[-1]['macd_hist']
# 添加None值检查
if prev_hist is None or curr_hist is None:
logger.debug(f"{stock_code} MACD数据包含None值,跳过金叉检查")
return False
macd_cross = prev_hist < 0 and curr_hist > 0
# 检查均线多头排列(MA10 > MA20 > MA30 > MA60)
latest = indicators_df.iloc[-1]
ma10 = latest['ma10']
ma20 = latest['ma20']
ma30 = latest['ma30']
ma60 = latest['ma60']
# 添加None值检查
if any(ma is None for ma in [ma10, ma20, ma30, ma60]):
logger.debug(f"{stock_code} 均线数据包含None值,跳过均线排列检查")
return False
ma_alignment = ma10 > ma20 > ma30 > ma60
# 检查是否满足买入条件
if macd_cross and ma_alignment:
logger.info(f"{stock_code} 满足买入条件: MACD金叉 + 均线多头排列")
return True
return False
except Exception as e:
logger.error(f"检查 {stock_code} 的买入信号时出错: {str(e)}")
return False
def check_sell_signal(self, stock_code):
"""
检查卖出信号
参数:
stock_code (str): 股票代码
返回:
bool: 是否有卖出信号
"""
try:
# 获取最近的指标数据
indicators_df = self.get_indicators_history(stock_code, days=10)
if indicators_df.empty:
logger.warning(f"没有足够的 {stock_code} 指标数据来检查卖出信号")
return False
# 计算MACD死叉信号
if len(indicators_df) >= 2:
# 检查前一天MACD柱为正,当天MACD柱为负(MACD死叉)
prev_hist = indicators_df.iloc[-2]['macd_hist']
curr_hist = indicators_df.iloc[-1]['macd_hist']
# 添加None值检查
if prev_hist is None or curr_hist is None:
logger.debug(f"{stock_code} MACD数据包含None值,跳过死叉检查")
return False
macd_cross = prev_hist > 0 and curr_hist < 0
# 检查均线空头排列(MA10 < MA20 < MA30 < MA60)
latest = indicators_df.iloc[-1]
ma10 = latest['ma10']
ma20 = latest['ma20']
ma30 = latest['ma30']
ma60 = latest['ma60']
# 添加None值检查
if any(ma is None for ma in [ma10, ma20, ma30, ma60]):
logger.debug(f"{stock_code} 均线数据包含None值,跳过均线排列检查")
return False
ma_alignment = ma10 < ma20 < ma30 < ma60
# 检查是否满足卖出条件
if macd_cross and ma_alignment:
logger.info(f"{stock_code} 满足卖出条件: MACD死叉 + 均线空头排列")
return True
return False
except Exception as e:
logger.error(f"检查 {stock_code} 的卖出信号时出错: {str(e)}")
return False
def update_all_stock_indicators(self, force_update=False):
"""
更新所有股票的技术指标
参数:
force_update (bool): 是否强制更新所有数据的指标
"""
for stock_code in config.STOCK_POOL:
self.calculate_all_indicators(stock_code, force_update)
# 单例模式
_instance = None
def get_indicator_calculator():
"""获取IndicatorCalculator单例"""
global _instance
if _instance is None:
_instance = IndicatorCalculator()
return _instance