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
b3973859
Unverified
Commit
b3973859
authored
May 23, 2018
by
iLampard
Committed by
GitHub
May 23, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #12 from alpha-miner/master
merge update
parents
f4ddddae
5b940f54
Changes
12
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
610 additions
and
403 deletions
+610
-403
data_preparing.py
alphamind/model/data_preparing.py
+2
-3
strategy.py
alphamind/strategy/strategy.py
+3
-3
Example 1 - Factor IC analysis.ipynb
notebooks/Example 1 - Factor IC analysis.ipynb
+495
-16
Example 10 - Quadratic Optimizer Comparison with CVXOPT.ipynb
...ple 10 - Quadratic Optimizer Comparison with CVXOPT.ipynb
+1
-1
Example 11 - Long Short Strategy Model.ipynb
notebooks/Example 11 - Long Short Strategy Model.ipynb
+2
-2
Example 12 - Machine Learning Model Prediction.ipynb
...ooks/Example 12 - Machine Learning Model Prediction.ipynb
+30
-324
Example 13 - Evaluation within Industry Groups.ipynb
...ooks/Example 13 - Evaluation within Industry Groups.ipynb
+3
-3
Example 2 - Strategy Analysis.ipynb
notebooks/Example 2 - Strategy Analysis.ipynb
+11
-8
Example 3 - Multi Weight Gap Comparison.ipynb
notebooks/Example 3 - Multi Weight Gap Comparison.ipynb
+51
-33
Example 6 - Target Volatility Builder.ipynb
notebooks/Example 6 - Target Volatility Builder.ipynb
+10
-8
Example 7 - Portfolio Optimizer Performance.ipynb
notebooks/Example 7 - Portfolio Optimizer Performance.ipynb
+1
-1
Example 8 - Neutralize Algorithm Comparison.ipynb
notebooks/Example 8 - Neutralize Algorithm Comparison.ipynb
+1
-1
No files found.
alphamind/model/data_preparing.py
View file @
b3973859
...
@@ -108,7 +108,7 @@ def prepare_data(engine: SqlEngine,
...
@@ -108,7 +108,7 @@ def prepare_data(engine: SqlEngine,
df
.
dropna
(
inplace
=
True
)
df
.
dropna
(
inplace
=
True
)
return
dates
,
df
[[
'trade_date'
,
'code'
,
'dx'
]],
df
[
return
dates
,
df
[[
'trade_date'
,
'code'
,
'dx'
]],
df
[
[
'trade_date'
,
'code'
,
'weight'
,
'i
sOpen'
,
'i
ndustry_code'
,
'industry'
]
+
transformer
.
names
]
[
'trade_date'
,
'code'
,
'weight'
,
'industry_code'
,
'industry'
]
+
transformer
.
names
]
def
batch_processing
(
names
,
def
batch_processing
(
names
,
...
@@ -231,7 +231,6 @@ def fetch_data_package(engine: SqlEngine,
...
@@ -231,7 +231,6 @@ def fetch_data_package(engine: SqlEngine,
target_df
[
'weight'
]
=
train_x
[
'weight'
]
target_df
[
'weight'
]
=
train_x
[
'weight'
]
target_df
[
'industry'
]
=
train_x
[
'industry'
]
target_df
[
'industry'
]
=
train_x
[
'industry'
]
target_df
[
'industry_code'
]
=
train_x
[
'industry_code'
]
target_df
[
'industry_code'
]
=
train_x
[
'industry_code'
]
target_df
[
'isOpen'
]
=
train_x
[
'isOpen'
]
if
neutralized_risk
:
if
neutralized_risk
:
for
i
,
name
in
enumerate
(
neutralized_risk
):
for
i
,
name
in
enumerate
(
neutralized_risk
):
...
@@ -314,7 +313,7 @@ def fetch_train_phase(engine,
...
@@ -314,7 +313,7 @@ def fetch_train_phase(engine,
df
=
pd
.
merge
(
factor_df
,
target_df
,
on
=
[
'trade_date'
,
'code'
])
.
dropna
()
df
=
pd
.
merge
(
factor_df
,
target_df
,
on
=
[
'trade_date'
,
'code'
])
.
dropna
()
target_df
,
factor_df
=
df
[[
'trade_date'
,
'code'
,
'dx'
]],
df
[
target_df
,
factor_df
=
df
[[
'trade_date'
,
'code'
,
'dx'
]],
df
[
[
'trade_date'
,
'code'
,
'isOpen'
]
+
transformer
.
names
]
[
'trade_date'
,
'code'
]
+
transformer
.
names
]
target_df
,
dates
,
date_label
,
risk_exp
,
x_values
,
y_values
,
_
,
_
,
codes
=
\
target_df
,
dates
,
date_label
,
risk_exp
,
x_values
,
y_values
,
_
,
_
,
codes
=
\
_merge_df
(
engine
,
transformer
.
names
,
factor_df
,
target_df
,
universe
,
dates
,
risk_model
,
neutralized_risk
)
_merge_df
(
engine
,
transformer
.
names
,
factor_df
,
target_df
,
universe
,
dates
,
risk_model
,
neutralized_risk
)
...
...
alphamind/strategy/strategy.py
View file @
b3973859
...
@@ -166,7 +166,7 @@ class Strategy(object):
...
@@ -166,7 +166,7 @@ class Strategy(object):
if
self
.
dask_client
is
None
:
if
self
.
dask_client
is
None
:
models
=
{}
models
=
{}
for
ref_date
,
_
in
self
.
total_data_groups
:
for
ref_date
,
_
in
total_data_groups
:
models
[
ref_date
]
=
train_model
(
ref_date
.
strftime
(
'
%
Y-
%
m-
%
d'
),
self
.
alpha_model
,
self
.
data_meta
)
models
[
ref_date
]
=
train_model
(
ref_date
.
strftime
(
'
%
Y-
%
m-
%
d'
),
self
.
alpha_model
,
self
.
data_meta
)
else
:
else
:
def
worker
(
parameters
):
def
worker
(
parameters
):
...
@@ -334,8 +334,8 @@ if __name__ == '__main__':
...
@@ -334,8 +334,8 @@ if __name__ == '__main__':
universe
=
universe
,
universe
=
universe
,
batch
=
1
,
batch
=
1
,
neutralized_risk
=
neutralized_risk
,
neutralized_risk
=
neutralized_risk
,
pre_process
=
None
,
# [winsorize_normal, standardize],
pre_process
=
None
,
post_process
=
None
,
# [standardize],
post_process
=
None
,
warm_start
=
1
)
warm_start
=
1
)
industries
=
industry_list
(
'sw_adj'
,
1
)
industries
=
industry_list
(
'sw_adj'
,
1
)
...
...
notebooks/Example 1 - Factor IC analysis.ipynb
View file @
b3973859
This diff is collapsed.
Click to expand it.
notebooks/Example 10 - Quadratic Optimizer Comparison with CVXOPT.ipynb
View file @
b3973859
...
@@ -40,7 +40,7 @@
...
@@ -40,7 +40,7 @@
"ref_date = '2018-02-08'\n",
"ref_date = '2018-02-08'\n",
"\n",
"\n",
"engine = SqlEngine(os.environ['DB_URI'])\n",
"engine = SqlEngine(os.environ['DB_URI'])\n",
"universe = Universe('
custom', ['ashare_ex']
)\n",
"universe = Universe('
ashare_ex'
)\n",
"codes = engine.fetch_codes(ref_date, universe)\n",
"codes = engine.fetch_codes(ref_date, universe)\n",
"\n",
"\n",
"risk_cov, risk_exposure = engine.fetch_risk_model(ref_date, codes)\n",
"risk_cov, risk_exposure = engine.fetch_risk_model(ref_date, codes)\n",
...
...
notebooks/Example 11 - Long Short Strategy Model.ipynb
View file @
b3973859
...
@@ -38,7 +38,7 @@
...
@@ -38,7 +38,7 @@
"freq = '5b'\n",
"freq = '5b'\n",
"horizon = map_freq(freq)\n",
"horizon = map_freq(freq)\n",
"neutralized_risk = risk_styles + industry_styles\n",
"neutralized_risk = risk_styles + industry_styles\n",
"universe = Universe(
\"custom\", ['zz500']
)\n",
"universe = Universe(
'zz500'
)\n",
"data_source = os.environ['DB_URI']\n",
"data_source = os.environ['DB_URI']\n",
"offset = 1\n",
"offset = 1\n",
"method = 'ls'\n",
"method = 'ls'\n",
...
@@ -185,7 +185,7 @@
...
@@ -185,7 +185,7 @@
"name": "python",
"name": "python",
"nbconvert_exporter": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"pygments_lexer": "ipython3",
"version": "3.6.
3
"
"version": "3.6.
5
"
},
},
"varInspector": {
"varInspector": {
"cols": {
"cols": {
...
...
notebooks/Example 12 - Machine Learning Model Prediction.ipynb
View file @
b3973859
This diff is collapsed.
Click to expand it.
notebooks/Example 13 - Evaluation within Industry Groups.ipynb
View file @
b3973859
...
@@ -30,7 +30,7 @@
...
@@ -30,7 +30,7 @@
"from alphamind.api import *\n",
"from alphamind.api import *\n",
"\n",
"\n",
"factor = 'CFO2EV'\n",
"factor = 'CFO2EV'\n",
"universe = Universe('
custom', ['zz800']
)\n",
"universe = Universe('
zz800'
)\n",
"start_date = '2010-01-01'\n",
"start_date = '2010-01-01'\n",
"end_date = '2018-04-26'\n",
"end_date = '2018-04-26'\n",
"freq = '20b'\n",
"freq = '20b'\n",
...
@@ -185,7 +185,7 @@
...
@@ -185,7 +185,7 @@
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"sample_factor3 = sample_factor3[['code', '
isOpen', '
f3', 'industry']]\n",
"sample_factor3 = sample_factor3[['code', 'f3', 'industry']]\n",
"sample_factor3.sort_values('f3', ascending=False).head(15)"
"sample_factor3.sort_values('f3', ascending=False).head(15)"
]
]
},
},
...
@@ -306,7 +306,7 @@
...
@@ -306,7 +306,7 @@
"name": "python",
"name": "python",
"nbconvert_exporter": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"pygments_lexer": "ipython3",
"version": "3.6.
3
"
"version": "3.6.
5
"
},
},
"varInspector": {
"varInspector": {
"cols": {
"cols": {
...
...
notebooks/Example 2 - Strategy Analysis.ipynb
View file @
b3973859
...
@@ -116,12 +116,7 @@
...
@@ -116,12 +116,7 @@
"outputs": [],
"outputs": [],
"source": [
"source": [
"# Running settings\n",
"# Running settings\n",
"running_setting = RunningSetting(universe,\n",
"running_setting = RunningSetting(weights_bandwidth=weights_bandwidth,\n",
" start_date,\n",
" end_date,\n",
" freq,\n",
" benchmark=benchmark_code,\n",
" weights_bandwidth=weights_bandwidth,\n",
" rebalance_method=method,\n",
" rebalance_method=method,\n",
" bounds=bounds,\n",
" bounds=bounds,\n",
" turn_over_target=turn_over_target)"
" turn_over_target=turn_over_target)"
...
@@ -134,8 +129,16 @@
...
@@ -134,8 +129,16 @@
"outputs": [],
"outputs": [],
"source": [
"source": [
"# Strategy\n",
"# Strategy\n",
"strategy = Strategy(alpha_model, data_meta, running_setting)\n",
"strategy = Strategy(alpha_model,\n",
"ret_df, positions = strategy.run()"
" data_meta,\n",
" universe=universe,\n",
" start_date=start_date,\n",
" end_date=end_date,\n",
" freq=freq,\n",
" benchmark=benchmark_code)\n",
"\n",
"strategy.prepare_backtest_data()\n",
"ret_df, positions = strategy.run(running_setting=running_setting)"
]
]
},
},
{
{
...
...
notebooks/Example 3 - Multi Weight Gap Comparison.ipynb
View file @
b3973859
...
@@ -42,32 +42,42 @@
...
@@ -42,32 +42,42 @@
"benchmark_code = 905\n",
"benchmark_code = 905\n",
"universe = Universe('zz800') + Universe('cyb')\n",
"universe = Universe('zz800') + Universe('cyb')\n",
"\n",
"\n",
"def create_scenario(weights_bandwidth=0.02, target_vol=0.01, method='risk_neutral'):\n",
"start_date = '2017-01-01'\n",
" start_date = '2017-01-01'\n",
"end_date = '2018-04-27'\n",
" end_date = '2018-04-27'\n",
"freq = '10b'\n",
" freq = '10b'\n",
"neutralized_risk = None\n",
" neutralized_risk = None\n",
"\n",
"\n",
"alpha_factors = {\n",
" alpha_factors = {\n",
" 'f01': CSQuantiles(LAST('EPS'), groups='sw1'),\n",
" 'f01': CSQuantiles(LAST('EPS'), groups='sw1'),\n",
" 'f02': CSQuantiles(LAST('ROE'), groups='sw1'),\n",
" 'f02': CSQuantiles(LAST('ROE'), groups='sw1'),\n",
"}\n",
" }\n",
"\n",
"\n",
"weights = dict(\n",
" weights = dict(\n",
" f01=1.,\n",
" f01=1.,\n",
" f02=1.\n",
" f02=1.\n",
" )\n",
" )\n",
"\n",
"\n",
"alpha_model = ConstLinearModel(features=alpha_factors, weights=weights)\n",
" alpha_model = ConstLinearModel(features=alpha_factors, weights=weights)\n",
"\n",
"\n",
"data_meta = DataMeta(freq=freq,\n",
" data_meta = DataMeta(freq=freq,\n",
" universe=universe,\n",
" universe=universe,\n",
" batch=1,\n",
" batch=1,\n",
" neutralized_risk=None,\n",
" neutralized_risk=None,\n",
" pre_process=None,\n",
" pre_process=None,\n",
" post_process=None,\n",
" post_process=None,\n",
" data_source=os.environ['DB_URI'])\n",
" data_source=os.environ['DB_URI'])\n",
"\n",
"strategy = Strategy(alpha_model,\n",
" data_meta,\n",
" universe=universe,\n",
" start_date=start_date,\n",
" end_date=end_date,\n",
" freq=freq,\n",
" benchmark=benchmark_code)\n",
"\n",
"strategy.prepare_backtest_data()\n",
"\n",
"\n",
"def create_scenario(weights_bandwidth=0.02, target_vol=0.01, method='risk_neutral'):\n",
" industry_names = industry_list('sw', 1)\n",
" industry_names = industry_list('sw', 1)\n",
" constraint_risk = ['SIZE', 'SIZENL', 'BETA']\n",
" constraint_risk = ['SIZE', 'SIZENL', 'BETA']\n",
" total_risk_names = constraint_risk + ['benchmark', 'total']\n",
" total_risk_names = constraint_risk + ['benchmark', 'total']\n",
...
@@ -93,19 +103,13 @@
...
@@ -93,19 +103,13 @@
" u_val.append(0.0)\n",
" u_val.append(0.0)\n",
"\n",
"\n",
" bounds = create_box_bounds(total_risk_names, b_type, l_val, u_val)\n",
" bounds = create_box_bounds(total_risk_names, b_type, l_val, u_val)\n",
" running_setting = RunningSetting(universe,\n",
" running_setting = RunningSetting(weights_bandwidth=weights_bandwidth,\n",
" start_date,\n",
" end_date,\n",
" freq,\n",
" benchmark=benchmark_code,\n",
" weights_bandwidth=weights_bandwidth,\n",
" rebalance_method=method,\n",
" rebalance_method=method,\n",
" bounds=bounds,\n",
" bounds=bounds,\n",
" target_vol=target_vol,\n",
" target_vol=target_vol,\n",
" turn_over_target=0.4)\n",
" turn_over_target=0.4)\n",
"\n",
"\n",
" strategy = Strategy(alpha_model, data_meta, running_setting)\n",
" ret_df, positions = strategy.run(running_setting)\n",
" ret_df, positions = strategy.run()\n",
" return ret_df"
" return ret_df"
]
]
},
},
...
@@ -150,6 +154,7 @@
...
@@ -150,6 +154,7 @@
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"%%time\n",
"weight_gaps = [0.005, 0.010, 0.015, 0.020]\n",
"weight_gaps = [0.005, 0.010, 0.015, 0.020]\n",
"\n",
"\n",
"with pd.ExcelWriter(f'zz800_cyb_{benchmark_code}_gap.xlsx', engine='xlsxwriter') as writer:\n",
"with pd.ExcelWriter(f'zz800_cyb_{benchmark_code}_gap.xlsx', engine='xlsxwriter') as writer:\n",
...
@@ -166,6 +171,7 @@
...
@@ -166,6 +171,7 @@
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
"%%time\n",
"target_vols = [0.015, 0.030, 0.045, 0.060]\n",
"target_vols = [0.015, 0.030, 0.045, 0.060]\n",
"\n",
"\n",
"with pd.ExcelWriter(f'zz800_cyb_{benchmark_code}_tv.xlsx', engine='xlsxwriter') as writer:\n",
"with pd.ExcelWriter(f'zz800_cyb_{benchmark_code}_tv.xlsx', engine='xlsxwriter') as writer:\n",
...
@@ -190,6 +196,18 @@
...
@@ -190,6 +196,18 @@
"language": "python",
"language": "python",
"name": "python3"
"name": "python3"
},
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
},
"varInspector": {
"varInspector": {
"cols": {
"cols": {
"lenName": 16,
"lenName": 16,
...
...
notebooks/Example 6 - Target Volatility Builder.ipynb
View file @
b3973859
...
@@ -224,12 +224,7 @@
...
@@ -224,12 +224,7 @@
"\"\"\"\n",
"\"\"\"\n",
"Running Settings\n",
"Running Settings\n",
"\"\"\"\n",
"\"\"\"\n",
"running_setting = RunningSetting(universe,\n",
"running_setting = RunningSetting(weights_bandwidth=weights_bandwidth,\n",
" start_date,\n",
" end_date,\n",
" freq,\n",
" benchmark=benchmark_code,\n",
" weights_bandwidth=weights_bandwidth,\n",
" rebalance_method='tv',\n",
" rebalance_method='tv',\n",
" bounds=bounds,\n",
" bounds=bounds,\n",
" target_vol=target_vol)"
" target_vol=target_vol)"
...
@@ -244,8 +239,15 @@
...
@@ -244,8 +239,15 @@
"\"\"\"\n",
"\"\"\"\n",
"Strategy run\n",
"Strategy run\n",
"\"\"\"\n",
"\"\"\"\n",
"strategy = Strategy(alpha_model, data_meta, running_setting)\n",
"strategy = Strategy(alpha_model,\n",
"ret_df, positions = strategy.run()"
" data_meta,\n",
" universe=universe,\n",
" start_date=start_date,\n",
" end_date=end_date,\n",
" freq=freq,\n",
" benchmark=benchmark_code)\n",
"strategy.prepare_backtest_data()\n",
"ret_df, positions = strategy.run(running_setting)"
]
]
},
},
{
{
...
...
notebooks/Example 7 - Portfolio Optimizer Performance.ipynb
View file @
b3973859
...
@@ -63,7 +63,7 @@
...
@@ -63,7 +63,7 @@
"data_source = os.environ['DB_URI']\n",
"data_source = os.environ['DB_URI']\n",
"engine = SqlEngine(data_source)\n",
"engine = SqlEngine(data_source)\n",
"\n",
"\n",
"universes = [Universe(
'custom', [u_name]
) for u_name in u_names]\n",
"universes = [Universe(
u_name
) for u_name in u_names]\n",
"codes_set = [engine.fetch_codes(ref_date, universe=universe) for universe in universes]\n",
"codes_set = [engine.fetch_codes(ref_date, universe=universe) for universe in universes]\n",
"data_set = [engine.fetch_data(ref_date, factor, codes, benchmark=b_code, risk_model=risk_model) for codes, b_code in zip(codes_set, b_codes)]"
"data_set = [engine.fetch_data(ref_date, factor, codes, benchmark=b_code, risk_model=risk_model) for codes, b_code in zip(codes_set, b_codes)]"
]
]
...
...
notebooks/Example 8 - Neutralize Algorithm Comparison.ipynb
View file @
b3973859
...
@@ -46,7 +46,7 @@
...
@@ -46,7 +46,7 @@
"factor = 'EPS'\n",
"factor = 'EPS'\n",
"\n",
"\n",
"engine = SqlEngine(os.environ['DB_URI'])\n",
"engine = SqlEngine(os.environ['DB_URI'])\n",
"universe = Universe('
custom', ['zz800']
)"
"universe = Universe('
zz800'
)"
]
]
},
},
{
{
...
...
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