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
12c20abc
Commit
12c20abc
authored
Sep 01, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
made sql engine work with experimental data
parent
ad9d7888
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
29 deletions
+28
-29
sqlengine.py
alphamind/data/engines/sqlengine.py
+28
-29
No files found.
alphamind/data/engines/sqlengine.py
View file @
12c20abc
...
...
@@ -22,18 +22,10 @@ from alphamind.data.dbmodel.models import Strategy
from
alphamind.data.dbmodel.models
import
DailyReturn
from
alphamind.data.dbmodel.models
import
IndexComponent
from
alphamind.data.dbmodel.models
import
Industry
from
alphamind.data.dbmodel.models
import
Uqer
from
alphamind.data.dbmodel.models
import
Tiny
from
alphamind.data.dbmodel.models
import
LegacyFactor
from
alphamind.data.dbmodel.models
import
Experimental
from
alphamind.data.dbmodel.models
import
SpecificRiskDay
from
alphamind.data.dbmodel.models
import
SpecificRiskShort
from
alphamind.data.dbmodel.models
import
SpecificRiskLong
from
alphamind.data.dbmodel.models
import
RiskCovDay
from
alphamind.data.dbmodel.models
import
RiskCovShort
from
alphamind.data.dbmodel.models
import
RiskCovLong
from
alphamind.data.dbmodel.models
import
RiskExposure
from
alphamind.data.dbmodel.models
import
Market
from
alphamind.data.dbmodel.models
import
FullFactorView
from
alphamind.data.dbmodel.models
import
Universe
as
UniverseTable
from
alphamind.data.transformer
import
Transformer
...
...
@@ -86,7 +78,7 @@ macro_styles = ['COUNTRY']
total_risk_factors
=
risk_styles
+
industry_styles
+
macro_styles
factor_tables
=
[
Uqer
,
LegacyFactor
,
Tiny
,
Experimental
,
RiskExposure
,
Market
]
factor_tables
=
[
FullFactorView
,
Experimental
]
def
append_industry_info
(
df
):
...
...
@@ -109,18 +101,9 @@ def _map_risk_model_table(risk_model: str) -> tuple:
raise
ValueError
(
"risk model name {0} is not recognized"
.
format
(
risk_model
))
def
_map_factors
(
factors
:
Iterable
[
str
])
->
List
:
factor_cols
=
[]
excluded
=
{
'trade_date'
,
'code'
,
'isOpen'
,
}
for
f
in
factors
:
if
f
not
in
excluded
and
f
in
FullFactorView
.
__table__
.
columns
:
factor_cols
.
append
(
FullFactorView
.
__table__
.
columns
[
f
])
return
factor_cols
def
_map_factors_old
(
factors
:
Iterable
[
str
])
->
List
:
def
_map_factors
(
factors
:
Iterable
[
str
])
->
Dict
:
factor_cols
=
{}
excluded
=
{
'trade_date'
,
'code'
,
'isOpen'
,
}
excluded
=
{
'trade_date'
,
'code'
,
'isOpen'
}
for
f
in
factors
:
for
t
in
factor_tables
:
if
f
not
in
excluded
and
f
in
t
.
__table__
.
columns
:
...
...
@@ -261,8 +244,16 @@ class SqlEngine(object):
start_date
=
advanceDateByCalendar
(
'china.sse'
,
ref_date
,
str
(
-
warm_start
)
+
'b'
)
.
strftime
(
'
%
Y-
%
m-
%
d'
)
end_date
=
ref_date
query
=
select
([
FullFactorView
.
trade_date
,
FullFactorView
.
code
,
FullFactorView
.
isOpen
]
+
factor_cols
)
\
.
where
(
and_
(
FullFactorView
.
trade_date
.
between
(
start_date
,
end_date
),
FullFactorView
.
code
.
in_
(
codes
)))
big_table
=
FullFactorView
for
t
in
set
(
factor_cols
.
values
()):
if
t
.
__table__
.
name
!=
FullFactorView
.
__table__
.
name
:
big_table
=
outerjoin
(
big_table
,
t
,
and_
(
FullFactorView
.
trade_date
==
t
.
trade_date
,
FullFactorView
.
code
==
t
.
code
))
query
=
select
([
FullFactorView
.
trade_date
,
FullFactorView
.
code
,
FullFactorView
.
isOpen
]
+
list
(
factor_cols
.
keys
()))
\
.
select_from
(
big_table
)
.
where
(
and_
(
FullFactorView
.
trade_date
.
between
(
start_date
,
end_date
),
FullFactorView
.
code
.
in_
(
codes
)))
df
=
pd
.
read_sql
(
query
,
self
.
engine
)
.
sort_values
([
'trade_date'
,
'code'
])
.
set_index
(
'trade_date'
)
res
=
transformer
.
transform
(
'code'
,
df
)
...
...
@@ -316,12 +307,19 @@ class SqlEngine(object):
cond
=
universe
.
query_range
(
real_start_date
,
real_end_date
,
real_dates
)
big_table
=
join
(
FullFactorView
,
UniverseTable
,
big_table
=
FullFactorView
for
t
in
set
(
factor_cols
.
values
()):
if
t
.
__table__
.
name
!=
FullFactorView
.
__table__
.
name
:
big_table
=
outerjoin
(
big_table
,
t
,
and_
(
FullFactorView
.
trade_date
==
t
.
trade_date
,
FullFactorView
.
code
==
t
.
code
))
big_table
=
join
(
big_table
,
UniverseTable
,
and_
(
FullFactorView
.
trade_date
==
UniverseTable
.
trade_date
,
FullFactorView
.
code
==
UniverseTable
.
code
,
cond
))
query
=
select
([
FullFactorView
.
trade_date
,
FullFactorView
.
code
,
FullFactorView
.
isOpen
]
+
factor_cols
)
\
query
=
select
([
FullFactorView
.
trade_date
,
FullFactorView
.
code
,
FullFactorView
.
isOpen
]
+
list
(
factor_cols
.
keys
())
)
\
.
select_from
(
big_table
)
df
=
pd
.
read_sql
(
query
,
self
.
engine
)
.
sort_values
([
'trade_date'
,
'code'
])
.
set_index
(
'trade_date'
)
...
...
@@ -551,13 +549,13 @@ if __name__ == '__main__':
from
PyFin.api
import
*
from
alphamind.api
import
alpha_logger
db_url
=
'postgresql+psycopg2://postgres:we083826@localhost/alpha'
# db_url = 'mssql+pymssql://licheng
:A12345678!@10.63.6.220/alpha'
#
db_url = 'postgresql+psycopg2://postgres:we083826@localhost/alpha'
db_url
=
'postgresql+psycopg2://postgres
:A12345678!@10.63.6.220/alpha'
universe
=
Universe
(
'custom'
,
[
'zz500'
])
engine
=
SqlEngine
(
db_url
)
ref_date
=
'2017-08-02'
start_date
=
'201
2
-01-01'
start_date
=
'201
7
-01-01'
end_date
=
'2017-08-31'
dates
=
makeSchedule
(
start_date
,
end_date
,
'1w'
,
'china.sse'
)
...
...
@@ -569,7 +567,7 @@ if __name__ == '__main__':
start_date
=
start_date
,
end_date
=
end_date
,
dates
=
dates
,
factors
=
[
'
RVOL'
,
'EPS'
,
'CFinc1'
,
'BDTO'
,
'VAL'
,
'CHV'
,
'GREV'
,
'ROEDiluted
'
])
factors
=
[
'
EPS
'
])
alpha_logger
.
info
(
'end'
)
data2
=
engine
.
fetch_industry_range
(
universe
,
start_date
=
start_date
,
end_date
=
end_date
,
dates
=
dates
)
alpha_logger
.
info
(
'end'
)
...
...
@@ -579,4 +577,5 @@ if __name__ == '__main__':
alpha_logger
.
info
(
'end'
)
data2
=
engine
.
fetch_codes_range
(
universe
,
start_date
=
start_date
,
end_date
=
end_date
,
dates
=
dates
)
alpha_logger
.
info
(
'end'
)
alpha_logger
.
info
(
len
(
codes
))
print
(
data1
)
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