2018-01-12 09:55:49 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import numpy as np
|
|
|
|
|
2018-01-28 09:51:26 +00:00
|
|
|
import plotly
|
|
|
|
from plotly import tools
|
|
|
|
from plotly.offline import plot
|
|
|
|
import plotly.graph_objs as go
|
|
|
|
|
2018-01-12 09:55:49 +00:00
|
|
|
import freqtrade.optimize as optimize
|
|
|
|
import freqtrade.misc as misc
|
|
|
|
import freqtrade.exchange as exchange
|
2018-01-23 05:17:54 +00:00
|
|
|
from freqtrade.strategy.strategy import Strategy
|
2018-01-12 09:55:49 +00:00
|
|
|
|
|
|
|
|
2018-01-21 12:44:30 +00:00
|
|
|
def plot_parse_args(args):
|
|
|
|
parser = misc.common_args_parser('Graph profits')
|
2018-01-12 18:18:31 +00:00
|
|
|
# FIX: perhaps delete those backtesting options that are not feasible (shows up in -h)
|
2018-01-12 09:55:49 +00:00
|
|
|
misc.backtesting_options(parser)
|
2018-01-21 12:44:30 +00:00
|
|
|
misc.scripts_options(parser)
|
2018-01-12 09:55:49 +00:00
|
|
|
return parser.parse_args(args)
|
|
|
|
|
|
|
|
|
2018-01-12 21:15:50 +00:00
|
|
|
# data:: [ pair, profit-%, enter, exit, time, duration]
|
|
|
|
# data:: ['BTC_XMR', 0.00537847, '1511176800', '1511178000', 5057, 1]
|
|
|
|
# FIX: make use of the enter/exit dates to insert the
|
|
|
|
# profit more precisely into the pg array
|
2018-01-12 18:18:31 +00:00
|
|
|
def make_profit_array(data, px, filter_pairs=[]):
|
|
|
|
pg = np.zeros(px)
|
2018-01-12 09:55:49 +00:00
|
|
|
# Go through the trades
|
|
|
|
# and make an total profit
|
|
|
|
# array
|
|
|
|
for trade in data:
|
|
|
|
pair = trade[0]
|
2018-01-12 18:18:31 +00:00
|
|
|
if filter_pairs and pair not in filter_pairs:
|
2018-01-12 09:55:49 +00:00
|
|
|
continue
|
|
|
|
profit = trade[1]
|
2018-01-12 21:15:50 +00:00
|
|
|
tim = trade[4]
|
|
|
|
dur = trade[5]
|
2018-01-20 18:49:04 +00:00
|
|
|
ix = tim + dur - 1
|
|
|
|
if ix < px:
|
|
|
|
pg[ix] += profit
|
2018-01-12 09:55:49 +00:00
|
|
|
|
|
|
|
# rewrite the pg array to go from
|
|
|
|
# total profits at each timeframe
|
|
|
|
# to accumulated profits
|
|
|
|
pa = 0
|
2018-01-23 05:20:17 +00:00
|
|
|
for x in range(0, len(pg)):
|
2018-01-12 09:55:49 +00:00
|
|
|
p = pg[x] # Get current total percent
|
|
|
|
pa += p # Add to the accumulated percent
|
|
|
|
pg[x] = pa # write back to save memory
|
|
|
|
|
|
|
|
return pg
|
|
|
|
|
|
|
|
|
|
|
|
def plot_profit(args) -> None:
|
|
|
|
"""
|
|
|
|
Plots the total profit for all pairs.
|
|
|
|
Note, the profit calculation isn't realistic.
|
|
|
|
But should be somewhat proportional, and therefor useful
|
|
|
|
in helping out to find a good algorithm.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# We need to use the same pairs, same tick_interval
|
|
|
|
# and same timeperiod as used in backtesting
|
|
|
|
# to match the tickerdata against the profits-results
|
|
|
|
|
2018-01-12 18:18:31 +00:00
|
|
|
filter_pairs = args.pair
|
2018-01-12 09:55:49 +00:00
|
|
|
|
|
|
|
config = misc.load_config(args.config)
|
2018-01-23 05:17:54 +00:00
|
|
|
config.update({'strategy': args.strategy})
|
|
|
|
|
|
|
|
# Init strategy
|
|
|
|
strategy = Strategy()
|
|
|
|
strategy.init(config)
|
|
|
|
|
2018-01-12 09:55:49 +00:00
|
|
|
pairs = config['exchange']['pair_whitelist']
|
2018-01-23 05:17:54 +00:00
|
|
|
|
2018-01-12 18:18:31 +00:00
|
|
|
if filter_pairs:
|
|
|
|
filter_pairs = filter_pairs.split(',')
|
|
|
|
pairs = list(set(pairs) & set(filter_pairs))
|
|
|
|
print('Filter, keep pairs %s' % pairs)
|
2018-01-12 09:55:49 +00:00
|
|
|
|
2018-01-20 18:49:04 +00:00
|
|
|
timerange = misc.parse_timerange(args.timerange)
|
2018-01-12 09:55:49 +00:00
|
|
|
tickers = optimize.load_data(args.datadir, pairs=pairs,
|
2018-01-23 05:17:54 +00:00
|
|
|
ticker_interval=strategy.ticker_interval,
|
2018-01-20 18:49:04 +00:00
|
|
|
refresh_pairs=False,
|
|
|
|
timerange=timerange)
|
2018-01-12 09:55:49 +00:00
|
|
|
dataframes = optimize.preprocess(tickers)
|
|
|
|
|
2018-01-21 12:44:30 +00:00
|
|
|
# NOTE: the dataframes are of unequal length,
|
|
|
|
# 'dates' is an merged date array of them all.
|
2018-01-12 09:55:49 +00:00
|
|
|
|
2018-01-21 12:44:30 +00:00
|
|
|
dates = misc.common_datearray(dataframes)
|
|
|
|
max_x = dates.size
|
2018-01-12 09:55:49 +00:00
|
|
|
|
2018-01-21 12:44:30 +00:00
|
|
|
# Make an average close price of all the pairs that was involved.
|
|
|
|
# this could be useful to gauge the overall market trend
|
2018-01-12 09:55:49 +00:00
|
|
|
# We are essentially saying:
|
|
|
|
# array <- sum dataframes[*]['close'] / num_items dataframes
|
|
|
|
# FIX: there should be some onliner numpy/panda for this
|
2018-01-12 18:18:31 +00:00
|
|
|
avgclose = np.zeros(max_x)
|
2018-01-12 09:55:49 +00:00
|
|
|
num = 0
|
|
|
|
for pair, pair_data in dataframes.items():
|
2018-01-12 18:18:31 +00:00
|
|
|
close = pair_data['close']
|
|
|
|
maxprice = max(close) # Normalize price to [0,1]
|
2018-01-23 05:20:17 +00:00
|
|
|
print('Pair %s has length %s' % (pair, len(close)))
|
2018-01-12 18:18:31 +00:00
|
|
|
for x in range(0, len(close)):
|
|
|
|
avgclose[x] += close[x] / maxprice
|
|
|
|
# avgclose += close
|
|
|
|
num += 1
|
2018-01-12 09:55:49 +00:00
|
|
|
avgclose /= num
|
|
|
|
|
|
|
|
# Load the profits results
|
|
|
|
# And make an profits-growth array
|
|
|
|
|
|
|
|
filename = 'backtest-result.json'
|
|
|
|
with open(filename) as file:
|
2018-01-23 05:20:17 +00:00
|
|
|
data = json.load(file)
|
2018-01-12 18:18:31 +00:00
|
|
|
pg = make_profit_array(data, max_x, filter_pairs)
|
2018-01-12 09:55:49 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Plot the pairs average close prices, and total profit growth
|
|
|
|
#
|
|
|
|
|
2018-01-28 09:51:26 +00:00
|
|
|
avgclose = go.Scattergl(
|
|
|
|
x=dates,
|
|
|
|
y=avgclose,
|
|
|
|
name='Avg close price',
|
|
|
|
)
|
|
|
|
profit = go.Scattergl(
|
|
|
|
x=dates,
|
|
|
|
y=pg,
|
|
|
|
name='Profit',
|
|
|
|
)
|
|
|
|
|
|
|
|
fig = tools.make_subplots(rows=3, cols=1, shared_xaxes=True, row_width=[1, 1, 1])
|
2018-01-21 12:44:30 +00:00
|
|
|
|
2018-01-28 09:51:26 +00:00
|
|
|
fig.append_trace(avgclose, 1, 1)
|
|
|
|
fig.append_trace(profit, 2, 1)
|
2018-01-12 09:55:49 +00:00
|
|
|
|
|
|
|
for pair in pairs:
|
2018-01-12 18:18:31 +00:00
|
|
|
pg = make_profit_array(data, max_x, pair)
|
2018-01-28 09:51:26 +00:00
|
|
|
pair_profit = go.Scattergl(
|
|
|
|
x=dates,
|
|
|
|
y=pg,
|
|
|
|
name=pair,
|
|
|
|
)
|
|
|
|
fig.append_trace(pair_profit, 3, 1)
|
|
|
|
|
|
|
|
plot(fig, filename='freqtrade-profit-plot.html')
|
2018-01-12 09:55:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
args = plot_parse_args(sys.argv[1:])
|
|
|
|
plot_profit(args)
|