real_time_signal.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import pandas as pd
  2. from jqdatasdk import *
  3. auth('18616891214', 'Ea?*7f68nD.dafcW34d!')
  4. import threading
  5. from sqlalchemy import create_engine
  6. import pymysql
  7. from datetime import datetime as dt
  8. start = dt.now()
  9. # 确定级别
  10. fre = '1d'
  11. # 连接数据库
  12. db = pymysql.connect(host='localhost',
  13. user='root',
  14. port=3307,
  15. password='r6kEwqWU9!v3',
  16. database='hlfx')
  17. # engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
  18. engine2 = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx?charset=utf8')
  19. # 获取所有表名——确定计算范围
  20. cursor = db.cursor()
  21. cursor.execute("show tables like '%%%s%%' "% fre)
  22. # stocks = [tuple[0] for tuple in cursor.fetchall()]
  23. stocks = list(get_all_securities(['stock'], date='2021-12-31').index)
  24. # stocks = ['301058.XSHE']
  25. # stocks = stocks[0:500]
  26. print(dt.now(), 'stocks范围已获取!')
  27. # 获取各stock的去包含dataframe
  28. stk = locals()
  29. for stock in stocks:
  30. try:
  31. stk[stock] = pd.read_sql_query('select date,open,close,high,low,volume,money,HL from `stk%s_%s`' % (stock, fre),
  32. engine2)
  33. except BaseException:
  34. continue
  35. print(dt.now(), '数据库数据已赋值!')
  36. thd = threading.local()
  37. def qbh_hlfx(stocks, df):
  38. for stock in stocks:
  39. try:
  40. # thd.new_df = pd.DataFrame(columns=('date', 'open', 'close', 'high', 'low', 'volume', 'money', 'HL'))
  41. thd.df_day = stk[stock]
  42. thd.get_bars = df.loc[stock]
  43. stk_len = len(thd.df_day)
  44. # 先处理去包含
  45. for i in thd.get_bars.index:
  46. # 不包含
  47. if (thd.df_day.iloc[-1, 3] > thd.get_bars.loc[i, 'high']
  48. and thd.df_day.iloc[-1, 4] > thd.get_bars.loc[i, 'low']) \
  49. or (thd.df_day.iloc[-1, 3] < thd.get_bars.loc[i, 'high']
  50. and thd.df_day.iloc[-1, 4] < thd.get_bars.loc[i, 'low']):
  51. thd.df_day = pd.concat([thd.df_day, thd.get_bars.iloc[[i]]], ignore_index=True)
  52. # 包含
  53. else:
  54. # (new_df.iloc[-1,3]>=df_day.loc[i,'high'] and new_df.iloc[-1,4]<= df_day.loc[i,'low']):
  55. # 左高,下降
  56. if thd.df_day.iloc[-2, 3] > thd.df_day.iloc[-1, 3]:
  57. thd.df_day.iloc[-1, 3] = min(thd.df_day.iloc[-1, 3], thd.get_bars.loc[i, 'high'])
  58. thd.df_day.iloc[-1, 4] = min(thd.df_day.iloc[-1, 4], thd.get_bars.loc[i, 'low'])
  59. else:
  60. # 右高,上升
  61. thd.df_day.iloc[-1, 3] = max(thd.df_day.iloc[-1, 3], thd.get_bars.loc[i, 'high'])
  62. thd.df_day.iloc[-1, 4] = max(thd.df_day.iloc[-1, 4], thd.get_bars.loc[i, 'low'])
  63. # return thd.df_day
  64. if len(thd.df_day.index) > 2:
  65. # 寻找顶底分型
  66. for x in range(stk_len, len(thd.df_day.index)):
  67. m = x - 1
  68. # 底
  69. if ((thd.df_day.loc[x,'high']>thd.df_day.loc[x-1,'high']) and (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(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(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 (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. print(thd.df_day)
  118. else:
  119. pass
  120. except BaseException:
  121. continue
  122. # while True:
  123. df = get_bars(stocks, count=2, unit=fre,
  124. fields=['date', 'open', 'close', 'high', 'low', 'volume', 'money'], include_now=True, df=True)
  125. print(dt.now(), 'get_bars 成功')
  126. # strattime = dt.now()
  127. qbh_hlfx(stocks, df)
  128. # endtime = dt.now()
  129. # end = dt.now()
  130. # print('单次时长为:', (endtime - strattime).seconds, '\n', '全时长为:', (end - start).seconds)