def fetch_(): ts.set_token(cfg.get_datayes_key()) eq = ts.Equity() df = eq.Equ(equTypeCD='A', listStatusCD='L', field='ticker') df['ticker'] = df['ticker'].map(lambda x: str(x).zfill(6)) start, end = '20150901', '20160326' # thread can not make full use of cpu for i, row in df.iterrows(): csv = cfg.get_ratio_table_path(row['ticker'], start, end) if os.path.exists(csv): print("{0}/{1} {2} exists.".format(i, len(df.index), row['ticker'])) continue # code_queue.put(row['ticker']) # code_list.append(str(row['ticker'])) proc_queue.put(row['ticker']) # for i in range(3): # thread = ExportThread(thread_id, code_queue, start, end) # thread.start() # threads.append(thread) # thread_id += 1 # # get_code_and_export(0, code_queue, start, end) # # for t in threads: # t.join() # # print("Exit main thread.") processes = 4 for i in range(processes): p = ExportProcess(i+1, start, end, proc_queue) p.start() procs.append(p) for p in procs: p.join() print("Exit main")
def export_ratio_table(code, start, end, thread_id): # queue_lock.acquire() ts.set_token(cfg.get_datayes_key()) mkt = ts.Market() # print("exporting " + code + " from " + start + " to " + end) st = time.time() df = mkt.MktEqud(ticker=code, beginDate=start, endDate=end, field='ticker,tradeDate,preClosePrice,openPrice,highestPrice,lowestPrice,closePrice') print(" Thread {0} fetch online: {1}".format(thread_id, time.time()-st)) # queue_lock.release() # df = ts.get_h_data(code, start, end) # print(df) wave_ratio_df = pd.DataFrame(columns=["max_ratio", "min_ratio"]) for i, row in df.iterrows(): dict = wv.calc_wave_ratio(row["preClosePrice"], row["openPrice"], row["highestPrice"], row["lowestPrice"]) wave_ratio_df.loc[row["tradeDate"]] = dict st = time.time() idx_col = wv.calc_ratio_table_index_and_columns(max_ratio=0.03, min_ratio=-0.03) index, columns = idx_col["index"], idx_col["columns"] ratio_table = wv.calc_ratio_table(wave_ratio_df, index, columns) print(" Thread {0} calc ratio table: {1}".format(thread_id, time.time()-st)) st = time.time() length_ratio_df = wv.calc_length_ratio(ratio_table, len(wave_ratio_df.index)) print(" Thread {0} calc length ratio: {1}".format(thread_id, time.time()-st)) # write csv st = time.time() ratio_table.to_csv(cfg.get_ratio_table_path(code, start, end)) length_ratio_df.to_csv(cfg.get_length_ratio_path(code, start, end)) # print(" save csv: {0}".format(time.time()-st)) return length_ratio_df
# -*- coding: utf-8 -*- import config as cfg import tushare as ts import numpy as np import pandas as pd import os ts.set_token(cfg.get_datayes_key()) def merge_length(start, end): lens = np.arange(start=0.0100, stop=0.0200, step=0.0001) columns = [] for x in lens: columns.append("%.4f" % x) print(columns) df = pd.DataFrame(columns=columns) eq = ts.Equity() all = eq.Equ(equTypeCD='A', listStatusCD='L', field='ticker') all['ticker'] = all['ticker'].map(lambda x: str(x).zfill(6)) for i, row in all.iterrows(): path = cfg.get_length_ratio_path(row['ticker'], start, end) try: if os.path.exists(path): len_df = pd.DataFrame.from_csv(path) len_df.index = len_df.index.map(lambda x: "%.4f" % x) # print(len_df.index) for len in columns: # print(len_df.loc[len, 'ratio'])
# -*- coding: utf-8 -*- import config as cfg import tushare as ts import numpy as np import pandas as pd import os ts.set_token(cfg.get_datayes_key()) def merge_length(start, end): lens = np.arange(start=0.0100, stop=0.0200, step=0.0001) columns = [] for x in lens: columns.append("%.4f" % x) print(columns) df =pd.DataFrame(columns=columns) eq = ts.Equity() all = eq.Equ(equTypeCD='A', listStatusCD='L', field='ticker') all['ticker'] = all['ticker'].map(lambda x:str(x).zfill(6)) for i, row in all.iterrows(): path = cfg.get_length_ratio_path(row['ticker'], start, end) try: if os.path.exists(path): len_df = pd.DataFrame.from_csv(path) len_df.index = len_df.index.map(lambda x : "%.4f" % x) # print(len_df.index) for len in columns: # print(len_df.loc[len, 'ratio'])