Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
A
alpha-mind
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dr.李
alpha-mind
Commits
ebd16b85
Commit
ebd16b85
authored
Jan 24, 2018
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added more industry styles
parent
4944d755
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
102 additions
and
20 deletions
+102
-20
industries.py
alphamind/data/engines/industries.py
+68
-0
sqlengine.py
alphamind/data/engines/sqlengine.py
+23
-19
utilities.py
alphamind/data/engines/utilities.py
+11
-1
No files found.
alphamind/data/engines/industries.py
0 → 100644
View file @
ebd16b85
This diff is collapsed.
Click to expand it.
alphamind/data/engines/sqlengine.py
View file @
ebd16b85
...
...
@@ -207,12 +207,12 @@ class SqlEngine(object):
cond
=
universe
.
_query_statements
(
start_date
,
end_date
,
None
)
big_table
=
join
(
Market
,
UniverseTable
,
and_
(
Market
.
trade_date
==
UniverseTable
.
trade_date
,
Market
.
code
==
UniverseTable
.
code
,
cond
)
)
and_
(
Market
.
trade_date
==
UniverseTable
.
trade_date
,
Market
.
code
==
UniverseTable
.
code
,
cond
)
)
query
=
select
([
Market
.
trade_date
,
Market
.
code
,
stats
])
\
.
select_from
(
big_table
)
...
...
@@ -379,7 +379,7 @@ class SqlEngine(object):
FullFactor
.
code
==
UniverseTable
.
code
,
cond
)
)
)
query
=
select
(
[
FullFactor
.
trade_date
,
FullFactor
.
code
,
FullFactor
.
isOpen
]
+
list
(
factor_cols
.
keys
()))
\
...
...
@@ -498,7 +498,7 @@ class SqlEngine(object):
FullFactor
.
code
==
UniverseTable
.
code
,
cond
)
)
)
query
=
select
(
[
FullFactor
.
trade_date
,
FullFactor
.
code
,
special_risk_col
]
+
risk_exposure_cols
)
.
select_from
(
big_table
)
\
...
...
@@ -508,7 +508,8 @@ class SqlEngine(object):
if
universe
.
is_filtered
:
codes
=
universe
.
query
(
self
,
start_date
,
end_date
,
dates
)
risk_exp
=
pd
.
merge
(
risk_exp
,
codes
,
how
=
'inner'
,
on
=
[
'trade_date'
,
'code'
])
.
sort_values
([
'trade_date'
,
'code'
])
risk_exp
=
pd
.
merge
(
risk_exp
,
codes
,
how
=
'inner'
,
on
=
[
'trade_date'
,
'code'
])
.
sort_values
(
[
'trade_date'
,
'code'
])
return
risk_cov
,
risk_exp
...
...
@@ -536,7 +537,8 @@ class SqlEngine(object):
start_date
:
str
=
None
,
end_date
:
str
=
None
,
dates
:
Iterable
[
str
]
=
None
,
category
:
str
=
'sw'
):
category
:
str
=
'sw'
,
level
:
int
=
1
):
industry_category_name
=
_map_industry_category
(
category
)
cond
=
universe
.
_query_statements
(
start_date
,
end_date
,
dates
)
...
...
@@ -548,12 +550,15 @@ class SqlEngine(object):
Industry
.
industry
==
industry_category_name
,
cond
)
)
)
code_name
=
'industryID'
+
str
(
level
)
category_name
=
'industryName'
+
str
(
level
)
query
=
select
([
Industry
.
trade_date
,
Industry
.
code
,
Industry
.
industryID1
.
label
(
'industry_code'
),
Industry
.
industryName1
.
label
(
'industry'
)])
.
select_from
(
big_table
)
.
distinct
()
getattr
(
Industry
,
code_name
)
.
label
(
'industry_code'
),
getattr
(
Industry
,
category_name
)
.
label
(
'industry'
)])
.
select_from
(
big_table
)
.
distinct
()
df
=
pd
.
read_sql
(
query
,
self
.
engine
)
if
universe
.
is_filtered
:
...
...
@@ -802,10 +807,10 @@ class SqlEngine(object):
else
:
id_filter
=
'in_'
t
=
select
([
table
.
trade_id
])
.
\
t
=
select
([
table
.
trade_id
])
.
\
where
(
and_
(
table
.
trade_date
<=
ref_date
,
table
.
operation
==
'withdraw'
))
.
alias
(
't'
)
query
=
select
([
table
])
.
\
query
=
select
([
table
])
.
\
where
(
and_
(
getattr
(
table
.
trade_id
,
id_filter
)(
t
),
table
.
trade_date
<=
ref_date
,
table
.
operation
==
'lend'
))
...
...
@@ -823,7 +828,7 @@ class SqlEngine(object):
rule
=
x
[
'price_rule'
]
.
split
(
'@'
)
if
rule
[
0
]
in
[
'closePrice'
,
'openPrice'
]:
query
=
select
([
getattr
(
Market
,
rule
[
0
])])
.
\
query
=
select
([
getattr
(
Market
,
rule
[
0
])])
.
\
where
(
and_
(
Market
.
code
==
code
,
Market
.
trade_date
==
rule
[
1
]))
data
=
pd
.
read_sql
(
query
,
self
.
engine
)
if
not
data
.
empty
:
...
...
@@ -835,6 +840,7 @@ class SqlEngine(object):
else
:
raise
KeyError
(
'do not have rule for
%
s'
%
x
[
'price_rule'
])
return
price
df
[
'price'
]
=
df
.
apply
(
lambda
x
:
parse_price_rule
(
x
),
axis
=
1
)
df
.
drop
([
'remark'
,
'price_rule'
,
'operation'
],
axis
=
1
,
inplace
=
True
)
...
...
@@ -848,12 +854,10 @@ class SqlEngine(object):
if
__name__
==
'__main__'
:
universe
=
Universe
(
'ss'
,
[
'hs300'
])
engine
=
SqlEngine
()
df
=
engine
.
fetch_
outright_status
(
'2017-12-28'
)
df
=
engine
.
fetch_
industry_range
(
universe
,
'2017-12-28'
,
'2017-12-31'
,
category
=
'dx'
,
level
=
3
)
print
(
df
)
alphamind/data/engines/utilities.py
View file @
ebd16b85
...
...
@@ -43,5 +43,15 @@ def _map_factors(factors: Iterable[str], used_factor_tables) -> Dict:
def
_map_industry_category
(
category
:
str
)
->
str
:
if
category
==
'sw'
:
return
'申万行业分类'
elif
category
==
'zz'
:
return
'中证行业分类'
elif
category
==
'dx'
:
return
'中证行业分类'
elif
category
==
'zjh'
:
return
'证监会行业V2012'
else
:
raise
ValueError
(
"No other industry is supported at the current time"
)
\ No newline at end of file
raise
ValueError
(
"No other industry is supported at the current time"
)
def
industry_list
(
catrgory
,
level
=
1
):
pass
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment