123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- import multiprocessing as mp
- import pandas as pd
- import pymysql
- from sqlalchemy import create_engine
- from datetime import datetime as dt
- import datetime
- def hlfx(stocks,fre,table_list):
- engine = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/stocks?charset=utf8')
- engine2 = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx?charset=utf8')
- for stock in stocks:
-
- if ('stk%s_%s' % (stock, fre)) in table_list:
-
- index_len = pd.read_sql_table('stk%s_%s' % (stock, fre), con=engine2).iloc[-1, 0]
-
- startdate = pd.read_sql_table('stk%s_%s' % (stock, fre), con=engine2).iloc[-1, 1]
-
- get_price = pd.read_sql_query(
- 'select date,open,close,high,low,volume,money from `stk%s_%s`' % (stock, fre), engine)
- get_price = get_price.loc[get_price['date'] > startdate]
- df_day = pd.read_sql_query(
- 'select date,open,close,high,low,volume,money,HL from `stk%s_%s`' % (stock, fre), engine2)
- if index_len > 2:
-
- for i in get_price.index:
-
- if (df_day.iloc[-1, 3] > get_price.loc[i, 'high']
- and df_day.iloc[-1, 4] > get_price.loc[i, 'low']) \
- or (df_day.iloc[-1, 3] < get_price.loc[i, 'high']
- and df_day.iloc[-1, 4] < get_price.loc[i, 'low']):
- df_day = pd.concat([df_day, get_price.loc[[i]]], ignore_index=True)
-
-
- else:
-
-
- if df_day.iloc[-2, 3] > df_day.iloc[-1, 3]:
- df_day.iloc[-1, 3] = min(df_day.iloc[-1, 3], get_price.loc[i, 'high'])
- df_day.iloc[-1, 4] = min(df_day.iloc[-1, 4], get_price.loc[i, 'low'])
- else:
-
- df_day.iloc[-1, 3] = max(df_day.iloc[-1, 3], get_price.loc[i, 'high'])
- df_day.iloc[-1, 4] = max(df_day.iloc[-1, 4], get_price.loc[i, 'low'])
-
- if len(df_day.index) > 2:
-
- for x in range(index_len, len(df_day.index)):
- m = x - 1
-
- if ((df_day.loc[x, 'high'] > df_day.loc[x - 1, 'high']) and (
- df_day.loc[x - 2, 'high'] > df_day.loc[x - 1, 'high'])):
-
-
- df_day.loc[x, 'HL'] = 'L*'
- while m:
- if df_day.loc[m, 'HL'] == 'H':
- if (x - m) > 3:
- df_day.loc[x, 'HL'] = 'L'
- if x == len(df_day.index) - 1:
-
- pass
- break
- elif (df_day.loc[m, 'HL'] == 'L'):
- if df_day.loc[x - 1, 'low'] < df_day.loc[m - 1, 'low']:
-
- df_day.loc[x, 'HL'] = 'L'
- if x == len(df_day.index) - 1:
- pass
-
-
- break
- else:
- break
- m = m - 1
- if m == 0:
- df_day.loc[x, 'HL'] = 'L'
-
- elif ((df_day.loc[x, 'high'] < df_day.loc[x - 1, 'high']) and (
- df_day.loc[x - 2, 'high'] < df_day.loc[x - 1, 'high'])):
-
-
- df_day.loc[x, 'HL'] = 'H*'
- while m:
- if df_day.loc[m, 'HL'] == 'L':
- if x - m > 3:
- df_day.loc[x, 'HL'] = 'H'
- if x == len(df_day.index) - 1:
-
- pass
- break
- elif (df_day.loc[m, 'HL'] == 'H'):
- if df_day.loc[x - 1, 'high'] > df_day.loc[m - 1, 'high']:
-
- df_day.loc[x, 'HL'] = 'H'
- if x == len(df_day.index) - 1:
- pass
-
- break
- break
- m = m - 1
- if m == 0:
- df_day.loc[x, 'HL'] = 'H'
- else:
- df_day.loc[x, 'HL'] = '-'
-
- df_day[index_len + 1:].to_sql('stk%s_%s' % (stock, fre), con=engine2, index=True,
- if_exists='append')
- else:
- df_day = pd.concat([df_day, get_price], ignore_index=True)
- df_day[index_len + 1:].to_sql('stk%s_%s' % (stock, fre), con=engine2, index=True,
- if_exists='append')
- else:
-
- df_day = pd.DataFrame(columns=('date', 'open', 'close', 'high', 'low', 'volume', 'money', 'HL'))
- get_price = pd.read_sql_query(
- 'select date,open,close,high,low,volume,money from `stk%s_%s`' % (stock, fre), engine)
-
- for i in get_price.index:
- if i == 0 or i == 1:
- df_day = pd.concat([df_day, get_price.iloc[[i]]], ignore_index=True)
-
- elif (df_day.iloc[-1, 3] > get_price.loc[i, 'high']
- and df_day.iloc[-1, 4] > get_price.loc[i, 'low']) \
- or (df_day.iloc[-1, 3] < get_price.loc[i, 'high']
- and df_day.iloc[-1, 4] < get_price.loc[i, 'low']):
- df_day = pd.concat([df_day, get_price.loc[[i]]], ignore_index=True)
-
- else:
-
- if df_day.iloc[-2, 3] > df_day.iloc[-1, 3]:
- df_day.iloc[-1, 3] = min(df_day.iloc[-1, 3], get_price.loc[i, 'high'])
- df_day.iloc[-1, 4] = min(df_day.iloc[-1, 4], get_price.loc[i, 'low'])
- else:
-
- df_day.iloc[-1, 3] = max(df_day.iloc[-1, 3], get_price.loc[i, 'high'])
- df_day.iloc[-1, 4] = max(df_day.iloc[-1, 4], get_price.loc[i, 'low'])
- if len(df_day.index) > 2:
-
- for x in range(2, len(df_day.index)):
- m = x - 1
-
- if ((df_day.loc[x, 'high'] > df_day.loc[x - 1, 'high']) and (
- df_day.loc[x - 2, 'high'] > df_day.loc[x - 1, 'high'])):
-
-
- df_day.loc[x, 'HL'] = 'L*'
- while m:
- if df_day.loc[m, 'HL'] == 'H':
- if (x - m) > 3:
- df_day.loc[x, 'HL'] = 'L'
- if x == len(df_day.index) - 1:
- pass
-
- break
- elif (df_day.loc[m, 'HL'] == 'L'):
- if df_day.loc[x - 1, 'low'] < df_day.loc[m - 1, 'low']:
-
- df_day.loc[x, 'HL'] = 'L'
- if x == len(df_day.index) - 1:
- pass
-
- break
- else:
- break
- m = m - 1
- if m == 0:
- df_day.loc[x, 'HL'] = 'L'
-
- elif ((df_day.loc[x, 'high'] < df_day.loc[x - 1, 'high']) and (
- df_day.loc[x - 2, 'high'] < df_day.loc[x - 1, 'high'])):
-
-
- df_day.loc[x, 'HL'] = 'H*'
- while m:
- if df_day.loc[m, 'HL'] == 'L':
- if x - m > 3:
- df_day.loc[x, 'HL'] = 'H'
- if x == len(df_day.index) - 1:
-
- pass
- break
- elif (df_day.loc[m, 'HL'] == 'H'):
- if df_day.loc[x - 1, 'high'] > df_day.loc[m - 1, 'high']:
-
- df_day.loc[x, 'HL'] = 'H'
- if x == len(df_day.index) - 1:
- pass
-
- break
- break
- m = m - 1
- if m == 0:
- df_day.loc[x, 'HL'] = 'H'
- else:
- df_day.loc[x, 'HL'] = '-'
-
- df_day.to_sql('stk%s_%s' % (stock, fre), con=engine2, index=True, if_exists='append')
- if __name__ == '__main__':
- engine_stocks_list = create_engine('mysql+pymysql://root:r6kEwqWU9!v3@localhost:3307/hlfx_pool?charset=utf8')
-
- stocks = pd.read_sql_query(
- 'select securities from stocks_list', engine_stocks_list)
- stocks = stocks.iloc[-1, 0]
- stocks = stocks.split(",")
- print(len(stocks), type(stocks), stocks)
-
- start = dt.now()
-
-
-
- for fre in ['1d', '30m']:
- start = dt.now()
- print(fre)
-
- db = pymysql.connect(host='localhost',
- user='root',
- port=3307,
- password='r6kEwqWU9!v3',
- database='hlfx')
- cursor = db.cursor()
- cursor.execute("show tables like '%%%s%%' " % fre)
- table_list = [tuple[0] for tuple in cursor.fetchall()]
- print('取得 table_list %s' % fre)
- step = 800
- mp_list = []
- print(len(stocks))
- for i in range(0, len(stocks), step):
- p = mp.Process(target=hlfx, args=(stocks[i:i + step], fre, table_list, ))
- mp_list.append(p)
- p.start()
- for processing in mp_list:
- processing.join()
-
- end = dt.now()
- print('总时长:', (end - start).seconds)
|