from op_futures.op_objects.plugin_data import PluginData
from op_futures.op_futures_data.op_generate_bar import BarGenerate
from op_futures.op_strategy_plugins.double_ma_strategy import DoubleMaStrategy
from tqz_extern.tqz_constant import BackTesterType
class BackTesterEngine:
def __init__(self, plugins: [PluginData]):
self.plugins: [PluginData] = plugins
def run(self):
"""
start back tester engine.
"""
# check all plugin is run able.
self.__check_plugins()
# do all plugins back testing.
for plugin_data in self.plugins:
plugin_data.strategy.on_init()
if plugin_data.back_tester_type is BackTesterType.BAR_TYPE:
[plugin_data.strategy.on_bar(bar=bar_data) for bar_data in plugin_data.datas]
elif plugin_data.back_tester_type is BackTesterType.TICK_TYPE:
[plugin_data.strategy.on_tick(tick=tick_data) for tick_data in plugin_data.datas]
else:
assert True, f'Bad back_tester_type: {plugin_data.back_tester_type}.'
plugin_data.strategy.on_stop()
# TODO: to be continue. 落所有的order订单数据, 结合当天最后的价格, 核对权益是否计算是否正确...;
# calculate profit and loss.
plugin_data.back_testing()
def __check_plugins(self) -> bool:
assert len(self.plugins) > 0, f'strategy plugins is empty..'
plugin_names = []
for plugin_data in self.plugins:
assert len(plugin_data.datas) > 0, f'plugin {plugin_data.name} history data is empty.'
plugin_names.append(plugin_data.name)
assert len(plugin_names) == len(list(set(plugin_names))), f'plugins: {str(plugin_names)} has same name.'
if __name__ == '__main__':
"""
BackTesterEngine(
plugins=[
PluginData(
name='rb2401_dma',
strategy=DoubleMaStrategy(fast_window=10, slow_window=60),
datas=BarGenerate('../op_machine_learning/rb2401.csv').make_bar_dataframe(interval_secs=60).bars_list_value(),
back_tester_type=BackTesterType.BAR_TYPE
)
]
).run()
"""
BackTesterEngine(
plugins=[
PluginData(
name='rb2401_dma',
strategy=DoubleMaStrategy(fast_window=500, slow_window=3000),
datas=BarGenerate('../op_machine_learning/rb2401.csv').ticks_list_value(),
back_tester_type=BackTesterType.TICK_TYPE
)
]
).run()
09-02