Skip to content

Commit

Permalink
fixed bug in pnl
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaron committed Mar 3, 2024
1 parent 11914eb commit fdadfec
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
2 changes: 1 addition & 1 deletion roboquant/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.2.2"
__version__ = "0.2.3"

from roboquant import brokers
from roboquant import feeds
Expand Down
35 changes: 14 additions & 21 deletions roboquant/feeds/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,20 @@ def __background():
thread.start()
return channel

def plot(self, plt, *symbols: str, price_type: str = "DEFAULT", timeframe: Timeframe | None = None, **kwargs):
def plot(self, plt, symbol: str, price_type: str = "DEFAULT", timeframe: Timeframe | None = None, **kwargs):
"""Plot the prices of one or more symbols"""
channel = self.play_background(timeframe)
result = {}
times = []
prices = []
while evt := channel.get():
for symbol, price in evt.get_prices(price_type).items():
if symbols and symbol not in symbols:
continue
if symbol not in result:
result[symbol] = ([], [])
data = result[symbol]
data[0].append(evt.time)
data[1].append(price)

for symbol, data in result.items():
plt.plot(data[0], data[1], **kwargs)
if hasattr(plt, "set_title"):
# assume we are in a subplot
plt.set_title(symbol)
else:
plt.title(symbol)
plt.show()


price = evt.get_price(symbol, price_type)
if price is not None:
times.append(evt.time)
prices.append(price)

plt.plot(times, prices, **kwargs)
if hasattr(plt, "set_title"):
# assume we are in a subplot
plt.set_title(symbol)
else:
plt.title(symbol)
2 changes: 1 addition & 1 deletion roboquant/journals/pnlmetric.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __get_max_drawdown(self, equity) -> float:
return self.max_drawdown

def __get_max_gain(self, equity) -> float:
self.min_equity = max(equity, self.min_equity)
self.min_equity = min(equity, self.min_equity)
gain = equity / self.min_equity - 1.0
self.max_gain = max(gain, self.max_gain)
return self.max_gain
2 changes: 1 addition & 1 deletion roboquant/strategies/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def calc(self, evt):


class FeatureStrategy(Strategy, ABC):
"""Abstract base class for strategies based on one or mroe features"""
"""Abstract base class for strategies based on one or more features"""

def __init__(self, history: int, dtype="float32"):
self._features_x = []
Expand Down
24 changes: 19 additions & 5 deletions tests/samples/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@
import roboquant as rq

if __name__ == "__main__":
# Plot historic data
feed = rq.feeds.YahooFeed("JPM", "IBM", "F", start_date="2000-01-01")
feed.plot(plt)

# Plot metrics
# Plot historic price data
feed = rq.feeds.YahooFeed("JPM", "IBM", "F", start_date="2010-01-01")
_, axs = plt.subplots(2, figsize=(10, 10))
feed.plot(axs[0], "JPM")
feed.plot(axs[1], "IBM")
plt.show()

# run a single back-test
strategy = rq.strategies.EMACrossover()
journal = rq.journals.MetricsJournal.pnl()
rq.run(feed, strategy, journal=journal)
journal.plot(plt.subplot(), "pnl/equity", color="green")

# Plot the equity curve with some customization
journal.plot(plt, "pnl/equity", color="green", linestyle="--", linewidth=0.5)
plt.show()

# Plot all the recorded metrics
metric_names = journal.get_metric_names()
fig, axs = plt.subplots(3, 3, figsize=(15, 15))
axs = axs.flatten()
for ax, metric_name in zip(axs, metric_names):
journal.plot(ax, metric_name)
plt.show()

0 comments on commit fdadfec

Please sign in to comment.