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
0a5a5b7e
Commit
0a5a5b7e
authored
Aug 16, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update examples
parent
24995683
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
138 additions
and
97 deletions
+138
-97
.gitignore
.gitignore
+0
-1
__init__.py
alphamind/examples/__init__.py
+0
-6
config.py
alphamind/examples/config.py
+0
-62
factor_analysis_example.py
alphamind/examples/factor_analysis_example.py
+79
-0
quantile_analysis_example.py
alphamind/examples/quantile_analysis_example.py
+59
-0
single_day_portfolio.py
alphamind/examples/single_day_portfolio.py
+0
-28
No files found.
.gitignore
View file @
0a5a5b7e
...
...
@@ -10,5 +10,4 @@ Alpha_Mind.egg-info/*
*.nbc
*.nbi
/notebooks/.ipynb_checkpoints
alphamind/examples/*
notebooks/*
\ No newline at end of file
alphamind/examples/__init__.py
deleted
100644 → 0
View file @
24995683
# -*- coding: utf-8 -*-
"""
Created on 2017-5-15
@author: cheng.li
"""
\ No newline at end of file
alphamind/examples/config.py
deleted
100644 → 0
View file @
24995683
# -*- coding: utf-8 -*-
"""
Created on 2017-5-15
@author: cheng.li
"""
risk_factors_500
=
[
'CommunicationsAndTransportation'
,
'LeisureServices'
,
'MultiMedia'
,
'PublicUtility'
,
'Agriculture'
,
'ChemicalIndustry'
,
'MedicationAndBio'
,
'CommercialTrade'
,
'DefenseIndustry'
,
'HouseholdAppliances'
,
'ConstructionAndMaterial'
,
'BuildingDecoration'
,
'RealEstate'
,
'DiversifiedMetal'
,
'Machinary'
,
'MotorVehicle'
,
'ElectronicIndustry'
,
'ElectricalEquip'
,
'TextileAndGarment'
,
'Synthetics'
,
'Computer'
,
'LightManufacturing'
,
'Telecoms'
,
'ExtractiveIndustry'
,
'Metal'
,
'FoodAndBeverage'
,
'Size'
]
risk_factors_300
=
[
'CommunicationsAndTransportation'
,
'LeisureServices'
,
'MultiMedia'
,
'PublicUtility'
,
'Agriculture'
,
'ChemicalIndustry'
,
'MedicationAndBio'
,
'CommercialTrade'
,
'DefenseIndustry'
,
'HouseholdAppliances'
,
'ConstructionAndMaterial'
,
'BuildingDecoration'
,
'RealEstate'
,
'DiversifiedMetal'
,
'Machinary'
,
'MotorVehicle'
,
'ElectronicIndustry'
,
'ElectricalEquip'
,
'TextileAndGarment'
,
'Synthetics'
,
'Computer'
,
'Telecoms'
,
'ExtractiveIndustry'
,
'Metal'
,
'FoodAndBeverage'
,
'Size'
]
alphamind/examples/
example2
.py
→
alphamind/examples/
factor_analysis_example
.py
View file @
0a5a5b7e
# -*- coding: utf-8 -*-
"""
Created on 2017-
7-10
Created on 2017-
8-16
@author: cheng.li
"""
import
numpy
as
np
import
pandas
as
pd
from
alphamind.analysis.factoranalysis
import
factor_analysis
from
alphamind.data.engines.sqlengine
import
risk_styles
from
alphamind.data.engines.sqlengine
import
industry_styles
from
alphamind.portfolio.constraints
import
Constraints
from
alphamind.data.engines.sqlengine
import
SqlEngine
from
alphamind.data.engines.universe
import
Universe
from
PyFin.api
import
bizDatesList
engine
=
SqlEngine
(
'mssql+pymssql://licheng:A12345678!@10.63.6.220/alpha'
)
universe
=
Universe
(
'custom'
,
[
'zz500'
])
dates
=
bizDatesList
(
'china.sse'
,
'2017-01-01'
,
'2017-08-05'
)
factors
=
[
'EPS'
,
'FEARNG'
,
'VAL'
,
'NIAP'
]
f_weights
=
np
.
array
([
1.
,
1.
,
1.
,
1.
])
from
matplotlib
import
pyplot
as
plt
from
PyFin.api
import
makeSchedule
from
alphamind.api
import
*
engine
=
SqlEngine
(
"mssql+pymssql://licheng:A12345678!@10.63.6.220/alpha"
)
universe
=
Universe
(
'custom'
,
[
'zz500'
])
neutralize_risk
=
[
'SIZE'
]
+
industry_styles
constraint_risk
=
[]
constraint_risk
=
[
'SIZE'
]
+
industry_styles
n_bins
=
5
factors
=
[
'ROEDiluted'
]
factor_weights
=
np
.
array
([
1.
])
dates
=
makeSchedule
(
'2017-01-01'
,
'2017-08-14'
,
tenor
=
'1w'
,
calendar
=
'china.sse'
)
final_res
=
np
.
zeros
((
len
(
dates
),
n_bins
))
rets
=
[]
for
date
in
dates
:
for
i
,
date
in
enumerate
(
dates
)
:
print
(
date
)
ref_date
=
date
.
strftime
(
'
%
Y-
%
m-
%
d'
)
codes
=
engine
.
fetch_codes
(
ref_date
,
universe
)
data
=
engine
.
fetch_data
(
ref_date
,
factors
,
codes
,
905
,
risk_model
=
'short'
)
returns
=
engine
.
fetch_dx_return
(
ref_date
,
codes
,
0
)
total_data
=
pd
.
merge
(
data
[
'factor'
],
returns
,
on
=
[
'Code'
])
.
dropna
()
risk_cov
=
data
[
'risk_cov'
]
total_risks
=
risk_cov
.
Factor
risk_cov
=
risk_cov
[
total_risks
]
risk_exp
=
total_data
[
total_risks
]
stocks_cov
=
((
risk_exp
.
values
@
risk_cov
.
values
@
risk_exp
.
values
.
T
)
+
np
.
diag
(
total_data
.
SRISK
**
2
))
/
10000.
codes
=
engine
.
fetch_codes
(
ref_date
,
universe
)
f_data
=
total_data
[
factors
]
data
=
engine
.
fetch_data
(
ref_date
,
factors
,
codes
,
905
)
returns
=
engine
.
fetch_dx_return
(
ref_date
,
codes
,
horizon
=
4
)
total_data
=
pd
.
merge
(
data
[
'factor'
],
returns
,
on
=
[
'Code'
])
.
dropna
()
risk_exp
=
total_data
[
neutralize_risk
]
.
values
.
astype
(
float
)
industry
=
total_data
.
industry_code
.
values
dx_return
=
total_data
.
dx
.
values
benchmark
=
total_data
.
weight
.
values
risk_exp
=
total_data
[
neutralize_risk
]
.
values
constraint_exp
=
total_data
[
constraint_risk
]
.
values
risk_exp_expand
=
np
.
concatenate
((
constraint_exp
,
np
.
ones
((
len
(
risk_exp
),
1
))),
axis
=
1
)
.
astype
(
float
)
risk_names
=
constraint_risk
+
[
'total'
]
risk_target
=
risk_exp_expand
.
T
@
benchmark
lbound
=
0.
ubound
=
0.0
5
+
benchmark
lbound
=
np
.
zeros
(
len
(
total_data
))
ubound
=
0.0
1
+
benchmark
constraint
=
Constraints
(
risk_exp_expand
,
risk_names
)
for
i
,
name
in
enumerate
(
risk_names
):
constraint
.
set_constraints
(
name
,
lower_bound
=
risk_target
[
i
],
upper_bound
=
risk_target
[
i
])
f_data
=
total_data
[
factors
]
try
:
pos
,
analysis
=
factor_analysis
(
f_data
,
f_weights
,
industry
,
dx_return
,
benchmark
=
benchmark
,
factor_weights
,
industry
=
industry
,
d1returns
=
dx_return
,
risk_exp
=
risk_exp
,
benchmark
=
benchmark
,
is_tradable
=
total_data
.
isOpen
.
values
.
astype
(
bool
),
method
=
'mv'
,
constraints
=
constraint
,
cov
=
stocks_cov
,
use_rank
=
100
,
lam
=
100.
,
lbound
=
lbound
,
ubound
=
ubound
)
method
=
'risk_neutral'
,
constraints
=
constraint
)
except
:
print
(
date
,
' is error!'
)
rets
.
append
(
0.
)
print
(
"{0} is error!"
.
format
(
date
))
else
:
rets
.
append
(
analysis
.
er
[
-
1
])
ret_series
=
pd
.
Series
(
rets
,
dates
)
\ No newline at end of file
ret_series
=
pd
.
Series
(
rets
,
dates
)
ret_series
.
cumsum
()
.
plot
(
figsize
=
(
12
,
6
))
plt
.
title
(
'{0} weekly reblance'
.
format
(
factors
))
plt
.
show
()
alphamind/examples/quantile_analysis_example.py
0 → 100644
View file @
0a5a5b7e
# -*- coding: utf-8 -*-
"""
Created on 2017-8-16
@author: cheng.li
"""
import
numpy
as
np
import
pandas
as
pd
from
matplotlib
import
pyplot
as
plt
from
PyFin.api
import
makeSchedule
from
alphamind.api
import
*
engine
=
SqlEngine
(
"mssql+pymssql://licheng:A12345678!@10.63.6.220/alpha"
)
universe
=
Universe
(
'custom'
,
[
'zz500'
])
neutralize_risk
=
[
'SIZE'
]
+
industry_styles
n_bins
=
5
factors
=
[
'ROEDiluted'
]
factor_weights
=
np
.
array
([
1.
])
dates
=
makeSchedule
(
'2017-01-01'
,
'2017-08-14'
,
tenor
=
'1w'
,
calendar
=
'china.sse'
)
final_res
=
np
.
zeros
((
len
(
dates
),
n_bins
))
for
i
,
date
in
enumerate
(
dates
):
print
(
date
)
ref_date
=
date
.
strftime
(
'
%
Y-
%
m-
%
d'
)
codes
=
engine
.
fetch_codes
(
ref_date
,
universe
)
data
=
engine
.
fetch_data
(
ref_date
,
factors
,
codes
,
905
)
returns
=
engine
.
fetch_dx_return
(
ref_date
,
codes
,
horizon
=
4
)
total_data
=
pd
.
merge
(
data
[
'factor'
],
returns
,
on
=
[
'Code'
])
.
dropna
()
risk_exp
=
total_data
[
neutralize_risk
]
.
values
.
astype
(
float
)
dx_return
=
total_data
.
dx
.
values
benchmark
=
total_data
.
weight
.
values
f_data
=
total_data
[
factors
]
try
:
res
=
quantile_analysis
(
f_data
,
factor_weights
,
dx_return
,
risk_exp
=
risk_exp
,
n_bins
=
n_bins
,
benchmark
=
benchmark
)
except
:
print
(
date
,
' is error!'
)
res
=
np
.
zeros
(
n_bins
)
final_res
[
i
]
=
res
df
=
pd
.
DataFrame
(
final_res
,
index
=
dates
)
df
.
cumsum
()
.
plot
(
figsize
=
(
12
,
6
))
plt
.
title
(
'{0} weekly reblance'
.
format
(
factors
))
plt
.
show
()
\ No newline at end of file
alphamind/examples/single_day_portfolio.py
deleted
100644 → 0
View file @
24995683
# -*- coding: utf-8 -*-
"""
Created on 2017-5-15
@author: cheng.li
"""
import
sqlalchemy
as
sa
from
alphamind.data.engines.sqlengine
import
risk_styles
from
alphamind.data.engines.sqlengine
import
industry_styles
from
alphamind.data.engines.sqlengine
import
SqlEngine
from
alphamind.data.engines.universe
import
Universe
from
alphamind.data.neutralize
import
neutralize
from
alphamind.data.standardize
import
standardize
from
alphamind.data.winsorize
import
winsorize_normal
from
PyFin.api
import
bizDatesList
universe
=
Universe
(
"zz500"
,
[
'zz500'
])
engine
=
SqlEngine
(
"mysql+pymysql://root:we083826@localhost/alpha?charset=utf8"
)
dates
=
bizDatesList
(
'china.sse'
,
'2017-01-01'
,
'2017-07-10'
)
for
date
in
dates
:
print
(
date
)
ref_date
=
date
.
strftime
(
"
%
Y-
%
m-
%
d"
)
codes
=
engine
.
fetch_codes
(
ref_date
,
universe
)
engine
.
fetch_data
(
ref_date
,
[
'EPS'
],
codes
)
\ 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