Пример #1
0
async def _check_backtesting_results(backtesting1, backtesting2):
    if backtesting1 is not None and backtesting2 is not None:
        exchange_manager_1 = get_exchange_manager_from_exchange_id(
            get_independent_backtesting_exchange_manager_ids(backtesting1)[0])
        exchange_manager_2 = get_exchange_manager_from_exchange_id(
            get_independent_backtesting_exchange_manager_ids(backtesting2)[0])

        _, previous_profitability, previous_market_profitability, _, _ = get_profitability_stats(exchange_manager_1)
        _, current_profitability, current_market_profitability, _, _ = get_profitability_stats(exchange_manager_2)

        trades = get_trade_history(exchange_manager_1)
        open_orders = get_open_orders(exchange_manager_1)
        # ensure at least one order is either open or got filled
        assert trades + open_orders

        trades = open_orders = exchange_manager_1 = exchange_manager_2 = None  # prevent memory leak

        # ensure no randomness in backtesting
        assert previous_profitability == current_profitability
        assert current_profitability != 0
        assert previous_profitability != 0
        assert previous_market_profitability == current_market_profitability

    backtestings = tuple(b for b in (backtesting1, backtesting2) if b is not None)
    for backtesting in backtestings:
        await stop_independent_backtesting(backtesting, memory_check=True, should_raise=True)
        await asyncio.wait_for(backtesting.post_backtesting_task, 5)
    for backtesting in backtestings:
        asyncio.get_event_loop().call_soon(check_independent_backtesting_remaining_objects, backtesting)
    async def _handle_order_notification(self, dict_order, linked_notification, order_identifier,
                                         exchange_manager, exchange):
        notification = None
        order_status = parse_order_status(dict_order)

        if order_status is OrderStatus.OPEN:
            notification = OrderCreationNotification(linked_notification, dict_order, exchange)
            # update last notification for this order
            self.previous_notifications_by_identifier[order_identifier] = notification
        else:
            is_simulated = is_trader_simulated(exchange_manager)
            if order_status is OrderStatus.CANCELED or \
                    (order_status is OrderStatus.CLOSED
                     and dict_order[ExchangeConstantsOrderColumns.FILLED.value] == 0):
                notification = OrderEndNotification(linked_notification, None, exchange, [dict_order],
                                                    None, None, None, False, is_simulated)
            elif order_status in (OrderStatus.CLOSED, OrderStatus.FILLED):
                _,  profitability_percent, profitability_diff, _,  _ = get_profitability_stats(exchange_manager)
                order_profitability = get_order_profitability(exchange_manager,
                                                              dict_order[ExchangeConstantsOrderColumns.ID.value])
                notification = OrderEndNotification(linked_notification, dict_order, exchange, [], order_profitability,
                                                    profitability_percent, profitability_diff, True, is_simulated)
            # remove order from previous_notifications_by_identifier: no more notification from it to be received
            if order_identifier in self.previous_notifications_by_identifier:
                self.previous_notifications_by_identifier.pop(order_identifier)
        await self._notification_callback(notification)
def get_global_profitability():
    simulated_global_profitability = 0
    real_global_profitability = 0
    simulated_no_trade_profitability = 0
    real_no_trade_profitability = 0
    simulated_full_origin_value = 0
    real_full_origin_value = 0
    market_average_profitability = None
    has_real_trader = False
    has_simulated_trader = False

    for exchange_manager in get_exchange_managers():
        if is_trader_enabled(exchange_manager):

            current_value, _, _, market_average_profitability, initial_portfolio_current_profitability = \
                get_profitability_stats(exchange_manager)

            if is_trader_simulated(exchange_manager):
                simulated_full_origin_value += get_origin_portfolio_value(
                    exchange_manager)
                simulated_global_profitability += current_value
                simulated_no_trade_profitability += initial_portfolio_current_profitability
                has_simulated_trader = True
            else:
                real_full_origin_value += get_origin_portfolio_value(
                    exchange_manager)
                real_global_profitability += current_value
                real_no_trade_profitability += initial_portfolio_current_profitability
                has_real_trader = True

    simulated_percent_profitability = simulated_global_profitability * 100 / simulated_full_origin_value \
        if simulated_full_origin_value > 0 else 0
    real_percent_profitability = real_global_profitability * 100 / real_full_origin_value \
        if real_full_origin_value > 0 else 0

    return has_real_trader, has_simulated_trader, \
        real_global_profitability, simulated_global_profitability, \
        real_percent_profitability, simulated_percent_profitability, \
        real_no_trade_profitability, simulated_no_trade_profitability, \
        market_average_profitability