fushare Python Library

repository·master·Indexed 43 days ago

https://github.com/lowinli/fushare

A Python library providing a crawler interface for commodity futures data. It supports retrieving roll yields, registered receipts, spot prices, basis, member position rankings, and daily K-line data from exchanges including DCE, SHFE, CZCE, and CFFEX. Features include automated daily monitoring and data scraping for versions 1.2.1 and above.

Tokens
1.6K
Snippets
7
Records
9
Agent score
5%

What's inside fushare

  1. Configure QQ Mail SMTP for Python email notifications

    master

    To allow Python programs to send emails via QQ Mail, you must enable the SMTP service and generate an authorization code (secret). This authorization code is used as the password in your Python script instead of your actual QQ login password.

    1. Log in to your QQ Mail account.
    2. Go to Settings (设置).
    3. Navigate to the Account (账户) tab.
    4. Scroll down to find the SMTP service section.
    5. Enable the SMTP service and generate an Authorization Code (授权码).
    6. Use this generated code as the secret in your Python implementation.
  2. Configure daily monitoring and automated downloads

    master

    For fushare >= 1.2.1, you can automate data scraping and email notifications.

    1. Download the example files from the repository.
    2. Locate the example/setting configuration file.
    3. Configure the following keys:
      • root: The default directory where scraped data will be stored (ensure this directory exists).
      • qqEmail: Your QQ email address for receiving data notifications.
      • secret: Your QQ email SMTP password (requires SMTP service enabled on your QQ account).
    4. Run monitor.cmd to start the automated process. Data is automatically downloaded every day at 17:00.
  3. Get member position rankings

    master

    Access member position data through several methods:

    • get_rank_sum_daily(start, end, vars): Returns the sum of member positions for the top 5, 10, 15, or 20 ranks over a period.
    • Exchange-specific rank tables:
      • get_dce_rank_table() (Dalian Commodity Exchange)
      • get_cffex_rank_table() (China Financial Futures Exchange)
      • get_czce_rank_table() (Zhengzhou Commodity Exchange)
      • get_shfe_rank_table() (Shanghai Futures Exchange)

    Exchange Data Nuances:

    • DCE: Only publishes total position rankings by commodity (not split by delivery month).
    • SHFE/CFFEX: Publishes rankings by delivery month (commodity totals are calculated by summing contracts).
    • CZCE: Publishes both contract-level and commodity-level rankings (original exchange data).
    import fushare as f
    # Get sum of top rankings
    f.get_rank_sum_daily(start='20180718', end='20180719', vars=['IF', 'C'])
    
    # Get specific exchange rank table
    f.get_shfe_rank_table()
  4. Get specific contract roll yield with get_rollYield

    master

    Use get_rollYield to find the roll yield between two specific contracts on a specific date.

    import fushare as f
    f.get_rollYield(date='20180718', var='IF', symbol1='IF1812', symbol2='IF1811')
  5. Get daily K-line data with get_future_daily

    master

    Retrieve daily K-line data for contracts or weighted index bars from exchange websites.

    Parameters:

    • market: One of 'dce' (Dalian), 'shfe' (Shanghai), 'czce' (Zhengzhou), or 'cffex' (China Financial).
    • indexBar: If set to True, the function generates a synthetic index contract (e.g., RB99) weighted by open interest in the resulting DataFrame.
    import fushare as f
    # Get daily K-line data with open-interest weighted index
    f.get_future_daily(start='20190107', end='20190108', market='SHFE', indexBar=True)
  6. Get roll yield data with get_rollYield_bar

    master

    The get_rollYield_bar function downloads roll yield data. The type parameter determines the sequence type:

    • type='date': The spread between the most active (主力) and second most active (次主力) contracts for a specific commodity across different dates.
    • type='symbol': The prices of all delivery month contracts for a specific commodity on a specific date.
    • type='var': A combination of roll yields for the most active and second most active contracts across all commodities for a specific trading day.

    Note: Most active and second most active contracts are defined by sorting delivery month contracts by open interest (持仓量) in descending order.

    import fushare as f
    # Get roll yield for a commodity over a date range
    f.get_rollYield_bar(type='date', var='RB', start='20180618', end='20180718', plot=True)
    
    # Get roll yield for all contracts of a commodity on a specific date
    f.get_rollYield_bar(type='symbol', var='RB', date='20180718', plot=True)
    
    # Get roll yield for all commodities on a specific date
    f.get_rollYield_bar(type='var', date='20180718', plot=True)
  7. Get registered receipts with get_reciept

    master

    The get_reciept function retrieves daily registered receipt data from exchanges, which reflects market inventory changes.

    Note: The vars parameter must be a list, even if you are only requesting a single commodity.

    import fushare as f
    f.get_reciept(start='20180712', end='20180719', vars=['CU', 'NI'])
  8. Get spot prices and basis with get_spotPrice

    master

    Retrieve commodity basis data.

    get_spotPrice(date) returns data for a single day, including: commodity, spot price, nearest contract, nearest contract price, most active contract, most active contract price, nearest contract basis, most active contract basis, nearest contract basis rate, and most active contract basis rate.

    get_spotPrice_daily(start, end, vars) retrieves historical basis data for a range of dates and specific commodities.

    Note: Spot prices are scraped from the BizChina (生意社) website and are supported from 2011 to the present.

    import fushare as f
    # Get current day basis
    f.get_spotPrice('20180712')
    
    # Get historical basis for specific commodities
    f.get_spotPrice_daily(start='20180710', end='20180719', vars=['CU', 'RB'])