Skip to content

Commit

Permalink
renamed most time to dt param names
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaron committed Aug 24, 2024
1 parent 8bb8bf2 commit 0211c0c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 37 deletions.
10 changes: 5 additions & 5 deletions roboquant/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ class Event:
Time is always a datetime object with the timezone set at UTC.
"""

def __init__(self, time: datetime, items: list[Any]):
assert time.tzname() == "UTC", "event with non UTC timezone"
self.time = time
def __init__(self, dt: datetime, items: list[Any]):
assert dt.tzname() == "UTC", "event with non UTC timezone"
self.time: datetime = dt
self.items: list[Any] = items

@staticmethod
def empty(time: datetime):
def empty(dt: datetime):
"""Return a new empty event at the provided time"""
return Event(time, [])
return Event(dt, [])

def is_empty(self) -> bool:
"""return True if this is an empty event without any items, False otherwise"""
Expand Down
8 changes: 4 additions & 4 deletions roboquant/feeds/historic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self):
self.__modified = False
self.__assets: set[Asset] = set()

def _add_item(self, time: datetime, item: PriceItem):
def _add_item(self, dt: datetime, item: PriceItem):
"""Add a price-item at a moment in time to this feed.
Subclasses should invoke this method to populate the historic-feed.
Expand All @@ -30,10 +30,10 @@ def _add_item(self, time: datetime, item: PriceItem):

self.__modified = True

if time not in self.__data:
self.__data[time] = [item]
if dt not in self.__data:
self.__data[dt] = [item]
else:
items = self.__data[time]
items = self.__data[dt]
items.append(item)

def assets(self) -> list[Asset]:
Expand Down
6 changes: 3 additions & 3 deletions roboquant/ml/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def create_signals(self, event: Event) -> list[Signal]:
return []

@abstractmethod
def predict(self, x: NDArray, time: datetime) -> list[Signal]:
def predict(self, x: NDArray, dt: datetime) -> list[Signal]:
...


