-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
QAAnalysis_dataframe.py
executable file
·233 lines (179 loc) · 5.67 KB
/
QAAnalysis_dataframe.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
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
# coding:utf-8
#
# The MIT License (MIT)
#
# Copyright (c) 2016-2019 yutiansut/QUANTAXIS
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import statistics
from functools import lru_cache
#import scipy
#import statsmodels
#from scipy import integrate, optimize, stats
#from QUANTAXIS.QAData.QADataStruct import QA_DataStruct_Index_day,QA_DataStruct_Index_min,QA_DataStruct_Stock_day,QA_DataStruct_Stock_min
class QAAnalysis_stock():
"""
行情分析器
计算所有的指标
"""
def __init__(self, dataStruct, *args, **kwargs):
try:
# 如果是QA_Data_系列
self.data = dataStruct.data
self._data = dataStruct
except AttributeError:
# 如果是dataframe
self.data = dataStruct
# self.data=DataSturct.data
def __repr__(self):
return '< QAAnalysis_Stock >'
def __call__(self):
return self.data
# 使用property进行懒运算
@property
def open(self):
return self.data['open']
@property
def high(self):
return self.data['high']
@property
def low(self):
return self.data['low']
@property
def close(self):
return self.data['close']
@property
def vol(self):
if 'volume' in self.data.columns:
return self.data['volume']
else:
return self.data['vol']
@property
def volume(self):
if 'volume' in self.data.columns:
return self.data['volume']
else:
return self.data['vol']
@property
def date(self):
return self.data.index.levels[self.data.index.names.index(
'date')] if 'date' in self.data.index.names else self.data['date']
@property
def datetime(self):
return self.data.index.levels[self.data.index.names.index(
'datetime')] if 'datetime' in self.data.index.names else self.data.index.levels[self.data.index.names.index(
'date')]
@property
def index(self):
return self.data.index
# 均价
@property
def price(self):
return 0.25 * (self.open + self.close + self.high + self.low)
@property
def max(self):
return self.price.max()
@property
def min(self):
return self.price.min()
@property
def mean(self):
return self.price.mean()
# 一阶差分序列
@property
def price_diff(self):
return self.price.diff(1)
# 样本方差(无偏估计) population variance
@property
def pvariance(self):
return statistics.pvariance(self.price)
# 方差
@property
def variance(self):
return statistics.variance(self.price)
# 标准差
@property
def day_pct_change(self):
return (self.open - self.close) / self.open
@property
def stdev(self):
return statistics.stdev(self.price)
# 样本标准差
@property
def pstdev(self):
return statistics.pstdev(self.price)
# 调和平均数
@property
def mean_harmonic(self):
return statistics.harmonic_mean(self.price)
# 众数
@property
def mode(self):
return statistics.mode(self.price)
# 波动率
# 振幅
@property
def amplitude(self):
return self.max - self.min
# 偏度 Skewness
@property
def skewnewss(self):
return self.price.skew()
# 峰度Kurtosis
@property
def kurtosis(self):
return self.price.kurt()
# 百分数变化
@property
def pct_change(self):
return self.price.pct_change()
# 平均绝对偏差
@property
def mad(self):
return self.price.mad()
# 函数 指标计算
@lru_cache()
def add_func(self, func, *arg, **kwargs):
return func(self.data, *arg, **kwargs)
def shadow_calc(data):
"""计算上下影线
Arguments:
data {DataStruct.slice} -- 输入的是一个行情切片
Returns:
up_shadow {float} -- 上影线
down_shdow {float} -- 下影线
entity {float} -- 实体部分
date {str} -- 时间
code {str} -- 代码
"""
up_shadow = abs(data.high - (max(data.open, data.close)))
down_shadow = abs(data.low - (min(data.open, data.close)))
entity = abs(data.open - data.close)
towards = True if data.open < data.close else False
print('=' * 15)
print('up_shadow : {}'.format(up_shadow))
print('down_shadow : {}'.format(down_shadow))
print('entity: {}'.format(entity))
print('towards : {}'.format(towards))
return up_shadow, down_shadow, entity, data.date, data.code
class shadow():
def __init__(self, data):
self.data = data
def shadow_panel(self):
return