adjust plot_dataframe to use btanalysis

This commit is contained in:
Matthias 2019-03-07 21:23:53 +01:00
parent e1f48c2b46
commit 9f7f089d8a
2 changed files with 16 additions and 34 deletions

View File

@ -8,6 +8,10 @@ import pandas as pd
from freqtrade.misc import json_load from freqtrade.misc import json_load
# must align with columns in backtest.py
BT_DATA_COLUMNS = ["pair", "profitperc", "open_time", "close_time", "index", "duration",
"open_rate", "close_rate", "open_at_end", "sell_reason"]
def load_backtest_data(filename) -> pd.DataFrame: def load_backtest_data(filename) -> pd.DataFrame:
""" """
@ -24,11 +28,7 @@ def load_backtest_data(filename) -> pd.DataFrame:
with filename.open() as file: with filename.open() as file:
data = json_load(file) data = json_load(file)
# must align with columns in backtest.py df = pd.DataFrame(data, columns=BT_DATA_COLUMNS)
columns = ["pair", "profitperc", "open_time", "close_time", "index", "duration",
"open_rate", "close_rate", "open_at_end", "sell_reason"]
df = pd.DataFrame(data, columns=columns)
df['open_time'] = pd.to_datetime(df['open_time'], df['open_time'] = pd.to_datetime(df['open_time'],
unit='s', unit='s',

View File

@ -41,6 +41,7 @@ from plotly.offline import plot
from freqtrade import persistence from freqtrade import persistence
from freqtrade.arguments import Arguments, TimeRange from freqtrade.arguments import Arguments, TimeRange
from freqtrade.data import history from freqtrade.data import history
from freqtrade.data.btanalysis import load_backtest_data, BT_DATA_COLUMNS
from freqtrade.exchange import Exchange from freqtrade.exchange import Exchange
from freqtrade.optimize.backtesting import setup_configuration from freqtrade.optimize.backtesting import setup_configuration
from freqtrade.persistence import Trade from freqtrade.persistence import Trade
@ -56,7 +57,8 @@ def load_trades(args: Namespace, pair: str, timerange: TimeRange) -> pd.DataFram
trades: pd.DataFrame = pd.DataFrame() trades: pd.DataFrame = pd.DataFrame()
if args.db_url: if args.db_url:
persistence.init(_CONF) persistence.init(_CONF)
columns = ["pair", "profit", "opents", "closets", "open_rate", "close_rate", "duration"] columns = ["pair", "profit", "open_time", "close_time",
"open_rate", "close_rate", "duration"]
for x in Trade.query.all(): for x in Trade.query.all():
print("date: {}".format(x.open_date)) print("date: {}".format(x.open_date))
@ -71,33 +73,13 @@ def load_trades(args: Namespace, pair: str, timerange: TimeRange) -> pd.DataFram
columns=columns) columns=columns)
elif args.exportfilename: elif args.exportfilename:
file = Path(args.exportfilename)
# must align with columns in backtest.py
columns = ["pair", "profit", "opents", "closets", "index", "duration",
"open_rate", "close_rate", "open_at_end", "sell_reason"]
if file.exists():
with file.open() as f:
data = json.load(f)
trades = pd.DataFrame(data, columns=columns)
trades = trades.loc[trades["pair"] == pair]
if timerange:
if timerange.starttype == 'date':
trades = trades.loc[trades["opents"] >= timerange.startts]
if timerange.stoptype == 'date':
trades = trades.loc[trades["opents"] <= timerange.stopts]
trades['opents'] = pd.to_datetime( file = Path(args.exportfilename)
trades['opents'], if file.exists():
unit='s', load_backtest_data(file)
utc=True,
infer_datetime_format=True)
trades['closets'] = pd.to_datetime(
trades['closets'],
unit='s',
utc=True,
infer_datetime_format=True)
else: else:
trades = pd.DataFrame([], columns=columns) trades = pd.DataFrame([], columns=BT_DATA_COLUMNS)
return trades return trades
@ -206,7 +188,7 @@ def extract_trades_of_period(dataframe, trades) -> pd.DataFrame:
Compare trades and backtested pair DataFrames to get trades performed on backtested period Compare trades and backtested pair DataFrames to get trades performed on backtested period
:return: the DataFrame of a trades of period :return: the DataFrame of a trades of period
""" """
trades = trades.loc[trades['opents'] >= dataframe.iloc[0]['date']] trades = trades.loc[trades['open_time'] >= dataframe.iloc[0]['date']]
return trades return trades
@ -279,7 +261,7 @@ def generate_graph(
) )
trade_buys = go.Scattergl( trade_buys = go.Scattergl(
x=trades["opents"], x=trades["open_time"],
y=trades["open_rate"], y=trades["open_rate"],
mode='markers', mode='markers',
name='trade_buy', name='trade_buy',
@ -291,7 +273,7 @@ def generate_graph(
) )
) )
trade_sells = go.Scattergl( trade_sells = go.Scattergl(
x=trades["closets"], x=trades["close_time"],
y=trades["close_rate"], y=trades["close_rate"],
mode='markers', mode='markers',
name='trade_sell', name='trade_sell',