stable/scripts/plot_dataframe.py

101 lines
3.4 KiB
Python
Raw Normal View History

2017-11-09 21:29:23 +00:00
#!/usr/bin/env python3
2018-01-06 07:38:47 +00:00
import sys
import logging
2018-01-06 07:38:47 +00:00
import argparse
import matplotlib
# matplotlib.use("Qt5Agg")
import matplotlib.dates as mdates
2017-11-09 21:29:23 +00:00
import matplotlib.pyplot as plt
from pandas import DataFrame
import talib.abstract as ta
import freqtrade.vendor.qtpylib.indicators as qtpylib
2017-11-09 21:29:23 +00:00
from freqtrade import exchange, analyze
2018-01-09 11:06:28 +00:00
from freqtrade.misc import common_args_parser
from freqtrade.strategy.strategy import Strategy
import freqtrade.misc as misc
import freqtrade.optimize as optimize
import freqtrade.analyze as analyze
2017-11-09 21:29:23 +00:00
logger = logging.getLogger(__name__)
2017-11-09 21:29:23 +00:00
def plot_parse_args(args):
parser = misc.common_args_parser('Graph dataframe')
misc.backtesting_options(parser)
misc.scripts_options(parser)
2018-01-06 07:38:47 +00:00
return parser.parse_args(args)
def plot_analyzed_dataframe(args) -> None:
2017-11-09 21:29:23 +00:00
"""
Calls analyze() and plots the returned dataframe
:param pair: pair as str
:return: None
"""
2018-01-06 07:38:47 +00:00
pair = args.pair
pairs = [pair]
timerange = misc.parse_timerange(args.timerange)
# Init strategy
strategy = Strategy()
strategy.init({'strategy': args.strategy})
tick_interval = strategy.ticker_interval
tickers = {}
if args.live:
logger.info('Downloading pair.')
# Init Bittrex to use public API
exchange._API = exchange.Bittrex({'key': '', 'secret': ''})
tickers[pair] = exchange.get_ticker_history(pair, tick_interval)
else:
tickers = optimize.load_data(args.datadir, pairs=pairs,
ticker_interval=tick_interval,
refresh_pairs=False,
timerange=timerange)
dataframes = optimize.tickerdata_to_dataframe(tickers)
dataframe = dataframes[pair]
dataframe = analyze.populate_buy_trend(dataframe)
dataframe = analyze.populate_sell_trend(dataframe)
dates = misc.datesarray_to_datetimearray(dataframe['date'])
2017-11-09 21:29:23 +00:00
# Two subplots sharing x axis
fig, (ax1, ax2, ax3) = plt.subplots(3, sharex=True)
fig.suptitle(pair + " " + str(tick_interval), fontsize=14, fontweight='bold')
ax1.plot(dates, dataframe['close'], label='close')
# ax1.plot(dates, dataframe['sell'], 'ro', label='sell')
ax1.plot(dates, dataframe['sma'], '--', label='SMA')
ax1.plot(dates, dataframe['tema'], ':', label='TEMA')
ax1.plot(dates, dataframe['blower'], '-.', label='BB low')
ax1.plot(dates, dataframe['close'] * dataframe['buy'], 'bo', label='buy')
ax1.plot(dates, dataframe['close'] * dataframe['sell'], 'ro', label='sell')
2017-11-09 21:29:23 +00:00
ax1.legend()
ax2.plot(dates, dataframe['adx'], label='ADX')
ax2.plot(dates, dataframe['mfi'], label='MFI')
# ax2.plot(dates, [25] * len(dataframe.index.values))
2017-11-09 21:29:23 +00:00
ax2.legend()
ax3.plot(dates, dataframe['fastk'], label='k')
ax3.plot(dates, dataframe['fastd'], label='d')
ax3.plot(dates, [20] * len(dataframe.index.values))
2017-11-09 21:29:23 +00:00
ax3.legend()
xfmt = mdates.DateFormatter('%d-%m-%y %H:%M') # Dont let matplotlib autoformat date
ax3.xaxis.set_major_formatter(xfmt)
2017-11-09 21:29:23 +00:00
# Fine-tune figure; make subplots close to each other and hide x ticks for
# all but bottom plot.
fig.subplots_adjust(hspace=0)
fig.autofmt_xdate() # Rotate the dates
2017-11-09 21:29:23 +00:00
plt.setp([a.get_xticklabels() for a in fig.axes[:-1]], visible=False)
plt.show()
if __name__ == '__main__':
2018-01-06 07:38:47 +00:00
args = plot_parse_args(sys.argv[1:])
plot_analyzed_dataframe(args)