Move extract_trades_of_period to btanlaysis

This commit is contained in:
Matthias 2019-06-16 11:12:19 +02:00
parent 1c53aa5687
commit 1cd8415723
3 changed files with 59 additions and 18 deletions

View File

@ -108,3 +108,13 @@ def load_trades(db_url: str = None, exportfilename: str = None) -> pd.DataFrame:
trades = load_backtest_data(file)
return trades
def extract_trades_of_period(dataframe: pd.DataFrame, trades: pd.DataFrame) -> pd.DataFrame:
"""
Compare trades and backtested pair DataFrames to get trades performed on backtested period
:return: the DataFrame of a trades of period
"""
trades = trades.loc[(trades['open_time'] >= dataframe.iloc[0]['date']) &
(trades['close_time'] <= dataframe.iloc[-1]['date'])]
return trades

View File

@ -1,11 +1,18 @@
import pytest
from unittest.mock import MagicMock
from pandas import DataFrame
from freqtrade.data.btanalysis import BT_DATA_COLUMNS, load_backtest_data, load_trades
from freqtrade.data.history import make_testdata_path
from freqtrade.persistence import init, Trade
from freqtrade.tests.test_persistence import init_persistence, create_mock_trades
from arrow import Arrow
import pytest
from pandas import DataFrame, to_datetime
from freqtrade.arguments import Arguments, TimeRange
from freqtrade.data.btanalysis import (BT_DATA_COLUMNS,
extract_trades_of_period,
load_backtest_data, load_trades)
from freqtrade.data.history import load_pair_history, make_testdata_path
from freqtrade.persistence import Trade, init
from freqtrade.strategy.interface import SellType
from freqtrade.tests.test_persistence import (create_mock_trades,
init_persistence)
def test_load_backtest_data():
@ -45,3 +52,37 @@ def test_load_trades_db(default_conf, fee, mocker):
assert isinstance(trades, DataFrame)
assert "pair" in trades.columns
assert "open_time" in trades.columns
def test_extract_trades_of_period():
pair = "UNITTEST/BTC"
timerange = TimeRange(None, 'line', 0, -1000)
data = load_pair_history(pair=pair, ticker_interval='1m',
datadir=None, timerange=timerange)
# timerange = 2017-11-14 06:07 - 2017-11-14 22:58:00
trades = DataFrame(
{'pair': [pair, pair, pair, pair],
'profit_percent': [0.0, 0.1, -0.2, -0.5],
'profit_abs': [0.0, 1, -2, -5],
'open_time': to_datetime([Arrow(2017, 11, 13, 15, 40, 0).datetime,
Arrow(2017, 11, 14, 9, 41, 0).datetime,
Arrow(2017, 11, 14, 14, 20, 0).datetime,
Arrow(2017, 11, 15, 3, 40, 0).datetime,
], utc=True
),
'close_time': to_datetime([Arrow(2017, 11, 13, 16, 40, 0).datetime,
Arrow(2017, 11, 14, 10, 41, 0).datetime,
Arrow(2017, 11, 14, 15, 25, 0).datetime,
Arrow(2017, 11, 15, 3, 55, 0).datetime,
], utc=True)
})
trades1 = extract_trades_of_period(data, trades)
# First and last trade are dropped as they are out of range
assert len(trades1) == 2
assert trades1.iloc[0].open_time == Arrow(2017, 11, 14, 9, 41, 0).datetime
assert trades1.iloc[0].close_time == Arrow(2017, 11, 14, 10, 41, 0).datetime
assert trades1.iloc[-1].open_time == Arrow(2017, 11, 14, 14, 20, 0).datetime
assert trades1.iloc[-1].close_time == Arrow(2017, 11, 14, 15, 25, 0).datetime

View File

@ -35,9 +35,10 @@ import pandas as pd
from freqtrade.arguments import Arguments, TimeRange
from freqtrade.data import history
from freqtrade.data.btanalysis import load_trades
from freqtrade.plot.plotting import generate_graph, generate_plot_file
from freqtrade.exchange import Exchange
from freqtrade.optimize import setup_configuration
from freqtrade.plot.plotting import (extract_trades_of_period, generate_graph,
generate_plot_file)
from freqtrade.resolvers import StrategyResolver
from freqtrade.state import RunMode
@ -119,17 +120,6 @@ def generate_dataframe(strategy, tickers, pair) -> pd.DataFrame:
return dataframe
def extract_trades_of_period(dataframe, trades) -> pd.DataFrame:
"""
Compare trades and backtested pair DataFrames to get trades performed on backtested period
:return: the DataFrame of a trades of period
"""
# TODO: Document and move to btanalysis (?)
trades = trades.loc[(trades['open_time'] >= dataframe.iloc[0]['date']) &
(trades['close_time'] <= dataframe.iloc[-1]['date'])]
return trades
def analyse_and_plot_pairs(args: Namespace):
"""
From arguments provided in cli: