Added the ability for the backtester to use unrealised PnL from the Position objects to calculate a tick-by-tick equity curve. Added a performance directory that calculates drawdown statistics. Modified the output.py script to use Seaborn and output the equity curve, returns and drawdown curve.
This commit is contained in:
@@ -39,6 +39,7 @@ def backtest(
|
||||
if event is not None:
|
||||
if event.type == 'TICK':
|
||||
strategy.calculate_signals(event)
|
||||
portfolio.update_portfolio(event)
|
||||
elif event.type == 'SIGNAL':
|
||||
portfolio.execute_signal(event)
|
||||
elif event.type == 'ORDER':
|
||||
@@ -70,7 +71,9 @@ if __name__ == "__main__":
|
||||
)
|
||||
|
||||
# Create the portfolio object to track trades
|
||||
portfolio = Portfolio(ticker, events, equity=equity)
|
||||
portfolio = Portfolio(
|
||||
ticker, events, equity=equity, backtest=True
|
||||
)
|
||||
|
||||
# Create the simulated execution handler
|
||||
execution = SimulatedExecution()
|
||||
|
||||
+29
-5
@@ -1,7 +1,13 @@
|
||||
import os, os.path
|
||||
|
||||
import pandas as pd
|
||||
import matplotlib
|
||||
try:
|
||||
matplotlib.use('TkAgg')
|
||||
except:
|
||||
pass
|
||||
import matplotlib.pyplot as plt
|
||||
import seaborn as sns
|
||||
|
||||
from qsforex.settings import OUTPUT_RESULTS_DIR
|
||||
|
||||
@@ -14,11 +20,29 @@ if __name__ == "__main__":
|
||||
It requires OUTPUT_RESULTS_DIR to be set in the project
|
||||
settings.
|
||||
"""
|
||||
sns.set_palette("deep", desat=.6)
|
||||
sns.set_context(rc={"figure.figsize": (8, 4)})
|
||||
|
||||
equity_file = os.path.join(OUTPUT_RESULTS_DIR, "equity.csv")
|
||||
equity = pd.io.parsers.read_csv(
|
||||
equity_file, header=True,
|
||||
names=["time", "balance"],
|
||||
parse_dates=True, index_col=0
|
||||
equity_file, parse_dates=True, header=0, index_col=0
|
||||
)
|
||||
equity["balance"].plot()
|
||||
plt.show()
|
||||
|
||||
# Plot three charts: Equity curve, period returns, drawdowns
|
||||
fig = plt.figure()
|
||||
fig.patch.set_facecolor('white') # Set the outer colour to white
|
||||
|
||||
# Plot the equity curve
|
||||
ax1 = fig.add_subplot(311, ylabel='Portfolio value')
|
||||
equity["Equity"].plot(ax=ax1, color=sns.color_palette()[0])
|
||||
|
||||
# Plot the returns
|
||||
ax2 = fig.add_subplot(312, ylabel='Period returns')
|
||||
equity['Returns'].plot(ax=ax2, color=sns.color_palette()[1])
|
||||
|
||||
# Plot the returns
|
||||
ax3 = fig.add_subplot(313, ylabel='Drawdowns')
|
||||
equity['Drawdown'].plot(ax=ax3, color=sns.color_palette()[2])
|
||||
|
||||
# Plot the figure
|
||||
plt.show()
|
||||
|
||||
Reference in New Issue
Block a user