hlfx.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import threading
  2. import pymysql
  3. import pandas as pd
  4. from sqlalchemy import create_engine
  5. # 数据库引擎
  6. # engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh_hlfx?charset=utf8')
  7. # 连接数据库
  8. db = pymysql.connect(host='localhost',
  9. user='root',
  10. port=3307,
  11. password='r6kEwqWU9!v3',
  12. database='qbh_hlfx_backup')
  13. fre = '1d'
  14. cursor = db.cursor()
  15. # cursor.execute("select table_name from information_schema.tables where table_schema='qbh_hlfx_backup' and table_name like {}".format('\'%{}\''.format(fre)))
  16. cursor.execute('show tables like {}'.format('\'%{}\''.format(fre)))
  17. table_list = [tuple[0] for tuple in cursor.fetchall()]
  18. # print(table_list)
  19. stk = threading.local()
  20. # 主程序
  21. # 找顶底(hdx lfx)分型
  22. def hlfx(table_list, engine, tosql):
  23. for table in table_list:
  24. # stk.fxdf = pd.DataFrame(columns=('date', 'open', 'close', 'high', 'low', 'volume', 'money', 'HL'))
  25. stk.df_day = pd.read_sql_query('select date,open,close,high,low,volume,money,HL from %s' % table, engine)
  26. stk.df_day.to_sql(name='%s' % table, con=tosql, index=True, if_exists='replace')
  27. with tosql.connect() as con_backup:
  28. con_backup.execute('ALTER TABLE %s ADD PRIMARY KEY (`date`);' % table)
  29. for i in stk.df_day.index:
  30. m = i - 1
  31. if i <= 3:
  32. # stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
  33. stk.df_day.loc[i, 'HL'] = '-'
  34. # 底
  35. 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'])):
  36. # 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])):
  37. # stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
  38. stk.df_day.loc[i, 'HL'] = 'L*'
  39. while m:
  40. if m == 1:
  41. stk.df_day.loc[i, 'HL'] = 'l'
  42. elif stk.df_day.loc[m, 'HL'] == 'H' or stk.df_day.loc[m, 'HL'] == 'h':
  43. if(i-m) > 3:
  44. stk.df_day.loc[i, 'HL'] = 'L'
  45. break
  46. elif (stk.df_day.loc[m, 'HL'] == 'L' or stk.df_day.loc[m, 'HL'] == 'l'):
  47. if stk.df_day.loc[i-1, 'low'] < stk.df_day.loc[m-1, 'low']:
  48. # 前一个为顶,且中间存在不包含 or 更低的底
  49. stk.df_day.loc[i, 'HL'] = 'L'
  50. break
  51. else:
  52. break
  53. m = m-1
  54. # 顶
  55. 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'])):
  56. # 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])):
  57. # stk.fxdf = pd.concat([stk.fxdf, stk.df_day.iloc[[i]]], ignore_index=True)
  58. stk.df_day.loc[i, 'HL'] = 'H*'
  59. while m:
  60. if m == 1:
  61. stk.df_day.loc[i, 'HL'] = 'h'
  62. elif stk.df_day.loc[m, 'HL'] == 'L' or stk.df_day.loc[m, 'HL'] == 'l':
  63. if i-m > 3:
  64. stk.df_day.loc[i, 'HL'] = 'H'
  65. stk.df_day.loc[i, 9] = stk.df_day.loc[i, 'close'] - stk.df_day.loc[m, 'close']
  66. break
  67. elif (stk.df_day.loc[m, 'HL'] == 'H' or stk.df_day.loc[m, 'HL'] == 'h'):
  68. if stk.df_day.loc[i-1, 'high'] > stk.df_day.loc[m-1, 'high']:
  69. # 前一个为底,且中间存在不包含 or 更高的顶
  70. stk.df_day.loc[i, 'HL'] = 'H'
  71. break
  72. break
  73. m = m-1
  74. else:
  75. stk.df_day.loc[i, 'HL'] = '-'
  76. stk.df_day.to_sql('%s' % table, con=engine, index=True, if_exists='replace', chunksize=20000)
  77. print(table, '\n', stk.df_day)
  78. stk.df_day.to_csv('/Users/daniel/Library/CloudStorage/OneDrive-个人/个人/python_stocks/20220212hlfx2/hlfx%s.csv' % table)
  79. stk.df_day.to_sql(name='%s' % table, con=tosql, index=True, if_exists='replace')
  80. with tosql.connect() as con_backup:
  81. con_backup.execute('ALTER TABLE %s ADD PRIMARY KEY (`date`);' % table)
  82. # table_list = ['stk002237_1d','stk000004_1d']
  83. # engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh_hlfx_backup?charset=utf8')
  84. # tosql = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/bb22?charset=utf8')
  85. # hlfx(table_list, engine, tosql)
  86. step = 50
  87. thread_list = []
  88. engine = []
  89. tosql = []
  90. for i in range(0, len(table_list), step):
  91. engine.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/qbh_hlfx_backup?charset=utf8'))
  92. tosql.append(create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/bb22?charset=utf8'))
  93. thread = threading.Thread(target=hlfx, args=(table_list[i:i + step], engine[i], tosql[i],))
  94. thread.start()
  95. thread_list.append(thread)
  96. for thread in thread_list:
  97. thread.join()