updata_qbh_hlfx.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 = '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/stocks?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]
  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. if len(thd.df_day.index) > 2:
  63. # 寻找顶底分型
  64. for x in range(index_len, len(thd.df_day.index)):
  65. m = x - 1
  66. # 底
  67. if ((thd.df_day.loc[x, 'high'] > thd.df_day.loc[x - 1, 'high']) and (
  68. thd.df_day.loc[x - 2, 'high'] > thd.df_day.loc[x - 1, 'high'])):
  69. # 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])):
  70. # stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
  71. thd.df_day.loc[x, 'HL'] = 'L*'
  72. while m:
  73. if thd.df_day.loc[m, 'HL'] == 'H':
  74. if (x - m) > 3:
  75. thd.df_day.loc[x, 'HL'] = 'L'
  76. if x == len(thd.df_day.index) - 1:
  77. print(stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '买买买买买!!')
  78. break
  79. elif (thd.df_day.loc[m, 'HL'] == 'L'):
  80. if thd.df_day.loc[x - 1, 'low'] < thd.df_day.loc[m - 1, 'low']:
  81. # 前一个为底,且中间存在不包含 or 更低的底
  82. thd.df_day.loc[x, 'HL'] = 'L'
  83. if x == len(thd.df_day.index) - 1:
  84. # pass
  85. print(stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '中继后的底————买吗?!')
  86. break
  87. else:
  88. break
  89. m = m - 1
  90. # 顶
  91. elif ((thd.df_day.loc[x, 'high'] < thd.df_day.loc[x - 1, 'high']) and (
  92. thd.df_day.loc[x - 2, 'high'] < thd.df_day.loc[x - 1, 'high'])):
  93. # 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])):
  94. # stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
  95. thd.df_day.loc[x, 'HL'] = 'H*'
  96. while m:
  97. if thd.df_day.loc[m, 'HL'] == 'L':
  98. if x - m > 3:
  99. thd.df_day.loc[x, 'HL'] = 'H'
  100. if x == len(thd.df_day.index) - 1:
  101. # print(stock, '!!!!!!!', '\n', '卖卖卖卖卖卖卖!')
  102. pass
  103. thd.df_day.loc[x, 9] = thd.df_day.loc[x, 'close'] - thd.df_day.loc[m, 'close']
  104. break
  105. elif (thd.df_day.loc[m, 'HL'] == 'H'):
  106. if thd.df_day.loc[x - 1, 'high'] > thd.df_day.loc[m - 1, 'high']:
  107. # 前一个为顶,且中间存在不包含 or 更高的顶
  108. thd.df_day.loc[x, 'HL'] = 'H'
  109. if x == len(thd.df_day.index) - 1:
  110. pass
  111. # print(stock, '/\/\/\/\/\/\/', '一顶更有一顶高!')
  112. break
  113. break
  114. m = m - 1
  115. else:
  116. thd.df_day.loc[x, 'HL'] = '-'
  117. # 更新数据库
  118. thd.df_day[index_len + 1:].to_sql('stk%s_%s' % (stock, fre), con=engine2, index=True, if_exists='append')
  119. else:
  120. # 没有历史数据表
  121. thd.df_day = pd.DataFrame(columns=('date', 'open', 'close', 'high', 'low', 'volume', 'money', 'HL'))
  122. thd.get_price = pd.read_sql_query(
  123. 'select date,open,close,high,low,volume,money from `stk%s_%s`' % (stock, fre), engine)
  124. # 先处理去包含
  125. for i in thd.get_price.index:
  126. if i == 0 or i == 1:
  127. thd.df_day = pd.concat([thd.df_day, thd.get_price.iloc[[i]]], ignore_index=True)
  128. # 不包含
  129. elif (thd.df_day.iloc[-1, 3] > thd.get_price.loc[i, 'high']
  130. and thd.df_day.iloc[-1, 4] > thd.get_price.loc[i, 'low']) \
  131. or (thd.df_day.iloc[-1, 3] < thd.get_price.loc[i, 'high']
  132. and thd.df_day.iloc[-1, 4] < thd.get_price.loc[i, 'low']):
  133. thd.df_day = pd.concat([thd.df_day, thd.get_price.loc[[i]]], ignore_index=True)
  134. # 包含
  135. else:
  136. # 左高,下降
  137. if thd.df_day.iloc[-2, 3] > thd.df_day.iloc[-1, 3]:
  138. thd.df_day.iloc[-1, 3] = min(thd.df_day.iloc[-1, 3], thd.get_price.loc[i, 'high'])
  139. thd.df_day.iloc[-1, 4] = min(thd.df_day.iloc[-1, 4], thd.get_price.loc[i, 'low'])
  140. else:
  141. # 右高,上升
  142. thd.df_day.iloc[-1, 3] = max(thd.df_day.iloc[-1, 3], thd.get_price.loc[i, 'high'])
  143. thd.df_day.iloc[-1, 4] = max(thd.df_day.iloc[-1, 4], thd.get_price.loc[i, 'low'])
  144. if len(thd.df_day.index) > 2:
  145. # 寻找顶底分型
  146. for x in range(index_len, len(thd.df_day.index)):
  147. m = x - 1
  148. # 底
  149. if ((thd.df_day.loc[x, 'high'] > thd.df_day.loc[x - 1, 'high']) and (
  150. thd.df_day.loc[x - 2, 'high'] > thd.df_day.loc[x - 1, 'high'])):
  151. # 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])):
  152. # stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
  153. thd.df_day.loc[x, 'HL'] = 'L*'
  154. while m:
  155. if thd.df_day.loc[m, 'HL'] == 'H':
  156. if (x - m) > 3:
  157. thd.df_day.loc[x, 'HL'] = 'L'
  158. if x == len(thd.df_day.index) - 1:
  159. print(stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '买买买买买!!')
  160. break
  161. elif (thd.df_day.loc[m, 'HL'] == 'L'):
  162. if thd.df_day.loc[x - 1, 'low'] < thd.df_day.loc[m - 1, 'low']:
  163. # 前一个为底,且中间存在不包含 or 更低的底
  164. thd.df_day.loc[x, 'HL'] = 'L'
  165. if x == len(thd.df_day.index) - 1:
  166. # pass
  167. print(stock, '$$$$$$$', '\n', thd.df_day.loc[x, 'date'], '中继后的底————买吗?!')
  168. break
  169. else:
  170. break
  171. m = m - 1
  172. # 顶
  173. elif ((thd.df_day.loc[x, 'high'] < thd.df_day.loc[x - 1, 'high']) and (
  174. thd.df_day.loc[x - 2, 'high'] < thd.df_day.loc[x - 1, 'high'])):
  175. # 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])):
  176. # stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
  177. thd.df_day.loc[x, 'HL'] = 'H*'
  178. while m:
  179. if thd.df_day.loc[m, 'HL'] == 'L':
  180. if x - m > 3:
  181. thd.df_day.loc[x, 'HL'] = 'H'
  182. if x == len(thd.df_day.index) - 1:
  183. # print(stock, '!!!!!!!', '\n', '卖卖卖卖卖卖卖!')
  184. pass
  185. thd.df_day.loc[x, 9] = thd.df_day.loc[x, 'close'] - thd.df_day.loc[m, 'close']
  186. break
  187. elif (thd.df_day.loc[m, 'HL'] == 'H'):
  188. if thd.df_day.loc[x - 1, 'high'] > thd.df_day.loc[m - 1, 'high']:
  189. # 前一个为顶,且中间存在不包含 or 更高的顶
  190. thd.df_day.loc[x, 'HL'] = 'H'
  191. if x == len(thd.df_day.index) - 1:
  192. pass
  193. # print(stock, '/\/\/\/\/\/\/', '一顶更有一顶高!')
  194. break
  195. break
  196. m = m - 1
  197. else:
  198. thd.df_day.loc[x, 'HL'] = '-'
  199. # 更新数据库
  200. thd.df_day.to_sql('stk%s_%s' % (stock, fre), con=engine2, index=True, if_exists='append')