Expand Down Expand Up @@ -116,7 +116,7 @@ def __init__(
self.sell_pct = sell_pct
self.asset = asset

def predict(self, x, time) -> list[Signal]:
def predict(self, x, dt) -> list[Signal]:
x = torch.asarray(x)
x = torch.unsqueeze(x, dim=0) # add the batch dimension

Expand All @@ -129,7 +129,7 @@ def predict(self, x, time) -> list[Signal]:
else:
p = output.item()

logger.info("prediction p=%s time=%s", p, time)
logger.info("prediction p=%s time=%s", p, dt)
if p >= self.buy_pct:
return [Signal.buy(self.asset)]
if p <= self.sell_pct:
Expand Down
36 changes: 18 additions & 18 deletions roboquant/monetary.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import io
import logging
import os
from time import time as stime
from time import time
import zipfile
from abc import ABC, abstractmethod
from bisect import bisect_left
Expand Down Expand Up @@ -49,7 +49,7 @@ class CurrencyConverter(ABC):
"""Abstract base class for currency converters"""

@abstractmethod
def convert(self, amount: "Amount", to_currency: Currency, time: datetime) -> float:
def convert(self, amount: "Amount", to_currency: Currency, dt: datetime) -> float:
"""Convert the monetary amount into another currency at the provided time."""
...

Expand All @@ -61,7 +61,7 @@ def register(self):
class NoConversion(CurrencyConverter):
"""The default currency converter that doesn't convert between currencies"""

def convert(self, amount: "Amount", to_currency: Currency, time: datetime) -> float:
def convert(self, amount: "Amount", to_currency: Currency, dt: datetime) -> float:
raise NotImplementedError("The default NoConversion doesn't support any conversions")


Expand All @@ -87,7 +87,7 @@ def exists(self):
"""True if there is already a recently (< 12 hours) downloaded file"""
if not self.__file_name.exists():
return False
diff = stime() - os.path.getctime(self.__file_name)
diff = time() - os.path.getctime(self.__file_name)
return diff < 12.0 * 3600.0

@property
Expand Down Expand Up @@ -117,14 +117,14 @@ def _parse(self):
for v in self._rates.values():
v.reverse()

def _get_rate(self, currency: Currency, time: datetime) -> float:
def _get_rate(self, currency: Currency, dt: datetime) -> float:
rates = self._rates[currency]
idx = bisect_left(rates, time, key=lambda r: r[0])
idx = bisect_left(rates, dt, key=lambda r: r[0])
idx = min(idx, len(rates) - 1)
return rates[idx][1]

def convert(self, amount: "Amount", to_currency: Currency, time: datetime) -> float:
return amount.value * self._get_rate(to_currency, time) / self._get_rate(amount.currency, time)
def convert(self, amount: "Amount", to_currency: Currency, dt: datetime) -> float:
return amount.value * self._get_rate(to_currency, dt) / self._get_rate(amount.currency, dt)


class StaticConversion(CurrencyConverter):
Expand All @@ -138,15 +138,15 @@ def __init__(self, base_currency: Currency, rates: dict[Currency, float]):
self.rates = rates
self.rates[base_currency] = 1.0

def convert(self, amount: "Amount", to_currency: Currency, time: datetime) -> float:
def convert(self, amount: "Amount", to_currency: Currency, dt: datetime) -> float:
return amount.value * self.rates[to_currency] / self.rates[amount.currency]


class One2OneConversion(CurrencyConverter):
"""Currency converter that always converts 1 to 1 between currencies.
So for example, 1 USD equals 1 EUR equals 1 GPB"""

def convert(self, amount: "Amount", to_currency: str, time: datetime) -> float:
def convert(self, amount: "Amount", to_currency: str, dt: datetime) -> float:
return amount.value


Expand Down Expand Up @@ -176,10 +176,10 @@ def __add__(self, other: "Amount") -> "Wallet":
return Wallet(self, other)

def __matmul__(self, other: Currency) -> "Amount":
time = datetime.now(tz=timezone.utc)
return Amount(other, self.convert_to(other, time))
dt = datetime.now(tz=timezone.utc)
return Amount(other, self.convert_to(other, dt))

def convert_to(self, currency: Currency, time: datetime) -> float:
def convert_to(self, currency: Currency, dt: datetime) -> float:
"""Convert this amount to another currency and return the monetary value.
If an exchange rate is required, it will invoke the registered `Amount.converter` under the hood.
"""
Expand All @@ -188,7 +188,7 @@ def convert_to(self, currency: Currency, time: datetime) -> float:
if self.value == 0.0:
return 0.0

return Amount.__converter.convert(self, currency, time)
return Amount.__converter.convert(self, currency, dt)

def __repr__(self) -> str:
return f"{self.value:,.2f}@{self.currency}"
Expand Down Expand Up @@ -229,17 +229,17 @@ def __sub__(self, other: "Amount | Wallet"):
return result

def __matmul__(self, other: Currency) -> Amount:
time = datetime.now(tz=timezone.utc)
return Amount(other, self.convert_to(other, time))
dt = datetime.now(tz=timezone.utc)
return Amount(other, self.convert_to(other, dt))

def deepcopy(self) -> "Wallet":
result = Wallet()
result.update(self)
return result

def convert_to(self, currency: Currency, time: datetime) -> float:
def convert_to(self, currency: Currency, dt: datetime) -> float:
"""convert all the amounts hold in this wallet to a single currency and return the value"""
return sum(amount.convert_to(currency, time) for amount in self.amounts())
return sum(amount.convert_to(currency, dt) for amount in self.amounts())

def __repr__(self) -> str:
return " + ".join([f"{a}" for a in self.amounts()])
4 changes: 2 additions & 2 deletions roboquant/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def cancel(self) -> "Order":
result.size = Decimal(0)
return result

def is_expired(self, time: datetime) -> bool:
return time > self.gtd if self.gtd else False
def is_expired(self, dt: datetime) -> bool:
return dt > self.gtd if self.gtd else False

def modify(self, size: Decimal | str | int | float | None = None, limit: float | None = None) -> "Order":
"""Create an update-order. You can update the size and/or limit of an order. The returned order has the same id
Expand Down
6 changes: 3 additions & 3 deletions roboquant/timeframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ def next(inclusive=False, **kwargs):
end = start + td
return Timeframe(start, end, inclusive)

def __contains__(self, time: datetime):
def __contains__(self, dt: datetime):
if self.inclusive:
return self.start <= time <= self.end
return self.start <= time < self.end
return self.start <= dt <= self.end
return self.start <= dt < self.end

def __repr__(self):
if self.is_empty():
Expand Down
4 changes: 2 additions & 2 deletions roboquant/traders/flextrader.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ def create_orders(self, signals: list[Signal], event: Event, account: Account) -

return orders

def _get_orders(self, asset: Asset, size: Decimal, item: PriceItem, signal: Signal, time: datetime) -> list[Order]:
def _get_orders(self, asset: Asset, size: Decimal, item: PriceItem, signal: Signal, dt: datetime) -> list[Order]:
# pylint: disable=unused-argument
"""Return zero or more orders for the provided symbol and size."""
gtd = None if not self.valid_for else time + self.valid_for
gtd = None if not self.valid_for else dt + self.valid_for
return [Order(asset, size, item.price(), gtd)]

def __str__(self) -> str:
Expand Down

0 comments on commit 0211c0c

Please sign in to comment.