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
575c79ca
Commit
575c79ca
authored
Dec 30, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added example_101
parent
dce05956
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
115 additions
and
0 deletions
+115
-0
example_101.py
alphamind/examples/example_101.py
+115
-0
No files found.
alphamind/examples/example_101.py
0 → 100644
View file @
575c79ca
# -*- coding: utf-8 -*-
"""
Created on 2017-12-30
@author: cheng.li
"""
import
numpy
as
np
import
pandas
as
pd
from
matplotlib
import
pyplot
as
plt
from
PyFin.api
import
*
from
alphamind.api
import
*
engine
=
SqlEngine
()
start_date
=
'2017-01-01'
end_date
=
'2017-12-25'
universe
=
Universe
(
'custom'
,
[
'zz800'
])
neutralize_risk
=
[
'SIZE'
]
+
industry_styles
factors
=
[
CSRes
(
LAST
(
'closePrice'
)
/
LAST
(
'openPrice'
),
LAST
(
'turnoverVol'
)),
LAST
(
'lowestPrice'
)]
benchmark
=
300
build_type
=
'risk_neutral'
freq
=
'5b'
horizon
=
map_freq
(
freq
)
factors_data
=
fetch_data_package
(
engine
,
alpha_factors
=
factors
,
start_date
=
start_date
,
end_date
=
end_date
,
frequency
=
freq
,
universe
=
universe
,
benchmark
=
benchmark
,
batch
=
1
,
neutralized_risk
=
neutralize_risk
,
pre_process
=
[
winsorize_normal
,
standardize
],
post_process
=
[
winsorize_normal
,
standardize
])
x_names
=
factors_data
[
'x_names'
]
train_x
=
factors_data
[
'train'
][
'x'
]
train_y
=
factors_data
[
'train'
][
'y'
]
ref_dates
=
sorted
(
train_x
.
keys
())
predict_x
=
factors_data
[
'predict'
][
'x'
]
settlement
=
factors_data
[
'settlement'
]
benchmark_w
=
settlement
[
'weight'
]
.
values
industry_names
=
settlement
[
'industry'
]
.
values
realized_r
=
settlement
[
'dx'
]
.
values
risk_exp
=
settlement
[
neutralize_risk
]
.
values
"""
Training phase
"""
models_series
=
pd
.
Series
()
for
date
in
ref_dates
:
x
=
train_x
[
date
]
y
=
train_y
[
date
]
.
flatten
()
model
=
LinearRegression
(
fit_intercept
=
False
,
features
=
x_names
)
model
.
fit
(
x
,
y
)
models_series
.
loc
[
date
]
=
model
alpha_logger
.
info
(
'trade_date: {0} training finished'
.
format
(
date
))
"""
Predicting and re-balance phase
"""
index_dates
=
[]
final_res
=
np
.
zeros
(
len
(
ref_dates
))
for
i
,
date
in
enumerate
(
ref_dates
):
this_date_x
=
predict_x
[
date
]
index
=
settlement
.
trade_date
==
date
this_benchmark_w
=
benchmark_w
[
index
]
this_industry_names
=
industry_names
[
index
]
this_realized_r
=
realized_r
[
index
]
# linear regression model
model
=
models_series
[
date
]
predict_y
=
model
.
predict
(
this_date_x
)
# set constraint
this_risk_exp
=
risk_exp
[
index
]
lbound
=
np
.
zeros
(
len
(
this_date_x
))
ubound
=
0.02
*
np
.
ones
(
len
(
this_date_x
))
cons
=
Constraints
()
cons
.
add_exposure
(
neutralize_risk
,
this_risk_exp
)
risk_target
=
this_risk_exp
.
T
@
this_benchmark_w
for
k
,
name
in
enumerate
(
neutralize_risk
):
cons
.
set_constraints
(
name
,
risk_target
[
k
],
risk_target
[
k
])
weights
,
analysis
=
er_portfolio_analysis
(
predict_y
,
this_industry_names
,
this_realized_r
,
constraints
=
cons
,
detail_analysis
=
True
,
benchmark
=
this_benchmark_w
,
method
=
build_type
)
final_res
[
i
]
=
analysis
[
'er'
][
'total'
]
alpha_logger
.
info
(
'trade_date: {0} predicting finished'
.
format
(
date
))
# Plot the cumulative returns
df
=
pd
.
Series
(
final_res
,
index
=
ref_dates
)
df
.
sort_index
(
inplace
=
True
)
df
.
cumsum
()
.
plot
()
plt
.
title
(
'Factors model {1} ({0})'
.
format
(
build_type
,
models_series
.
iloc
[
0
]
.
__class__
.
__name__
))
plt
.
show
()
\ 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