test_fundamentals.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import os
  2. import pandas as pd
  3. import xtquant.xtdata
  4. from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
  5. from xtquant.xttype import StockAccount
  6. from xtquant import xtdata, xtconstant
  7. from functools import reduce
  8. from datetime import datetime as dt
  9. import pymysql
  10. import math
  11. import multiprocessing as mp
  12. import traceback
  13. db_pool = pymysql.connect(host='localhost',
  14. user='root',
  15. port=3307,
  16. password='r6kEwqWU9!v3',
  17. database='hlfx_pool')
  18. cursor_pool = db_pool.cursor()
  19. def err_call_back(err):
  20. print(f'问题在这里~ error:{str(err)}')
  21. traceback.print_exc()
  22. def to_sql(stocks, sector_list):
  23. print(f'my pid is {os.getpid()}')
  24. for stock in stocks:
  25. sec_lt = []
  26. for i in sector_list:
  27. sector = xtdata.get_stock_list_in_sector(i)
  28. if stock in sector:
  29. sec_lt.append(i)
  30. print(f'{stock}属于:')
  31. print(f'{sec_lt}板块')
  32. results_list = ','.join(set(sec_lt))
  33. print(results_list)
  34. sql = "INSERT INTO sector_data (stock_code, sector) VALUES('%s', '%s')" \
  35. % (stock, results_list)
  36. cursor_pool.execute(sql)
  37. db_pool.commit()
  38. if __name__ == '__main__':
  39. sttime = dt.now()
  40. stock_list = xtdata.get_stock_list_in_sector('沪深A股')
  41. sector_list = xtdata.get_sector_list()
  42. step = math.ceil(len(stock_list) / mp.cpu_count())
  43. pool = mp.Pool(processes=mp.cpu_count())
  44. for i in range(0, len(stock_list), step):
  45. pool.apply_async(func=to_sql, args=(stock_list[i:i+step], sector_list,), error_callback=err_call_back)
  46. pool.close()
  47. pool.join()
  48. print(dt.now()-sttime)
  49. # exit()
  50. # pd.set_option('display.unicode.ambiguous_as_wide', True)
  51. # pd.set_option('display.unicode.east_asian_width', True)
  52. # pd.set_option('display.width', 1000)
  53. #
  54. # stocks = xtdata.get_stock_list_in_sector('沪深A股')
  55. # # xtdata.download_financial_data(stocks, ['Balance', 'Income', 'CashFlow'])
  56. # stocks.sort()
  57. # funda = xtdata.get_financial_data(stocks[0:10], ['Balance', 'Income', 'CashFlow'], start_time='20220101', end_time='20230220')
  58. # for stock in stocks[0:10]:
  59. # bal = funda[stock]['Balance'][['m_timetag', 'goodwill']]
  60. # profit = funda[stock]['Income'][['m_timetag', 'tot_profit', 'net_profit_incl_min_int_inc_after', 's_fa_eps_basic']]
  61. # cflow = funda[stock]['CashFlow'][['m_timetag', 'net_profit']]
  62. # dfs = [bal, profit, cflow]
  63. # df = reduce(lambda x, y: pd.merge(x, y, on='m_timetag', how='inner'), dfs)
  64. # df.columns = ['披露时间', '商誉', '利润总额', '净利润', '每股收益', '净利润']
  65. #
  66. # print(stock, '\n', df)