### Install fushare Library Source: https://context7.com/lowinli/fushare/llms.txt Installs the fushare Python library using pip. This command fetches and installs the latest version of the library and its dependencies from the Python Package Index (PyPI). ```bash pip install fushare ``` -------------------------------- ### Get Spot Prices and Basis Data (fushare) Source: https://context7.com/lowinli/fushare/llms.txt Retrieves commodity spot prices and calculates the basis (difference between futures and spot prices) from 100ppi.com. This function takes a specific date as input and returns a DataFrame with spot price, nearest futures contract price, and dominant futures contract price. ```python import fushare as f # Get spot prices and basis for a single date df = f.get_spotPrice('20180712') print(df) # Output columns: # var - commodity variety code # SP - spot price (float) # nearSymbol - nearest delivery contract symbol # nearPrice - nearest contract settlement price (float) # domSymbol - dominant contract symbol # domPrice - dominant contract settlement price (float) ``` -------------------------------- ### Get Warehouse Receipts Data (fushare) Source: https://context7.com/lowinli/fushare/llms.txt Retrieves registered warehouse receipt data from DCE, SHFE, and CZCE exchanges. This function requires a start and end date, and optionally a list of commodity varieties. The output includes receipt quantity, daily change, and date. ```python import fushare as f # Get warehouse receipt data for copper (CU) and nickel (NI) over a date range df = f.get_reciept( start='20180712', end='20180719', vars=['CU', 'NI'] # Must be a list even for single commodity ) print(df) # Output columns: # var - commodity variety code (string) # reciept - warehouse receipt quantity (int) # reciept_chg - daily change in receipts (int) # date - date in YYYYMMDD format (string) # Get receipts for all commodities (default behavior) df_all = f.get_reciept(start='20190301', end='20190310') print(df_all) ``` -------------------------------- ### Get Spot Price Daily Data - f.get_spotPrice_daily Source: https://context7.com/lowinli/fushare/llms.txt Retrieves historical spot price data for specified commodities and date ranges. Note that spot price data is available from 2011-01-04 onwards. This function is useful for analyzing basis relationships between contracts. ```python import fushare as f # Get historical basis data for specific commodities df_daily = f.get_spotPrice_daily( start='20180710', end='20180719', vars=['CU', 'RB'] # Copper and Rebar ) print(df_daily) # Note: Spot price data available from 2011-01-04 onwards ``` -------------------------------- ### Get Exchange-Specific Position Tables Source: https://context7.com/lowinli/fushare/llms.txt Retrieves detailed position ranking tables for individual exchanges (SHFE, DCE, CZCE, CFFEX). Each exchange publishes data in different formats: DCE publishes variety-level rankings, while SHFE and CFFEX publish contract-level rankings aggregated by variety. CZCE also provides contract-level rankings. ```python import fushare as f # Get SHFE (Shanghai Futures Exchange) position rankings shfe_data = f.get_shfe_rank_table(date='20190124', vars=['CU', 'AL', 'RB']) # Returns dict: {symbol: DataFrame} # Get DCE (Dalian Commodity Exchange) position rankings dce_data = f.get_dce_rank_table(date='20190124', vars=['M', 'C', 'I']) # Returns dict: {variety: DataFrame} # Get CZCE (Zhengzhou Commodity Exchange) position rankings czce_data = f.get_czce_rank_table(date='20190124', vars=['CF', 'SR', 'TA']) # Returns dict: {symbol: DataFrame} # Get CFFEX (China Financial Futures Exchange) position rankings cffex_data = f.get_cffex_rank_table(date='20190124', vars=['IF', 'IC', 'IH']) # Returns dict: {symbol: DataFrame} # Each DataFrame contains columns: # rank - position rank (1-20) # vol_party_name - member name for volume ranking # vol - trading volume # vol_chg - volume change # long_party_name - member name for long position ranking # long_openIntr - long open interest # long_openIntr_chg - long position change # short_party_name - member name for short position ranking # short_openIntr - short open interest # short_openIntr_chg - short position change # symbol - contract symbol # variety - commodity variety ``` -------------------------------- ### Get Daily OHLC Price Data - f.get_future_daily Source: https://context7.com/lowinli/fushare/llms.txt Retrieves daily OHLC (Open, High, Low, Close) bar data directly from exchange websites for all four Chinese futures exchanges. It can optionally generate weighted index contracts (e.g., RB99) using open interest weighting. The output includes price, volume, and open interest data. ```python import fushare as f # Get daily data from Shanghai Futures Exchange with index bars df = f.get_future_daily( start='20190107', end='20190108', market='SHFE', # Options: 'SHFE', 'DCE', 'CZCE', 'CFFEX' indexBar=True # Generate weighted index contracts (e.g., RB99) ) print(df) # Output columns: # symbol - contract code (e.g., 'rb1905' or 'RB99' for index) # date - date in YYYYMMDD format # open - opening price # high - highest price # low - lowest price # close - closing price # volume - trading volume # open_interest - open interest (positions) # turnover - trading turnover value # settle - settlement price # pre_settle - previous day settlement price # variety - commodity variety code # Get data from other exchanges df_czce = f.get_future_daily(start='20190107', end='20190108', market='CZCE') df_dce = f.get_future_daily(start='20190107', end='20190108', market='DCE') df_cffex = f.get_future_daily(start='20190107', end='20190108', market='CFFEX') ``` -------------------------------- ### Get Roll Yield Bar Data (fushare) Source: https://context7.com/lowinli/fushare/llms.txt Retrieves roll yield data for futures contracts, supporting time series, term structure, and cross-sectional queries. It requires specifying the query type, commodity variety, and date range or specific date. Optional plotting is available. ```python import fushare as f # Get roll yield time series for a specific commodity (RB = rebar steel) # Returns daily roll yield between dominant and sub-dominant contracts df_date = f.get_rollYield_bar( type='date', var='RB', start='20180618', end='20180718', plot=True # Optional: display matplotlib chart ) print(df_date) # Output columns: rollYield, nearBy (near month contract), deferred (far month contract) # Index: dates # Get term structure for a specific date (all delivery months for one commodity) df_symbol = f.get_rollYield_bar( type='symbol', var='RB', date='20180718', plot=True ) print(df_symbol) # Output: DataFrame with close prices for each delivery month contract # Get cross-sectional roll yield for all commodities on a specific date df_var = f.get_rollYield_bar( type='var', date='20180718', plot=True ) print(df_var) # Output columns: rollYield, nearBy, deferred, date # Index: commodity variety codes (RB, CU, AL, etc.) ``` -------------------------------- ### Get Individual Exchange Daily Data - f.get_shfe_daily Source: https://context7.com/lowinli/fushare/llms.txt Provides direct access to daily data for individual exchanges, with exchange-specific data handling. This function allows for fetching daily price information from the Shanghai Futures Exchange (SHFE). ```python import fushare as f # Shanghai Futures Exchange daily data shfe_df = f.get_shfe_daily(date='20190107') ``` -------------------------------- ### Get Member Position Rankings Daily - f.get_rank_sum_daily Source: https://context7.com/lowinli/fushare/llms.txt Retrieves aggregated position data from the top 5, 10, 15, and 20 member firms across all four exchanges. This data is useful for sentiment analysis and flow-based trading strategies. The output includes volume, volume change, and open interest data for different member tiers. ```python import fushare as f # Get aggregated member position rankings for IF (Index Futures) and C (Corn) df = f.get_rank_sum_daily( start='20180718', end='20180719', vars=['IF', 'C'] ) print(df) # Output columns include: # symbol - contract symbol # variety - commodity variety code # vol_top5 - total volume by top 5 members # vol_chg_top5 - volume change by top 5 members # long_openIntr_top5 - long positions by top 5 members # long_openIntr_chg_top5 - long position changes by top 5 # short_openIntr_top5 - short positions by top 5 members # short_openIntr_chg_top5 - short position changes by top 5 # vol_top10, vol_top15, vol_top20, etc. # date - date in YYYYMMDD format ``` -------------------------------- ### Automated Daily Data Monitoring and Email Notification Workflow Source: https://context7.com/lowinli/fushare/llms.txt A Python script demonstrating an automated workflow to download daily fundamental data and send email notifications. It includes functions to download various data types (roll yield, basis, position rankings, warehouse receipts) and a monitor function that checks the time and triggers the download process daily. ```python import fushare as f import datetime from time import sleep def download_daily_data(date): """Download all fundamental data for a given date""" date = f.cons.convert_date(date) calendar = f.cons.get_calendar() if date.strftime('%Y%m%d') not in calendar: print(f'{date} is not a trading day') return root = '/path/to/save/' # Roll yield cross-section df_roll = f.get_rollYield_bar(type='var', date=date) df_roll.to_csv(f'{root}roll_yield_{date}.csv') # Spot prices and basis df_basis = f.get_spotPrice(date) df_basis.to_csv(f'{root}basis_{date}.csv') # Member position rankings df_rank = f.get_rank_sum_daily(start=date, end=date) df_rank.to_csv(f'{root}position_rank_{date}.csv') # Warehouse receipts df_receipt = f.get_reciept(start=date, end=date) df_receipt.to_csv(f'{root}warehouse_receipt_{date}.csv') # Optional: send email notification attachments = [ f'roll_yield_{date}.csv', f'basis_{date}.csv', f'position_rank_{date}.csv', f'warehouse_receipt_{date}.csv' ] f.sendEmail( 'Daily Data Ready', 'your_email@qq.com', 'smtp_password', 'your_email@qq.com', 'smtp.qq.com', '465', attachments, root, SSL=True ) def monitor(trigger_time='17:00'): """Run continuously and trigger download at specified time""" while True: now = datetime.datetime.now() if now.strftime('%H:%M') == trigger_time: download_daily_data(now.strftime('%Y%m%d')) sleep(40) # Run the monitor if __name__ == '__main__': monitor() ``` -------------------------------- ### Fetch DCE, CZCE, CFFEX, and SHFE Daily Data Source: https://context7.com/lowinli/fushare/llms.txt Retrieves daily futures and options data from Dalian Commodity Exchange (DCE), daily data from Zhengzhou Commodity Exchange (CZCE) and China Financial Futures Exchange (CFFEX), and Volume-Weighted Average Price (VWAP) data from Shanghai Futures Exchange (SHFE) for a specified date. ```python import fushare as f # Dalian Commodity Exchange daily data (supports futures and options) dce_future_df = f.get_dce_daily(date='20190107', type='future') dce_option_df = f.get_dce_daily(date='20190107', type='option') # Zhengzhou Commodity Exchange daily data czce_df = f.get_czce_daily(date='20190107') # China Financial Futures Exchange daily data cffex_df = f.get_cffex_daily(date='20190107') # Shanghai Futures Exchange VWAP (Volume-Weighted Average Price) vwap_df = f.get_shfe_vwap(date='20190107') # Output columns: date, symbol, time_range, vwap # time_range: '9:00-10:15' or '9:00-15:00' ``` -------------------------------- ### Send Email Notifications with Attachments using fushare Source: https://context7.com/lowinli/fushare/llms.txt Utility function to send email notifications, optionally with file attachments. It requires sender and recipient email details, SMTP server information, and attachment names and root directory if applicable. Supports SSL for secure connections. ```python from fushare import sendEmail # Send email with CSV attachments via QQ Mail sendEmail( msg='Daily Futures Data Report', # Email subject fromEmail='your_email@qq.com', # Sender email password='your_smtp_password', # SMTP authorization code (not login password) toEmail='recipient@qq.com', # Recipient email host='smtp.qq.com', # SMTP server port='465', # SMTP port attachName=['data1.csv', 'data2.csv'], # Attachment filenames (string or list) attachRoot='/path/to/files/', # Directory containing attachments SSL=True # Use SSL (required for QQ Mail) ) # Without attachments sendEmail( msg='Data Download Complete', fromEmail='your_email@qq.com', password='your_smtp_password', toEmail='recipient@qq.com', host='smtp.qq.com', port='465', SSL=True ) ``` -------------------------------- ### Calculate Roll Yield (fushare) Source: https://context7.com/lowinli/fushare/llms.txt Calculates the annualized roll yield for futures contracts. It can compute the yield between the dominant and sub-dominant contracts of a commodity or between two specified contracts. Requires the date and commodity variety, and optionally contract symbols. ```python import fushare as f # Get roll yield for dominant and sub-dominant contracts of IF (CSI 300 Index Futures) result = f.get_rollYield(date='20180718', var='IF') print(result) # Output: tuple (rollYield, nearBy_symbol, deferred_symbol) # Calculate roll yield between two specific contracts result = f.get_rollYield( date='20180718', var='IF', symbol1='IF1812', symbol2='IF1811' ) print(result) # Output: (roll_yield_value, 'IF1811', 'IF1812') ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.