-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Franz Philip Brandmueller
committed
Oct 24, 2023
1 parent
20c6b8c
commit e515ea7
Showing
6 changed files
with
104 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.0.3" | ||
__version__ = "0.0.4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |