updata_qbh_hlfx.py 12 KB

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