| 1 | # pragma pylint: disable=missing-docstring, C0103, C0330 |
| 2 | # pragma pylint: disable=protected-access, too-many-lines, invalid-name, too-many-arguments |
| 3 | |
| 4 | import logging |
| 5 | import math |
| 6 | from unittest.mock import MagicMock |
| 7 | |
| 8 | import arrow |
| 9 | import numpy as np |
| 10 | import pytest |
| 11 | from pandas import DataFrame, to_datetime |
| 12 | |
| 13 | from freqtrade.data.converter import ohlcv_to_dataframe |
| 14 | from freqtrade.edge import Edge, PairInfo |
| 15 | from freqtrade.exceptions import OperationalException |
| 16 | from freqtrade.strategy.interface import SellType |
| 17 | from tests.conftest import get_patched_freqtradebot, log_has |
| 18 | from tests.optimize import (BTContainer, BTrade, _build_backtest_dataframe, |
| 19 | _get_frame_time_from_offset) |
| 20 | |
| 21 | |
| 22 | # Cases to be tested: |
| 23 | # 1) Open trade should be removed from the end |
| 24 | # 2) Two complete trades within dataframe (with sell hit for all) |
| 25 | # 3) Entered, sl 1%, candle drops 8% => Trade closed, 1% loss |
| 26 | # 4) Entered, sl 3%, candle drops 4%, recovers to 1% => Trade closed, 3% loss |
| 27 | # 5) Stoploss and sell are hit. should sell on stoploss |
| 28 | #################################################################### |
| 29 | |
| 30 | tests_start_time = arrow.get(2018, 10, 3) |
| 31 | timeframe_in_minute = 60 |
| 32 | _ohlc = {'date': 0, 'buy': 1, 'open': 2, 'high': 3, 'low': 4, 'close': 5, 'sell': 6, 'volume': 7} |
| 33 | |
| 34 | # Helpers for this test file |
| 35 | |
| 36 | |
| 37 | def _validate_ohlc(buy_ohlc_sell_matrice): |
| 38 | for index, ohlc in enumerate(buy_ohlc_sell_matrice): |
| 39 | # if not high < open < low or not high < close < low |
| 40 | if not ohlc[3] >= ohlc[2] >= ohlc[4] or not ohlc[3] >= ohlc[5] >= ohlc[4]: |
| 41 | raise Exception('Line ' + str(index + 1) + ' of ohlc has invalid values!') |
| 42 | return True |
| 43 | |
| 44 | |
| 45 | def _build_dataframe(buy_ohlc_sell_matrice): |
| 46 | _validate_ohlc(buy_ohlc_sell_matrice) |
| 47 | data = [] |
| 48 | for ohlc in buy_ohlc_sell_matrice: |
| 49 | d = { |
| 50 | 'date': tests_start_time.shift( |
| 51 | minutes=( |
| 52 | ohlc[0] * |
| 53 | timeframe_in_minute)).int_timestamp * |
| 54 | 1000, |
| 55 | 'buy': ohlc[1], |
| 56 | 'open': ohlc[2], |
| 57 | 'high': ohlc[3], |
| 58 | 'low': ohlc[4], |
| 59 | 'close': ohlc[5], |
| 60 | 'sell': ohlc[6]} |
| 61 | data.append(d) |
| 62 | |
| 63 | frame = DataFrame(data) |
| 64 | frame['date'] = to_datetime(frame['date'], |
| 65 | unit='ms', |
| 66 | utc=True, |
| 67 | infer_datetime_format=True) |
| 68 | |
| 69 | return frame |
| 70 | |
| 71 | |
| 72 | def _time_on_candle(number): |
| 73 | return np.datetime64(tests_start_time.shift( |
| 74 | minutes=(number * timeframe_in_minute)).int_timestamp * 1000, 'ms') |
| 75 | |
| 76 | |
| 77 | # End helper functions |
| 78 | # Open trade should be removed from the end |
| 79 | tc0 = BTContainer(data=[ |
| 80 | # D O H L C V B S |
| 81 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 82 | [1, 5000, 5025, 4975, 4987, 6172, 0, 1]], # enter trade (signal on last candle) |
| 83 | stop_loss=-0.99, roi={"0": float('inf')}, profit_perc=0.00, |
| 84 | trades=[] |
| 85 | ) |
| 86 | |
| 87 | # Two complete trades within dataframe(with sell hit for all) |
| 88 | tc1 = BTContainer(data=[ |
| 89 | # D O H L C V B S |
| 90 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 91 | [1, 5000, 5025, 4975, 4987, 6172, 0, 1], # enter trade (signal on last candle) |
| 92 | [2, 5000, 5025, 4975, 4987, 6172, 0, 0], # exit at open |
| 93 | [3, 5000, 5025, 4975, 4987, 6172, 1, 0], # no action |
| 94 | [4, 5000, 5025, 4975, 4987, 6172, 0, 0], # should enter the trade |
| 95 | [5, 5000, 5025, 4975, 4987, 6172, 0, 1], # no action |
| 96 | [6, 5000, 5025, 4975, 4987, 6172, 0, 0], # should sell |
| 97 | ], |
| 98 | stop_loss=-0.99, roi={"0": float('inf')}, profit_perc=0.00, |
| 99 | trades=[BTrade(sell_reason=SellType.SELL_SIGNAL, open_tick=1, close_tick=2), |
| 100 | BTrade(sell_reason=SellType.SELL_SIGNAL, open_tick=4, close_tick=6)] |
| 101 | ) |
| 102 | |
| 103 | # 3) Entered, sl 1%, candle drops 8% => Trade closed, 1% loss |
| 104 | tc2 = BTContainer(data=[ |
| 105 | # D O H L C V B S |
| 106 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 107 | [1, 5000, 5025, 4600, 4987, 6172, 0, 0], # enter trade, stoploss hit |
| 108 | [2, 5000, 5025, 4975, 4987, 6172, 0, 0], |
| 109 | ], |
| 110 | stop_loss=-0.01, roi={"0": float('inf')}, profit_perc=-0.01, |
| 111 | trades=[BTrade(sell_reason=SellType.STOP_LOSS, open_tick=1, close_tick=1)] |
| 112 | ) |
| 113 | |
| 114 | # 4) Entered, sl 3 %, candle drops 4%, recovers to 1 % = > Trade closed, 3 % loss |
| 115 | tc3 = BTContainer(data=[ |
| 116 | # D O H L C V B S |
| 117 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 118 | [1, 5000, 5025, 4800, 4987, 6172, 0, 0], # enter trade, stoploss hit |
| 119 | [2, 5000, 5025, 4975, 4987, 6172, 0, 0], |
| 120 | ], |
| 121 | stop_loss=-0.03, roi={"0": float('inf')}, profit_perc=-0.03, |
| 122 | trades=[BTrade(sell_reason=SellType.STOP_LOSS, open_tick=1, close_tick=1)] |
| 123 | ) |
| 124 | |
| 125 | # 5) Stoploss and sell are hit. should sell on stoploss |
| 126 | tc4 = BTContainer(data=[ |
| 127 | # D O H L C V B S |
| 128 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 129 | [1, 5000, 5025, 4800, 4987, 6172, 0, 1], # enter trade, stoploss hit, sell signal |
| 130 | [2, 5000, 5025, 4975, 4987, 6172, 0, 0], |
| 131 | ], |
| 132 | stop_loss=-0.03, roi={"0": float('inf')}, profit_perc=-0.03, |
| 133 | trades=[BTrade(sell_reason=SellType.STOP_LOSS, open_tick=1, close_tick=1)] |
| 134 | ) |
| 135 | |
| 136 | TESTS = [ |
| 137 | tc0, |
| 138 | tc1, |
| 139 | tc2, |
| 140 | tc3, |
| 141 | tc4 |
| 142 | ] |
| 143 | |
| 144 | |
| 145 | @pytest.mark.parametrize("data", TESTS) |
| 146 | def test_edge_results(edge_conf, mocker, caplog, data) -> None: |
| 147 | """ |
| 148 | run functional tests |
| 149 | """ |
| 150 | freqtrade = get_patched_freqtradebot(mocker, edge_conf) |
| 151 | edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy) |
| 152 | frame = _build_backtest_dataframe(data.data) |
| 153 | caplog.set_level(logging.DEBUG) |
| 154 | edge.fee = 0 |
| 155 | |
| 156 | trades = edge._find_trades_for_stoploss_range(frame, 'TEST/BTC', [data.stop_loss]) |
| 157 | results = edge._fill_calculable_fields(DataFrame(trades)) if trades else DataFrame() |
| 158 | |
| 159 | assert len(trades) == len(data.trades) |
| 160 | |
| 161 | if not results.empty: |
| 162 | assert round(results["profit_ratio"].sum(), 3) == round(data.profit_perc, 3) |
| 163 | |
| 164 | for c, trade in enumerate(data.trades): |
| 165 | res = results.iloc[c] |
| 166 | assert res.exit_type == trade.sell_reason |
| 167 | assert res.open_date == _get_frame_time_from_offset(trade.open_tick).replace(tzinfo=None) |
| 168 | assert res.close_date == _get_frame_time_from_offset(trade.close_tick).replace(tzinfo=None) |
| 169 | |
| 170 | |
| 171 | def test_adjust(mocker, edge_conf): |
| 172 | freqtrade = get_patched_freqtradebot(mocker, edge_conf) |
| 173 | edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy) |
| 174 | mocker.patch('freqtrade.edge.Edge._cached_pairs', mocker.PropertyMock( |
| 175 | return_value={ |
| 176 | 'E/F': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60), |
| 177 | 'C/D': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60), |
| 178 | 'N/O': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60) |
| 179 | } |
| 180 | )) |
| 181 | |
| 182 | pairs = ['A/B', 'C/D', 'E/F', 'G/H'] |
| 183 | assert(edge.adjust(pairs) == ['E/F', 'C/D']) |
| 184 | |
| 185 | |
| 186 | def test_stoploss(mocker, edge_conf): |
| 187 | freqtrade = get_patched_freqtradebot(mocker, edge_conf) |
| 188 | edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy) |
| 189 | mocker.patch('freqtrade.edge.Edge._cached_pairs', mocker.PropertyMock( |
| 190 | return_value={ |
| 191 | 'E/F': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60), |
| 192 | 'C/D': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60), |
| 193 | 'N/O': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60) |
| 194 | } |
| 195 | )) |
| 196 | |
| 197 | assert edge.stoploss('E/F') == -0.01 |
| 198 | |
| 199 | |
| 200 | def test_nonexisting_stoploss(mocker, edge_conf): |
| 201 | freqtrade = get_patched_freqtradebot(mocker, edge_conf) |
| 202 | edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy) |
| 203 | mocker.patch('freqtrade.edge.Edge._cached_pairs', mocker.PropertyMock( |
| 204 | return_value={ |
| 205 | 'E/F': PairInfo(-0.01, 0.66, 3.71, 0.50, 1.71, 10, 60), |
| 206 | } |
| 207 | )) |
| 208 | |
| 209 | assert edge.stoploss('N/O') == -0.1 |
| 210 | |
| 211 | |
| 212 | def test_edge_stake_amount(mocker, edge_conf): |
| 213 | freqtrade = get_patched_freqtradebot(mocker, edge_conf) |
| 214 | edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy) |
| 215 | mocker.patch('freqtrade.edge.Edge._cached_pairs', mocker.PropertyMock( |
| 216 | return_value={ |
| 217 | 'E/F': PairInfo(-0.02, 0.66, 3.71, 0.50, 1.71, 10, 60), |
| 218 | } |
| 219 | )) |
| 220 | assert edge._capital_ratio == 0.5 |
| 221 | assert edge.stake_amount('E/F', free_capital=100, total_capital=100, |
| 222 | capital_in_trade=25) == 31.25 |
| 223 | |
| 224 | assert edge.stake_amount('E/F', free_capital=20, total_capital=100, |
| 225 | capital_in_trade=25) == 20 |
| 226 | |
| 227 | assert edge.stake_amount('E/F', free_capital=0, total_capital=100, |
| 228 | capital_in_trade=25) == 0 |
| 229 | |
| 230 | # Test with increased allowed_risk |
| 231 | # Result should be no more than allowed capital |
| 232 | edge._allowed_risk = 0.4 |
| 233 | edge._capital_ratio = 0.5 |
| 234 | assert edge.stake_amount('E/F', free_capital=100, total_capital=100, |
| 235 | capital_in_trade=25) == 62.5 |
| 236 | |
| 237 | assert edge.stake_amount('E/F', free_capital=100, total_capital=100, |
| 238 | capital_in_trade=0) == 50 |
| 239 | |
| 240 | edge._capital_ratio = 1 |
| 241 | # Full capital is available |
| 242 | assert edge.stake_amount('E/F', free_capital=100, total_capital=100, |
| 243 | capital_in_trade=0) == 100 |
| 244 | # Full capital is available |
| 245 | assert edge.stake_amount('E/F', free_capital=0, total_capital=100, |
| 246 | capital_in_trade=0) == 0 |
| 247 | |
| 248 | |
| 249 | def test_nonexisting_stake_amount(mocker, edge_conf): |
| 250 | freqtrade = get_patched_freqtradebot(mocker, edge_conf) |
| 251 | edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy) |
| 252 | mocker.patch('freqtrade.edge.Edge._cached_pairs', mocker.PropertyMock( |
| 253 | return_value={ |
| 254 | 'E/F': PairInfo(-0.11, 0.66, 3.71, 0.50, 1.71, 10, 60), |
| 255 | } |
| 256 | )) |
| 257 | # should use strategy stoploss |
| 258 | assert edge.stake_amount('N/O', 1, 2, 1) == 0.15 |
| 259 | |
| 260 | |
| 261 | def test_edge_heartbeat_calculate(mocker, edge_conf): |
| 262 | freqtrade = get_patched_freqtradebot(mocker, edge_conf) |
| 263 | edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy) |
| 264 | heartbeat = edge_conf['edge']['process_throttle_secs'] |
| 265 | |
| 266 | # should not recalculate if heartbeat not reached |
| 267 | edge._last_updated = arrow.utcnow().int_timestamp - heartbeat + 1 |
| 268 | |
| 269 | assert edge.calculate(edge_conf['exchange']['pair_whitelist']) is False |
| 270 | |
| 271 | |
| 272 | def mocked_load_data(datadir, pairs=[], timeframe='0m', |
| 273 | timerange=None, *args, **kwargs): |
| 274 | hz = 0.1 |
| 275 | base = 0.001 |
| 276 | |
| 277 | NEOBTC = [ |
| 278 | [ |
| 279 | tests_start_time.shift(minutes=(x * timeframe_in_minute)).int_timestamp * 1000, |
| 280 | math.sin(x * hz) / 1000 + base, |
| 281 | math.sin(x * hz) / 1000 + base + 0.0001, |
| 282 | math.sin(x * hz) / 1000 + base - 0.0001, |
| 283 | math.sin(x * hz) / 1000 + base, |
| 284 | 123.45 |
| 285 | ] for x in range(0, 500)] |
| 286 | |
| 287 | hz = 0.2 |
| 288 | base = 0.002 |
| 289 | LTCBTC = [ |
| 290 | [ |
| 291 | tests_start_time.shift(minutes=(x * timeframe_in_minute)).int_timestamp * 1000, |
| 292 | math.sin(x * hz) / 1000 + base, |
| 293 | math.sin(x * hz) / 1000 + base + 0.0001, |
| 294 | math.sin(x * hz) / 1000 + base - 0.0001, |
| 295 | math.sin(x * hz) / 1000 + base, |
| 296 | 123.45 |
| 297 | ] for x in range(0, 500)] |
| 298 | |
| 299 | pairdata = {'NEO/BTC': ohlcv_to_dataframe(NEOBTC, '1h', pair="NEO/BTC", |
| 300 | fill_missing=True), |
| 301 | 'LTC/BTC': ohlcv_to_dataframe(LTCBTC, '1h', pair="LTC/BTC", |
| 302 | fill_missing=True)} |
| 303 | return pairdata |
| 304 | |
| 305 | |
| 306 | def test_edge_process_downloaded_data(mocker, edge_conf): |
| 307 | freqtrade = get_patched_freqtradebot(mocker, edge_conf) |
| 308 | mocker.patch('freqtrade.exchange.Exchange.get_fee', MagicMock(return_value=0.001)) |
| 309 | mocker.patch('freqtrade.edge.edge_positioning.refresh_data', MagicMock()) |
| 310 | mocker.patch('freqtrade.edge.edge_positioning.load_data', mocked_load_data) |
| 311 | edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy) |
| 312 | |
| 313 | assert edge.calculate(edge_conf['exchange']['pair_whitelist']) |
| 314 | assert len(edge._cached_pairs) == 2 |
| 315 | assert edge._last_updated <= arrow.utcnow().int_timestamp + 2 |
| 316 | |
| 317 | |
| 318 | def test_edge_process_no_data(mocker, edge_conf, caplog): |
| 319 | freqtrade = get_patched_freqtradebot(mocker, edge_conf) |
| 320 | mocker.patch('freqtrade.exchange.Exchange.get_fee', MagicMock(return_value=0.001)) |
| 321 | mocker.patch('freqtrade.edge.edge_positioning.refresh_data', MagicMock()) |
| 322 | mocker.patch('freqtrade.edge.edge_positioning.load_data', MagicMock(return_value={})) |
| 323 | edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy) |
| 324 | |
| 325 | assert not edge.calculate(edge_conf['exchange']['pair_whitelist']) |
| 326 | assert len(edge._cached_pairs) == 0 |
| 327 | assert log_has("No data found. Edge is stopped ...", caplog) |
| 328 | assert edge._last_updated == 0 |
| 329 | |
| 330 | |
| 331 | def test_edge_process_no_trades(mocker, edge_conf, caplog): |
| 332 | freqtrade = get_patched_freqtradebot(mocker, edge_conf) |
| 333 | mocker.patch('freqtrade.exchange.Exchange.get_fee', MagicMock(return_value=0.001)) |
| 334 | mocker.patch('freqtrade.edge.edge_positioning.refresh_data', MagicMock()) |
| 335 | mocker.patch('freqtrade.edge.edge_positioning.load_data', mocked_load_data) |
| 336 | # Return empty |
| 337 | mocker.patch('freqtrade.edge.Edge._find_trades_for_stoploss_range', MagicMock(return_value=[])) |
| 338 | edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy) |
| 339 | |
| 340 | assert not edge.calculate(edge_conf['exchange']['pair_whitelist']) |
| 341 | assert len(edge._cached_pairs) == 0 |
| 342 | assert log_has("No trades found.", caplog) |
| 343 | |
| 344 | |
| 345 | def test_edge_init_error(mocker, edge_conf,): |
| 346 | edge_conf['stake_amount'] = 0.5 |
| 347 | mocker.patch('freqtrade.exchange.Exchange.get_fee', MagicMock(return_value=0.001)) |
| 348 | with pytest.raises(OperationalException, match='Edge works only with unlimited stake amount'): |
| 349 | get_patched_freqtradebot(mocker, edge_conf) |
| 350 | |
| 351 | |
| 352 | @pytest.mark.parametrize("fee,risk_reward_ratio,expectancy", [ |
| 353 | (0.0005, 306.5384615384, 101.5128205128), |
| 354 | (0.001, 152.6923076923, 50.2307692308), |
| 355 | ]) |
| 356 | def test_process_expectancy(mocker, edge_conf, fee, risk_reward_ratio, expectancy): |
| 357 | edge_conf['edge']['min_trade_number'] = 2 |
| 358 | freqtrade = get_patched_freqtradebot(mocker, edge_conf) |
| 359 | |
| 360 | def get_fee(*args, **kwargs): |
| 361 | return fee |
| 362 | |
| 363 | freqtrade.exchange.get_fee = get_fee |
| 364 | edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy) |
| 365 | |
| 366 | trades = [ |
| 367 | {'pair': 'TEST/BTC', |
| 368 | 'stoploss': -0.9, |
| 369 | 'profit_percent': '', |
| 370 | 'profit_abs': '', |
| 371 | 'open_date': np.datetime64('2018-10-03T00:05:00.000000000'), |
| 372 | 'close_date': np.datetime64('2018-10-03T00:10:00.000000000'), |
| 373 | 'trade_duration': '', |
| 374 | 'open_rate': 17, |
| 375 | 'close_rate': 17, |
| 376 | 'exit_type': 'sell_signal'}, |
| 377 | |
| 378 | {'pair': 'TEST/BTC', |
| 379 | 'stoploss': -0.9, |
| 380 | 'profit_percent': '', |
| 381 | 'profit_abs': '', |
| 382 | 'open_date': np.datetime64('2018-10-03T00:20:00.000000000'), |
| 383 | 'close_date': np.datetime64('2018-10-03T00:25:00.000000000'), |
| 384 | 'trade_duration': '', |
| 385 | 'open_rate': 20, |
| 386 | 'close_rate': 20, |
| 387 | 'exit_type': 'sell_signal'}, |
| 388 | |
| 389 | {'pair': 'TEST/BTC', |
| 390 | 'stoploss': -0.9, |
| 391 | 'profit_percent': '', |
| 392 | 'profit_abs': '', |
| 393 | 'open_date': np.datetime64('2018-10-03T00:30:00.000000000'), |
| 394 | 'close_date': np.datetime64('2018-10-03T00:40:00.000000000'), |
| 395 | 'trade_duration': '', |
| 396 | 'open_rate': 26, |
| 397 | 'close_rate': 34, |
| 398 | 'exit_type': 'sell_signal'} |
| 399 | ] |
| 400 | |
| 401 | trades_df = DataFrame(trades) |
| 402 | trades_df = edge._fill_calculable_fields(trades_df) |
| 403 | final = edge._process_expectancy(trades_df) |
| 404 | assert len(final) == 1 |
| 405 | |
| 406 | assert 'TEST/BTC' in final |
| 407 | assert final['TEST/BTC'].stoploss == -0.9 |
| 408 | assert round(final['TEST/BTC'].winrate, 10) == 0.3333333333 |
| 409 | assert round(final['TEST/BTC'].risk_reward_ratio, 10) == risk_reward_ratio |
| 410 | assert round(final['TEST/BTC'].required_risk_reward, 10) == 2.0 |
| 411 | assert round(final['TEST/BTC'].expectancy, 10) == expectancy |
| 412 | |
| 413 | # Pop last item so no trade is profitable |
| 414 | trades.pop() |
| 415 | trades_df = DataFrame(trades) |
| 416 | trades_df = edge._fill_calculable_fields(trades_df) |
| 417 | final = edge._process_expectancy(trades_df) |
| 418 | assert len(final) == 0 |
| 419 | assert isinstance(final, dict) |
| 420 | |
| 421 | |
| 422 | def test_process_expectancy_remove_pumps(mocker, edge_conf, fee,): |
| 423 | edge_conf['edge']['min_trade_number'] = 2 |
| 424 | edge_conf['edge']['remove_pumps'] = True |
| 425 | freqtrade = get_patched_freqtradebot(mocker, edge_conf) |
| 426 | |
| 427 | freqtrade.exchange.get_fee = fee |
| 428 | edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy) |
| 429 | |
| 430 | trades = [ |
| 431 | {'pair': 'TEST/BTC', |
| 432 | 'stoploss': -0.9, |
| 433 | 'profit_percent': '', |
| 434 | 'profit_abs': '', |
| 435 | 'open_date': np.datetime64('2018-10-03T00:05:00.000000000'), |
| 436 | 'close_date': np.datetime64('2018-10-03T00:10:00.000000000'), |
| 437 | 'open_index': 1, |
| 438 | 'close_index': 1, |
| 439 | 'trade_duration': '', |
| 440 | 'open_rate': 17, |
| 441 | 'close_rate': 15, |
| 442 | 'exit_type': 'sell_signal'}, |
| 443 | |
| 444 | {'pair': 'TEST/BTC', |
| 445 | 'stoploss': -0.9, |
| 446 | 'profit_percent': '', |
| 447 | 'profit_abs': '', |
| 448 | 'open_date': np.datetime64('2018-10-03T00:20:00.000000000'), |
| 449 | 'close_date': np.datetime64('2018-10-03T00:25:00.000000000'), |
| 450 | 'open_index': 4, |
| 451 | 'close_index': 4, |
| 452 | 'trade_duration': '', |
| 453 | 'open_rate': 20, |
| 454 | 'close_rate': 10, |
| 455 | 'exit_type': 'sell_signal'}, |
| 456 | {'pair': 'TEST/BTC', |
| 457 | 'stoploss': -0.9, |
| 458 | 'profit_percent': '', |
| 459 | 'profit_abs': '', |
| 460 | 'open_date': np.datetime64('2018-10-03T00:20:00.000000000'), |
| 461 | 'close_date': np.datetime64('2018-10-03T00:25:00.000000000'), |
| 462 | 'open_index': 4, |
| 463 | 'close_index': 4, |
| 464 | 'trade_duration': '', |
| 465 | 'open_rate': 20, |
| 466 | 'close_rate': 10, |
| 467 | 'exit_type': 'sell_signal'}, |
| 468 | {'pair': 'TEST/BTC', |
| 469 | 'stoploss': -0.9, |
| 470 | 'profit_percent': '', |
| 471 | 'profit_abs': '', |
| 472 | 'open_date': np.datetime64('2018-10-03T00:20:00.000000000'), |
| 473 | 'close_date': np.datetime64('2018-10-03T00:25:00.000000000'), |
| 474 | 'open_index': 4, |
| 475 | 'close_index': 4, |
| 476 | 'trade_duration': '', |
| 477 | 'open_rate': 20, |
| 478 | 'close_rate': 10, |
| 479 | 'exit_type': 'sell_signal'}, |
| 480 | {'pair': 'TEST/BTC', |
| 481 | 'stoploss': -0.9, |
| 482 | 'profit_percent': '', |
| 483 | 'profit_abs': '', |
| 484 | 'open_date': np.datetime64('2018-10-03T00:20:00.000000000'), |
| 485 | 'close_date': np.datetime64('2018-10-03T00:25:00.000000000'), |
| 486 | 'open_index': 4, |
| 487 | 'close_index': 4, |
| 488 | 'trade_duration': '', |
| 489 | 'open_rate': 20, |
| 490 | 'close_rate': 10, |
| 491 | 'exit_type': 'sell_signal'}, |
| 492 | |
| 493 | {'pair': 'TEST/BTC', |
| 494 | 'stoploss': -0.9, |
| 495 | 'profit_percent': '', |
| 496 | 'profit_abs': '', |
| 497 | 'open_date': np.datetime64('2018-10-03T00:30:00.000000000'), |
| 498 | 'close_date': np.datetime64('2018-10-03T00:40:00.000000000'), |
| 499 | 'open_index': 6, |
| 500 | 'close_index': 7, |
| 501 | 'trade_duration': '', |
| 502 | 'open_rate': 26, |
| 503 | 'close_rate': 134, |
| 504 | 'exit_type': 'sell_signal'} |
| 505 | ] |
| 506 | |
| 507 | trades_df = DataFrame(trades) |
| 508 | trades_df = edge._fill_calculable_fields(trades_df) |
| 509 | final = edge._process_expectancy(trades_df) |
| 510 | |
| 511 | assert 'TEST/BTC' in final |
| 512 | assert final['TEST/BTC'].stoploss == -0.9 |
| 513 | assert final['TEST/BTC'].nb_trades == len(trades_df) - 1 |
| 514 | assert round(final['TEST/BTC'].winrate, 10) == 0.0 |
| 515 | |
| 516 | |
| 517 | def test_process_expectancy_only_wins(mocker, edge_conf, fee,): |
| 518 | edge_conf['edge']['min_trade_number'] = 2 |
| 519 | freqtrade = get_patched_freqtradebot(mocker, edge_conf) |
| 520 | |
| 521 | freqtrade.exchange.get_fee = fee |
| 522 | edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy) |
| 523 | |
| 524 | trades = [ |
| 525 | {'pair': 'TEST/BTC', |
| 526 | 'stoploss': -0.9, |
| 527 | 'profit_percent': '', |
| 528 | 'profit_abs': '', |
| 529 | 'open_date': np.datetime64('2018-10-03T00:05:00.000000000'), |
| 530 | 'close_date': np.datetime64('2018-10-03T00:10:00.000000000'), |
| 531 | 'open_index': 1, |
| 532 | 'close_index': 1, |
| 533 | 'trade_duration': '', |
| 534 | 'open_rate': 15, |
| 535 | 'close_rate': 17, |
| 536 | 'exit_type': 'sell_signal'}, |
| 537 | {'pair': 'TEST/BTC', |
| 538 | 'stoploss': -0.9, |
| 539 | 'profit_percent': '', |
| 540 | 'profit_abs': '', |
| 541 | 'open_date': np.datetime64('2018-10-03T00:20:00.000000000'), |
| 542 | 'close_date': np.datetime64('2018-10-03T00:25:00.000000000'), |
| 543 | 'open_index': 4, |
| 544 | 'close_index': 4, |
| 545 | 'trade_duration': '', |
| 546 | 'open_rate': 10, |
| 547 | 'close_rate': 20, |
| 548 | 'exit_type': 'sell_signal'}, |
| 549 | {'pair': 'TEST/BTC', |
| 550 | 'stoploss': -0.9, |
| 551 | 'profit_percent': '', |
| 552 | 'profit_abs': '', |
| 553 | 'open_date': np.datetime64('2018-10-03T00:30:00.000000000'), |
| 554 | 'close_date': np.datetime64('2018-10-03T00:40:00.000000000'), |
| 555 | 'open_index': 6, |
| 556 | 'close_index': 7, |
| 557 | 'trade_duration': '', |
| 558 | 'open_rate': 26, |
| 559 | 'close_rate': 134, |
| 560 | 'exit_type': 'sell_signal'} |
| 561 | ] |
| 562 | |
| 563 | trades_df = DataFrame(trades) |
| 564 | trades_df = edge._fill_calculable_fields(trades_df) |
| 565 | final = edge._process_expectancy(trades_df) |
| 566 | |
| 567 | assert 'TEST/BTC' in final |
| 568 | assert final['TEST/BTC'].stoploss == -0.9 |
| 569 | assert final['TEST/BTC'].nb_trades == len(trades_df) |
| 570 | assert round(final['TEST/BTC'].winrate, 10) == 1.0 |
| 571 | assert round(final['TEST/BTC'].risk_reward_ratio, 10) == float('inf') |
| 572 | assert round(final['TEST/BTC'].expectancy, 10) == float('inf') |