Plot_dataframe.py: make it works with the new object model

This commit is contained in:
Gerald Lonlas 2018-03-04 17:52:58 -08:00
parent d685646446
commit 9ae2491b1e

View File

@ -1,26 +1,33 @@
#!/usr/bin/env python3 #!/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 sys
import logging
from typing import Dict
from plotly import tools from plotly import tools
from plotly.offline import plot from plotly.offline import plot
import plotly.graph_objs as go import plotly.graph_objs as go
from freqtrade import exchange, analyze from freqtrade.arguments import Arguments
from freqtrade.strategy.strategy import Strategy from freqtrade.analyze import Analyze
import freqtrade.misc as misc from freqtrade import exchange
from freqtrade.logger import Logger
import freqtrade.optimize as optimize import freqtrade.optimize as optimize
logger = logging.getLogger(__name__) logger = Logger(name="Graph dataframe").get_logger()
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)
def plot_analyzed_dataframe(args) -> None: def plot_analyzed_dataframe(args) -> None:
@ -30,12 +37,19 @@ def plot_analyzed_dataframe(args) -> None:
:return: None :return: None
""" """
pair = args.pair.replace('-', '_') pair = args.pair.replace('-', '_')
timerange = misc.parse_timerange(args.timerange) timerange = Arguments.parse_timerange(args.timerange)
# Init strategy # Init strategy
strategy = Strategy() try:
strategy.init({'strategy': args.strategy}) analyze = Analyze({'strategy': args.strategy})
tick_interval = strategy.ticker_interval 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 = {} tickers = {}
if args.live: if args.live:
@ -44,27 +58,32 @@ def plot_analyzed_dataframe(args) -> None:
exchange._API = exchange.Bittrex({'key': '', 'secret': ''}) exchange._API = exchange.Bittrex({'key': '', 'secret': ''})
tickers[pair] = exchange.get_ticker_history(pair, tick_interval) tickers[pair] = exchange.get_ticker_history(pair, tick_interval)
else: else:
tickers = optimize.load_data(args.datadir, pairs=[pair], tickers = optimize.load_data(
ticker_interval=tick_interval, datadir=args.datadir,
refresh_pairs=False, pairs=[pair],
timerange=timerange) ticker_interval=tick_interval,
dataframes = optimize.tickerdata_to_dataframe(tickers) refresh_pairs=False,
timerange=timerange
)
dataframes = analyze.tickerdata_to_dataframe(tickers)
dataframe = dataframes[pair] dataframe = dataframes[pair]
dataframe = analyze.populate_buy_trend(dataframe) dataframe = analyze.populate_buy_trend(dataframe)
dataframe = analyze.populate_sell_trend(dataframe) dataframe = analyze.populate_sell_trend(dataframe)
if (len(dataframe.index) > 750): if len(dataframe.index) > 750:
logger.warn('Ticker contained more than 750 candles, clipping.') logger.warning('Ticker contained more than 750 candles, clipping.')
df = dataframe.tail(750) data = dataframe.tail(750)
candles = go.Candlestick(x=df.date, candles = go.Candlestick(
open=df.open, x=data.date,
high=df.high, open=data.open,
low=df.low, high=data.high,
close=df.close, low=data.low,
name='Price') close=data.close,
name='Price'
)
df_buy = df[df['buy'] == 1] df_buy = data[data['buy'] == 1]
buys = go.Scattergl( buys = go.Scattergl(
x=df_buy.date, x=df_buy.date,
y=df_buy.close, y=df_buy.close,
@ -73,13 +92,11 @@ def plot_analyzed_dataframe(args) -> None:
marker=dict( marker=dict(
symbol='triangle-up-dot', symbol='triangle-up-dot',
size=9, size=9,
line=dict( line=dict(width=1),
width=1,
),
color='green', color='green',
) )
) )
df_sell = df[df['sell'] == 1] df_sell = data[data['sell'] == 1]
sells = go.Scattergl( sells = go.Scattergl(
x=df_sell.date, x=df_sell.date,
y=df_sell.close, y=df_sell.close,
@ -88,30 +105,28 @@ def plot_analyzed_dataframe(args) -> None:
marker=dict( marker=dict(
symbol='triangle-down-dot', symbol='triangle-down-dot',
size=9, size=9,
line=dict( line=dict(width=1),
width=1,
),
color='red', color='red',
) )
) )
bb_lower = go.Scatter( bb_lower = go.Scatter(
x=df.date, x=data.date,
y=df.bb_lowerband, y=data.bb_lowerband,
name='BB lower', name='BB lower',
line={'color': "transparent"}, line={'color': "transparent"},
) )
bb_upper = go.Scatter( bb_upper = go.Scatter(
x=df.date, x=data.date,
y=df.bb_upperband, y=data.bb_upperband,
name='BB upper', name='BB upper',
fill="tonexty", fill="tonexty",
fillcolor="rgba(0,176,246,0.2)", fillcolor="rgba(0,176,246,0.2)",
line={'color': "transparent"}, line={'color': "transparent"},
) )
macd = go.Scattergl(x=df['date'], y=df['macd'], name='MACD') macd = go.Scattergl(x=data['date'], y=data['macd'], name='MACD')
macdsignal = go.Scattergl(x=df['date'], y=df['macdsignal'], name='MACD signal') macdsignal = go.Scattergl(x=data['date'], y=data['macdsignal'], name='MACD signal')
volume = go.Bar(x=df['date'], y=df['volume'], name='Volume') volume = go.Bar(x=data['date'], y=data['volume'], name='Volume')
fig = tools.make_subplots( fig = tools.make_subplots(
rows=3, rows=3,
@ -138,6 +153,31 @@ def plot_analyzed_dataframe(args) -> None:
plot(fig, filename='freqtrade-plot.html') 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__': if __name__ == '__main__':
args = plot_parse_args(sys.argv[1:]) main(sys.argv[1:])
plot_analyzed_dataframe(args)