Add the following code cell to this notebook after the backtest is run:
from tradeexecutor.visual.benchmark import visualise_long_short_benchmark
from tradeexecutor.visual.equity_curve import calculate_long_compounding_realised_trading_profitability, calculate_compounding_realised_trading_profitability
from plotly import graph_objects as go
import pandas as pd
compounding = calculate_compounding_realised_trading_profitability(state)
initial_cash = 10_000
series2 = []
for p in state.stats.portfolio:
pnl = (p.total_equity-initial_cash)/initial_cash
series2.append((p.calculated_at, pnl))
times, values = zip(*series2)
equity = pd.Series(values, index=times)
trace1 = go.Scatter(
x=compounding.index,
y=compounding.values,
mode='lines'
)
trace2 = go.Scatter(
x=equity.index,
y=equity.values,
mode='lines'
)
fig = go.Figure()
fig.add_trace(trace1)
fig.add_trace(trace2)
fig.show()
Run the notebook and notice how the two curves don't match as they should
Now go to the strategy and make sure it can only have 1 open position at a time. By changing to these lines:
signal_locked = False
has_been_opened = False
def decide_trades:
...
global signal_locked
global has_been_opened
if signal_locked == True and position_manager.is_any_open():
has_been_opened = True
if has_been_opened == True and not position_manager.is_any_open():
signal_locked = False
has_been_opened = False
if signal_locked == False:
if momentum >= positive_mometum_threshold:
signal_locked = True
alpha_model.set_signal(
pair,
momentum,
stop_loss=stop_loss,
take_profit=take_profit,
)
elif momentum <= negative_mometum_threshold:
if strategy_universe.can_open_short(timestamp, pair):
# Only open a short if we have lending markets available at this point
signal_locked = True
alpha_model.set_signal(
pair,
momentum,
stop_loss=stop_loss,
take_profit=take_profit,
leverage=1.0,
)
else:
# Momentum is ~0,
# not worth of a signal
pass
Also change the trading cycle and time bucket to 1 hour for more trades
Run the original notebook and notice how the two curves line up much closer.
Steps to reproduce
Also change the
trading cycle
andtime bucket
to 1 hour for more tradesRun the original notebook and notice how the two curves line up much closer.