qbh.py 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. # 连接数据库
  8. # db_stk_sql = pymysql.connect(host='localhost',
  9. # user='root',
  10. # port=3307,
  11. # password='r6kEwqWU9!v3',
  12. # database='stocks',
  13. # connect_timeout=600)
  14. #
  15. #
  16. # db_qbh = pymysql.connect(host='localhost',
  17. # user='root',
  18. # port=3307,
  19. # password='r6kEwqWU9!v3',
  20. # database='qbh',
  21. # charset='utf8')
  22. #
  23. #
  24. # cursor = db_qbh.cursor()
  25. # engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh_hlfx?charset=utf8')
  26. engine2 = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
  27. stocks = list(get_all_securities(['stock'], date='2021-12-31').index)
  28. # stocks =stocks[0:40]
  29. thd = threading.local()
  30. def qbh(stocks , engine):
  31. for stock in stocks:
  32. print(stock)
  33. thd.new_df = pd.DataFrame(columns=('date', 'open', 'close', 'high', 'low', 'volume', 'money', 'HL'))
  34. # print(new_df.head())
  35. thd.df_day = stk['stk' + stock[:6]]
  36. for i in thd.df_day.index:
  37. if i == 0 or i == 1:
  38. thd.new_df = pd.concat([thd.new_df, thd.df_day.iloc[[i]]], ignore_index=True)
  39. # 不包含
  40. elif (thd.new_df.iloc[-1, 3] > thd.df_day.loc[i, 'high']
  41. and thd.new_df.iloc[-1, 4] > thd.df_day.loc[i, 'low']) \
  42. or (thd.new_df.iloc[-1, 3] < thd.df_day.loc[i, 'high']
  43. and thd.new_df.iloc[-1, 4] < thd.df_day.loc[i, 'low']):
  44. thd.new_df = pd.concat([thd.new_df, thd.df_day.iloc[[i]]], ignore_index=True)
  45. # 包含
  46. else:
  47. # (new_df.iloc[-1,3]>=df_day.loc[i,'high'] and new_df.iloc[-1,4]<= df_day.loc[i,'low']):
  48. if thd.new_df.iloc[-2, 3] > thd.new_df.iloc[-1, 3]: #左高,下降
  49. thd.new_df.iloc[-1, 3] = min(thd.new_df.iloc[-1, 3], thd.df_day.loc[i, 'high'])
  50. thd.new_df.iloc[-1, 4] = min(thd.new_df.iloc[-1, 4], thd.df_day.loc[i, 'low'])
  51. else:
  52. # 右高,上升
  53. thd.new_df.iloc[-1, 3] = max(thd.new_df.iloc[-1, 3], thd.df_day.loc[i, 'high'])
  54. thd.new_df.iloc[-1, 4] = max(thd.new_df.iloc[-1, 4], thd.df_day.loc[i, 'low'])
  55. thd.new_df.to_sql('stk%s_%s' % (stock[:6], u), con=engine, index=True, if_exists='replace')
  56. with engine.connect() as con:
  57. con.execute('ALTER TABLE stk%s_%s ADD PRIMARY KEY (`date`);'% (stock[:6], u))
  58. print(stock)
  59. print("**************")
  60. # new_df.to_csv('new_df.csv')
  61. #return new_df
  62. stk = locals()
  63. engine = locals()
  64. u = '1d'
  65. # 获取数据存入DataFrame
  66. for stock in stocks:
  67. stk['stk'+stock[:6]] = pd.read_sql_query('select date,open,close,high,low,volume,money from stk%s_%s' % (stock[:6], u), engine2)
  68. # print(stock, stk['stk'+stock[:6]])
  69. print("###############################################################################################################"
  70. "###############################################################################################################"
  71. "###############################################################################################################"
  72. "###############################################################################################################"
  73. "###############################################################################################################"
  74. "###############################################################################################################"
  75. "###############################################################################################################")
  76. # 开始去包含
  77. # qbh(stocks)
  78. for m in range(0, len(stocks), 50):
  79. engine[m] = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh_hlfx?charset=utf8', pool_recycle=3600)
  80. threading.Thread(target=qbh, args=(stocks[m:m + 50], engine[m])).start()