diff --git a/scripts/plot_dataframe.py b/scripts/plot_dataframe.py index e62465bf7..e7a52b2bf 100755 --- a/scripts/plot_dataframe.py +++ b/scripts/plot_dataframe.py @@ -1,26 +1,33 @@ #!/usr/bin/env python3 +""" +Script to display when the bot will buy a specific pair + +Mandatory Cli parameters: +-p / --pair: pair to examine + +Optional Cli parameters +-s / --strategy: strategy to use +-d / --datadir: path to pair backtest data +--timerange: specify what timerange of data to use. +-l / --live: Live, to download the latest ticker for the pair +""" import sys -import logging + +from typing import Dict from plotly import tools from plotly.offline import plot import plotly.graph_objs as go -from freqtrade import exchange, analyze -from freqtrade.strategy.strategy import Strategy -import freqtrade.misc as misc +from freqtrade.arguments import Arguments +from freqtrade.analyze import Analyze +from freqtrade import exchange +from freqtrade.logger import Logger import freqtrade.optimize as optimize -logger = logging.getLogger(__name__) - - -def plot_parse_args(args): - parser = misc.common_args_parser('Graph dataframe') - misc.backtesting_options(parser) - misc.scripts_options(parser) - return parser.parse_args(args) +logger = Logger(name="Graph dataframe").get_logger() def plot_analyzed_dataframe(args) -> None: @@ -30,12 +37,19 @@ def plot_analyzed_dataframe(args) -> None: :return: None """ pair = args.pair.replace('-', '_') - timerange = misc.parse_timerange(args.timerange) + timerange = Arguments.parse_timerange(args.timerange) # Init strategy - strategy = Strategy() - strategy.init({'strategy': args.strategy}) - tick_interval = strategy.ticker_interval + try: + analyze = Analyze({'strategy': args.strategy}) + except AttributeError: + logger.critical( + 'Impossible to load the strategy. Please check the file "user_data/strategies/%s.py"', + args.strategy + ) + exit() + + tick_interval = analyze.strategy.ticker_interval tickers = {} if args.live: @@ -44,27 +58,32 @@ def plot_analyzed_dataframe(args) -> None: exchange._API = exchange.Bittrex({'key': '', 'secret': ''}) tickers[pair] = exchange.get_ticker_history(pair, tick_interval) else: - tickers = optimize.load_data(args.datadir, pairs=[pair], - ticker_interval=tick_interval, - refresh_pairs=False, - timerange=timerange) - dataframes = optimize.tickerdata_to_dataframe(tickers) + tickers = optimize.load_data( + datadir=args.datadir, + pairs=[pair], + ticker_interval=tick_interval, + refresh_pairs=False, + timerange=timerange + ) + dataframes = analyze.tickerdata_to_dataframe(tickers) dataframe = dataframes[pair] dataframe = analyze.populate_buy_trend(dataframe) dataframe = analyze.populate_sell_trend(dataframe) - if (len(dataframe.index) > 750): - logger.warn('Ticker contained more than 750 candles, clipping.') - df = dataframe.tail(750) + if len(dataframe.index) > 750: + logger.warning('Ticker contained more than 750 candles, clipping.') + data = dataframe.tail(750) - candles = go.Candlestick(x=df.date, - open=df.open, - high=df.high, - low=df.low, - close=df.close, - name='Price') + candles = go.Candlestick( + x=data.date, + open=data.open, + high=data.high, + low=data.low, + close=data.close, + name='Price' + ) - df_buy = df[df['buy'] == 1] + df_buy = data[data['buy'] == 1] buys = go.Scattergl( x=df_buy.date, y=df_buy.close, @@ -73,13 +92,11 @@ def plot_analyzed_dataframe(args) -> None: marker=dict( symbol='triangle-up-dot', size=9, - line=dict( - width=1, - ), + line=dict(width=1), color='green', ) ) - df_sell = df[df['sell'] == 1] + df_sell = data[data['sell'] == 1] sells = go.Scattergl( x=df_sell.date, y=df_sell.close, @@ -88,30 +105,28 @@ def plot_analyzed_dataframe(args) -> None: marker=dict( symbol='triangle-down-dot', size=9, - line=dict( - width=1, - ), + line=dict(width=1), color='red', ) ) bb_lower = go.Scatter( - x=df.date, - y=df.bb_lowerband, + x=data.date, + y=data.bb_lowerband, name='BB lower', line={'color': "transparent"}, ) bb_upper = go.Scatter( - x=df.date, - y=df.bb_upperband, + x=data.date, + y=data.bb_upperband, name='BB upper', fill="tonexty", fillcolor="rgba(0,176,246,0.2)", line={'color': "transparent"}, ) - macd = go.Scattergl(x=df['date'], y=df['macd'], name='MACD') - macdsignal = go.Scattergl(x=df['date'], y=df['macdsignal'], name='MACD signal') - volume = go.Bar(x=df['date'], y=df['volume'], name='Volume') + macd = go.Scattergl(x=data['date'], y=data['macd'], name='MACD') + macdsignal = go.Scattergl(x=data['date'], y=data['macdsignal'], name='MACD signal') + volume = go.Bar(x=data['date'], y=data['volume'], name='Volume') fig = tools.make_subplots( rows=3, @@ -138,6 +153,31 @@ def plot_analyzed_dataframe(args) -> None: plot(fig, filename='freqtrade-plot.html') +def plot_parse_args(args): + """ + Parse args passed to the script + :param args: Cli arguments + :return: args: Array with all arguments + """ + arguments = Arguments(args, 'Graph dataframe') + arguments.scripts_options() + arguments.common_args_parser() + arguments.optimizer_shared_options(arguments.parser) + arguments.backtesting_options(arguments.parser) + + return arguments.parse_args() + + +def main(sysargv: Dict) -> None: + """ + This function will initiate the bot and start the trading loop. + :return: None + """ + logger.info('Starting Plot Dataframe') + plot_analyzed_dataframe( + plot_parse_args(sysargv) + ) + + if __name__ == '__main__': - args = plot_parse_args(sys.argv[1:]) - plot_analyzed_dataframe(args) + main(sys.argv[1:])