Пример #1
0
def test_generate_text_table(default_conf, mocker):

    results = pd.DataFrame(
        {
            'pair': ['ETH/BTC', 'ETH/BTC'],
            'profit_percent': [0.1, 0.2],
            'profit_abs': [0.2, 0.4],
            'trade_duration': [10, 30],
            'profit': [2, 0],
            'loss': [0, 0]
        }
    )

    result_str = (
        '| pair    |   buy count |   avg profit % |   cum profit % |   '
        'tot profit BTC |   tot profit % | avg duration   |   profit |   loss |\n'
        '|:--------|------------:|---------------:|---------------:|'
        '-----------------:|---------------:|:---------------|---------:|-------:|\n'
        '| ETH/BTC |           2 |          15.00 |          30.00 |       '
        '0.60000000 |          15.00 | 0:20:00        |        2 |      0 |\n'
        '| TOTAL   |           2 |          15.00 |          30.00 |       '
        '0.60000000 |          15.00 | 0:20:00        |        2 |      0 |'
    )
    assert generate_text_table(data={'ETH/BTC': {}},
                               stake_currency='BTC', max_open_trades=2,
                               results=results) == result_str
Пример #2
0
def test_generate_text_table(default_conf, mocker):

    results = pd.DataFrame(
        {
            'pair': ['ETH/BTC', 'ETH/BTC'],
            'profit_percent': [0.1, 0.2],
            'profit_abs': [0.2, 0.4],
            'trade_duration': [10, 30],
            'wins': [2, 0],
            'draws': [0, 0],
            'losses': [0, 0]
        }
    )

    result_str = (
        '|    Pair |   Buys |   Avg Profit % |   Cum Profit % |   Tot Profit BTC |'
        '   Tot Profit % |   Avg Duration |   Wins |   Draws |   Losses |\n'
        '|---------+--------+----------------+----------------+------------------+'
        '----------------+----------------+--------+---------+----------|\n'
        '| ETH/BTC |      2 |          15.00 |          30.00 |       0.60000000 |'
        '          15.00 |        0:20:00 |      2 |       0 |        0 |\n'
        '|   TOTAL |      2 |          15.00 |          30.00 |       0.60000000 |'
        '          15.00 |        0:20:00 |      2 |       0 |        0 |'
    )
    assert generate_text_table(data={'ETH/BTC': {}},
                               stake_currency='BTC', max_open_trades=2,
                               results=results) == result_str
Пример #3
0
    def start(self) -> None:
        """
        Run backtesting end-to-end
        :return: None
        """
        data: Dict[str, Any] = {}

        logger.info('Using stake_currency: %s ...',
                    self.config['stake_currency'])
        logger.info('Using stake_amount: %s ...', self.config['stake_amount'])

        # Use max_open_trades in backtesting, except --disable-max-market-positions is set
        if self.config.get('use_max_market_positions', True):
            max_open_trades = self.config['max_open_trades']
        else:
            logger.info(
                'Ignoring max_open_trades (--disable-max-market-positions was used) ...'
            )
            max_open_trades = 0
        position_stacking = self.config.get('position_stacking', False)

        data, timerange = self.load_bt_data()

        all_results = {}
        for strat in self.strategylist:
            logger.info("Running backtesting for Strategy %s",
                        strat.get_strategy_name())
            self._set_strategy(strat)

            # need to reprocess data every time to populate signals
            preprocessed = self.strategy.tickerdata_to_dataframe(data)

            # Trim startup period from analyzed dataframe
            for pair, df in preprocessed.items():
                preprocessed[pair] = history.trim_dataframe(df, timerange)
            min_date, max_date = history.get_timerange(preprocessed)

            logger.info('Backtesting with data from %s up to %s (%s days)..',
                        min_date.isoformat(), max_date.isoformat(),
                        (max_date - min_date).days)
            # Execute backtest and print results
            all_results[self.strategy.get_strategy_name()] = self.backtest(
                processed=preprocessed,
                stake_amount=self.config['stake_amount'],
                start_date=min_date,
                end_date=max_date,
                max_open_trades=max_open_trades,
                position_stacking=position_stacking,
            )

        for strategy, results in all_results.items():

            if self.config.get('export', False):
                self._store_backtest_result(
                    Path(self.config['exportfilename']), results,
                    strategy if len(self.strategylist) > 1 else None)

            print(f"Result for strategy {strategy}")
            print(' BACKTESTING REPORT '.center(133, '='))
            print(
                generate_text_table(
                    data,
                    stake_currency=self.config['stake_currency'],
                    max_open_trades=self.config['max_open_trades'],
                    results=results))

            print(' SELL REASON STATS '.center(133, '='))
            print(generate_text_table_sell_reason(data, results))

            print(' LEFT OPEN TRADES REPORT '.center(133, '='))
            print(
                generate_text_table(
                    data,
                    stake_currency=self.config['stake_currency'],
                    max_open_trades=self.config['max_open_trades'],
                    results=results.loc[results.open_at_end],
                    skip_nan=True))
            print()
        if len(all_results) > 1:
            # Print Strategy summary table
            print(' Strategy Summary '.center(133, '='))
            print(
                generate_text_table_strategy(self.config['stake_currency'],
                                             self.config['max_open_trades'],
                                             all_results=all_results))
            print('\nFor more details, please look at the detail tables above')