Commit 4182e1a8 authored by Dr.李's avatar Dr.李

added execution module

parent 9f2cbc30
# -*- coding: utf-8 -*-
"""
Created on 2017-9-22
@author: cheng.li
"""
import abc
import pandas as pd
class ExecutorBase(metaclass=abc.ABCMeta):
def __init__(self):
pass
@abc.abstractmethod
def execute(self, target_pos: pd.DataFrame) -> pd.DataFrame:
pass
\ No newline at end of file
# -*- coding: utf-8 -*-
"""
Created on 2017-9-22
@author: cheng.li
"""
import pandas as pd
from alphamind.execution.baseexecutor import ExecutorBase
class NativeExecutor(ExecutorBase):
def __init__(self):
super().__init__()
def execute(self, target_pos: pd.DataFrame) -> pd.DataFrame:
return target_pos.copy()
# -*- coding: utf-8 -*-
"""
Created on 2017-9-22
@author: cheng.li
"""
import numpy as np
import pandas as pd
from alphamind.execution.baseexecutor import ExecutorBase
class ThresholdExecutor(ExecutorBase):
def __init__(self, turn_over_threshold: float):
super().__init__()
self.threshold = turn_over_threshold
self.current_pos = pd.DataFrame()
def execute(self, target_pos: pd.DataFrame) -> pd.DataFrame:
if self.current_pos.empty:
self.current_pos = target_pos.copy()
return target_pos.copy()
else:
pos_merged = pd.merge(target_pos, self.current_pos, on=['code'], how='outer')
pos_merged.fillna(0, inplace=True)
pos_merged['industry'] = pos_merged['industry_x'].where(pos_merged['industry_x'] != 0, pos_merged['industry_y'])
turn_over = np.abs(pos_merged.weight_x - pos_merged.weight_y).sum()
if turn_over >= self.threshold:
self.current_pos = target_pos.copy()
return self.current_pos.copy()
else:
return self.current_pos.copy()
# -*- coding: utf-8 -*-
"""
Created on 2017-9-22
@author: cheng.li
"""
import unittest
import pandas as pd
from alphamind.execution.thresholdexecutor import ThresholdExecutor
class TestThresholdExecutor(unittest.TestCase):
def test_threshold_executor(self):
target_pos = pd.DataFrame({'code': [1, 2, 3],
'weight': [0.2, 0.3, 0.5],
'industry': ['a', 'b', 'c']})
executor = ThresholdExecutor(turn_over_threshold=0.5)
# 1st round
executed_pos = executor.execute(target_pos)
self.assertTrue(target_pos.equals(executed_pos))
# 2nd round
target_pos = pd.DataFrame({'code': [1, 2, 4],
'weight': [0.3, 0.2, 0.5],
'industry': ['a', 'b', 'd']})
executed_pos = executor.execute(target_pos)
self.assertTrue(target_pos.equals(executed_pos))
self.assertTrue(executed_pos.equals(executor.current_pos))
# 3nd round
target_pos = pd.DataFrame({'code': [1, 3, 4],
'weight': [0.3, 0.2, 0.5],
'industry': ['a', 'c', 'd']})
executed_pos2 = executor.execute(target_pos)
self.assertTrue(executed_pos.equals(executed_pos2))
if __name__ == '__main__':
unittest.main()
...@@ -28,6 +28,7 @@ from alphamind.tests.analysis.test_factoranalysis import TestFactorAnalysis ...@@ -28,6 +28,7 @@ from alphamind.tests.analysis.test_factoranalysis import TestFactorAnalysis
from alphamind.tests.analysis.test_quantilieanalysis import TestQuantileAnalysis from alphamind.tests.analysis.test_quantilieanalysis import TestQuantileAnalysis
from alphamind.tests.model.test_linearmodel import TestLinearModel from alphamind.tests.model.test_linearmodel import TestLinearModel
from alphamind.tests.model.test_loader import TestLoader from alphamind.tests.model.test_loader import TestLoader
from alphamind.tests.execution.test_thresholdexecutor import TestThresholdExecutor
if __name__ == '__main__': if __name__ == '__main__':
...@@ -47,6 +48,7 @@ if __name__ == '__main__': ...@@ -47,6 +48,7 @@ if __name__ == '__main__':
TestFactorAnalysis, TestFactorAnalysis,
TestQuantileAnalysis, TestQuantileAnalysis,
TestLinearModel, TestLinearModel,
TestLoader], TestLoader,
TestThresholdExecutor],
alpha_logger) alpha_logger)
runner.run() runner.run()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment