Skip to content

Commit

Permalink
fix: module imports / add tests
Browse files Browse the repository at this point in the history
fix: module imports / add tests
  • Loading branch information
philsv committed Oct 24, 2023
1 parent 20c6b8c commit a6c3e7e
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 44 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ pip install pycot-reports
## How to use

```python
from pycot import legacy_report, disaggregated_report, financial_report
from pycot.reports import legacy_report, disaggregated_report, financial_report
```

Lets have a look at some examples.

### Legacy Report (All Contracts)

```python
from pycot import legacy_report
from pycot.reports import legacy_report
contract_name = ("FED FUNDS - CHICAGO BOARD OF TRADE", "30-DAY FEDERAL FUNDS - CHICAGO BOARD OF TRADE")
df = legacy_report("legacy_fut", contract_name)
```
Expand All @@ -57,7 +57,7 @@ Date ...
### Disaggregated Report (Commodities)

```python
from pycot import disaggregated_report
from pycot.reports import disaggregated_report
contract_name = ("BRENT LAST DAY - NEW YORK MERCANTILE EXCHANGE", "BRENT CRUDE OIL LAST DAY - NEW YORK MERCANTILE EXCHANGE")
df = disaggregated_report("disaggregated_futopt", contract_name)
```
Expand All @@ -84,7 +84,7 @@ Date .
### Financial Report (Financial Instruments)

```python
from pycot import financial_report
from pycot.reports import financial_report
contract_name = ("UST 10Y NOTE - CHICAGO BOARD OF TRADE", "10-YEAR U.S. TREASURY NOTES - CHICAGO BOARD OF TRADE", "10 YEAR U.S. TREASURY NOTES - CHICAGO BOARD OF TRADE")
df = financial_report("traders_in_financial_futures_fut", contract_name)
```
Expand Down
3 changes: 2 additions & 1 deletion pycot/extract_report_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

import pandas as pd
import requests
from exceptions import InvalidReportType

from pycot.exceptions import InvalidReportType

BASE_PATH = Path(__file__).parent.parent

Expand Down
44 changes: 6 additions & 38 deletions pycot/pycot.py → pycot/reports.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,10 @@
from functools import lru_cache

import numpy as np
import pandas as pd
from exceptions import InvalidReportType
from extract_report_data import COT, get_formating_data


def format_dataframe(
data: pd.DataFrame,
names: list,
columns: list,
) -> pd.DataFrame:
"""
Helper function to format the data retrieved from the CFTC website.
"""
df = data.reindex(columns=columns) # Limit the columns to the ones we need (please adjust to your liking in format_columns.json)
df.rename(columns=dict(zip(columns, names)), inplace=True)
df["Date"] = pd.to_datetime(df["Date"])
df.set_index(["Date", "Contract Name"], inplace=True)
df.replace(".", np.nan, inplace=True)
df = df.astype(float)
df.reset_index(inplace=True)
df.set_index("Date", inplace=True)
return df


def get_contract(
df: pd.DataFrame,
contract_name: str | tuple,
) -> pd.DataFrame:
"""
Retrieves the Commitment of Traders Reports data for a specific contract or list of contracts.
"""
if isinstance(contract_name, str):
return df[df["Contract Name"] == contract_name]

data = [df[df["Contract Name"] == contract] for contract in contract_name if contract in df["Contract Name"].unique()]
return pd.concat(data)
from pycot.exceptions import InvalidReportType
from pycot.extract_report_data import COT, get_formating_data
from pycot.utils import format_dataframe, get_contract


@lru_cache
Expand All @@ -54,7 +22,7 @@ def legacy_report(
A pandas DataFrame with the legacy futures Commitment of Traders data.
Example:
>>> from pycot import legacy_report
>>> from pycot.reports import legacy_report
>>> legacy_report("legacy_fut", ("FED FUNDS - CHICAGO BOARD OF TRADE", "30-DAY FEDERAL FUNDS - CHICAGO BOARD OF TRADE"))
"""
if report_type not in ["legacy_fut", "legacy_futopt"]:
Expand Down Expand Up @@ -84,7 +52,7 @@ def disaggregated_report(
A pandas DataFrame with the disaggregated futures and options report.
Example:
>>> from pycot import disaggregated_report
>>> from pycot.reports import disaggregated_report
>>> disaggregated_report("disaggregated_fut", ("BRENT LAST DAY - NEW YORK MERCANTILE EXCHANGE", "BRENT CRUDE OIL LAST DAY - NEW YORK MERCANTILE EXCHANGE"))
"""
if report_type not in ["disaggregated_fut", "disaggregated_futopt"]:
Expand Down Expand Up @@ -121,7 +89,7 @@ def financial_report(
A pandas DataFrame with the financial futures and options report.
Example:
>>> from pycot import financial_report
>>> from pycot.reports import financial_report
>>> financial_report("financial_fut", ("UST 10Y NOTE - CHICAGO BOARD OF TRADE", "10-YEAR U.S. TREASURY NOTES - CHICAGO BOARD OF TRADE", "10 YEAR U.S. TREASURY NOTES - CHICAGO BOARD OF TRADE"))
"""
if report_type not in ["traders_in_financial_futures_fut", "traders_in_financial_futures_futopt"]:
Expand Down
35 changes: 35 additions & 0 deletions pycot/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import numpy as np
import pandas as pd


def format_dataframe(
data: pd.DataFrame,
names: list,
columns: list,
) -> pd.DataFrame:
"""
Helper function to format the data retrieved from the CFTC website.
"""
df = data.reindex(columns=columns) # Limit the columns to the ones we need (please adjust to your liking in format_columns.json)
df.rename(columns=dict(zip(columns, names)), inplace=True)
df["Date"] = pd.to_datetime(df["Date"])
df.set_index(["Date", "Contract Name"], inplace=True)
df.replace(".", np.nan, inplace=True)
df = df.astype(float)
df.reset_index(inplace=True)
df.set_index("Date", inplace=True)
return df


def get_contract(
df: pd.DataFrame,
contract_name: str | tuple,
) -> pd.DataFrame:
"""
Retrieves the Commitment of Traders Reports data for a specific contract or list of contracts.
"""
if isinstance(contract_name, str):
return df[df["Contract Name"] == contract_name]

data = [df[df["Contract Name"] == contract] for contract in contract_name if contract in df["Contract Name"].unique()]
return pd.concat(data)
2 changes: 1 addition & 1 deletion pycot/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.3"
__version__ = "0.0.4"
56 changes: 56 additions & 0 deletions tests/test_reports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import pandas as pd
import pytest

from pycot import reports


@pytest.mark.parametrize(
"report_type,contract_name",
[
(
"legacy_fut",
(
"FED FUNDS - CHICAGO BOARD OF TRADE",
"30-DAY FEDERAL FUNDS - CHICAGO BOARD OF TRADE",
),
),
],
)
def test_legacy_report(report_type, contract_name):
df = reports.legacy_report(report_type, contract_name)
assert isinstance(df, pd.DataFrame)


@pytest.mark.parametrize(
"report_type,contract_name",
[
(
"disaggregated_futopt",
(
"BRENT LAST DAY - NEW YORK MERCANTILE EXCHANGE",
"BRENT CRUDE OIL LAST DAY - NEW YORK MERCANTILE EXCHANGE",
),
),
],
)
def test_disaggregated_report(report_type, contract_name):
df = reports.disaggregated_report(report_type, contract_name)
assert isinstance(df, pd.DataFrame)


@pytest.mark.parametrize(
"report_type,contract_name",
[
(
"traders_in_financial_futures_fut",
(
"UST 10Y NOTE - CHICAGO BOARD OF TRADE",
"10-YEAR U.S. TREASURY NOTES - CHICAGO BOARD OF TRADE",
"10 YEAR U.S. TREASURY NOTES - CHICAGO BOARD OF TRADE",
),
),
],
)
def test_financial_report(report_type, contract_name):
df = reports.financial_report(report_type, contract_name)
assert isinstance(df, pd.DataFrame)

0 comments on commit a6c3e7e

Please sign in to comment.