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
8be2302c
Commit
8be2302c
authored
Nov 07, 2020
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
FIX: added industry range and industry matrix
parent
a29ba940
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
93 additions
and
2 deletions
+93
-2
sqlengine_rl.py
alphamind/data/engines/sqlengine_rl.py
+93
-2
No files found.
alphamind/data/engines/sqlengine_rl.py
View file @
8be2302c
...
...
@@ -26,7 +26,8 @@ from PyFin.api import advanceDateByCalendar
from
alphamind.data.dbmodel.models_rl
import
(
Market
,
Industry
Industry
,
RiskExposure
)
if
"DB_VENDOR"
in
os
.
environ
and
os
.
environ
[
"DB_VENDOR"
]
.
lower
()
==
"rl"
:
...
...
@@ -188,6 +189,92 @@ class SqlEngine:
return
pd
.
read_sql
(
query
,
self
.
engine
)
.
dropna
()
.
drop_duplicates
([
'code'
])
def
fetch_industry_matrix
(
self
,
ref_date
:
str
,
codes
:
Iterable
[
int
]
=
None
,
category
:
str
=
'sw'
,
level
:
int
=
1
):
df
=
self
.
fetch_industry
(
ref_date
,
codes
,
category
,
level
)
df
[
'industry_name'
]
=
df
[
'industry'
]
df
=
pd
.
get_dummies
(
df
,
columns
=
[
'industry'
],
prefix
=
""
,
prefix_sep
=
""
)
return
df
.
drop
(
'industry_code'
,
axis
=
1
)
def
fetch_industry_range
(
self
,
universe
:
Universe
,
start_date
:
str
=
None
,
end_date
:
str
=
None
,
dates
:
Iterable
[
str
]
=
None
,
category
:
str
=
'sw'
,
level
:
int
=
1
):
code_name
=
'industry_code'
+
str
(
level
)
category_name
=
'industry_name'
+
str
(
level
)
cond
=
universe
.
_query_statements
(
start_date
,
end_date
,
dates
)
query
=
select
([
Industry
.
code
.
label
(
"code"
),
Industry
.
trade_date
,
getattr
(
Industry
,
code_name
)
.
label
(
'industry_code'
),
getattr
(
Industry
,
category_name
)
.
label
(
'industry'
)])
.
where
(
and_
(
*
cond
,
Industry
.
code
==
UniverseTable
.
code
,
Industry
.
trade_date
==
UniverseTable
.
trade_date
,
Industry
.
flag
==
1
)
)
.
distinct
()
return
pd
.
read_sql
(
query
,
self
.
session
.
bind
)
def
fetch_risk_model
(
self
,
ref_date
:
str
,
codes
:
Iterable
[
int
],
risk_model
:
str
=
'short'
,
excluded
:
Iterable
[
str
]
=
None
,
model_type
:
str
=
None
)
->
Union
[
FactorRiskModel
,
Tuple
[
pd
.
DataFrame
,
pd
.
DataFrame
]]:
risk_cov_table
,
special_risk_table
=
_map_risk_model_table
(
risk_model
)
cov_risk_cols
=
[
risk_cov_table
.
__table__
.
columns
[
f
]
for
f
in
total_risk_factors
]
query
=
select
([
risk_cov_table
.
FactorID
,
risk_cov_table
.
Factor
]
+
cov_risk_cols
)
.
where
(
risk_cov_table
.
trade_date
==
ref_date
)
risk_cov
=
pd
.
read_sql
(
query
,
self
.
engine
)
.
sort_values
(
'FactorID'
)
if
excluded
:
risk_exposure_cols
=
[
RiskExposure
.
__table__
.
columns
[
f
]
for
f
in
total_risk_factors
if
f
not
in
set
(
excluded
)]
else
:
risk_exposure_cols
=
[
RiskExposure
.
__table__
.
columns
[
f
]
for
f
in
total_risk_factors
]
big_table
=
join
(
RiskExposure
,
special_risk_table
,
and_
(
RiskExposure
.
code
==
special_risk_table
.
code
,
RiskExposure
.
trade_date
==
special_risk_table
.
trade_date
))
query
=
select
(
[
RiskExposure
.
code
,
special_risk_table
.
SRISK
.
label
(
'srisk'
)]
+
risk_exposure_cols
)
\
.
select_from
(
big_table
)
.
where
(
and_
(
RiskExposure
.
trade_date
==
ref_date
,
RiskExposure
.
code
.
in_
(
codes
)
))
risk_exp
=
pd
.
read_sql
(
query
,
self
.
engine
)
.
dropna
()
if
not
model_type
:
return
risk_cov
,
risk_exp
elif
model_type
==
'factor'
:
factor_names
=
risk_cov
.
Factor
.
tolist
()
new_risk_cov
=
risk_cov
.
set_index
(
'Factor'
)
factor_cov
=
new_risk_cov
.
loc
[
factor_names
,
factor_names
]
/
10000.
new_risk_exp
=
risk_exp
.
set_index
(
'code'
)
factor_loading
=
new_risk_exp
.
loc
[:,
factor_names
]
idsync
=
new_risk_exp
[
'srisk'
]
*
new_risk_exp
[
'srisk'
]
/
10000
return
FactorRiskModel
(
factor_cov
,
factor_loading
,
idsync
),
risk_cov
,
risk_exp
if
__name__
==
"__main__"
:
db_url
=
"mysql+mysqldb://reader:Reader#2020@121.37.138.1:13317/vision?charset=utf8"
...
...
@@ -196,7 +283,7 @@ if __name__ == "__main__":
universe
=
Universe
(
"hs300"
)
start_date
=
'2020-09-29'
end_date
=
'2020-10-10'
df
=
sql_engine
.
fetch_codes_range
(
start_date
=
'start_date'
,
end_date
=
end_date
,
universe
=
Universe
(
"hs300"
))
df
=
sql_engine
.
fetch_codes_range
(
start_date
=
start_date
,
end_date
=
end_date
,
universe
=
Universe
(
"hs300"
))
print
(
df
)
df
=
sql_engine
.
fetch_dx_return
(
"2020-10-09"
,
codes
=
[
"2010031963"
])
print
(
df
)
...
...
@@ -204,3 +291,7 @@ if __name__ == "__main__":
print
(
df
)
df
=
sql_engine
.
fetch_industry
(
ref_date
=
"2020-10-09"
,
codes
=
[
"2010031963"
])
print
(
df
)
df
=
sql_engine
.
fetch_industry_matrix
(
ref_date
=
"2020-10-09"
,
codes
=
[
"2010031963"
])
print
(
df
)
df
=
sql_engine
.
fetch_industry_range
(
start_date
=
start_date
,
end_date
=
end_date
,
universe
=
Universe
(
"hs300"
))
print
(
df
)
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