sanity checks before plotting, cleanup
This commit is contained in:
parent
6df0b39f81
commit
e0a1e5417f
@ -25,8 +25,7 @@ def generate_row(fig, row, indicators: List[str], data: pd.DataFrame) -> tools.m
|
|||||||
"""
|
"""
|
||||||
for indicator in indicators:
|
for indicator in indicators:
|
||||||
if indicator in data:
|
if indicator in data:
|
||||||
# TODO: Replace all Scatter with Scattergl for performance!!
|
scattergl = go.Scattergl(
|
||||||
scattergl = go.Scatter(
|
|
||||||
x=data['date'],
|
x=data['date'],
|
||||||
y=data[indicator],
|
y=data[indicator],
|
||||||
mode='lines',
|
mode='lines',
|
||||||
@ -48,7 +47,7 @@ def plot_trades(fig, trades: pd.DataFrame):
|
|||||||
Plot trades to "fig"
|
Plot trades to "fig"
|
||||||
"""
|
"""
|
||||||
# Trades can be empty
|
# Trades can be empty
|
||||||
if trades is not None:
|
if trades is not None and len(trades) > 0:
|
||||||
trade_buys = go.Scatter(
|
trade_buys = go.Scatter(
|
||||||
x=trades["open_time"],
|
x=trades["open_time"],
|
||||||
y=trades["open_rate"],
|
y=trades["open_rate"],
|
||||||
@ -123,44 +122,50 @@ def generate_graph(
|
|||||||
|
|
||||||
if 'buy' in data.columns:
|
if 'buy' in data.columns:
|
||||||
df_buy = data[data['buy'] == 1]
|
df_buy = data[data['buy'] == 1]
|
||||||
buys = go.Scatter(
|
if len(df_buy) > 0:
|
||||||
x=df_buy.date,
|
buys = go.Scattergl(
|
||||||
y=df_buy.close,
|
x=df_buy.date,
|
||||||
mode='markers',
|
y=df_buy.close,
|
||||||
name='buy',
|
mode='markers',
|
||||||
marker=dict(
|
name='buy',
|
||||||
symbol='triangle-up-dot',
|
marker=dict(
|
||||||
size=9,
|
symbol='triangle-up-dot',
|
||||||
line=dict(width=1),
|
size=9,
|
||||||
color='green',
|
line=dict(width=1),
|
||||||
|
color='green',
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
fig.append_trace(buys, 1, 1)
|
||||||
fig.append_trace(buys, 1, 1)
|
else:
|
||||||
|
logger.warning("No buy-signals found.")
|
||||||
|
|
||||||
if 'sell' in data.columns:
|
if 'sell' in data.columns:
|
||||||
df_sell = data[data['sell'] == 1]
|
df_sell = data[data['sell'] == 1]
|
||||||
sells = go.Scatter(
|
if len(df_sell) > 0:
|
||||||
x=df_sell.date,
|
sells = go.Scattergl(
|
||||||
y=df_sell.close,
|
x=df_sell.date,
|
||||||
mode='markers',
|
y=df_sell.close,
|
||||||
name='sell',
|
mode='markers',
|
||||||
marker=dict(
|
name='sell',
|
||||||
symbol='triangle-down-dot',
|
marker=dict(
|
||||||
size=9,
|
symbol='triangle-down-dot',
|
||||||
line=dict(width=1),
|
size=9,
|
||||||
color='red',
|
line=dict(width=1),
|
||||||
|
color='red',
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
fig.append_trace(sells, 1, 1)
|
||||||
fig.append_trace(sells, 1, 1)
|
else:
|
||||||
|
logger.warning("No sell-signals found.")
|
||||||
|
|
||||||
if 'bb_lowerband' in data and 'bb_upperband' in data:
|
if 'bb_lowerband' in data and 'bb_upperband' in data:
|
||||||
bb_lower = go.Scatter(
|
bb_lower = go.Scattergl(
|
||||||
x=data.date,
|
x=data.date,
|
||||||
y=data.bb_lowerband,
|
y=data.bb_lowerband,
|
||||||
name='BB lower',
|
name='BB lower',
|
||||||
line={'color': 'rgba(255,255,255,0)'},
|
line={'color': 'rgba(255,255,255,0)'},
|
||||||
)
|
)
|
||||||
bb_upper = go.Scatter(
|
bb_upper = go.Scattergl(
|
||||||
x=data.date,
|
x=data.date,
|
||||||
y=data.bb_upperband,
|
y=data.bb_upperband,
|
||||||
name='BB upper',
|
name='BB upper',
|
||||||
|
@ -51,11 +51,18 @@ _CONF: Dict[str, Any] = {}
|
|||||||
timeZone = pytz.UTC
|
timeZone = pytz.UTC
|
||||||
|
|
||||||
|
|
||||||
def load_trades(args: Namespace, pair: str) -> pd.DataFrame:
|
def load_trades(db_url: str = None, exportfilename: str = None) -> pd.DataFrame:
|
||||||
trades: pd.DataFrame = pd.DataFrame()
|
"""
|
||||||
if args.db_url:
|
Load trades, either from a DB (using dburl) or via a backtest export file.
|
||||||
persistence.init(args.db_url, clean_open_orders=False)
|
:param db_url: Sqlite url (default format sqlite:///tradesv3.dry-run.sqlite)
|
||||||
|
:param exportfilename: Path to a file exported from backtesting
|
||||||
|
:returns: Dataframe containing Trades
|
||||||
|
"""
|
||||||
|
# TODO: Document and move to btanalysis
|
||||||
|
trades: pd.DataFrame = pd.DataFrame([], columns=BT_DATA_COLUMNS)
|
||||||
|
|
||||||
|
if db_url:
|
||||||
|
persistence.init(db_url, clean_open_orders=False)
|
||||||
columns = ["pair", "profit", "open_time", "close_time",
|
columns = ["pair", "profit", "open_time", "close_time",
|
||||||
"open_rate", "close_rate", "duration"]
|
"open_rate", "close_rate", "duration"]
|
||||||
|
|
||||||
@ -68,18 +75,15 @@ def load_trades(args: Namespace, pair: str) -> pd.DataFrame:
|
|||||||
t.open_rate, t.close_rate,
|
t.open_rate, t.close_rate,
|
||||||
t.close_date.timestamp() - t.open_date.timestamp()
|
t.close_date.timestamp() - t.open_date.timestamp()
|
||||||
if t.close_date else None)
|
if t.close_date else None)
|
||||||
for t in Trade.query.filter(Trade.pair.is_(pair)).all()],
|
for t in Trade.query.all()],
|
||||||
columns=columns)
|
columns=columns)
|
||||||
|
|
||||||
elif args.exportfilename:
|
elif exportfilename:
|
||||||
|
|
||||||
file = Path(args.exportfilename)
|
file = Path(exportfilename)
|
||||||
if file.exists():
|
if file.exists():
|
||||||
trades = load_backtest_data(file)
|
trades = load_backtest_data(file)
|
||||||
|
|
||||||
else:
|
|
||||||
trades = pd.DataFrame([], columns=BT_DATA_COLUMNS)
|
|
||||||
|
|
||||||
return trades
|
return trades
|
||||||
|
|
||||||
|
|
||||||
@ -181,6 +185,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
|
||||||
"""
|
"""
|
||||||
|
# TODO: Document and move to btanalysis (?)
|
||||||
trades = trades.loc[(trades['open_time'] >= dataframe.iloc[0]['date']) &
|
trades = trades.loc[(trades['open_time'] >= dataframe.iloc[0]['date']) &
|
||||||
(trades['close_time'] <= dataframe.iloc[-1]['date'])]
|
(trades['close_time'] <= dataframe.iloc[-1]['date'])]
|
||||||
return trades
|
return trades
|
||||||
@ -211,7 +216,9 @@ def analyse_and_plot_pairs(args: Namespace):
|
|||||||
tickers[pair] = data
|
tickers[pair] = data
|
||||||
dataframe = generate_dataframe(strategy, tickers, pair)
|
dataframe = generate_dataframe(strategy, tickers, pair)
|
||||||
|
|
||||||
trades = load_trades(args, pair)
|
trades = load_trades(pair, db_url=args.db_url,
|
||||||
|
exportfilename=args.exportfilename)
|
||||||
|
trades = trades.loc[trades['pair'] == pair]
|
||||||
trades = extract_trades_of_period(dataframe, trades)
|
trades = extract_trades_of_period(dataframe, trades)
|
||||||
|
|
||||||
fig = generate_graph(
|
fig = generate_graph(
|
||||||
|
Loading…
Reference in New Issue
Block a user