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
4440b488
Commit
4440b488
authored
Aug 18, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
modified
parent
903df29d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
120 additions
and
92 deletions
+120
-92
models.py
alphamind/data/dbmodel/models.py
+8
-8
factor_analysis_example.py
alphamind/examples/factor_analysis_example.py
+75
-55
quantile_analysis_example.py
alphamind/examples/quantile_analysis_example.py
+37
-29
No files found.
alphamind/data/dbmodel/models.py
View file @
4440b488
...
...
@@ -1074,19 +1074,18 @@ class Uqer(Base):
class
SecurityMaster
(
Base
):
__tablename__
=
'security_master'
Code
=
Column
(
Integer
,
primary_key
=
True
)
exchangeCD
=
Column
(
Text
())
exchangeCD
=
Column
(
String
(
4
))
ListSectorCD
=
Column
(
BigInteger
)
ListSector
=
Column
(
Text
(
))
ListSector
=
Column
(
String
(
6
))
transCurrCD
=
Column
(
Text
())
secShortName
=
Column
(
Text
(
))
secShortName
=
Column
(
String
(
10
))
secFullName
=
Column
(
Text
())
listStatusCD
=
Column
(
Text
(
))
listStatusCD
=
Column
(
String
(
2
))
listDate
=
Column
(
DateTime
)
delistDate
=
Column
(
DateTime
)
equTypeCD
=
Column
(
Text
(
))
equType
=
Column
(
Text
(
))
exCountryCD
=
Column
(
Text
(
))
equTypeCD
=
Column
(
String
(
4
))
equType
=
Column
(
String
(
10
))
exCountryCD
=
Column
(
String
(
3
))
partyID
=
Column
(
BigInteger
)
totalShares
=
Column
(
Float
(
53
))
nonrestFloatShares
=
Column
(
Float
(
53
))
...
...
@@ -1095,6 +1094,7 @@ class SecurityMaster(Base):
primeOperating
=
Column
(
Text
())
endDate
=
Column
(
DateTime
)
TShEquity
=
Column
(
Float
(
53
))
Code
=
Column
(
Integer
,
primary_key
=
True
)
if
__name__
==
'__main__'
:
...
...
alphamind/examples/factor_analysis_example.py
View file @
4440b488
...
...
@@ -11,69 +11,89 @@ from matplotlib import pyplot as plt
from
PyFin.api
import
makeSchedule
from
alphamind.api
import
*
strategies
=
{
'prod'
:
{
'factors'
:
[
'RVOL'
,
'EPS'
,
'DROEAfterNonRecurring'
,
'DivP'
,
'CFinc1'
,
'BDTO'
],
'weights'
:
[
0.05
,
0.3
,
0.35
,
0.075
,
0.15
,
0.05
]
},
'candidate'
:
{
'factors'
:
[
'RVOL'
,
'EPS'
,
'CFinc1'
,
'BDTO'
,
'VAL'
,
'GREV'
,
'ROEDiluted'
],
'weights'
:
[
0.02
,
0.2
,
0.15
,
0.05
,
0.2
,
0.2
,
0.2
]
}
}
engine
=
SqlEngine
(
"mssql+pymssql://licheng:A12345678!@10.63.6.220/alpha"
)
universe
=
Universe
(
'custom'
,
[
'zz500'
])
benchmark_code
=
905
neutralize_risk
=
[
'SIZE'
]
+
industry_styles
constraint_risk
=
[
'SIZE'
]
+
industry_styles
n_bins
=
5
factors
=
[
'ROEDiluted'
]
factor_weights
=
np
.
array
([
1.
])
dates
=
makeSchedule
(
'2017-01-01'
,
dates
=
makeSchedule
(
'2012-01-14'
,
'2017-08-14'
,
tenor
=
'1w'
,
calendar
=
'china.sse'
)
final_res
=
np
.
zeros
((
len
(
dates
),
n_bins
))
rets
=
[]
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
)
industry
=
total_data
.
industry_code
.
values
dx_return
=
total_data
.
dx
.
values
benchmark
=
total_data
.
weight
.
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
=
np
.
zeros
(
len
(
total_data
))
ubound
=
0.01
+
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
,
factor_weights
,
industry
=
industry
,
d1returns
=
dx_return
,
risk_exp
=
risk_exp
,
benchmark
=
benchmark
,
is_tradable
=
total_data
.
isOpen
.
values
.
astype
(
bool
),
method
=
'risk_neutral'
,
constraints
=
constraint
)
except
:
print
(
date
,
' is error!'
)
rets
.
append
(
0.
)
else
:
rets
.
append
(
analysis
.
er
[
-
1
])
ret_series
=
pd
.
Series
(
rets
,
dates
)
ret_series
.
cumsum
()
.
plot
(
figsize
=
(
12
,
6
))
plt
.
title
(
'{0} weekly reblance'
.
format
(
factors
))
plt
.
show
()
total_data_dict
=
{}
for
strategy
in
strategies
:
factors
=
strategies
[
strategy
][
'factors'
]
factor_weights
=
strategies
[
strategy
][
'weights'
]
rets
=
[]
for
i
,
date
in
enumerate
(
dates
):
ref_date
=
date
.
strftime
(
'
%
Y-
%
m-
%
d'
)
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
()
print
(
date
,
': '
,
len
(
total_data
))
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
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
=
np
.
zeros
(
len
(
total_data
))
ubound
=
0.01
+
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
,
factor_weights
,
industry
=
industry
,
d1returns
=
dx_return
,
risk_exp
=
risk_exp
,
benchmark
=
benchmark
,
is_tradable
=
total_data
.
isOpen
.
values
.
astype
(
bool
),
method
=
'risk_neutral'
,
constraints
=
constraint
,
use_rank
=
100
)
except
Exception
as
e
:
print
(
e
)
rets
.
append
(
0.
)
else
:
rets
.
append
(
analysis
.
er
[
-
1
]
/
benchmark
.
sum
())
total_data_dict
[
strategy
]
=
rets
ret_df
=
pd
.
DataFrame
(
total_data_dict
,
index
=
dates
)
ret_df
.
cumsum
()
.
plot
(
figsize
=
(
12
,
6
))
plt
.
savefig
(
"backtest_big_universe_20170814.png"
)
alphamind/examples/quantile_analysis_example.py
View file @
4440b488
...
...
@@ -12,48 +12,56 @@ from PyFin.api import makeSchedule
from
alphamind.api
import
*
engine
=
SqlEngine
(
"mssql+pymssql://licheng:A12345678!@10.63.6.220/alpha"
)
universe
=
Universe
(
'custom'
,
[
'zz500'
])
#engine = SqlEngine('postgresql+psycopg2://postgres:A12345678!@10.63.6.220/alpha')
universe
=
Universe
(
'custom'
,
[
'pm500_mirror'
])
neutralize_risk
=
[
'SIZE'
]
+
industry_styles
n_bins
=
5
factors
=
[
'ROEDiluted'
]
factor_weights
=
np
.
array
([
1.
])
dates
=
makeSchedule
(
'201
7-01-01
'
,
dates
=
makeSchedule
(
'201
6-08-14
'
,
'2017-08-14'
,
tenor
=
'1w'
,
calendar
=
'china.sse'
)
final_res
=
np
.
zeros
((
len
(
dates
),
n_bins
))
prod_factors
=
[
'EARNYILD'
,
'ROAEBIT'
]
for
factor
in
prod_factors
:
factors
=
[
factor
]
final_res
=
np
.
zeros
((
len
(
dates
),
n_bins
))
for
i
,
date
in
enumerate
(
dates
):
ref_date
=
date
.
strftime
(
'
%
Y-
%
m-
%
d'
)
for
i
,
date
in
enumerate
(
dates
):
print
(
date
)
ref_date
=
date
.
strftime
(
'
%
Y-
%
m-
%
d'
)
codes
=
engine
.
fetch_codes
(
ref_date
,
universe
)
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
)
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
()
print
(
date
,
': '
,
len
(
total_data
))
risk_exp
=
total_data
[
neutralize_risk
]
.
values
.
astype
(
float
)
dx_return
=
total_data
.
dx
.
values
benchmark
=
total_data
.
weight
.
values
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
Exception
as
e
:
print
(
e
)
res
=
np
.
zeros
(
n_bins
)
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
/
benchmark
.
sum
()
final_res
[
i
]
=
res
df
=
pd
.
DataFrame
(
final_res
,
index
=
dates
)
df
.
cumsum
()
.
plot
(
figsize
=
(
12
,
6
))
plt
.
title
(
'{0} weekly re-balance'
.
format
(
factors
[
0
]))
plt
.
savefig
(
'{0}_big_universe_20170814.png'
.
format
(
factors
[
0
]))
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
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