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
c2f950d3
Commit
c2f950d3
authored
Jul 11, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update example
parent
8ae8ce45
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
17 deletions
+35
-17
sqlengine.py
alphamind/data/engines/sqlengine.py
+15
-3
example2.py
alphamind/examples/example2.py
+20
-14
No files found.
alphamind/data/engines/sqlengine.py
View file @
c2f950d3
...
...
@@ -12,6 +12,7 @@ import numpy as np
import
pandas
as
pd
import
sqlalchemy
as
sa
from
alphamind.data.engines.universe
import
Universe
from
PyFin.api
import
advanceDateByCalendar
risk_styles
=
[
'BETA'
,
'MOMENTUM'
,
...
...
@@ -102,10 +103,21 @@ class SqlEngine(object):
return
sorted
(
codes_set
)
def
fetch_d1_return
(
self
,
ref_date
,
codes
):
def
fetch_dx_return
(
self
,
ref_date
,
codes
,
expiry_date
=
None
,
horizon
=
1
):
start_date
=
ref_date
if
not
expiry_date
and
horizon
:
end_date
=
advanceDateByCalendar
(
'china.sse'
,
ref_date
,
str
(
horizon
)
+
'b'
)
.
strftime
(
'
%
Y
%
m
%
d'
)
elif
expiry_date
:
end_date
=
expiry_date
codes_str
=
','
.
join
(
str
(
c
)
for
c
in
codes
)
sql
=
"select Code, d1 from daily_return where Date = '{ref_date}' and Code in ({codes})"
.
format
(
ref_date
=
ref_date
,
codes
=
codes_str
)
sql
=
"select Code, sum(d1) as dx from daily_return "
\
"where Date >= '{start_date}' and Date < '{end_date}'"
\
" and Code in ({codes}) GROUP BY Code"
.
format
(
start_date
=
start_date
,
end_date
=
end_date
,
codes
=
codes_str
)
return
pd
.
read_sql
(
sql
,
self
.
engine
)
def
fetch_data
(
self
,
ref_date
,
...
...
alphamind/examples/example2.py
View file @
c2f950d3
...
...
@@ -9,49 +9,55 @@ import pandas as pd
from
alphamind.analysis.factoranalysis
import
factor_analysis
from
alphamind.data.engines.sqlengine
import
SqlEngine
from
alphamind.data.engines.universe
import
Universe
from
alphamind.data.engines.sqlengine
import
risk_styles
from
alphamind.data.engines.sqlengine
import
industry_styles
from
PyFin.api
import
bizDatesList
engine
=
SqlEngine
(
'mssql+pymssql://licheng:A12345678!@10.63.6.220/alpha'
)
universe
=
Universe
(
'custom'
,
[
'zz500'
])
total_risks
=
risk_styles
+
industry_styles
type
=
'risk_neutral'
used_risk_styles
=
[
'SIZE'
]
def
calculate_one_day
(
ref_date
,
factors
,
weights
):
total_risks
=
used_risk_styles
+
industry_styles
build_type
=
'risk_neutral'
def
calculate_one_day
(
ref_date
,
factors
,
factor_weights
):
print
(
ref_date
)
codes
=
engine
.
fetch_codes
(
ref_date
,
universe
)
total_data
=
engine
.
fetch_data
(
ref_date
,
factors
,
codes
,
905
)
factor_data
=
total_data
[
'factor'
]
factor_df
=
factor_data
[[
'Code'
,
'industry'
,
'd1'
,
'weight'
,
'isOpen'
]
+
total_risks
+
factors
]
.
dropna
()
factor_df
=
factor_data
[[
'Code'
,
'industry'
,
'weight'
,
'isOpen'
]
+
total_risks
+
factors
]
.
dropna
()
dx_return
=
engine
.
fetch_dx_return
(
ref_date
,
codes
)
factor_df
=
pd
.
merge
(
factor_df
,
dx_return
,
on
=
[
'Code'
])
weights
,
_
=
factor_analysis
(
factor_df
[
factors
],
weights
,
factor_
weights
,
factor_df
.
industry
.
values
,
factor_df
.
d1
.
values
,
None
,
detail_analysis
=
False
,
benchmark
=
factor_df
.
weight
.
values
,
risk_exp
=
factor_df
[
total_risks
]
.
values
,
is_tradable
=
factor_df
.
isOpen
.
values
,
method
=
type
)
return
ref_date
,
(
weights
.
weight
-
factor_df
.
weight
)
.
dot
(
factor_df
.
d1
)
is_tradable
=
factor_df
.
isOpen
.
values
.
astype
(
bool
),
method
=
build_type
)
return
ref_date
,
(
weights
.
weight
-
factor_df
.
weight
)
.
dot
(
factor_df
.
dx
)
if
__name__
==
'__main__'
:
from
matplotlib
import
pyplot
as
plt
factors
=
[
'
EPS'
]
#, 'FY12P', 'VAL', 'CFinc1', 'BDTO', 'RVOL
']
weights
=
[
1.
]
#, 1., 1., 1., 0.5, 0.
5]
biz_dates
=
bizDatesList
(
'china.sse'
,
'201
3-01-01'
,
'2017-07-05
'
)
factors
=
[
'
BDTO'
,
'CFinc1'
,
'DivP'
,
'EPS'
,
'RVOL'
,
'DROEAfterNonRecurring
'
]
factor_weights
=
[
0.10
,
0.30
,
0.15
,
0.18
,
0.11
,
0.3
5
]
biz_dates
=
bizDatesList
(
'china.sse'
,
'201
7-01-01'
,
'2017-07-07
'
)
ers
=
[]
dates
=
[]
for
ref_date
in
biz_dates
:
ref_date
,
er
=
calculate_one_day
(
ref_date
,
factors
,
weights
)
ref_date
,
er
=
calculate_one_day
(
ref_date
,
factors
,
factor_
weights
)
dates
.
append
(
ref_date
)
ers
.
append
(
er
)
...
...
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