Skip to content

Commit

Permalink
add list_available_contracts method
Browse files Browse the repository at this point in the history
  • Loading branch information
philsv committed Jun 5, 2024
1 parent 7a89b36 commit df5c6b5
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 12 deletions.
28 changes: 23 additions & 5 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.1.0&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.1&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 Down Expand Up @@ -135,13 +135,31 @@ Date .
...
```

## Contract Names
## List Available Contracts

The only tricky part is the contract name.
```python
from pycot.reports import CommitmentOfTraders

cot = CommitmentOfTraders("legacy_fut")
contracts: np.ndarray = cot.list_available_contracts()
```

You can find the contract name in the [CFTC Commitment of Traders](https://www.cftc.gov/MarketReports/CommitmentsofTraders/index.htm) reports. The contract name is the first column in the report.
Output Example:

You can also find a curated list of contract names in the [contract_names.json](https://github.com/philsv/pycot/tree/main/pycot/data/contract_names.json)
```python
array(['1-MONTH SOFR - CHICAGO MERCANTILE EXCHANGE',
'10 YEAR DELIVERABLE IR - CHICAGO BOARD OF TRADE',
'10 YEAR DELIVERABLE IR SWAP - CHICAGO BOARD OF TRADE',
'10 YEAR ERIS SOFR SWAP - CHICAGO BOARD OF TRADE',
'10 YEAR ERIS SWAP - CHICAGO BOARD OF TRADE',
'10-YEAR U.S. TREASURY NOTES - CHICAGO BOARD OF TRADE',
...
'UST 10Y NOTE - CHICAGO BOARD OF TRADE',
'UST 2Y NOTE - CHICAGO BOARD OF TRADE',
'UST 5Y NOTE - CHICAGO BOARD OF TRADE',
'UST BOND - CHICAGO BOARD OF TRADE',
'VIX FUTURES - CBOE FUTURES EXCHANGE'], dtype=object)
```

## Release Shedule

Expand Down
38 changes: 38 additions & 0 deletions pycot/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,44 @@ def financial_report(
df["Leveraged Money Short"] = df["Leveraged Money Short"] * -1
df = df.sort_index()[::-1]
return df

def list_available_contracts(
self,
) -> np.ndarray:
"""
Lists all available contracts for a selected Commitment of Traders report.
Returns:
A pandas DataFrame with the available contracts for the selected report.
Example:
>>> from pycot.reports import CommitmentsOfTraders
>>> cot = CommitmentsOfTraders("legacy_fut")
>>> cot.list_available_contracts()
>>> ...
>>> cot.report(contract_name)
"""
if self.report_type is None:
raise ValueError("Please select a report type to list available contracts")

if self.report_type in [
"legacy_fut",
"legacy_futopt",
]:
df = self.legacy_report
elif self.report_type in [
"disaggregated_fut",
"disaggregated_futopt",
]:
df = self.disaggregated_report
elif self.report_type in [
"traders_in_financial_futures_fut",
"traders_in_financial_futures_futopt",
]:
df = self.financial_report

available_contracts = df["Contract Name"].unique()
return np.sort(available_contracts)

def report(
self,
Expand Down
2 changes: 1 addition & 1 deletion pycot/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.0"
__version__ = "0.1.1"
34 changes: 28 additions & 6 deletions tests/test_reports.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import time

import numpy as np
import pandas as pd
import pytest

Expand Down Expand Up @@ -39,24 +40,45 @@ def test_individual_reports(report_type, contract_name):
assert isinstance(df, pd.DataFrame)


@pytest.mark.parametrize(
"report_type",
[
("legacy_fut"),
("disaggregated_futopt"),
("traders_in_financial_futures_fut"),
],
)
def test_list_available_contracts(report_type):
cot = CommitmentsOfTraders(report_type)
contracts = cot.list_available_contracts()
assert isinstance(contracts, np.ndarray)
assert len(contracts) > 0


@pytest.mark.parametrize(
"report_type, contract_1, contract_2",
[
(
"legacy_fut",
("FED FUNDS - CHICAGO BOARD OF TRADE", "30-DAY FEDERAL FUNDS - CHICAGO BOARD OF TRADE"),
("BBG COMMODITY - CHICAGO BOARD OF TRADE", "BLOOMBERG COMMODITY INDEX - CHICAGO BOARD OF TRADE"),
(
"FED FUNDS - CHICAGO BOARD OF TRADE",
"30-DAY FEDERAL FUNDS - CHICAGO BOARD OF TRADE",
),
(
"BBG COMMODITY - CHICAGO BOARD OF TRADE",
"BLOOMBERG COMMODITY INDEX - CHICAGO BOARD OF TRADE",
),
),
],
)
def test_cot_report(report_type, contract_1, contract_2):
def test_cot_report(report_type, contract_1, contract_2):
start_time_1 = time.perf_counter()
cot = CommitmentsOfTraders(report_type)
df_1 = cot.report(contract_1)
cot = CommitmentsOfTraders(report_type)
df_1 = cot.report(contract_1)
end_time_1 = time.perf_counter() - start_time_1

start_time_2 = time.perf_counter()
df_2 = cot.report(contract_2)
df_2 = cot.report(contract_2)
end_time_2 = time.perf_counter() - start_time_2
assert end_time_1 > end_time_2
assert isinstance(df_1, pd.DataFrame)
Expand Down

0 comments on commit df5c6b5

Please sign in to comment.