updata_qbh_hlfx.py 14 KB

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