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
7dd0bb99
Commit
7dd0bb99
authored
Sep 25, 2017
by
Dr.李
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added tests for target vol executor
parent
0591851d
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
49 additions
and
6 deletions
+49
-6
baseexecutor.py
alphamind/execution/baseexecutor.py
+3
-0
naiveexecutor.py
alphamind/execution/naiveexecutor.py
+1
-1
targetvolexecutor.py
alphamind/execution/targetvolexecutor.py
+8
-2
thresholdexecutor.py
alphamind/execution/thresholdexecutor.py
+1
-1
test_targetvolexecutor.py
alphamind/tests/execution/test_targetvolexecutor.py
+32
-0
test_suite.py
alphamind/tests/test_suite.py
+3
-1
create_view.sql
scripts/create_view.sql
+1
-1
No files found.
alphamind/execution/baseexecutor.py
View file @
7dd0bb99
...
...
@@ -30,3 +30,6 @@ class ExecutorBase(metaclass=abc.ABCMeta):
def
set_current
(
self
,
current_pos
:
pd
.
DataFrame
):
self
.
current_pos
=
current_pos
.
copy
()
def
update
(
self
,
data_dict
:
dict
):
pass
alphamind/execution/naiveexecutor.py
View file @
7dd0bb99
...
...
@@ -17,7 +17,7 @@ class NaiveExecutor(ExecutorBase):
def
execute
(
self
,
target_pos
:
pd
.
DataFrame
)
->
Tuple
[
float
,
pd
.
DataFrame
]:
if
self
.
current_pos
.
empty
:
turn_over
=
target_pos
.
weight
.
sum
()
turn_over
=
target_pos
.
weight
.
abs
()
.
sum
()
else
:
turn_over
=
self
.
calc_turn_over
(
target_pos
,
self
.
current_pos
)
self
.
current_pos
=
target_pos
.
copy
()
...
...
alphamind/execution/targetvolexecutor.py
View file @
7dd0bb99
...
...
@@ -21,12 +21,18 @@ class TargetVolExecutor(ExecutorBase):
def
execute
(
self
,
target_pos
:
pd
.
DataFrame
)
->
Tuple
[
float
,
pd
.
DataFrame
]:
if
not
self
.
m_vol
.
isFull
():
turn_over
=
self
.
calc_turn_over
(
target_pos
,
self
.
current_pos
)
if
self
.
current_pos
.
empty
:
turn_over
=
target_pos
.
abs
()
.
weight
.
sum
()
else
:
turn_over
=
self
.
calc_turn_over
(
target_pos
,
self
.
current_pos
)
return
turn_over
,
target_pos
else
:
c_vol
=
self
.
m_vol
.
result
()
self
.
multiplier
=
c_vol
/
self
.
target_vol
candidate_pos
=
target_pos
.
copy
()
candidate_pos
[
'weight'
]
=
candidate_pos
.
weight
*
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
update
(
self
,
data_dict
:
dict
):
self
.
m_vol
.
push
(
data_dict
)
alphamind/execution/thresholdexecutor.py
View file @
7dd0bb99
...
...
@@ -19,7 +19,7 @@ class ThresholdExecutor(ExecutorBase):
def
execute
(
self
,
target_pos
:
pd
.
DataFrame
)
->
Tuple
[
float
,
pd
.
DataFrame
]:
if
self
.
current_pos
.
empty
:
return
target_pos
.
weight
.
sum
(),
target_pos
return
target_pos
.
weight
.
abs
()
.
sum
(),
target_pos
else
:
turn_over
=
self
.
calc_turn_over
(
target_pos
,
self
.
current_pos
)
...
...
alphamind/tests/execution/test_targetvolexecutor.py
View file @
7dd0bb99
...
...
@@ -6,13 +6,45 @@ Created on 2017-9-22
"""
import
unittest
from
collections
import
deque
import
numpy
as
np
import
pandas
as
pd
from
alphamind.execution.targetvolexecutor
import
TargetVolExecutor
class
TestTargetVolExecutor
(
unittest
.
TestCase
):
def
test_target_vol_executor
(
self
):
n
=
100
window
=
30
target_vol
=
0.01
executor
=
TargetVolExecutor
(
window
=
window
,
target_vol
=
target_vol
)
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
))
weigths
=
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'
:
weigths
})
turn_over
,
executed_pos
=
executor
.
execute
(
pos
)
if
i
>=
window
:
c_vol
=
np
.
std
(
ret_deq
,
ddof
=
1
)
executed_pos
.
equals
(
pos
*
target_vol
/
c_vol
)
else
:
executed_pos
.
equals
(
pos
)
executor
.
set_current
(
executed_pos
)
daily_return
=
row
@
weigths
data_dict
=
{
'return'
:
daily_return
}
executor
.
update
(
data_dict
=
data_dict
)
ret_deq
.
append
(
daily_return
)
if
__name__
==
'__main__'
:
...
...
alphamind/tests/test_suite.py
View file @
7dd0bb99
...
...
@@ -30,6 +30,7 @@ from alphamind.tests.model.test_linearmodel import TestLinearModel
from
alphamind.tests.model.test_loader
import
TestLoader
from
alphamind.tests.execution.test_naiveexecutor
import
TestNaiveExecutor
from
alphamind.tests.execution.test_thresholdexecutor
import
TestThresholdExecutor
from
alphamind.tests.execution.test_targetvolexecutor
import
TestTargetVolExecutor
if
__name__
==
'__main__'
:
...
...
@@ -51,6 +52,7 @@ if __name__ == '__main__':
TestLinearModel
,
TestLoader
,
TestNaiveExecutor
,
TestThresholdExecutor
],
TestThresholdExecutor
,
TestTargetVolExecutor
],
alpha_logger
)
runner
.
run
()
scripts/create_view.sql
View file @
7dd0bb99
...
...
@@ -42,4 +42,4 @@ u."AccountsPayablesTDays",u."AccountsPayablesTRate",u."AdminiExpenseRate",u."ART
left
join
tiny
as
t
on
m
.
trade_date
=
t
.
trade_date
and
m
.
code
=
t
.
code
left
join
experimental
as
e
on
m
.
trade_date
=
e
.
trade_date
and
m
.
code
=
e
.
code
;
create
UNIQUE
index
on
full_factor_view
(
trade_date
,
code
)
\ No newline at end of file
create
UNIQUE
index
on
full_factor_view
(
trade_date
,
code
);
\ 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