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
38432d45
Commit
38432d45
authored
Oct 12, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added data log table
parent
b7fa9cc6
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
62 additions
and
32 deletions
+62
-32
models.py
alphamind/data/dbmodel/models.py
+15
-0
factor_analysis_example.py
alphamind/examples/factor_analysis_example.py
+12
-15
formula_expression.py
alphamind/examples/formula_expression.py
+13
-7
model_training.py
alphamind/examples/model_training.py
+22
-10
No files found.
alphamind/data/dbmodel/models.py
View file @
38432d45
...
@@ -2370,6 +2370,21 @@ class Uqer(Base):
...
@@ -2370,6 +2370,21 @@ class Uqer(Base):
NLSIZE
=
Column
(
Float
(
53
))
NLSIZE
=
Column
(
Float
(
53
))
class
DataLog
(
Base
):
__tablename__
=
'data_log'
__table_args__
=
(
Index
(
'data_log_idx'
,
'trade_date'
,
'factor'
,
'source'
,
'universe'
,
unique
=
True
),
)
trade_date
=
Column
(
DateTime
,
primary_key
=
True
,
nullable
=
False
)
factor
=
Column
(
String
(
30
),
primary_key
=
True
,
nullable
=
False
)
source
=
Column
(
String
(
30
),
primary_key
=
True
,
nullable
=
False
)
universe
=
Column
(
String
(
20
),
primary_key
=
True
,
nullable
=
False
)
coverage
=
Column
(
Float
(
53
))
maximum
=
Column
(
Float
(
53
))
minimum
=
Column
(
Float
(
53
))
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
from
sqlalchemy
import
create_engine
from
sqlalchemy
import
create_engine
...
...
alphamind/examples/factor_analysis_example.py
View file @
38432d45
...
@@ -11,23 +11,22 @@ from matplotlib import pyplot as plt
...
@@ -11,23 +11,22 @@ from matplotlib import pyplot as plt
from
alphamind.api
import
*
from
alphamind.api
import
*
from
PyFin.api
import
*
from
PyFin.api
import
*
strategies
=
{
strategies
=
{
'prod'
:
{
'prod'
:
{
# 'factors': ['RVOL', 'EPS', 'DROEAfterNonRecurring', 'DivP', 'CFinc1', 'BDTO'],
# 'factors': ['RVOL', 'EPS', 'DROEAfterNonRecurring', 'DivP', 'CFinc1', 'BDTO'],
# 'weights': [0.05, 0.3, 0.35, 0.075, 0.15, 0.05]
# 'weights': [0.05, 0.3, 0.35, 0.075, 0.15, 0.05]
#'factors': ['RVOL', 'EPS', 'DROEAfterNonRecurring', 'DivP', 'CFinc1', 'BDTO'],
#
'factors': ['RVOL', 'EPS', 'DROEAfterNonRecurring', 'DivP', 'CFinc1', 'BDTO'],
#'weights': [0.05, 0.3, 0.35, 0.075, 0.15, 0.05]
#
'weights': [0.05, 0.3, 0.35, 0.075, 0.15, 0.05]
'factors'
:
[
'VAL'
,
'RVOL'
,
'ROEDiluted'
,
'GREV'
,
'EPS'
,
'CHV'
,
'CFinc1'
,
'BDTO'
],
'factors'
:
[
'VAL'
,
'RVOL'
,
'ROEDiluted'
,
'GREV'
,
'EPS'
,
'CHV'
,
'CFinc1'
,
'BDTO'
],
'weights'
:
[
0.034129344
,
'weights'
:
[
0.034129344
,
0.015881607
,
0.015881607
,
0.048765746
,
0.048765746
,
0.042747382
,
0.042747382
,
-
0.015900173
,
-
0.015900173
,
0.019044573
,
0.019044573
,
-
0.001792638
,
-
0.001792638
,
0.014277867
,
0.014277867
,
]
]
},
},
# 'candidate': {
# 'candidate': {
# 'factors': ['RVOL', 'EPS', 'CFinc1', 'BDTO', 'VAL', 'GREV', 'ROEDiluted'],
# 'factors': ['RVOL', 'EPS', 'CFinc1', 'BDTO', 'VAL', 'GREV', 'ROEDiluted'],
...
@@ -35,7 +34,6 @@ strategies = {
...
@@ -35,7 +34,6 @@ strategies = {
# }
# }
}
}
engine
=
SqlEngine
(
'postgresql+psycopg2://postgres:A12345678!@10.63.6.220/alpha'
)
engine
=
SqlEngine
(
'postgresql+psycopg2://postgres:A12345678!@10.63.6.220/alpha'
)
universe
=
Universe
(
'custom'
,
[
'zz500'
])
universe
=
Universe
(
'custom'
,
[
'zz500'
])
benchmark_code
=
905
benchmark_code
=
905
...
@@ -54,8 +52,8 @@ elif freq == '3w':
...
@@ -54,8 +52,8 @@ elif freq == '3w':
elif
freq
==
'1d'
:
elif
freq
==
'1d'
:
horizon
=
0
horizon
=
0
dates
=
makeSchedule
(
'201
7
-01-01'
,
dates
=
makeSchedule
(
'201
2
-01-01'
,
'2017-09-
0
5'
,
'2017-09-
1
5'
,
tenor
=
freq
,
tenor
=
freq
,
calendar
=
'china.sse'
,
calendar
=
'china.sse'
,
dateGenerationRule
=
DateGeneration
.
Forward
)
dateGenerationRule
=
DateGeneration
.
Forward
)
...
@@ -120,7 +118,6 @@ for strategy in strategies:
...
@@ -120,7 +118,6 @@ for strategy in strategies:
total_data_dict
[
strategy
]
=
rets
total_data_dict
[
strategy
]
=
rets
ret_df
=
pd
.
DataFrame
(
total_data_dict
,
index
=
dates
)
ret_df
=
pd
.
DataFrame
(
total_data_dict
,
index
=
dates
)
ret_df
.
loc
[
advanceDateByCalendar
(
'china.sse'
,
dates
[
-
1
],
freq
)]
=
0.
ret_df
.
loc
[
advanceDateByCalendar
(
'china.sse'
,
dates
[
-
1
],
freq
)]
=
0.
ret_df
=
ret_df
.
shift
(
1
)
ret_df
=
ret_df
.
shift
(
1
)
...
...
alphamind/examples/formula_expression.py
View file @
38432d45
...
@@ -17,9 +17,15 @@ import datetime as dt
...
@@ -17,9 +17,15 @@ import datetime as dt
start
=
dt
.
datetime
.
now
()
start
=
dt
.
datetime
.
now
()
formula1
=
CSRank
(
DIFF
(
LOG
(
"turnoverValue"
)))
# formula1 = CSRank(DIFF(LOG("turnoverVol")))
formula2
=
CSRank
((
LAST
(
'closePrice'
)
-
LAST
(
'openPrice'
))
/
LAST
(
'openPrice'
))
# formula2 = CSRank((LAST('highestPrice') - LAST('lowestPrice')) / LAST('lowestPrice'))
expression
=
-
CORR
(
6
,
formula1
^
formula2
)
# expression = -CORR(6, formula1 ^ formula2)
factor1
=
LAST
(
'RVOL'
)
factor2
=
LAST
(
'IVR'
)
expression
=
RES
(
20
,
factor2
^
factor1
)
# expression = MA(1, "EPS")
alpha_factor_name
=
'alpha_factor'
alpha_factor_name
=
'alpha_factor'
alpha_factor
=
{
alpha_factor_name
:
expression
}
alpha_factor
=
{
alpha_factor_name
:
expression
}
...
@@ -30,11 +36,11 @@ engine = SqlEngine('postgresql+psycopg2://postgres:A12345678!@10.63.6.220/alpha'
...
@@ -30,11 +36,11 @@ engine = SqlEngine('postgresql+psycopg2://postgres:A12345678!@10.63.6.220/alpha'
universe
=
Universe
(
'custom'
,
[
'zz500'
])
universe
=
Universe
(
'custom'
,
[
'zz500'
])
benchmark_code
=
905
benchmark_code
=
905
neutralize_risk
=
[
'SIZE'
]
+
industry_styles
neutralize_risk
=
[
'SIZE'
]
+
industry_styles
freq
=
'1
d
'
freq
=
'1
w
'
n_bins
=
5
n_bins
=
5
dates
=
makeSchedule
(
'2012-0
4
-01'
,
dates
=
makeSchedule
(
'2012-0
1
-01'
,
'2017-09-
03
'
,
'2017-09-
18
'
,
tenor
=
freq
,
tenor
=
freq
,
calendar
=
'china.sse'
)
calendar
=
'china.sse'
)
...
@@ -42,7 +48,7 @@ factor_all_data = engine.fetch_data_range(universe,
...
@@ -42,7 +48,7 @@ factor_all_data = engine.fetch_data_range(universe,
alpha_factor
,
alpha_factor
,
dates
=
dates
,
dates
=
dates
,
benchmark
=
905
)[
'factor'
]
benchmark
=
905
)[
'factor'
]
return_all_data
=
engine
.
fetch_dx_return_range
(
universe
,
dates
=
dates
,
horizon
=
0
)
return_all_data
=
engine
.
fetch_dx_return_range
(
universe
,
dates
=
dates
,
horizon
=
4
)
factor_groups
=
factor_all_data
.
groupby
(
'trade_date'
)
factor_groups
=
factor_all_data
.
groupby
(
'trade_date'
)
return_groups
=
return_all_data
.
groupby
(
'trade_date'
)
return_groups
=
return_all_data
.
groupby
(
'trade_date'
)
...
...
alphamind/examples/model_training.py
View file @
38432d45
...
@@ -34,16 +34,16 @@ training - every 4 week
...
@@ -34,16 +34,16 @@ training - every 4 week
engine
=
SqlEngine
(
'postgresql+psycopg2://postgres:A12345678!@10.63.6.220/alpha'
)
engine
=
SqlEngine
(
'postgresql+psycopg2://postgres:A12345678!@10.63.6.220/alpha'
)
universe
=
Universe
(
'zz500'
,
[
'zz500'
])
universe
=
Universe
(
'zz500'
,
[
'zz500'
])
neutralize_risk
=
[
'SIZE'
]
+
industry_styles
neutralize_risk
=
industry_styles
portfolio_risk_neutralize
=
[
'SIZE'
]
portfolio_risk_neutralize
=
[]
portfolio_industry_neutralize
=
True
portfolio_industry_neutralize
=
True
alpha_factors
=
[
'
RVOL'
,
'EPS'
,
'DROEAfterNonRecurring'
,
'DivP
'
,
'CFinc1'
,
'BDTO'
]
# ['RVOL', 'EPS', 'CFinc1', 'BDTO', 'VAL', 'CHV', 'GREV', 'ROEDiluted'] # ['BDTO', 'RVOL', 'CHV', 'VAL', 'CFinc1'] # risk_styles
alpha_factors
=
[
'
VAL'
,
'RVOL'
,
'ROEDiluted'
,
'GREV'
,
'EPS'
,
'CHV
'
,
'CFinc1'
,
'BDTO'
]
# ['RVOL', 'EPS', 'CFinc1', 'BDTO', 'VAL', 'CHV', 'GREV', 'ROEDiluted'] # ['BDTO', 'RVOL', 'CHV', 'VAL', 'CFinc1'] # risk_styles
benchmark
=
905
benchmark
=
905
n_bins
=
5
n_bins
=
5
frequency
=
'2w'
frequency
=
'2w'
batch
=
1
batch
=
4
start_date
=
'2017-01-0
5
'
start_date
=
'2017-01-0
1
'
end_date
=
'2017-09-
05
'
end_date
=
'2017-09-
26
'
method
=
'risk_neutral'
method
=
'risk_neutral'
use_rank
=
100
use_rank
=
100
...
@@ -62,7 +62,7 @@ data_package = fetch_data_package(engine,
...
@@ -62,7 +62,7 @@ data_package = fetch_data_package(engine,
neutralized_risk
=
neutralize_risk
,
neutralized_risk
=
neutralize_risk
,
pre_process
=
[
winsorize_normal
,
standardize
],
pre_process
=
[
winsorize_normal
,
standardize
],
post_process
=
[
winsorize_normal
,
standardize
],
post_process
=
[
winsorize_normal
,
standardize
],
warm_start
=
8
)
warm_start
=
batch
)
'''
'''
training phase: using Linear - regression from scikit-learn
training phase: using Linear - regression from scikit-learn
...
@@ -76,12 +76,22 @@ dates = sorted(train_x.keys())
...
@@ -76,12 +76,22 @@ dates = sorted(train_x.keys())
model_df
=
pd
.
Series
()
model_df
=
pd
.
Series
()
for
train_date
in
dates
:
for
train_date
in
dates
:
#
model = LinearRegression(alpha_factors, fit_intercept=False)
model
=
LinearRegression
(
alpha_factors
,
fit_intercept
=
False
)
#model = LassoCV(fit_intercept=False)
#model = LassoCV(fit_intercept=False)
#model = AdaBoostRegressor(n_estimators=100)
#
model = AdaBoostRegressor(n_estimators=100)
#model = RandomForestRegressor(n_estimators=100, n_jobs=4)
#model = RandomForestRegressor(n_estimators=100, n_jobs=4)
#model = NuSVR(kernel='rbf', C=1e-3, gamma=0.1)
#model = NuSVR(kernel='rbf', C=1e-3, gamma=0.1)
model
=
ConstLinearModel
(
alpha_factors
,
np
.
array
([
0.05
,
0.3
,
0.35
,
0.075
,
0.15
,
0.05
]))
# model = ConstLinearModel(alpha_factors, np.array([0.034129344,
# 0.015881607,
# 0.048765746,
# 0.042747382,
# -0.015900173,
# 0.019044573,
# -0.001792638,
# 0.014277867,
# ]))
# model = ConstLinearModel(alpha_factors, np.array([1.] * len(alpha_factors)))
x
=
train_x
[
train_date
]
x
=
train_x
[
train_date
]
y
=
train_y
[
train_date
]
y
=
train_y
[
train_date
]
...
@@ -165,6 +175,8 @@ for i, predict_date in enumerate(dates):
...
@@ -165,6 +175,8 @@ for i, predict_date in enumerate(dates):
cons
.
set_constraints
(
name
,
benchmark_exp
[
k
],
benchmark_exp
[
k
])
cons
.
set_constraints
(
name
,
benchmark_exp
[
k
],
benchmark_exp
[
k
])
predict_y
=
model
.
predict
(
x
)
predict_y
=
model
.
predict
(
x
)
is_tradable
[:]
=
True
weights
,
analysis
=
er_portfolio_analysis
(
predict_y
,
weights
,
analysis
=
er_portfolio_analysis
(
predict_y
,
industry_names
,
industry_names
,
realized_r
,
realized_r
,
...
...
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