-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
210 additions
and
347 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
from .tracker import Tracker | ||
from roboquant.trackers.standardtracker import StandardTracker | ||
from roboquant.trackers.capmtracker import CAPMTracker | ||
from roboquant.trackers.tracker import Tracker | ||
from roboquant.trackers.alphabeta import AlphaBetaTracker | ||
from roboquant.trackers.basictracker import BasicTracker | ||
from roboquant.trackers.equitytracker import EquityTracker | ||
from roboquant.trackers.tensorboardtracker import TensorboardTracker | ||
from roboquant.trackers.markettracker import MarketTracker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,44 @@ | ||
from dataclasses import dataclass | ||
from datetime import datetime | ||
import logging | ||
from .tracker import Tracker | ||
|
||
from roboquant.trackers.tracker import Tracker | ||
from roboquant.account import Account | ||
from roboquant.event import Event | ||
from roboquant.order import Order | ||
from roboquant.signal import Signal | ||
from prettytable import PrettyTable | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@dataclass | ||
class BasicTracker(Tracker): | ||
"""Tracks a number of basic metrics: | ||
- start- and end-time | ||
- total number of events, items, signals and orders | ||
- equity | ||
- last time | ||
- total number of events, items, signals and orders until that time | ||
This tracker adds little overhead to a run, both CPU and memory wise. | ||
""" | ||
|
||
def __init__(self, price_type="DEFAULT"): | ||
self.start_time = None | ||
self.end_time = None | ||
time: datetime | None | ||
items: int | ||
orders: int | ||
signals: int | ||
events: int | ||
|
||
def __init__(self, output=False): | ||
self.time = None | ||
self.items = 0 | ||
self.orders = 0 | ||
self.signals = 0 | ||
self.events = 0 | ||
self.equity = None | ||
self.buying_power = 0.0 | ||
|
||
def log(self, event: Event, account: Account, signals: dict[str, Signal], orders: list[Order]): | ||
|
||
if self.start_time is None: | ||
self.start_time = event.time | ||
self.__output = output | ||
|
||
self.end_time = event.time | ||
def trace(self, event: Event, account: Account, signals: dict[str, Signal], orders: list[Order]): | ||
self.time = event.time | ||
self.items += len(event.items) | ||
self.orders += len(orders) | ||
self.events += 1 | ||
self.signals += len(signals) | ||
self.equity = account.equity | ||
self.buying_power = account.buying_power | ||
|
||
if logger.isEnabledFor(logging.INFO): | ||
logger.info( | ||
"time=%s events=%s items=%s signals=%s orders=%s equity=%s, buying-power=%s", | ||
self.end_time, | ||
self.events, | ||
self.items, | ||
self.signals, | ||
self.orders, | ||
self.equity, | ||
self.buying_power | ||
) | ||
|
||
def __repr__(self) -> str: | ||
|
||
def to_timefmt(time: datetime | None): | ||
return "-" if time is None else time.strftime("%Y-%m-%d %H:%M:%S") | ||
|
||
p = PrettyTable(["metric", "value"], align="r", float_format=".2") | ||
|
||
p.add_row(["start", to_timefmt(self.start_time)]) | ||
p.add_row(["end", to_timefmt(self.end_time)]) | ||
p.add_row(["events", self.events]) | ||
p.add_row(["items", self.items]) | ||
p.add_row(["signals", self.signals]) | ||
p.add_row(["orders", self.orders]) | ||
return p.get_string() | ||
if self.__output: | ||
print(self.__repr__() + "\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,56 @@ | ||
from datetime import datetime | ||
from .tracker import Tracker | ||
from roboquant.timeframe import Timeframe | ||
from roboquant.trackers.tracker import Tracker | ||
|
||
|
||
class EquityTracker(Tracker): | ||
"""Tracks the time of an event and the equity at that moment. | ||
If multiple events happen at the same time, only the first one will be registered. | ||
If multiple events happen at the same time, only the equity for first one will be registered. | ||
""" | ||
|
||
def __init__(self): | ||
self.timeline = [] | ||
self.equity = [] | ||
self.last = datetime.fromisoformat("1900-01-01T00:00:00+00:00") | ||
self.equities = [] | ||
self.__last = None | ||
|
||
def log(self, event, account, signals, orders): | ||
if event.time > self.last: | ||
def trace(self, event, account, signals, orders): | ||
if self.__last is None or event.time > self.__last: | ||
self.timeline.append(event.time) | ||
self.equity.append(account.equity) | ||
self.last = event.time | ||
self.equities.append(account.equity) | ||
self.__last = event.time | ||
|
||
def timeframe(self): | ||
return Timeframe(self.timeline[0], self.timeline[-1], True) | ||
|
||
def pnl(self, annualized=False): | ||
"""Return the profit & loss percentage, optionally annualized from the recorded durtion""" | ||
pnl = self.equities[-1]/self.equities[0] - 1 | ||
if annualized: | ||
return self.timeframe().annualize(pnl) | ||
else: | ||
return pnl | ||
|
||
def max_drawdown(self): | ||
max_equity = self.equities[0] | ||
result = 0.0 | ||
for equity in self.equities: | ||
if equity > max_equity: | ||
max_equity = equity | ||
|
||
dd = (equity - max_equity) / max_equity | ||
if dd < result: | ||
result = dd | ||
|
||
return result | ||
|
||
def max_gain(self): | ||
min_equity = self.equities[0] | ||
result = 0.0 | ||
for equity in self.equities: | ||
if equity < min_equity: | ||
min_equity = equity | ||
|
||
gain = (equity - min_equity) / min_equity | ||
if gain > result: | ||
result = gain | ||
|
||
return result |
Oops, something went wrong.