Skip to content

Commit

Permalink
simplified code
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaron committed Mar 29, 2024
1 parent 45b68d5 commit 21ec131
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
12 changes: 9 additions & 3 deletions roboquant/ml/envs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,24 @@


class Action2Signals:
"""Transforms an action into signals"""

def __init__(self, symbols: list[str]):
self.symbols = symbols

@staticmethod
def __limit(rating):
return max(-1.0, min(1.0, float(rating)))

def get_signals(self, action, _):
return [Signal(symbol, float(rating)) for symbol, rating in zip(self.symbols, action)]
return [Signal(symbol, self.__limit(rating)) for symbol, rating in zip(self.symbols, action)]

def get_action_space(self):
return spaces.Box(-1.0, 1.0, shape=(len(self.symbols),), dtype=np.float32)


class Action2Orders:
"""Transforms an action into orders"""

def __init__(self, symbols: list[str]):
self.symbols = symbols
Expand All @@ -50,10 +56,10 @@ def _account_rebalance(self, account: Account, new_sizes: dict[str, Decimal]) ->

return orders

def get_orders(self, action, event: Event, account: Account) -> list[Order]:
def get_orders(self, actions, event: Event, account: Account) -> list[Order]:
new_positions = {}
equity = account.equity()
for symbol, fraction in zip(self.symbols, action):
for symbol, fraction in zip(self.symbols, actions):
price = event.get_price(symbol)
if price:
rel_fraction = fraction / len(self.symbols)
Expand Down
40 changes: 15 additions & 25 deletions roboquant/timeframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,25 @@ def empty():
return Timeframe.fromisoformat("1900-01-01T00:00:00+00:00", "1900-01-01T00:00:00+00:00", False)

@staticmethod
def previous(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0, inclusive=False):
"""Convenient method to create a historic timeframe"""

td = timedelta(
days=days,
seconds=seconds,
microseconds=microseconds,
milliseconds=milliseconds,
minutes=minutes,
hours=hours,
weeks=weeks,
)
def previous(inclusive=False, **kwargs):
"""Convenient method to create a historic timeframe, the kwargs arguments will be passed to the timedelta
timeframe = Timeframe.previous(days=365)
"""

td = timedelta(**kwargs)
end = datetime.now(timezone.utc)
start = end - td
return Timeframe(start, end, inclusive)

@staticmethod
def next(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0, inclusive=False):
"""Convenient method to create a future timeframe"""

td = timedelta(
days=days,
seconds=seconds,
microseconds=microseconds,
milliseconds=milliseconds,
minutes=minutes,
hours=hours,
weeks=weeks,
)
def next(inclusive=False, **kwargs):
"""Convenient method to create a future timeframe, the kwargs arguments will be passed to the timedelta
timeframe = Timeframe.next(minutes=30)
"""

td = timedelta(**kwargs)
start = datetime.now(timezone.utc)
end = start + td
return Timeframe(start, end, inclusive)
Expand Down Expand Up @@ -97,7 +87,7 @@ def annualize(self, rate: float) -> float:

def split(self, n: int | timedelta | Any) -> list["Timeframe"]:
"""Split the timeframe in sequential parts and return the resulting list of timeframes.
The parameter `n` can be a number or a timedelta instance or other types like relativedelta that support
The parameter `n` can be a number, a timedelta instance or other types like relativedelta that support
datetime calculations.
"""

Expand Down

0 comments on commit 21ec131

Please sign in to comment.