Skip to content

Commit

Permalink
Merge pull request freqtrade#1040 from freqtrade/xmatthias_backtest_d…
Browse files Browse the repository at this point in the history
…uration

Fix backtest duration calculation
  • Loading branch information
vertti authored Jul 19, 2018
2 parents 64f9334 + aa69177 commit 6070d81
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
16 changes: 10 additions & 6 deletions freqtrade/optimize/backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
import operator
from argparse import Namespace
from datetime import datetime
from datetime import datetime, timedelta
from typing import Any, Dict, List, NamedTuple, Optional, Tuple

import arrow
Expand Down Expand Up @@ -88,7 +88,7 @@ def _generate_text_table(self, data: Dict[str, Dict], results: DataFrame) -> str
"""
stake_currency = str(self.config.get('stake_currency'))

floatfmt = ('s', 'd', '.2f', '.2f', '.8f', '.1f')
floatfmt = ('s', 'd', '.2f', '.2f', '.8f', 'd', '.1f', '.1f')
tabular_data = []
headers = ['pair', 'buy count', 'avg profit %', 'cum profit %',
'total profit ' + stake_currency, 'avg duration', 'profit', 'loss']
Expand All @@ -100,7 +100,8 @@ def _generate_text_table(self, data: Dict[str, Dict], results: DataFrame) -> str
result.profit_percent.mean() * 100.0,
result.profit_percent.sum() * 100.0,
result.profit_abs.sum(),
result.trade_duration.mean(),
str(timedelta(
minutes=round(result.trade_duration.mean()))) if not result.empty else '0:00',
len(result[result.profit_abs > 0]),
len(result[result.profit_abs < 0])
])
Expand All @@ -112,7 +113,8 @@ def _generate_text_table(self, data: Dict[str, Dict], results: DataFrame) -> str
results.profit_percent.mean() * 100.0,
results.profit_percent.sum() * 100.0,
results.profit_abs.sum(),
results.trade_duration.mean(),
str(timedelta(
minutes=round(results.trade_duration.mean()))) if not results.empty else '0:00',
len(results[results.profit_abs > 0]),
len(results[results.profit_abs < 0])
])
Expand Down Expand Up @@ -159,7 +161,8 @@ def _get_sell_trade_entry(
profit_abs=trade.calc_profit(rate=sell_row.open),
open_time=buy_row.date,
close_time=sell_row.date,
trade_duration=(sell_row.date - buy_row.date).seconds // 60,
trade_duration=int((
sell_row.date - buy_row.date).total_seconds() // 60),
open_index=buy_row.Index,
close_index=sell_row.Index,
open_at_end=False,
Expand All @@ -174,7 +177,8 @@ def _get_sell_trade_entry(
profit_abs=trade.calc_profit(rate=sell_row.open),
open_time=buy_row.date,
close_time=sell_row.date,
trade_duration=(sell_row.date - buy_row.date).seconds // 60,
trade_duration=int((
sell_row.date - buy_row.date).total_seconds() // 60),
open_index=buy_row.Index,
close_index=sell_row.Index,
open_at_end=True,
Expand Down
9 changes: 4 additions & 5 deletions freqtrade/tests/optimize/test_backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,15 +391,14 @@ def test_generate_text_table(default_conf, mocker):

result_str = (
'| pair | buy count | avg profit % | cum profit % | '
'total profit BTC | avg duration | profit | loss |\n'
'total profit BTC | avg duration | profit | loss |\n'
'|:--------|------------:|---------------:|---------------:|'
'-------------------:|---------------:|---------:|-------:|\n'
'-------------------:|:---------------|---------:|-------:|\n'
'| ETH/BTC | 2 | 15.00 | 30.00 | '
'0.60000000 | 20.0 | 2 | 0 |\n'
'0.60000000 | 0:20:00 | 2 | 0 |\n'
'| TOTAL | 2 | 15.00 | 30.00 | '
'0.60000000 | 20.0 | 2 | 0 |'
'0.60000000 | 0:20:00 | 2 | 0 |'
)
print(result_str)
assert backtesting._generate_text_table(data={'ETH/BTC': {}}, results=results) == result_str


Expand Down

0 comments on commit 6070d81

Please sign in to comment.