updata_qbh_hlfx.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. from jqdatasdk import *
  2. import pandas as pd
  3. import pymysql
  4. from sqlalchemy import create_engine
  5. import threading
  6. from datetime import datetime as dt
  7. import datetime
  8. # auth('18616891214', 'Ea?*7f68nD.dafcW34d!')
  9. engine_stocks_list = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx_pool?charset=utf8')
  10. # stocks = list(get_all_securities(['stock'], date=dt.today().strftime('%Y-%m-%d')).index)
  11. stocks = pd.read_sql_query(
  12. 'select securities from stocks_list', engine_stocks_list)
  13. stocks = stocks.iloc[-1, 0]
  14. stocks = stocks.split(",")
  15. print(len(stocks), type(stocks), stocks)
  16. # stocks = stocks[0:1]
  17. start = dt.now()
  18. # 确定级别
  19. # 注意修改time delta
  20. # fre = '30m'
  21. for fre in ['1d', '30m']:
  22. start = dt.now()
  23. print(fre)
  24. # 连接数据库
  25. db = pymysql.connect(host='localhost',
  26. user='root',
  27. port=3307,
  28. password='r6kEwqWU9!v3',
  29. database='hlfx')
  30. cursor = db.cursor()
  31. cursor.execute("show tables like '%%%s%%' " % fre)
  32. table_list = [tuple[0] for tuple in cursor.fetchall()]
  33. print('取得 table_list %s' % fre)
  34. stk = locals()
  35. thd = threading.local()
  36. def hlfx(stocks, engine, engine2):
  37. for thd.stock in stocks:
  38. print(thd.stock)
  39. if ('stk%s_%s' % (thd.stock, fre)) in table_list:
  40. # 有历史数据
  41. index_len = pd.read_sql_table('stk%s_%s' % (thd.stock, fre), con=engine2).iloc[-1, 0]
  42. if index_len > 2:
  43. # 注意修改time delta
  44. startdate = pd.read_sql_table('stk%s_%s' % (thd.stock, fre), con=engine2).iloc[-1, 1]
  45. # startdate = pd.read_sql_table('stk%s_%s' % (stock, fre), con=engine2).iloc[-1, 1] + datetime.timedelta(minutes= 5)
  46. thd.get_price = pd.read_sql_query(
  47. 'select date,open,close,high,low,volume,money from `stk%s_%s`' % (thd.stock, fre), engine)
  48. thd.get_price = thd.get_price.loc[thd.get_price['date'] > startdate]
  49. thd.df_day = pd.read_sql_query(
  50. 'select date,open,close,high,low,volume,money,HL from `stk%s_%s`' % (thd.stock, fre), engine2)
  51. # 先处理去包含
  52. for i in thd.get_price.index:
  53. # 不包含
  54. if (thd.df_day.iloc[-1, 3] > thd.get_price.loc[i, 'high']
  55. and thd.df_day.iloc[-1, 4] > thd.get_price.loc[i, 'low']) \
  56. or (thd.df_day.iloc[-1, 3] < thd.get_price.loc[i, 'high']
  57. and thd.df_day.iloc[-1, 4] < thd.get_price.loc[i, 'low']):
  58. thd.df_day = pd.concat([thd.df_day, thd.get_price.loc[[i]]], ignore_index=True)
  59. # print(thd.df_day)
  60. # 包含
  61. else:
  62. # (new_df.iloc[-1,3]>=df_day.loc[i,'high'] and new_df.iloc[-1,4]<= df_day.loc[i,'low']):
  63. # 左高,下降
  64. if thd.df_day.iloc[-2, 3] > thd.df_day.iloc[-1, 3]:
  65. thd.df_day.iloc[-1, 3] = min(thd.df_day.iloc[-1, 3], thd.get_price.loc[i, 'high'])
  66. thd.df_day.iloc[-1, 4] = min(thd.df_day.iloc[-1, 4], thd.get_price.loc[i, 'low'])
  67. else:
  68. # 右高,上升
  69. thd.df_day.iloc[-1, 3] = max(thd.df_day.iloc[-1, 3], thd.get_price.loc[i, 'high'])
  70. thd.df_day.iloc[-1, 4] = max(thd.df_day.iloc[-1, 4], thd.get_price.loc[i, 'low'])
  71. # 寻找顶底分型
  72. if len(thd.df_day.index) > 2:
  73. # 寻找顶底分型
  74. for x in range(index_len, len(thd.df_day.index)):
  75. m = x - 1
  76. # 底
  77. if ((thd.df_day.loc[x, 'high'] > thd.df_day.loc[x - 1, 'high']) and (
  78. thd.df_day.loc[x - 2, 'high'] > thd.df_day.loc[x - 1, 'high'])):
  79. # if ((stk.df_day.loc[i-2, 'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-3,'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-1,'date'] != stk.fxdf.iloc[-1,0])):
  80. # stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
  81. thd.df_day.loc[x, 'HL'] = 'L*'
  82. while m:
  83. if thd.df_day.loc[m, 'HL'] == 'H':
  84. if (x - m) > 3:
  85. thd.df_day.loc[x, 'HL'] = 'L'
  86. if x == len(thd.df_day.index) - 1:
  87. print(thd.stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '买买买买买!!')
  88. break
  89. elif (thd.df_day.loc[m, 'HL'] == 'L'):
  90. if thd.df_day.loc[x - 1, 'low'] < thd.df_day.loc[m - 1, 'low']:
  91. # 前一个为底,且中间存在不包含 or 更低的底
  92. thd.df_day.loc[x, 'HL'] = 'L'
  93. if x == len(thd.df_day.index) - 1:
  94. # pass
  95. print(thd.stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '中继后的底————买吗?!')
  96. break
  97. else:
  98. break
  99. m = m - 1
  100. if m == 0:
  101. thd.df_day.loc[x, 'HL'] = 'L'
  102. # 顶
  103. elif ((thd.df_day.loc[x, 'high'] < thd.df_day.loc[x - 1, 'high']) and (
  104. thd.df_day.loc[x - 2, 'high'] < thd.df_day.loc[x - 1, 'high'])):
  105. # if ((stk.df_day.loc[i-2, 'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-3,'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-1,'date'] != stk.fxdf.iloc[-1,0])):
  106. # stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
  107. thd.df_day.loc[x, 'HL'] = 'H*'
  108. while m:
  109. if thd.df_day.loc[m, 'HL'] == 'L':
  110. if x - m > 3:
  111. thd.df_day.loc[x, 'HL'] = 'H'
  112. if x == len(thd.df_day.index) - 1:
  113. # print(stock, '!!!!!!!', '\n', '卖卖卖卖卖卖卖!')
  114. pass
  115. break
  116. elif (thd.df_day.loc[m, 'HL'] == 'H'):
  117. if thd.df_day.loc[x - 1, 'high'] > thd.df_day.loc[m - 1, 'high']:
  118. # 前一个为顶,且中间存在不包含 or 更高的顶
  119. thd.df_day.loc[x, 'HL'] = 'H'
  120. if x == len(thd.df_day.index) - 1:
  121. pass
  122. # print(stock, '/\/\/\/\/\/\/', '一顶更有一顶高!')
  123. break
  124. break
  125. m = m - 1
  126. if m == 0:
  127. thd.df_day.loc[x, 'HL'] = 'H'
  128. else:
  129. thd.df_day.loc[x, 'HL'] = '-'
  130. # 更新数据库
  131. thd.df_day[index_len + 1:].to_sql('stk%s_%s' % (thd.stock, fre), con=engine2, index=True, if_exists='append')
  132. else:
  133. # 没有历史数据表
  134. thd.df_day = pd.DataFrame(columns=('date', 'open', 'close', 'high', 'low', 'volume', 'money', 'HL'))
  135. thd.get_price = pd.read_sql_query(
  136. 'select date,open,close,high,low,volume,money from `stk%s_%s`' % (thd.stock, fre), engine)
  137. # 先处理去包含
  138. for i in thd.get_price.index:
  139. if i == 0 or i == 1:
  140. thd.df_day = pd.concat([thd.df_day, thd.get_price.iloc[[i]]], ignore_index=True)
  141. # 不包含
  142. elif (thd.df_day.iloc[-1, 3] > thd.get_price.loc[i, 'high']
  143. and thd.df_day.iloc[-1, 4] > thd.get_price.loc[i, 'low']) \
  144. or (thd.df_day.iloc[-1, 3] < thd.get_price.loc[i, 'high']
  145. and thd.df_day.iloc[-1, 4] < thd.get_price.loc[i, 'low']):
  146. thd.df_day = pd.concat([thd.df_day, thd.get_price.loc[[i]]], ignore_index=True)
  147. # 包含
  148. else:
  149. # 左高,下降
  150. if thd.df_day.iloc[-2, 3] > thd.df_day.iloc[-1, 3]:
  151. thd.df_day.iloc[-1, 3] = min(thd.df_day.iloc[-1, 3], thd.get_price.loc[i, 'high'])
  152. thd.df_day.iloc[-1, 4] = min(thd.df_day.iloc[-1, 4], thd.get_price.loc[i, 'low'])
  153. else:
  154. # 右高,上升
  155. thd.df_day.iloc[-1, 3] = max(thd.df_day.iloc[-1, 3], thd.get_price.loc[i, 'high'])
  156. thd.df_day.iloc[-1, 4] = max(thd.df_day.iloc[-1, 4], thd.get_price.loc[i, 'low'])
  157. if len(thd.df_day.index) > 2:
  158. # 寻找顶底分型
  159. for x in range(2, len(thd.df_day.index)):
  160. m = x - 1
  161. # 底
  162. if ((thd.df_day.loc[x, 'high'] > thd.df_day.loc[x - 1, 'high']) and (
  163. thd.df_day.loc[x - 2, 'high'] > thd.df_day.loc[x - 1, 'high'])):
  164. # if ((stk.df_day.loc[i-2, 'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-3,'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-1,'date'] != stk.fxdf.iloc[-1,0])):
  165. # stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
  166. thd.df_day.loc[x, 'HL'] = 'L*'
  167. while m:
  168. if thd.df_day.loc[m, 'HL'] == 'H':
  169. if (x - m) > 3:
  170. thd.df_day.loc[x, 'HL'] = 'L'
  171. if x == len(thd.df_day.index) - 1:
  172. print(thd.stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '买买买买买!!')
  173. break
  174. elif (thd.df_day.loc[m, 'HL'] == 'L'):
  175. if thd.df_day.loc[x - 1, 'low'] < thd.df_day.loc[m - 1, 'low']:
  176. # 前一个为底,且中间存在不包含 or 更低的底
  177. thd.df_day.loc[x, 'HL'] = 'L'
  178. if x == len(thd.df_day.index) - 1:
  179. # pass
  180. print(thd.stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '中继后的底————买吗?!')
  181. break
  182. else:
  183. break
  184. m = m - 1
  185. if m == 0:
  186. thd.df_day.loc[x, 'HL'] = 'L'
  187. # 顶
  188. elif ((thd.df_day.loc[x, 'high'] < thd.df_day.loc[x - 1, 'high']) and (
  189. thd.df_day.loc[x - 2, 'high'] < thd.df_day.loc[x - 1, 'high'])):
  190. # if ((stk.df_day.loc[i-2, 'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-3,'date'] != stk.fxdf.iloc[-1,0]) and (stk.df_day.loc[i-1,'date'] != stk.fxdf.iloc[-1,0])):
  191. # stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
  192. thd.df_day.loc[x, 'HL'] = 'H*'
  193. while m:
  194. if thd.df_day.loc[m, 'HL'] == 'L':
  195. if x - m > 3:
  196. thd.df_day.loc[x, 'HL'] = 'H'
  197. if x == len(thd.df_day.index) - 1:
  198. # print(stock, '!!!!!!!', '\n', '卖卖卖卖卖卖卖!')
  199. pass
  200. break
  201. elif (thd.df_day.loc[m, 'HL'] == 'H'):
  202. if thd.df_day.loc[x - 1, 'high'] > thd.df_day.loc[m - 1, 'high']:
  203. # 前一个为顶,且中间存在不包含 or 更高的顶
  204. thd.df_day.loc[x, 'HL'] = 'H'
  205. if x == len(thd.df_day.index) - 1:
  206. pass
  207. # print(stock, '/\/\/\/\/\/\/', '一顶更有一顶高!')
  208. break
  209. break
  210. m = m - 1
  211. if m == 0:
  212. thd.df_day.loc[x, 'HL'] = 'H'
  213. else:
  214. thd.df_day.loc[x, 'HL'] = '-'
  215. # 更新数据库
  216. thd.df_day.to_sql('stk%s_%s' % (thd.stock, fre), con=engine2, index=True, if_exists='append')
  217. step = 700
  218. thread_list = []
  219. engine = []
  220. engine2 = []
  221. times_engine = 0
  222. print(len(stocks))
  223. for i in range(0, len(stocks), step):
  224. engine.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8'))
  225. engine2.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx?charset=utf8'))
  226. thread = threading.Thread(target=hlfx, args=(stocks[i:i + step], engine[times_engine], engine2[times_engine]))
  227. times_engine = times_engine + 1
  228. thread.start()
  229. thread_list.append(thread)
  230. for thread in thread_list:
  231. thread.join()
  232. db.close()
  233. end= dt.now()
  234. print('总时长:', (end - start).seconds)