Skip to content

Commit

Permalink
improved random walk
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaron committed Jun 15, 2024
1 parent aad4916 commit 31e74ed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 37 deletions.
28 changes: 20 additions & 8 deletions roboquant/feeds/randomwalk.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import numpy as np

from roboquant.event import Bar, Trade
from roboquant.event import Bar, Trade, Quote
from .historic import HistoricFeed


Expand All @@ -18,7 +18,7 @@ def __init__(
self,
n_symbols: int = 10,
n_prices: int = 1_000,
item_type: Literal["bar", "trade"] = "bar",
item_type: Literal["bar", "trade", "quote"] = "bar",
start_date: str | datetime = "2020-01-01T00:00:00+00:00",
frequency=timedelta(days=1),
start_price_min: float = 50.0,
Expand All @@ -36,26 +36,38 @@ def __init__(
start_date = start_date if isinstance(start_date, datetime) else datetime.fromisoformat(start_date)
start_date = start_date.astimezone(timezone.utc)
timeline = [start_date + frequency * i for i in range(n_prices)]
self.stdev = stdev

item_gen = self.__get_bar if item_type == "bar" else self.__get_trade
match item_type:
case "bar": item_gen = self.__get_bar
case "trade": item_gen = self.__get_trade
case "quote": item_gen = self.__get_quote
case _: raise ValueError("unsupported item_type", item_type)

for symbol in symbols:
prices = self.__price_path(rnd, n_prices, stdev, start_price_min, start_price_max)
for i in range(n_prices):
item = item_gen(symbol, prices[i], volume)
item = item_gen(symbol, prices[i], volume, stdev/2.0)
self._add_item(timeline[i], item)

@staticmethod
def __get_trade(symbol, price, volume):
def __get_trade(symbol, price, volume, _):
return Trade(symbol, price, volume)

@staticmethod
def __get_bar(symbol, price, volume):
high = price * (1.0 + abs(random.gauss(mu=0.0, sigma=1.0)))
low = price * (1.0 - abs(random.gauss(mu=0.0, sigma=1.0)))
def __get_bar(symbol, price, volume, spread):
high = price * (1.0 + abs(random.gauss(mu=0.0, sigma=spread)))
low = price * (1.0 - abs(random.gauss(mu=0.0, sigma=spread)))
close = random.uniform(low, high)
return Bar(symbol, array("f", [price, high, low, close, volume]))

@staticmethod
def __get_quote(symbol, price, volume, spread):
spread = abs(random.gauss(mu=0.0, sigma=spread)) * price / 2.0
ask = price + spread
bid = price - spread
return Quote(symbol, array("f", [price, ask, volume, bid, volume]))

@staticmethod
def __get_symbols(
rnd,
Expand Down
29 changes: 0 additions & 29 deletions roboquant/strategies/ordersizer.py

This file was deleted.

0 comments on commit 31e74ed

Please sign in to comment.