Skip to content

Commit

Permalink
undo unrelated changes
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminsingleton committed Nov 11, 2023
1 parent 3c8ec14 commit 12fc140
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 25 deletions.
6 changes: 3 additions & 3 deletions docs/tutorials/backtest_high_level.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ from nautilus_trader.backtest.node import BacktestNode, BacktestVenueConfig, Bac
from nautilus_trader.config.common import ImportableStrategyConfig
from nautilus_trader.persistence.catalog import ParquetDataCatalog
from nautilus_trader.persistence.wranglers import QuoteTickDataWrangler
from nautilus_trader.persistence.loaders import CSVDataLoader
from nautilus_trader.test_kit.providers import CSVTickDataLoader
from nautilus_trader.test_kit.providers import TestInstrumentProvider
```

Expand Down Expand Up @@ -64,7 +64,7 @@ Then we can create Nautilus `QuoteTick` objects by processing the DataFrame with

```python
# Here we just take the first data file found and load into a pandas DataFrame
df = CSVDataLoader.load(raw_files[0], timestamp_column=0, format="%Y%m%d %H%M%S%f")
df = CSVTickDataLoader.load(raw_files[0], index_col=0, format="%Y%m%d %H%M%S%f")
df.columns = ["bid_price", "ask_price"]

# Process quote ticks using a wrangler
Expand Down Expand Up @@ -178,4 +178,4 @@ node = BacktestNode(configs=[config])

