Coder Social home page Coder Social logo

philsv / pycot Goto Github PK

View Code? Open in Web Editor NEW
9.0 3.0 3.0 47 KB

Python library for retrieving the CFTC Commitment of Traders reports

Home Page: https://pypi.org/project/pycot-reports/

License: MIT License

Python 100.00%
cftc cot python commitments-of-traders

pycot's Introduction

pycot

PyPI version License: MIT Weekly Downloads Monthly Downloads Downloads

pycot is a easy to use python library for interacting with the CFTC Commitment of Traders reports.

Installation

pip install pycot-reports

Requirements

  • pandas
  • requests

How to use

from pycot.reports import CommitmentOfTraders

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?

If you want to retrieve data from the same report multiple times, you can use the report function. This will cache the results of the previous function call.

Lets have a look at an example:

from pycot.reports import CommitmentOfTraders

cot = CommitmentOfTraders("legacy_fut")

# 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)

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.report(contract_names)

Output Example:

                                            Contract Name  Open Interest  ...  Net Change, Large Spec  Net % of OI, Large Spec
Date                                                                      ...                                                                                                                                  
2023-07-11             FED FUNDS - CHICAGO BOARD OF TRADE      1440370.0  ...                -58101.0                    -11.5
2023-07-03             FED FUNDS - CHICAGO BOARD OF TRADE      1414525.0  ...                -17553.0                     -7.5
2023-06-27             FED FUNDS - CHICAGO BOARD OF TRADE      1746984.0  ...                 12437.0                     -5.1
2023-06-20             FED FUNDS - CHICAGO BOARD OF TRADE      1693141.0  ...                 84512.0                     -6.0
2023-06-13             FED FUNDS - CHICAGO BOARD OF TRADE      1556681.0  ...                 60704.0                    -12.0
...                                                   ...            ...  ...                     ...                      ...
1993-03-23  30-DAY FEDERAL FUNDS - CHICAGO BOARD OF TRADE        11298.0  ...                   106.0                      9.1
1993-03-16  30-DAY FEDERAL FUNDS - CHICAGO BOARD OF TRADE        11015.0  ...                    -8.0                      8.3
1993-03-09  30-DAY FEDERAL FUNDS - CHICAGO BOARD OF TRADE        10651.0  ...                   -51.0                      8.8
1993-03-02  30-DAY FEDERAL FUNDS - CHICAGO BOARD OF TRADE        10902.0  ...                  -190.0                      9.0
1993-02-23  30-DAY FEDERAL FUNDS - CHICAGO BOARD OF TRADE        12460.0  ...                   -83.0                      9.4
...

Disaggregated Report (Commodities)

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:

                                                Contract Name  Open Interest   ...  Net Change Managed Money  Net % of OI Managed Money
Date                                                                           ...                                                                                                                            
2023-07-11      BRENT LAST DAY - NEW YORK MERCANTILE EXCHANGE       138358.0   ...                  -2134.0                        -2.9
2023-07-03      BRENT LAST DAY - NEW YORK MERCANTILE EXCHANGE       130715.0   ...                   9436.0                        -1.4
2023-06-27      BRENT LAST DAY - NEW YORK MERCANTILE EXCHANGE       153190.0   ...                  -6135.0                        -7.4
2023-06-20      BRENT LAST DAY - NEW YORK MERCANTILE EXCHANGE       148800.0   ...                   2367.0                        -3.5
2023-06-13      BRENT LAST DAY - NEW YORK MERCANTILE EXCHANGE       147598.0   ...                  -3872.0                        -5.1
...                                                       ...            ...                            ...                         ...
2011-04-12  BRENT CRUDE OIL LAST DAY - NEW YORK MERCANTILE...        20546.0   ...                   -484.0                        17.1
2011-04-05  BRENT CRUDE OIL LAST DAY - NEW YORK MERCANTILE...        19533.0   ...                    655.0                        20.4
2011-03-29  BRENT CRUDE OIL LAST DAY - NEW YORK MERCANTILE...        18178.0   ...                   -276.0                        18.4
2011-03-15  BRENT CRUDE OIL LAST DAY - NEW YORK MERCANTILE...        20233.0   ...                    231.0                        17.9
2011-03-08  BRENT CRUDE OIL LAST DAY - NEW YORK MERCANTILE...        19639.0   ...                      NaN                        17.3
...

Financial Report (Financial Instruments)

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:

                                                Contract Name  Open Interest   ...  Net Change Lev Money  Net % of OI Lev Money
Date                                                                           ...                                                                                        
2023-07-11              UST 10Y NOTE - CHICAGO BOARD OF TRADE      4800091.0   ...              155532.0                  -26.8
2023-07-03              UST 10Y NOTE - CHICAGO BOARD OF TRADE      4737762.0   ...                7710.0                  -30.4
2023-06-27              UST 10Y NOTE - CHICAGO BOARD OF TRADE      4663919.0   ...              -51457.0                  -31.1
2023-06-20              UST 10Y NOTE - CHICAGO BOARD OF TRADE      4641767.0   ...              -53136.0                  -30.2
2023-06-13              UST 10Y NOTE - CHICAGO BOARD OF TRADE      4619668.0   ...               69602.0                  -29.1
...                                                       ...            ...   ...                   ...                    ...
2006-07-11  10-YEAR U.S. TREASURY NOTES - CHICAGO BOARD OF...      2112145.0   ...               28199.0                    1.8
2006-07-03  10-YEAR U.S. TREASURY NOTES - CHICAGO BOARD OF...      2136459.0   ...              -18122.0                    0.5
2006-06-27  10-YEAR U.S. TREASURY NOTES - CHICAGO BOARD OF...      2194364.0   ...               13929.0                    1.3
2006-06-20  10-YEAR U.S. TREASURY NOTES - CHICAGO BOARD OF...      2097072.0   ...              -27203.0                    0.6
2006-06-13  10-YEAR U.S. TREASURY NOTES - CHICAGO BOARD OF...      1912279.0   ...                   NaN                    2.2
...

List Available Contracts

from pycot.reports import CommitmentOfTraders

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

Output Example:

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

The CFTC releases the reports every Friday at 3:30pm Eastern Time.

pycot's People

Contributors

philsv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pycot's Issues

cache historical data

Please save downloaded Zips in data folder, if they are older than current year
Maybe add a config where to save them or "false" if you dont want to save at all. If nothing is set, defaults to "save to data folder"
Its not about the bandwidth, but the time improvement.

Example:

  • extract 20 different legacy futures one by one, without redownloading the entire file
  • while developing, shorten the time to wait to fetch the data, when you want to test your script again

error on unknown contract

when I request

df = cot_report(financial_report(), "CORN - CHICAGO BOARD OF TRADE")

I have a warning
/home/matze/.local/lib/python3.11/site-packages/pycot/utils.py:15: UserWarning: Could not infer format, so each element will be parsed individually, falling back to dateutil. To ensure parsing is consistent and as-expected, please specify a format.
df["Date"] = pd.to_datetime(df["Date"])

And i have an empty dataframe, hence Corn does not exists as a contract in that report type.
Could you create a function to list all available contracts of a specfied report type?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.