Skip to content

Commit

Permalink
Add CommitmentsOfTraders class and refactor code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
philsv committed Jun 5, 2024
1 parent a396c8b commit e09d607
Show file tree
Hide file tree
Showing 12 changed files with 479 additions and 370 deletions.
43 changes: 26 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pycot

[![PyPI version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=py&r=r&ts=1683906897&type=6e&v=0.0.7&x2=0)](https://badge.fury.io/py/pycot-reports)
[![PyPI version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=py&r=r&ts=1683906897&type=6e&v=0.1.0&x2=0)](https://badge.fury.io/py/pycot-reports)
[![License: MIT](https://img.shields.io/badge/License-MIT-red.svg)](https://github.com/philsv/pycot/blob/main/LICENSE)
[![Weekly Downloads](https://static.pepy.tech/personalized-badge/pycot-reports?period=week&units=international_system&left_color=grey&right_color=blue&left_text=downloads/week)](https://pepy.tech/project/pycot-reports)
[![Monthly Downloads](https://static.pepy.tech/personalized-badge/pycot-reports?period=month&units=international_system&left_color=grey&right_color=blue&left_text=downloads/month)](https://pepy.tech/project/pycot-reports)
Expand All @@ -22,9 +22,10 @@ pip install pycot-reports
## How to use

```python
from pycot import reports
from pycot.reports import CommitmentOfTraders

df = reports.legacy_report(report_type="legacy_fut", contract_name=("FED FUNDS - CHICAGO BOARD OF TRADE", "30-DAY FEDERAL FUNDS - CHICAGO BOARD OF TRADE"))
cot = CommitmentOfTraders("legacy_fut")
df = cot.report(("FED FUNDS - CHICAGO BOARD OF TRADE", "30-DAY FEDERAL FUNDS - CHICAGO BOARD OF TRADE"))
```

## How do I get cached results?
Expand All @@ -34,23 +35,27 @@ If you want to retrieve data from the same report multiple times, you can use th
Lets have a look at an example:

```python
from pycot.reports import cot_report, legacy_report
from pycot.reports import CommitmentOfTraders

fed_funds_contract = ("FED FUNDS - CHICAGO BOARD OF TRADE", "30-DAY FEDERAL FUNDS - CHICAGO BOARD OF TRADE")
fed_funds_df = cot_report(legacy_report(), fed_funds_contract) # will load the full report (~ 10-15 seconds)
cot = CommitmentOfTraders("legacy_fut")

bbg_contract = ("BBG COMMODITY - CHICAGO BOARD OF TRADE", "BLOOMBERG COMMODITY INDEX - CHICAGO BOARD OF TRADE")
bbg_df = cot_report(legacy_report(), bbg_contract) # cached, will not load the full report again
# will load the full report (~ 10-20 seconds)
fed_funds_df = cot.report(("FED FUNDS - CHICAGO BOARD OF TRADE", "30-DAY FEDERAL FUNDS - CHICAGO BOARD OF TRADE"))

# cached, will load instantly
bbg_df = cot.report(("BBG COMMODITY - CHICAGO BOARD OF TRADE", "BLOOMBERG COMMODITY INDEX - CHICAGO BOARD OF TRADE"))
```

## Report Types

### Legacy Report (All Contracts)

```python
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)
from pycot.reports import CommitmentOfTraders

cot = CommitmentOfTraders("legacy_fut")
contract_names = ("FED FUNDS - CHICAGO BOARD OF TRADE", "30-DAY FEDERAL FUNDS - CHICAGO BOARD OF TRADE")
df = cot.cot_report(contract_names)
```

Output Example:
Expand All @@ -75,9 +80,11 @@ Date ...
### Disaggregated Report (Commodities)

```python
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)
from pycot.reports import CommitmentOfTraders

cot = CommitmentOfTraders("disaggregated_futopt")
contract_names = ("BRENT LAST DAY - NEW YORK MERCANTILE EXCHANGE", "BRENT CRUDE OIL LAST DAY - NEW YORK MERCANTILE EXCHANGE")
df = cot.report(contract_names)
```

Output Example:
Expand All @@ -102,9 +109,11 @@ Date .
### Financial Report (Financial Instruments)

```python
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)
from pycot.reports import CommitmentOfTraders

cot = CommitmentOfTraders("traders_in_financial_futures_fut")
contract_names = ("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 = cot.report(contract_names)
```

Output Example:
Expand Down
43 changes: 43 additions & 0 deletions pycot/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import json
from functools import lru_cache
from pathlib import Path

from pydantic_settings import BaseSettings


def open_json(path: Path) -> dict:
"""
Opens a json file.
Returns:
The content as a dictionary.
"""
with open(path, "r") as f:
data = json.load(f)
return data


class Settings(BaseSettings):
"""
Settings for the pycot package.
"""

BASE_PATH: Path = Path(__file__).parent.parent
FORMAT_COLUMNS_PATH: Path = BASE_PATH / "pycot" / "store" / "format_columns.json"
COT_REPORTS_DATA_PATH: Path = BASE_PATH / "pycot" / "store" / "cot_reports_data.json"
CONTRACT_NAMES_PATH: Path = BASE_PATH / "pycot" / "store" / "contract_names.json"

FORMAT_COLUMNS: dict = open_json(FORMAT_COLUMNS_PATH)
COT_REPORTS_DATA: dict = open_json(COT_REPORTS_DATA_PATH)
CONTRACT_NAMES: dict = open_json(CONTRACT_NAMES_PATH)


@lru_cache()
def get_settings() -> Settings:
"""
Cache the settings object to avoid reading the environment variables
"""
return Settings()


settings = get_settings()
139 changes: 0 additions & 139 deletions pycot/extract_report_data.py

This file was deleted.

Loading

0 comments on commit e09d607

Please sign in to comment.