示例#1
0
def test_generate_text_table_sell_reason(default_conf, mocker):

    results = pd.DataFrame(
        {
            'pair': ['ETH/BTC', 'ETH/BTC', 'ETH/BTC'],
            'profit_percent': [0.1, 0.2, -0.1],
            'profit_abs': [0.2, 0.4, -0.2],
            '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]
        }
    )

    result_str = (
        '|   Sell Reason |   Sells |   Wins |   Draws |   Losses |'
        '   Avg Profit % |   Cum Profit % |   Tot Profit BTC |   Tot Profit % |\n'
        '|---------------+---------+--------+---------+----------+'
        '----------------+----------------+------------------+----------------|\n'
        '|           roi |       2 |      2 |       0 |        0 |'
        '             15 |             30 |              0.6 |             15 |\n'
        '|     stop_loss |       1 |      0 |       0 |        1 |'
        '            -10 |            -10 |             -0.2 |             -5 |'
    )
    assert generate_text_table_sell_reason(
        data={'ETH/BTC': {}},
        stake_currency='BTC', max_open_trades=2,
        results=results) == result_str
示例#2
0
def test_generate_text_table_sell_reason(default_conf, mocker):

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

    result_str = (
        '| Sell Reason   |   Count |   Profit |   Loss |   Profit % |\n'
        '|:--------------|--------:|---------:|-------:|-----------:|\n'
        '| roi           |       2 |        2 |      0 |         15 |\n'
        '| stop_loss     |       1 |        0 |      1 |        -10 |'
    )
    assert generate_text_table_sell_reason(
        data={'ETH/BTC': {}}, 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')