Пример #1
0
def test_generate_text_table_strategy(default_conf, mocker):
    results = {}
    results['TestStrategy1'] = pd.DataFrame({
        'pair': ['ETH/BTC', 'ETH/BTC', 'ETH/BTC'],
        'profit_percent': [0.1, 0.2, 0.3],
        'profit_abs': [0.2, 0.4, 0.5],
        'trade_duration': [10, 30, 10],
        'wins': [2, 0, 0],
        'draws': [0, 0, 0],
        'losses': [0, 0, 1],
        'sell_reason': [SellType.ROI, SellType.ROI, SellType.STOP_LOSS]
    })
    results['TestStrategy2'] = pd.DataFrame({
        'pair': ['LTC/BTC', 'LTC/BTC', 'LTC/BTC'],
        'profit_percent': [0.4, 0.2, 0.3],
        'profit_abs': [0.4, 0.4, 0.5],
        'trade_duration': [15, 30, 15],
        'wins': [4, 1, 0],
        'draws': [0, 0, 0],
        'losses': [0, 0, 1],
        'sell_reason': [SellType.ROI, SellType.ROI, SellType.STOP_LOSS]
    })

    result_str = (
        '| Strategy      |   Buys |   Avg Profit % |   Cum Profit % |   Tot'
        ' Profit BTC |   Tot Profit % | Avg Duration   |   Wins |   Draws |   Losses |\n'
        '|:--------------|-------:|---------------:|---------------:|------'
        '-----------:|---------------:|:---------------|-------:|--------:|---------:|\n'
        '| TestStrategy1 |      3 |          20.00 |          60.00 |      '
        ' 1.10000000 |          30.00 | 0:17:00        |      3 |       0 |        0 |\n'
        '| TestStrategy2 |      3 |          30.00 |          90.00 |      '
        ' 1.30000000 |          45.00 | 0:20:00        |      3 |       0 |        0 |'
    )
    assert generate_text_table_strategy('BTC', 2,
                                        all_results=results) == result_str
Пример #2
0
def test_generate_text_table_strategy(default_conf, mocker):
    results = {}
    results['ETH/BTC'] = pd.DataFrame(
        {
            'pair': ['ETH/BTC', 'ETH/BTC', 'ETH/BTC'],
            'profit_percent': [0.1, 0.2, 0.3],
            'profit_abs': [0.2, 0.4, 0.5],
            'trade_duration': [10, 30, 10],
            'profit': [2, 0, 0],
            'loss': [0, 0, 1],
            'sell_reason': [SellType.ROI, SellType.ROI, SellType.STOP_LOSS]
        }
    )
    results['LTC/BTC'] = pd.DataFrame(
        {
            'pair': ['LTC/BTC', 'LTC/BTC', 'LTC/BTC'],
            'profit_percent': [0.4, 0.2, 0.3],
            'profit_abs': [0.4, 0.4, 0.5],
            'trade_duration': [15, 30, 15],
            'profit': [4, 1, 0],
            'loss': [0, 0, 1],
            'sell_reason': [SellType.ROI, SellType.ROI, SellType.STOP_LOSS]
        }
    )

    result_str = (
        '| Strategy   |   buy count |   avg profit % |   cum profit % '
        '|   tot profit BTC |   tot profit % | avg duration   |   profit |   loss |\n'
        '|:-----------|------------:|---------------:|---------------:'
        '|-----------------:|---------------:|:---------------|---------:|-------:|\n'
        '| ETH/BTC    |           3 |          20.00 |          60.00 '
        '|       1.10000000 |          30.00 | 0:17:00        |        3 |      0 |\n'
        '| LTC/BTC    |           3 |          30.00 |          90.00 '
        '|       1.30000000 |          45.00 | 0:20:00        |        3 |      0 |'
    )
    assert generate_text_table_strategy('BTC', 2, all_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')