qbh.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. from jqdatasdk import *
  2. auth('18019403367', 'Qwer4321')
  3. import pandas as pd
  4. import pymysql
  5. from sqlalchemy import create_engine
  6. import threading
  7. from datetime import datetime as dt
  8. starttime = dt.now()
  9. # 连接数据库
  10. # db_stk_sql = pymysql.connect(host='localhost',
  11. # user='root',
  12. # port=3307,
  13. # password='r6kEwqWU9!v3',
  14. # database='stocks',
  15. # connect_timeout=600)
  16. #
  17. #
  18. # db_qbh = pymysql.connect(host='localhost',
  19. # user='root',
  20. # port=3307,
  21. # password='r6kEwqWU9!v3',
  22. # database='qbh',
  23. # charset='utf8')
  24. #
  25. #
  26. # cursor = db_qbh.cursor()
  27. # engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh_hlfx?charset=utf8')
  28. engine2 = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
  29. stocks = list(get_all_securities(['stock'], date='2021-12-31').index)
  30. # stocks =stocks[0:40]
  31. thd = threading.local()
  32. def qbh(stocks, engine, engine_backup):
  33. for stock in stocks:
  34. thd.new_df = pd.DataFrame(columns=('date', 'open', 'close', 'high', 'low', 'volume', 'money', 'HL'))
  35. # print(new_df.head())
  36. thd.df_day = stk['stk' + stock[:6]]
  37. for i in thd.df_day.index:
  38. if i == 0 or i == 1:
  39. thd.new_df = pd.concat([thd.new_df, thd.df_day.iloc[[i]]], ignore_index=True)
  40. # 不包含
  41. elif (thd.new_df.iloc[-1, 3] > thd.df_day.loc[i, 'high']
  42. and thd.new_df.iloc[-1, 4] > thd.df_day.loc[i, 'low']) \
  43. or (thd.new_df.iloc[-1, 3] < thd.df_day.loc[i, 'high']
  44. and thd.new_df.iloc[-1, 4] < thd.df_day.loc[i, 'low']):
  45. thd.new_df = pd.concat([thd.new_df, thd.df_day.iloc[[i]]], ignore_index=True)
  46. # 包含
  47. else:
  48. # (new_df.iloc[-1,3]>=df_day.loc[i,'high'] and new_df.iloc[-1,4]<= df_day.loc[i,'low']):
  49. # 左高,下降
  50. if thd.new_df.iloc[-2, 3] > thd.new_df.iloc[-1, 3]:
  51. thd.new_df.iloc[-1, 3] = min(thd.new_df.iloc[-1, 3], thd.df_day.loc[i, 'high'])
  52. thd.new_df.iloc[-1, 4] = min(thd.new_df.iloc[-1, 4], thd.df_day.loc[i, 'low'])
  53. else:
  54. # 右高,上升
  55. thd.new_df.iloc[-1, 3] = max(thd.new_df.iloc[-1, 3], thd.df_day.loc[i, 'high'])
  56. thd.new_df.iloc[-1, 4] = max(thd.new_df.iloc[-1, 4], thd.df_day.loc[i, 'low'])
  57. # thd.new_df.to_sql('stk%s_%s' % (stock[:6], u), con=engine, index=True, if_exists='replace')
  58. # with engine.connect() as con:
  59. # con.execute('ALTER TABLE stk%s_%s ADD PRIMARY KEY (`date`);' % (stock[:6], u))
  60. thd.new_df.to_sql('stk%s_%s' % (stock[:6], u), con=engine_backup, index=True, if_exists='replace')
  61. with engine_backup.connect() as con_backup:
  62. con_backup.execute('ALTER TABLE stk%s_%s ADD PRIMARY KEY (`date`);' % (stock[:6], u))
  63. # thd.new_df.to_csv(
  64. # '/Users/daniel/Library/CloudStorage/OneDrive-个人/个人/python_stocks/20220211qbh/qbh%s.csv' % stock[:6])
  65. print(stock)
  66. print("**************")
  67. #
  68. # # new_df.to_csv('new_df.csv')
  69. #
  70. # #return new_df
  71. stk = locals()
  72. engine = []
  73. engine_backup = []
  74. u = '1d'
  75. # 获取数据存入DataFrame
  76. for stock in stocks:
  77. stk['stk'+stock[:6]] = pd.read_sql_query('select date,open,close,high,low,volume,money from stk%s_%s' % (stock[:6], u), engine2)
  78. # print(stock, stk['stk'+stock[:6]])
  79. print("###############################################################################################################"
  80. "###############################################################################################################"
  81. "###############################################################################################################"
  82. "###############################################################################################################"
  83. "###############################################################################################################"
  84. "###############################################################################################################"
  85. "###############################################################################################################")
  86. # 开始去包含
  87. # qbh(stocks)
  88. thread_list = []
  89. step = 1000
  90. times_engine = 0
  91. for m in range(0, len(stocks), step):
  92. engine.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh_hlfx?charset=utf8', pool_recycle= 3600))
  93. engine_backup.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh_hlfx_backup?charset=utf8', pool_recycle= 3600))
  94. thread = threading.Thread(target=qbh, args=(stocks[m:m + step], engine[times_engine], engine_backup[times_engine]))
  95. times_engine =times_engine + 1
  96. thread.start()
  97. thread_list.append(thread)
  98. for thread in thread_list:
  99. thread.join()
  100. endtime = dt.now()
  101. print((endtime-starttime).seconds)