hlfx.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import threading
  2. import pymysql
  3. import pandas as pd
  4. from threading import Thread, current_thread, local
  5. from sqlalchemy import create_engine
  6. from datetime import datetime as dt
  7. starttime = dt.now()
  8. time = dt.strptime(dt.strftime(dt.now(),'%H:%M:%S'),'%H:%M:%S')
  9. # 数据库引擎
  10. # engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh_hlfx?charset=utf8')
  11. # 连接数据库
  12. db = pymysql.connect(host='localhost',
  13. user='root',
  14. port=3307,
  15. password='r6kEwqWU9!v3',
  16. database='qbh_hlfx')
  17. fre = '1d'
  18. cursor = db.cursor()
  19. cursor.execute("select table_name from information_schema.tables where table_schema='qbh_hlfx' and table_name like {}".format('\'%{}\''.format(fre)))
  20. table_list = [tuple[0] for tuple in cursor.fetchall()]
  21. # print(table_list)
  22. stk = threading.local()
  23. engine = locals()
  24. # 主程序
  25. # 找顶底(hdx lfx)分型
  26. def hlfx(table_list,engine):
  27. for table in table_list:
  28. # stk.fxdf = pd.DataFrame(columns=('date', 'open', 'close', 'high', 'low', 'volume', 'money', 'HL'))
  29. stk.df_day = pd.read_sql_query('select date,open,close,high,low,volume,money,HL from %s' % table, engine)
  30. for i in stk.df_day.index:
  31. m = i-1
  32. if i <= 3:
  33. # stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
  34. stk.df_day.loc[i, 'HL'] = '-'
  35. # 底
  36. elif ((stk.df_day.loc[i,'high']>stk.df_day.loc[i-1,'high']) and (stk.df_day.loc[i-2,'high']>stk.df_day.loc[i-1,'high'])):
  37. # 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])):
  38. # stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
  39. while m:
  40. if (stk.df_day.loc[m, 'HL'] == 'H' and (i-m) > 3) \
  41. or (stk.df_day.loc[m, 'HL'] == 'L' and stk.df_day.loc[i-1, 'low'] < stk.df_day.loc[m-1, 'low']):
  42. # 前一个为顶,且中间存在不包含 or 更低的底
  43. stk.df_day.loc[i, 'HL'] = 'L'
  44. break
  45. m = m-1
  46. # 顶
  47. elif ((stk.df_day.loc[i,'high']<stk.df_day.loc[i-1,'high']) and (stk.df_day.loc[i-2,'high']<stk.df_day.loc[i-1,'high'])):
  48. # 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])):
  49. # stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
  50. while m:
  51. if (stk.df_day.loc[m, 'HL'] == 'L' and (i-m) > 3) \
  52. or (stk.df_day.loc[m, 'HL'] == 'H' and stk.df_day.loc[i-1, 'high'] > stk.df_day.loc[m-1, 'high']):
  53. # 前一个为底,且中间存在不包含 or 更高的顶
  54. stk.df_day.loc[i, 'HL'] = 'H'
  55. break
  56. m = m-1
  57. else:
  58. stk.df_day.loc[i, 'HL'] = '-'
  59. # stk.df_day.to_sql('%s' % table, con=engine, index=True, if_exists='replace', chunksize=20000)
  60. print(table, '\n', stk.df_day)
  61. stk.df_day.to_csv('/Users/daniel/Library/CloudStorage/OneDrive-个人/个人/python_stocks/20220212hlfx/this%s.csv' % table)
  62. # table_list = ['stk002237_1d','stk000002_1d']
  63. # hlfx(table_list)
  64. step = 50
  65. for i in range(0, len(table_list), step):
  66. engine[i] = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh_hlfx_backup?charset=utf8')
  67. threading.Thread(target=hlfx, args=(table_list[i:i + step], engine[i])).start()
  68. endtime=dt.now()
  69. print((endtime - starttime).seconds)