Skip to content

Commit 9097b8e

Browse files
authored
Merge pull request #1388 from pchaos/master
修复QA_DataStruct_Indicators.get_timerange((self, start, end, code=None) , code为空时,应该返回日期段内所有的数据
2 parents d398d0f + 7a41ee9 commit 9097b8e

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

QUANTAXIS/QAData/QAIndicatorStruct.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ def get_timerange(self, start, end, code=None):
6767
获取某一段时间的某一只股票的指标
6868
"""
6969
try:
70-
return self.data.loc[(slice(pd.Timestamp(start), pd.Timestamp(end)), code), :]
70+
if code is None:
71+
# code为空时 返回start ~end之间的所有数据
72+
return self.data.loc[(slice(pd.Timestamp(start), pd.Timestamp(end))), :]
73+
else:
74+
return self.data.loc[(slice(pd.Timestamp(start), pd.Timestamp(end)), code), :]
7175
except:
7276
return ValueError('CANNOT FOUND THIS TIME RANGE')
7377

QUANTAXIS_Test/QAData_Test/__init__.py

Whitespace-only changes.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# -*- coding: utf-8 -*-
2+
""" test QAIndicatorStuct
3+
"""
4+
from unittest import TestCase
5+
import unittest
6+
# import datetime
7+
import QUANTAXIS as qa
8+
from QUANTAXIS.QAUtil.QACache import QA_util_cache as qacache
9+
# import numpy as np
10+
import pandas as pd
11+
from datetime import datetime
12+
from dateutil.relativedelta import relativedelta
13+
14+
15+
class testQAIndicatorStuct(unittest.TestCase):
16+
""""""
17+
def test_get_timerange(self):
18+
"""测试QA_DataStruct_Indicators.get_timerange((self, start, end, code=None)
19+
code为空时,是否返回日期段内所有的数据"""
20+
num = 50
21+
# codelist = self.getCodeList(count=num)
22+
codelist = self.getCodeList(count=num, isTesting=False)
23+
startdate = datetime.strptime('2017-08-01', '%Y-%m-%d')
24+
endday = '2018-10-21'
25+
data = qa.QA_fetch_stock_day_adv(codelist, '2017-08-01', '2018-10-21').to_qfq()
26+
ind = data.add_func(qa.QA_indicator_MA, 10)
27+
inc = qa.QA_DataStruct_Indicators(ind)
28+
startdate, enddate = '2018-08-01', '2018-08-31'
29+
# 相同时间段数据对比
30+
dftest = self.getTimeRange(inc, startdate, enddate, isUsingQA=False)
31+
dforgin = inc.get_timerange(startdate, enddate)
32+
self.assertTrue(dftest.equals(dforgin) , "两种计算方式结果应该一致。\n{} {}".format(dftest.tail(), dforgin.tail()))
33+
34+
35+
def getTimeRange(self, inc, startdate, enddate, code=None, isUsingQA=False):
36+
if isUsingQA:
37+
# 源码中bug修复时,使用以下代码
38+
dfindicator = inc.get_timerange(startdate, enddate, code) # QAUANTAXIS源码中有bug,code不起作用
39+
40+
else:
41+
# 源码中bug未修复时,使用以下代码
42+
if code is None: # QAUANTAXIS源码中有bug,code为空时,不返回数据
43+
dfindicator = inc.data.loc[(slice(pd.Timestamp(startdate),
44+
pd.Timestamp(self.str2date(enddate)))), :]
45+
else:
46+
dfindicator = inc.data.loc[(slice(pd.Timestamp(startdate),
47+
pd.Timestamp(self.str2date(enddate))), code),
48+
:]
49+
return dfindicator
50+
51+
def str2date(self, dayStr):
52+
if isinstance(dayStr, str):
53+
return datetime.strptime(dayStr, '%Y-%m-%d')
54+
else:
55+
return dayStr
56+
57+
def getCodeList(self, isTesting=True, count=5000):
58+
"""
59+
isTesting: 是否使用测试数据
60+
count: 返回最多结果集数量
61+
"""
62+
if isTesting:
63+
# 2018.8首板个股,测试用,减少调试时间
64+
codelist = ['000023', '000068', '000407', '000561', '000590', '000593', '000608', '000610', '000626',
65+
'000638',
66+
'000657', '000659', '000663', '000669', '000677', '000705', '000759', '000766', '000780',
67+
'000792',
68+
'000815', '000852', '000885', '000909', '000913', '000921', '000928', '000931', '002006',
69+
'002012',
70+
'002034']
71+
else:
72+
codelist = qa.QA_fetch_stock_list_adv().code.tolist()
73+
return codelist[:count]
74+
75+
if __name__ == '__main__':
76+
unittest.main()

0 commit comments

Comments
 (0)