forked from NeilLa/Neilyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindicators.py
109 lines (93 loc) · 3.76 KB
/
indicators.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
import pandas_ta as ta
import pandas as pd
import os
import inspect
import sys
from .utils.string import split_letters_numbers
# 将自建指标库添加到Python路径中
current_dir = os.path.dirname(os.path.abspath(__file__))
indicators_lib_path = os.path.join(current_dir, 'indicators_lib')
sys.path.append(indicators_lib_path)
def get_indicators(data, *args):
"""
对外的计算指标的接口,支持单个或多个 symbol。
"""
if isinstance(data, dict):
# 多 symbol 情况
all_indicators = {}
for symbol, df in data.items():
indicators_df = _calculate_indicators_for_single_symbol(df, *args)
all_indicators[symbol] = indicators_df
return all_indicators
else:
# 单 symbol 情况
return _calculate_indicators_for_single_symbol(data, *args)
def _calculate_indicators_for_single_symbol(data, *args):
"""
计算单个 symbol 的指标。
"""
indicators = args
indicators_df = pd.DataFrame()
indicators_df['close'] = data['close']
for indicator in indicators:
name, params = split_letters_numbers(indicator)
params = [int(p) for p in params if p.isdigit()] if params else []
if params:
col_name = f"{name}_{'_'.join(map(str, params))}"
else:
col_name = name
if hasattr(ta, name):
func = getattr(ta, name)
else:
# 再pandas_ta中找不到指标 则尝试在自建指标库中搜索
try:
custom_indicator_module = __import__(name)
func = getattr(custom_indicator_module, name)
except ImportError:
print(f'Indicator {name} not found in pandas_ta or indicators_lib.')
continue
except AttributeError:
print(f'Function {name} not found in module {name}.')
continue
try:
# 获取函数签名
sig = inspect.signature(func)
required_params = sig.parameters
# 构建传入的参数,根据函数所需的参数动态传递
kwargs = {}
# 处理 std_length 和 atr_length 参数
if 'std_length' in required_params and len(params) >= 1:
kwargs['std_length'] = params[0]
if 'atr_length' in required_params and len(params) >= 2:
kwargs['atr_length'] = params[1]
if 'length' in required_params and len(params) >= 1:
kwargs['length'] = params[0]
# 根据函数签名传递数据列 (open, high, low, close, volume)
if 'open' in required_params:
kwargs['open'] = data['open']
if 'high' in required_params:
kwargs['high'] = data['high']
if 'low' in required_params:
kwargs['low'] = data['low']
if 'close' in required_params:
kwargs['close'] = data['close']
if 'volume' in required_params and 'volume' in data.columns:
kwargs['volume'] = data['volume']
# 调用指标函数
result = func(**kwargs)
if result is None:
print(f'Indicator {name} returned None.')
continue
# 处理返回多个列的情况
if isinstance(result, pd.DataFrame):
for col in result.columns:
indicators_df[f'{col_name}_{col}'] = result[col]
else:
indicators_df[col_name] = result
# 填充空值
indicators_df = indicators_df.fillna(method='bfill')
except Exception as e:
print(f'Error calculating indicator {name}: {e}')
import traceback
traceback.print_exc()
return indicators_df