### Example Usage of order_stock Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1h7k4iqb1grk4/mindoc-1h7k5j4drr928.html Demonstrates how to initialize the tqcenter library, get a stock account, and place a buy order using the `order_stock` function. Ensure you have the tqcenter library installed and configured. ```python from tqcenter import tq from tqcenter import tqconst tq.initialize(__file__) myAccount = tq.stock_account(account="1190008847", account_type="STOCK") print(myAccount) order_res = tq.order_stock(account_id=myAccount, stock_code="688318.SH", order_type=tqconst.STOCK_BUY, order_volume=200, price_type=tqconst.PRICE_MY, price=160.0) print(order_res) ``` -------------------------------- ### Initialize and Use get_pricevol Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1ctuhthaq5qmg/mindoc-1hce2o96aktmk.html This example demonstrates how to initialize the tqcenter library, get a list of all stocks, and then use get_pricevol to fetch their price and volume information. ```python from tqcenter import tq tq.initialize(__file__) all_stocks = tq.get_stock_list(market='23') pv_info = tq.get_pricevol(stock_list=all_stocks) print(pv_info) ``` -------------------------------- ### Initialize and Get Formula Data Example Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1h3hrvkp4sc0g/mindoc-1h3httgemshno.html This snippet demonstrates how to initialize the tq center, set formula data using `formula_set_data_info`, retrieve the K-line data with `formula_get_data`, and print the result. Ensure you have downloaded the corresponding post-market data. ```python from tqcenter import tq tq.initialize(__file__) formula_set_res = tq.formula_set_data_info(stock_code='688318.SH',stock_period='1d', count=5,dividend_type=1) formula_kline = tq.formula_get_data() print(formula_kline) ``` -------------------------------- ### Example Usage of stock_account Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1h7k4iqb1grk4/mindoc-1h7k4k5tk6q64.html Demonstrates how to initialize the tq library and use the stock_account function to get a handle for a specific stock account. The returned handle is then printed. ```python from tqcenter import tq tq.initialize(__file__) myAccount = tq.stock_account(account="1190008847", account_type="STOCK") print(myAccount) ``` -------------------------------- ### Start Main Loop Source: https://help.tdx.com.cn/quant/docs/markdown/gzh0122inweixinwenz/gzh20260302wzlz.html This snippet shows the standard Python entry point for starting the main loop of an application. ```python if __name__ == "__main__": main_loop() ``` -------------------------------- ### Initialize tqcenter and Get Stock List (Codes Only) Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1ctuhttn72svo/mindoc-1h10qo3uj48fg.html Demonstrates how to initialize the tqcenter library and call `get_stock_list` to retrieve only the stock codes. ```python from tqcenter import tq tq.initialize(__file__) stock_list = tq.get_stock_list('16') print(stock_list) ``` -------------------------------- ### Example Usage of get_market_data Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1ctuhthaq5qmg/mindoc-1h10g60jt68sc.html This example demonstrates how to fetch the latest daily K-line data for a specific stock without any adjustments. Ensure tqcenter is initialized before use. ```python from tqcenter import tq tq.initialize(__file__) df = tq.get_market_data( field_list=[], stock_list=['688318.SH'], start_time='20251220', end_time='', count=1, dividend_type='none', period='1d', fill_data=True ) print(df) ``` -------------------------------- ### Initialize and Get Market Snapshot Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1ctuhthaq5qmg/mindoc-1h10iig4pb6e0.html Initializes the TQ center and retrieves the market snapshot for a specified stock. Use this to get real-time quote data. ```python from tqcenter import tq tq.initialize(__file__) market_snapshot = tq.get_market_snapshot(stock_code = '688260.SH', field_list=[]) print(market_snapshot) ``` -------------------------------- ### Initialize tqcenter and Get Stock List (Codes and Names) Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1ctuhttn72svo/mindoc-1h10qo3uj48fg.html Shows how to initialize tqcenter and use `get_stock_list` with `list_type=1` to obtain both stock codes and their names. ```python from tqcenter import tq tq.initialize(__file__) stock_list2 = tq.get_stock_list('16',list_type=1) print(stock_list2) ``` -------------------------------- ### Initialize and Get Formula List Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1h3hrvkp4sc0g/mindoc-1hce2u769tljc.html This snippet demonstrates how to initialize the tqcenter library and call the `formula_get_all` function to get a list of technical indicator formulas. The results are then printed. ```python from tqcenter import tq tq.initialize(__file__) formule_all = tq.formula_get_all(formula_type=0) print(formule_all) ``` -------------------------------- ### Initialize and Get ETF Info Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1h13a594nhvb4/mindoc-1h6hknp6pjppc.html Demonstrates how to initialize the tq library and call the `get_trackzs_etf_info` function with a specific index code. The results are then printed. ```python from tqcenter import tq tq.initialize(__file__) trackzs_etf_info = tq.get_trackzs_etf_info(zs_code='950162.CSI') print(trackzs_etf_info) ``` -------------------------------- ### Initialize and Get All Formulas Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1h3hrvkp4sc0g/mindoc-1hce356csmgmo.html This snippet demonstrates how to initialize the tqcenter library and retrieve all formulas of a specific type. Use this to get a list of available formulas before querying for specific ones. ```python from tqcenter import tq tq.initialize(__file__) formule_all = tq.formula_get_all(formula_type=0) print(formule_all) ``` -------------------------------- ### Example of Renaming a Custom Sector Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1h139a4ckchkk/mindoc-1h10s7n863d50.html This example demonstrates how to initialize the TQ center and then use the rename_sector function to rename a custom sector. It prints the result of the operation. ```python from tqcenter import tq tq.initialize(__file__) rename_ptr = tq.rename_sector(block_code='CSBK', block_name='测试板块重命名') print(rename_ptr) ``` -------------------------------- ### Initialize and Get Stock Info Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1ctuhthaq5qmg/mindoc-1h10jj7r7jol4.html This snippet shows how to initialize the tqcenter library and then call the get_stock_info function with a specific stock code. The results are then printed. ```python from tqcenter import tq tq.initialize(__file__) fdc = tq.get_stock_info(stock_code='688318.SH', field_list=[]) print(fdc) ``` -------------------------------- ### Example Usage of formula_set_data_info Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1h3hrvkp4sc0g/mindoc-1h3hs08rn02uc.html This snippet demonstrates how to initialize the tq center and call the `formula_set_data_info` function with specific parameters. It then prints the result of the operation. ```python from tqcenter import tq tq.initialize(__file__) formula_set_res = tq.formula_set_data_info(stock_code='688318.SH',stock_period='1d', count=100,dividend_type=1) print(formula_set_res) ``` -------------------------------- ### Example Usage of get_divid_factors Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1ctuhthaq5qmg/mindoc-1h10hsiat36k4.html This example demonstrates how to use the get_divid_factors function to retrieve all dividend and distribution data for a specific stock code. Ensure tqcenter is imported and initialized. ```python from tqcenter import tq tq.initialize(__file__) dividend_factors = tq.get_divid_factors( stock_code='688318.SH', start_time='', end_time='') print(dividend_factors) ``` -------------------------------- ### Sample Output: Stock Codes Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1ctuhttn72svo/mindoc-1h10qo3uj48fg.html Example output when `get_stock_list` is called to return only stock codes. ```json ['881001.SH', '881006.SH', '881015.SH', '881061.SH', '881070.SH', '881090.SH', '881105.SH', '881129.SH', '881150.SH', '881166.SH', '881183.SH', '881199.SH', '881211.SH', '881230.SH', '881260.SH', '881286.SH', '881292.SH', '881318.SH', '881337.SH', '881351.SH', '881368.SH', '881385.SH', '881393.SH', '881405.SH', '881417.SH', '881426.SH', '881441.SH', '881458.SH', '881469.SH', '881477.SH'] ``` -------------------------------- ### Sample Output: Stock Codes and Names Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1ctuhttn72svo/mindoc-1h10qo3uj48fg.html Example output when `get_stock_list` is called with `list_type=1` to return stock codes and their corresponding names. ```json [{'Code': '881001.SH', 'Name': '煤炭'}, {'Code': '881006.SH', 'Name': '石油'}, {'Code': '881015.SH', 'Name': '化工'}, {'Code': '881061.SH', 'Name': '钢铁'}, {'Code': '881070.SH', 'Name': '有色'}, {'Code': '881090.SH', 'Name': '建材'}, {'Code': '881105.SH', 'Name': '农林牧渔'}, {'Code': '881129.SH', 'Name': '食品饮料'}, {'Code': '881150.SH', 'Name': '纺织服饰'}, {'Code': '881166.SH', 'Name': '轻工制造'}, {'Code': '881183.SH', 'Name': '家电'}, {'Code': '881199.SH', 'Name': '商贸'}, {'Code': '881211.SH', 'Name': '汽车'}, {'Code': '881230.SH', 'Name': '医药医疗'}, {'Code': '881260.SH', 'Name': '电力设备'}, {'Code': '881286.SH', 'Name': '国防军工'}, {'Code': '881292.SH', 'Name': '机械设备'}, {'Code': '881318.SH', 'Name': '电子'}, {'Code': '881337.SH', 'Name': '通信'}, {'Code': '881351.SH', 'Name': '计算机'}, {'Code': '881368.SH', 'Name': '传媒'}, {'Code': '881385.SH', 'Name': '银行'}, {'Code': '881393.SH', 'Name': '非银金融'}, {'Code': '881405.SH', 'Name': '建筑'}, {'Code': '881417.SH', 'Name': '房地产'}, {'Code': '881426.SH', 'Name': '社会服务'}, {'Code': '881441.SH', 'Name': '交通运输'}, {'Code': '881458.SH', 'Name': '公用事业'}, {'Code': '881469.SH', 'Name': '环保'}, {'Code': '881477.SH', 'Name': '综合'}] ``` -------------------------------- ### Initialize and Use get_gb_info Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1ctuhthaq5qmg/mindoc-1h3ru0b1tssrc.html Demonstrates how to initialize the tqcenter library and call the `get_gb_info` function with specific parameters. The output is then printed. ```python from tqcenter import tq tq.initialize(__file__) gb_info = tq.get_gb_info(stock_code = '688318.SH', date_list=['20250101','20250601'], count=2) print(gb_info) ``` -------------------------------- ### Get Formula Data Function Signature Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1h3hrvkp4sc0g/mindoc-1h3httgemshno.html This is the function signature for `formula_get_data`. It requires prior setup using `formula_set_data` or `formula_set_data_info`. ```python def formula_get_data(cls): ``` -------------------------------- ### Get Dividend Factors Function Signature Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1ctuhthaq5qmg/mindoc-1h10hsiat36k4.html This is the function signature for get_divid_factors. It takes a stock code, start time, and end time as input and returns a pandas DataFrame. ```python get_divid_factors(stock_code: str, start_time: str, end_time: str) -> pd.DataFrame: ``` -------------------------------- ### Initialize and Get Convertible Bond Info Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1h13a594nhvb4/mindoc-1h137euvcjn98.html Demonstrates how to initialize the tqcenter library and use the get_kzz_info function to fetch data for a specific convertible bond. The result is then printed. ```python from tqcenter import tq tq.initialize(__file__) kzz_info = tq.get_kzz_info(stock_code = '123039.SZ') print(kzz_info) ``` -------------------------------- ### Install Python Libraries using pip Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1cfsjkbf8f3is/mindoc-1d00970eq1rtc.html Installs common Python libraries using pip with a specific mirror for faster downloads. Ensure Python is added to your PATH during installation. ```bash pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple ``` ```bash pip install pandas -i https://pypi.tuna.tsinghua.edu.cn/simple ``` ```bash pip install backtrader -i https://pypi.tuna.tsinghua.edu.cn/simple ``` ```bash pip install vectorbt -i https://pypi.tuna.tsinghua.edu.cn/simple ``` -------------------------------- ### Example Usage of get_gb_info_by_date Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1ctuhthaq5qmg/mindoc-1hc4303vsv1fk.html Demonstrates how to import the tq library, initialize it, and call the get_gb_info_by_date function with specific parameters. Requires prior download of K-line data. ```python from tqcenter import tq tq.initialize(__file__) gb_info_date = tq.get_gb_info_by_date(stock_code='688318.SH', start_date='20260101', end_date='') print(gb_info_date) ``` -------------------------------- ### Initialize and Query Stock Account Asset Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1h7k4iqb1grk4/mindoc-1h84fvcjulrnc.html This snippet demonstrates how to initialize the TQCenter library, set up a stock account, and query its asset information. Ensure you have the correct account ID and type. ```python from tqcenter import tq tq.initialize(__file__) myAccount = tq.stock_account(account="1190008847", account_type="STOCK") print(myAccount) zc_res = tq.query_stock_asset(account_id=myAccount) print(zc_res) ``` -------------------------------- ### Tongdaxin Formula Example: Draw Icons Source: https://help.tdx.com.cn/quant/docs/markdown/gzh0122inweixinwenz Example Tongdaxin formula to draw buy and sell signal icons on the chart based on the BUY_SIGNAL and SELL_SIGNAL variables. ```tongdaxin {绘制交易信号图标} DRAWICON(BUY_SIGNAL, LOW, 1); DRAWICON(SELL_SIGNAL, HIGH, 2); ``` -------------------------------- ### Initialize and Get IPO/Bond Subscription Info Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1ctuhthaq5qmg/mindoc-1h137jr3khrqo.html This snippet shows how to initialize the tq library and use the get_ipo_info function to fetch both new stock and new bond subscription information for today and the future. The result is then printed. ```python from tqcenter import tq tq.initialize(__file__) ištipo_info = tq.get_ipo_info(ipo_type=2, ipo_date=1) print(ipo_info) ``` -------------------------------- ### Tongdaxin Formula Example: MA and Signals Source: https://help.tdx.com.cn/quant/docs/markdown/gzh0122inweixinwenz Example Tongdaxin formula to display Moving Averages (MA5, MA10) and buy/sell signals on a chart using the SIGNALS_TQ function. ```tongdaxin MA5:SIGNALS_TQ(1,0); {引用ID=1的数据(MA5)} MA10:SIGNALS_TQ(2,0); {引用ID=2的数据(MA10)} {交易信号} BUY_SIGNAL:=SIGNALS_TQ(3,0); {买入信号} SELL_SIGNAL:=SIGNALS_TQ(4,0);{卖出信号} ``` -------------------------------- ### Initialize tq and get stock info Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1ctuhthaq5qmg/mindoc-1h3rtq1hij0ac.html This snippet demonstrates how to initialize the tq library and call the get_more_info function to fetch data for a specific stock. The returned data is then printed. ```python from tqcenter import tq tq.initialize(__file__) more_info = tq.get_more_info(stock_code = '688318.SH', field_list=[]) print(more_info) ``` -------------------------------- ### Get All A-Share Stocks (Limited for Testing) Source: https://help.tdx.com.cn/quant/docs/markdown/gzh0122inweixinwenz/gzh20260302wzlz.html Fetches a list of all A-share stocks, with a limit applied for testing purposes. This is the initial step to get the universe of stocks to analyze. ```python all_stocks = tq.get_stock_list(market='5')[:150] ``` -------------------------------- ### Initialize tqcenter and Query Stock Positions Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1h7k4iqb1grk4/mindoc-1h7k5ar9kc508.html This Python snippet demonstrates how to initialize the tqcenter library, create a stock account object, and then query the stock positions for that account. It prints both the account object and the retrieved positions. ```python from tqcenter import tq tq.initialize(__file__) myAccount = tq.stock_account(account="1190008847", account_type="STOCK") print(myAccount) stock_positions = tq.query_stock_positions(account_id=myAccount) print(stock_positions) ``` -------------------------------- ### Stock Order Cancellation Example Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-1h7k4iqb1grk4/mindoc-1h84elp5atr6o.html Demonstrates how to initialize the tqcenter library, query existing stock orders, and then cancel a specific order using its details. Ensure you have the correct account and order information. ```python from tqcenter import tq from tqcenter import tqconst tq.initialize(__file__) myAccount = tq.stock_account(account="1190008847", account_type="STOCK") print(myAccount) stock_orders = tq.query_stock_orders(account_id=myAccount, stock_code="") print(stock_orders) cancel_res = tq.cancel_order_stock(account_id=myAccount, stock_code=stock_orders[0]['Code'], order_id=stock_orders[0]['Wtbh']) print(cancel_res) ``` -------------------------------- ### Add TDX Installation Directory to Python Path Source: https://help.tdx.com.cn/quant/docs/markdown/mindoc-tdxpy.html Add the absolute path of the TDX installation directory's PYPlugins\user to sys.path before importing tqcenter to allow running Python files from any location. ```python import sys sys.path.append('C:/new_tdx64/PYPlugins/user') from tqcenter import tq tq.initialize(__file__) ``` -------------------------------- ### Tongdaxin Formula Example: Backtest Metrics Source: https://help.tdx.com.cn/quant/docs/markdown/gzh0122inweixinwenz Example Tongdaxin formula to display various backtesting performance metrics (open/close counts, net value, win rate, etc.) using SIGNALS_TQ. ```tongdaxin {回测指标展示 - 这些指标会随着时间轴移动而动态变化} 开仓次数:SIGNALS_TQ(5,0),COLORRED; 平仓次数:SIGNALS_TQ(6,0),COLORGREEN; 单位净值:SIGNALS_TQ(7,0),COLORWHITE; 基准净值:SIGNALS_TQ(8,0),COLORYELLOW; 胜率:SIGNALS_TQ(9,0),COLORMAGENTA; 年化收益率:SIGNALS_TQ(10,0),COLORCYAN; 基准年化收益率:SIGNALS_TQ(11,0),COLORLIBLUE; 贝塔值:SIGNALS_TQ(12,0),COLORBROWN; 最大回撤:SIGNALS_TQ(13,0),COLORGRAY; 夏普比率:SIGNALS_TQ(14,0),COLORLIMAGENTA; 每日资金:SIGNALS_TQ(15,0),COLORLIGRAY; 持仓量:SIGNALS_TQ(16,0),COLORLIRED; {显示每日持仓量} ```