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
570322c4
Commit
570322c4
authored
Sep 25, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added execution pipeline
parent
cae62afe
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
58 additions
and
6 deletions
+58
-6
pipeline.py
alphamind/execution/pipeline.py
+1
-1
targetvolexecutor.py
alphamind/execution/targetvolexecutor.py
+9
-2
test_pipeline.py
alphamind/tests/execution/test_pipeline.py
+45
-0
test_targetvolexecutor.py
alphamind/tests/execution/test_targetvolexecutor.py
+3
-3
No files found.
alphamind/execution/pipeline.py
View file @
570322c4
...
...
@@ -21,7 +21,7 @@ class ExecutionPipeline(object):
turn_over
,
planed_pos
=
0.
,
target_pos
for
executor
in
self
.
executors
:
turn_over
,
planed_pos
=
executor
.
execute
(
target
_pos
)
turn_over
,
planed_pos
=
executor
.
execute
(
planed
_pos
)
executed_pos
=
planed_pos
...
...
alphamind/execution/targetvolexecutor.py
View file @
570322c4
...
...
@@ -8,6 +8,7 @@ Created on 2017-9-22
from
typing
import
Tuple
import
pandas
as
pd
from
PyFin.Math.Accumulators
import
MovingStandardDeviation
from
PyFin.Math.Accumulators
import
MovingAverage
from
alphamind.execution.baseexecutor
import
ExecutorBase
...
...
@@ -16,6 +17,7 @@ class TargetVolExecutor(ExecutorBase):
def
__init__
(
self
,
window
=
30
,
target_vol
=
0.01
):
super
()
.
__init__
()
self
.
m_vol
=
MovingStandardDeviation
(
window
=
window
,
dependency
=
'return'
)
self
.
m_leverage
=
MovingAverage
(
window
=
window
,
dependency
=
'leverage'
)
self
.
target_vol
=
target_vol
self
.
multiplier
=
1.
...
...
@@ -28,11 +30,16 @@ class TargetVolExecutor(ExecutorBase):
return
turn_over
,
target_pos
else
:
c_vol
=
self
.
m_vol
.
result
()
self
.
multiplier
=
c_vol
/
self
.
target_vol
c_leverage
=
self
.
m_leverage
.
result
()
self
.
multiplier
=
self
.
target_vol
/
c_vol
*
c_leverage
candidate_pos
=
target_pos
.
copy
()
candidate_pos
[
'weight'
]
=
candidate_pos
.
weight
.
values
/
self
.
multiplier
candidate_pos
[
'weight'
]
=
candidate_pos
.
weight
.
values
*
self
.
multiplier
turn_over
=
self
.
calc_turn_over
(
candidate_pos
,
self
.
current_pos
)
return
turn_over
,
candidate_pos
def
set_current
(
self
,
current_pos
:
pd
.
DataFrame
):
super
()
.
set_current
(
current_pos
)
self
.
m_leverage
.
push
({
'leverage'
:
current_pos
.
weight
.
abs
()
.
sum
()})
def
update
(
self
,
data_dict
:
dict
):
self
.
m_vol
.
push
(
data_dict
)
alphamind/tests/execution/test_pipeline.py
0 → 100644
View file @
570322c4
# -*- coding: utf-8 -*-
"""
Created on 2017-9-25
@author: cheng.li
"""
import
unittest
from
collections
import
deque
import
numpy
as
np
import
pandas
as
pd
from
alphamind.execution.pipeline
import
ExecutionPipeline
from
alphamind.execution.thresholdexecutor
import
ThresholdExecutor
from
alphamind.execution.targetvolexecutor
import
TargetVolExecutor
class
TestExecutionPipelin
(
unittest
.
TestCase
):
def
test_execution_pipeline
(
self
):
n
=
100
window
=
60
target_vol
=
0.01
turn_over_threshold
=
0.5
executor1
=
TargetVolExecutor
(
window
=
window
,
target_vol
=
target_vol
)
executor2
=
ThresholdExecutor
(
turn_over_threshold
=
turn_over_threshold
)
execution_pipeline
=
ExecutionPipeline
(
executors
=
[
executor1
,
executor2
])
return_1
=
np
.
random
.
randn
(
2000
,
n
)
*
0.05
return_2
=
np
.
random
.
randn
(
2000
,
n
)
*
0.2
return_total
=
np
.
concatenate
((
return_1
,
return_2
))
codes
=
np
.
array
(
list
(
range
(
n
)))
ret_deq
=
deque
(
maxlen
=
window
)
for
i
,
row
in
enumerate
(
return_total
):
weights
=
np
.
random
.
randint
(
0
,
100
,
n
)
weights
=
weights
/
weights
.
sum
()
pos
=
pd
.
DataFrame
({
'code'
:
codes
,
'weight'
:
weights
})
turn_over
,
executed_pos
=
execution_pipeline
.
execute
(
pos
)
daily_return
=
row
@
executed_pos
.
weight
.
values
.
flatten
()
data_dict
=
{
'return'
:
daily_return
}
execution_pipeline
.
update
(
data_dict
=
data_dict
)
ret_deq
.
append
(
daily_return
)
alphamind/tests/execution/test_targetvolexecutor.py
View file @
570322c4
...
...
@@ -25,13 +25,13 @@ class TestTargetVolExecutor(unittest.TestCase):
return_2
=
np
.
random
.
randn
(
2000
,
n
)
*
0.2
return_total
=
np
.
concatenate
((
return_1
,
return_2
))
weig
th
s
=
np
.
ones
(
n
)
/
n
weig
ht
s
=
np
.
ones
(
n
)
/
n
codes
=
np
.
array
(
list
(
range
(
n
)))
ret_deq
=
deque
(
maxlen
=
window
)
for
i
,
row
in
enumerate
(
return_total
):
pos
=
pd
.
DataFrame
({
'code'
:
codes
,
'weight'
:
weig
th
s
})
pos
=
pd
.
DataFrame
({
'code'
:
codes
,
'weight'
:
weig
ht
s
})
turn_over
,
executed_pos
=
executor
.
execute
(
pos
)
if
i
>=
window
:
...
...
@@ -41,7 +41,7 @@ class TestTargetVolExecutor(unittest.TestCase):
executed_pos
.
equals
(
pos
)
executor
.
set_current
(
executed_pos
)
daily_return
=
row
@
weig
th
s
daily_return
=
row
@
weig
ht
s
data_dict
=
{
'return'
:
daily_return
}
executor
.
update
(
data_dict
=
data_dict
)
ret_deq
.
append
(
daily_return
)
...
...
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