Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pytradebacktest/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ def order(self, order: Order):

self.orders.append(order)

def load_instrument_candles(
self, instrument: Instrument, granularity: Granularity, count: int
):
self._data.load_instrument_candles(instrument, granularity, count)

def subscribe(
self, instrument: Instrument, granularity: Granularity
) -> IInstrumentData:
Expand Down
17 changes: 15 additions & 2 deletions pytradebacktest/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ def load(self) -> list[InstrumentData]:

class MarketData(IDataContext):

_index: pd.Timestamp

def __init__(self, loader: MarketDataLoader):
self._sources = loader.load()
self._init_index()
Expand All @@ -132,9 +134,17 @@ def universe(self):
return self._sources

@property
def index(self):
def index(self) -> pd.Timestamp:
return self._index

def load_instrument_candles(
self, instrument: Instrument, granularity: Granularity, count: int
):
_data = self.get(instrument, granularity)
_instrument_timestamp: pd.Timestamp = _data.df.index[count]
if _instrument_timestamp > self._index:
self._index = _instrument_timestamp

@property
def i(self) -> int:
return self._market_index.get_loc(self._index)
Expand Down Expand Up @@ -163,7 +173,10 @@ def next(self):

def __next(self):

for idx in self._market_index:
# Slice index incase some candles were loaded prior to starting
# the test run
_index = self._market_index[self.i :]
for idx in _index:
self._index = idx
for source in self._sources:
source.index = idx
Expand Down
28 changes: 26 additions & 2 deletions tests/unit/test_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from datetime import datetime
from datetime import datetime, timezone

from pytrade.instruments import FxInstrument, Granularity

from pytradebacktest.data import MarketData

Expand All @@ -11,7 +13,7 @@ def test_load_market_data(test_fx_universe):
def test_init_index(test_fx_universe):
data: MarketData = test_fx_universe

assert data._index == datetime(2024, 5, 1)
assert data.index == datetime(2024, 5, 1, tzinfo=timezone.utc)


def test_fx_length(test_fx_universe):
Expand All @@ -20,3 +22,25 @@ def test_fx_length(test_fx_universe):

def test_stock_length(test_stock_universe):
assert len(test_stock_universe) == 2148


def test_preload_candles(test_fx_universe):
data: MarketData = test_fx_universe

assert data.i == 0
assert data.index == datetime(2024, 5, 1, tzinfo=timezone.utc)

data.load_instrument_candles(FxInstrument.EURUSD, Granularity.M1, 5)

assert data.i == 5
assert data.index == datetime(2024, 5, 1, 0, 5, tzinfo=timezone.utc)

data.load_instrument_candles(FxInstrument.EURUSD, Granularity.M1, 10)

assert data.i == 10
assert data.index == datetime(2024, 5, 1, 0, 10, tzinfo=timezone.utc)

data.load_instrument_candles(FxInstrument.EURUSD, Granularity.M1, 7)

assert data.i == 10
assert data.index == datetime(2024, 5, 1, 0, 10, tzinfo=timezone.utc)