def test_save_strategy_run_and_find_it( mysql_connection: MySQLdb.Connection) -> None: storage = StrategyRunStorage(mysql_connection) strategy_run_to_save = StrategyRun( UUID('637f46a2-d008-48ba-9899-322abb56b425'), DUMMY_DATE, Pair('USD', 'BTC'), [StrategyRunMarket('yolo_market', 'yolo_plugin_name', {'foo': 'BAR'})], 'strategy_dummy_name', {'gandalf': 'Gandalf the Gray'}, DateTimeInterval(DUMMY_DATE, None), 'candle_dummy_storage', 'order_dummy_storage') storage.insert(strategy_run_to_save) strategy_runs = storage.find_by() assert len(strategy_runs) == 1 assert str(strategy_runs[0].strategy_run_id) == str( strategy_run_to_save.strategy_run_id)
def run_strategy( ctx: Context, strategy_name: str, pair: Tuple[str, str], market_names: Tuple[str], configuration_file: Union[str, None], candle_storage: str, order_storage: str, market_plugin: str ) -> None: pair_obj = Pair(pair[0], pair[1]) strategy_configuration: Dict = {} if configuration_file is not None: strategy_configuration = load_configuration_from_file(configuration_file) strategy_run_at = di_container.datetime_factory.now() strategy_run = StrategyRun( uuid.uuid4(), strategy_run_at, pair_obj, [StrategyRunMarket(market_plugin, market_name, {}) for market_name in market_names], strategy_name, strategy_configuration, DateTimeInterval(strategy_run_at, None), candle_storage, order_storage ) di_container.strategy_run_storage.insert(strategy_run) di_container.event_emitter.emit_new_strategy_run(strategy_run) try: di_container.strategy_standard_runner.run(strategy_run) except StrategyNotProvidedByAnyPluginException as e: _terminate_strategy_run(strategy_run) print_error_and_terminate(str(e)) except ForEndUserException as e: _terminate_strategy_run(strategy_run) print_error_and_terminate(str(e)) except KeyboardInterrupt: _terminate_strategy_run(strategy_run) pass
def process_reply_strategy(self, data: Dict) -> None: logger.info("[Rabbit] Processing task: %s | %r", TASK_REPLY_STRATEGY, data) interval = deserialize_datetime_interval(data) strategy_run = StrategyRun( uuid.uuid4(), self._date_time_factory.now(), deserialize_pair(data['pair']), [StrategyRunMarket(data['market_plugin_name'], data['market'], data['market_configuration'])], data['strategy_name'], data['strategy_configuration'], interval, data['candles_storage'], data['orders_storage'] ) self._strategy_run_storage.insert(strategy_run) self._event_emitter.emit_new_strategy_run(strategy_run) self._strategy_replayer.run(strategy_run) logger.info("[Rabbit] Finished task: %s | %r", TASK_REPLY_STRATEGY, data)
def create_strategy_to_test(candle_storage: CandleStorage, order_storage: OrderStorage, emitter_mock: EventEmitter, datetime_factory: DateTimeFactory): return DoubleCrossoverStrategy( candle_storage, OrderFacade(order_storage, create_portfolio_snapshot_mock(), emitter_mock), datetime_factory, StrategyRun( STRATEGY_RUN_ID, datetime.datetime(2017, 12, 2, 14, 0, 0, tzinfo=datetime.timezone.utc), BTC_USD_PAIR, [], '', { 'long_average_interval': 60 * 60, 'short_average_interval': 15 * 60, }, DateTimeInterval(None, None), '', ''))
def test_serialize_strategy() -> None: strategy_run_market = StrategyRun( UUID('8b3213c8-2c07-4283-8197-a9babfcc1ec8'), DUMMY_DATE, Pair('USD', 'BTC'), [], 'strategy_xyz', {}, DateTimeInterval(), 'c', 'o' ) assert serialize_strategy_run(strategy_run_market) == { 'candle_storage_name': 'c', 'interval': {'since': None, 'till': None}, 'markets': [], 'order_storage_name': 'o', 'pair': 'USD_BTC', 'run_at': '2017-11-26T10:11:12+00:00', 'strategy_configuration': {}, 'strategy_name': 'strategy_xyz', 'strategy_run_id': '8b3213c8-2c07-4283-8197-a9babfcc1ec8', }
UUID('16fd2706-8baf-433b-82eb-8c7fada847da'), STRATEGY_RUN_ID, DUMMY_MARKET_NAME, DIRECTION_BUY, datetime.datetime(2017, 11, 26, 10, 11, 12, tzinfo=datetime.timezone.utc), BTC_USD_PAIR, ORDER_TYPE_LIMIT, Decimal('1'), Decimal('8000'), 'aaa-id-from-market', ORDER_STATUS_CLOSED, datetime.datetime(2017, 11, 26, 10, 11, 12, tzinfo=datetime.timezone.utc)) DUMMY_OPEN_ORDER = Order( UUID('16fd2706-8baf-433b-82eb-8c7fada847db'), STRATEGY_RUN_ID, DUMMY_MARKET_NAME, DIRECTION_BUY, datetime.datetime(2017, 11, 26, 10, 11, 12, tzinfo=datetime.timezone.utc), BTC_USD_PAIR, ORDER_TYPE_LIMIT, Decimal('1'), Decimal('8000'), 'aaa-id-from-market') STRATEGY_RUN = StrategyRun( STRATEGY_RUN_ID, datetime.datetime(2017, 11, 26, 10, 11, 12, tzinfo=datetime.timezone.utc), BTC_USD_PAIR, [], '', { 'long_average_interval': 60 * 60, 'short_average_interval': 15 * 60, }, DateTimeInterval(None, None), '', '') CLOSED_ORDER_INFO = OrderMarketInfo(order=DUMMY_OPEN_ORDER, is_open=False, closed_at=datetime.datetime( 2017, 11, 26, 10, 11, 12, tzinfo=datetime.timezone.utc), quantity_remaining=Decimal('0'))
def _terminate_strategy_run(strategy_run: StrategyRun) -> None: closed_interval = strategy_run.interval.with_till(di_container.datetime_factory.now()) strategy_run.interval = closed_interval di_container.strategy_run_storage.update(strategy_run)