results = node.run()
results
```
```
4 changes: 2 additions & 2 deletions examples/notebooks/external_data_backtest.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"from nautilus_trader.config.common import ImportableStrategyConfig\n",
"from nautilus_trader.persistence.catalog import ParquetDataCatalog\n",
"from nautilus_trader.persistence.wranglers import QuoteTickDataWrangler\n",
"from nautilus_trader.test_kit.providers import CSVDataLoader\n",
"from nautilus_trader.test_kit.providers import CSVTickDataLoader\n",
"from nautilus_trader.test_kit.providers import TestInstrumentProvider"
]
},
Expand Down Expand Up @@ -73,7 +73,7 @@
"outputs": [],
"source": [
"# Here we just take the first data file found and load into a pandas DataFrame\n",
"df = CSVDataLoader.load(raw_files[0], index_col=0, format=\"%Y%m%d %H%M%S%f\")\n",
"df = CSVTickDataLoader.load(raw_files[0], index_col=0, format=\"%Y%m%d %H%M%S%f\")\n",
"df.columns = [\"bid_price\", \"ask_price\"]\n",
"\n",
"# Process quote ticks using a wrangler\n",
Expand Down
78 changes: 64 additions & 14 deletions nautilus_trader/persistence/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,26 @@
import pandas as pd


class CSVDataLoader:
class CSVTickDataLoader:
"""
Loads a CSV file to a `pandas.DataFrame`.
Provides a generic tick data CSV file loader.
"""

@staticmethod
def load(
file_path: PathLike[str] | str,
timestamp_column: str | int = "timestamp",
index_col: str | int = "timestamp",
format: str = "mixed",
) -> pd.DataFrame:
"""
Return a `pandas.DataFrame` loaded from the given CSV `file_path`.
Return a tick `pandas.DataFrame` loaded from the given CSV `file_path`.
Parameters
----------
file_path : str, path object or file-like object
The path to the CSV file.
timestamp_column : str | int, default 'timestamp'
Name of the timestamp column in the CSV file
index_col : str | int, default 'timestamp'
The index column.
format : str, default 'mixed'
The timestamp column format.
Expand All @@ -48,35 +48,61 @@ def load(
"""
df = pd.read_csv(
file_path,
index_col=timestamp_column,
index_col=index_col,
parse_dates=True,
)
df.index = pd.to_datetime(df.index, format=format)
return df


class ParquetDataLoader:
class CSVBarDataLoader:
"""
Loads Parquet data to a `pandas.DataFrame`.
Provides a generic bar data CSV file loader.
"""

@staticmethod
def load(file_path: PathLike[str] | str) -> pd.DataFrame:
"""
Return the bar `pandas.DataFrame` loaded from the given CSV `file_path`.
Parameters
----------
file_path : str, path object or file-like object
The path to the CSV file.
Returns
-------
pd.DataFrame
"""
df = pd.read_csv(
file_path,
index_col="timestamp",
parse_dates=True,
)
df.index = pd.to_datetime(df.index, format="mixed")
return df


class ParquetTickDataLoader:
"""
Provides a generic tick data Parquet file loader.
"""

@staticmethod
def load(
file_path: PathLike[str] | str,
timestamp_column: str = "timestamp",
format: str = "mixed",
) -> pd.DataFrame:
"""
Return the `pandas.DataFrame` loaded from the given Parquet `file_path`.
Return the tick `pandas.DataFrame` loaded from the given Parquet `file_path`.
Parameters
----------
file_path : str, path object or file-like object
The path to the Parquet file.
timestamp_column: str
Name of the timestamp column in the parquet data
format : str, default 'mixed'
The timestamp column format.
Returns
-------
Expand All @@ -85,7 +111,31 @@ def load(
"""
df = pd.read_parquet(file_path)
df = df.set_index(timestamp_column)
df.index = pd.to_datetime(df.index, format=format)
return df


class ParquetBarDataLoader:
"""
Provides a generic bar data Parquet file loader.
"""

@staticmethod
def load(file_path: PathLike[str] | str) -> pd.DataFrame:
"""
Return the bar `pandas.DataFrame` loaded from the given Parquet `file_path`.
Parameters
----------
file_path : str, path object or file-like object
The path to the Parquet file.
Returns
-------
pd.DataFrame
"""
df = pd.read_parquet(file_path)
df = df.set_index("timestamp")
return df


Expand Down
14 changes: 8 additions & 6 deletions nautilus_trader/test_kit/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@
from nautilus_trader.model.objects import Money
from nautilus_trader.model.objects import Price
from nautilus_trader.model.objects import Quantity
from nautilus_trader.persistence.loaders import CSVDataLoader
from nautilus_trader.persistence.loaders import ParquetDataLoader
from nautilus_trader.persistence.loaders import CSVBarDataLoader
from nautilus_trader.persistence.loaders import CSVTickDataLoader
from nautilus_trader.persistence.loaders import ParquetBarDataLoader
from nautilus_trader.persistence.loaders import ParquetTickDataLoader


class TestInstrumentProvider:
Expand Down Expand Up @@ -609,22 +611,22 @@ def read_csv(self, path: str, **kwargs: Any) -> TextFileReader:
def read_csv_ticks(self, path: str) -> pd.DataFrame:
uri = self._make_uri(path=path)
with fsspec.open(uri) as f:
return CSVDataLoader.load(file_path=f)
return CSVTickDataLoader.load(file_path=f)

def read_csv_bars(self, path: str) -> pd.DataFrame:
uri = self._make_uri(path=path)
with fsspec.open(uri) as f:
return CSVDataLoader.load(file_path=f)
return CSVBarDataLoader.load(file_path=f)

def read_parquet_ticks(self, path: str, timestamp_column: str = "timestamp") -> pd.DataFrame:
uri = self._make_uri(path=path)
with fsspec.open(uri) as f:
return ParquetDataLoader.load(file_path=f, timestamp_column=timestamp_column)
return ParquetTickDataLoader.load(file_path=f, timestamp_column=timestamp_column)

def read_parquet_bars(self, path: str) -> pd.DataFrame:
uri = self._make_uri(path=path)
with fsspec.open(uri) as f:
return ParquetDataLoader.load(file_path=f)
return ParquetBarDataLoader.load(file_path=f)


class TestDataGenerator:
Expand Down

0 comments on commit 12fc140

Please sign in to comment.