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
ca79db7e
Commit
ca79db7e
authored
Aug 20, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update example
parent
cc5ffcc0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
19 deletions
+24
-19
factor_analysis_example.py
alphamind/examples/factor_analysis_example.py
+21
-12
quantile_analysis_example.py
alphamind/examples/quantile_analysis_example.py
+3
-7
No files found.
alphamind/examples/factor_analysis_example.py
View file @
ca79db7e
...
...
@@ -29,31 +29,39 @@ universe = Universe('custom', ['zz500'])
benchmark_code
=
905
neutralize_risk
=
[
'SIZE'
]
+
industry_styles
constraint_risk
=
[
'SIZE'
]
+
industry_styles
n_bins
=
5
freq
=
'1w'
if
freq
==
'1m'
:
horizon
=
21
elif
freq
==
'1w'
:
horizon
=
4
elif
freq
==
'1d'
:
horizon
=
0
dates
=
makeSchedule
(
'2012-01-14'
,
'2017-08-14'
,
tenor
=
'1w'
,
tenor
=
freq
,
calendar
=
'china.sse'
)
final_res
=
np
.
zeros
((
len
(
dates
),
n_bins
))
total_data_dict
=
{}
for
strategy
in
strategies
:
factors
=
strategies
[
strategy
][
'factors'
]
factor_weights
=
strategies
[
strategy
][
'weights'
]
all_data
=
engine
.
fetch_data_range
(
universe
,
factors
,
dates
=
dates
,
benchmark
=
905
)
factor_all_data
=
all_data
[
'factor'
]
factor_groups
=
factor_all_data
.
groupby
(
'Date'
)
rets
=
[]
for
i
,
date
in
enumerate
(
dates
):
for
i
,
value
in
enumerate
(
factor_groups
):
date
=
value
[
0
]
data
=
value
[
1
]
codes
=
data
.
Code
.
tolist
()
ref_date
=
date
.
strftime
(
'
%
Y-
%
m-
%
d'
)
returns
=
engine
.
fetch_dx_return
(
ref_date
,
codes
,
horizon
=
horizon
)
codes
=
engine
.
fetch_codes
(
ref_date
,
universe
)
data
=
engine
.
fetch_data
(
ref_date
,
factors
,
codes
,
benchmark_code
)
returns
=
engine
.
fetch_dx_return
(
ref_date
,
codes
,
horizon
=
4
)
total_data
=
pd
.
merge
(
data
[
'factor'
],
returns
,
on
=
[
'Code'
])
.
dropna
()
total_data
=
pd
.
merge
(
data
,
returns
,
on
=
[
'Code'
])
.
dropna
()
print
(
date
,
': '
,
len
(
total_data
))
risk_exp
=
total_data
[
neutralize_risk
]
.
values
.
astype
(
float
)
industry
=
total_data
.
industry_code
.
values
...
...
@@ -97,3 +105,4 @@ for strategy in strategies:
ret_df
=
pd
.
DataFrame
(
total_data_dict
,
index
=
dates
)
ret_df
.
cumsum
()
.
plot
(
figsize
=
(
12
,
6
))
plt
.
savefig
(
"backtest_big_universe_20170814.png"
)
plt
.
show
()
alphamind/examples/quantile_analysis_example.py
View file @
ca79db7e
...
...
@@ -11,15 +11,14 @@ from matplotlib import pyplot as plt
from
PyFin.api
import
*
from
alphamind.api
import
*
#engine = SqlEngine("mssql+pymssql://licheng:A12345678!@10.63.6.220/alpha")
engine
=
SqlEngine
(
'postgresql+psycopg2://postgres:we083826@localhost/alpha'
)
engine
=
SqlEngine
(
"mssql+pymssql://licheng:A12345678!@10.63.6.220/alpha"
)
universe
=
Universe
(
'custom'
,
[
'zz500'
])
neutralize_risk
=
[
'SIZE'
]
+
industry_styles
n_bins
=
5
factor_weights
=
np
.
array
([
1.
])
freq
=
'1
d
'
freq
=
'1
w
'
if
freq
==
'1m'
:
horizon
=
21
...
...
@@ -40,7 +39,6 @@ prod_factors = ['EARNYILD', 'ROAEBIT', 'CHV', 'CFinc1']
all_data
=
engine
.
fetch_data_range
(
universe
,
prod_factors
,
dates
=
dates
,
benchmark
=
905
)
factor_all_data
=
all_data
[
'factor'
]
return_all_data
=
engine
.
fetch_dx_return_range
(
universe
,
start_date
,
end_date
,
dates
,
horizon
=
horizon
)
for
factor
in
prod_factors
:
...
...
@@ -48,14 +46,12 @@ for factor in prod_factors:
final_res
=
np
.
zeros
((
len
(
dates
),
n_bins
))
factor_groups
=
factor_all_data
.
groupby
(
'Date'
)
return_groups
=
return_all_data
.
groupby
(
'Date'
)
for
i
,
value
in
enumerate
(
factor_groups
):
date
=
value
[
0
]
data
=
value
[
1
]
codes
=
data
.
Code
.
tolist
()
ref_date
=
value
[
0
]
.
strftime
(
'
%
Y-
%
m-
%
d'
)
returns
=
return_groups
.
get_group
(
date
)
returns
=
engine
.
fetch_dx_return
(
date
,
codes
,
horizon
=
horizon
)
total_data
=
pd
.
merge
(
data
,
returns
,
on
=
[
'Code'
])
.
dropna
()
print
(
date
,
': '
,
len
(
total_data
))
...
...
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