def test_calc_wave_ratio(self): cases=[ {"pre_price":10, "open":10.5, "high":11, "low":9}, {"pre_price":10, "open":7, "high":11, "low":9}, {"pre_price":10, "open":12, "high":11, "low":9}, {"pre_price":0, "open":10.5, "high":11, "low":9}, ] for case in cases: try: dict = wv.calc_wave_ratio(case["pre_price"], case["open"], case["high"], case["low"]) self.assertEqual(dict["min_ratio"], -0.15) self.assertEqual(dict["max_ratio"], 0.05) except ValueError as ve: print(ve.message, "Test ValueError OK!") except ZeroDivisionError as zde: print(zde.message, "Test ZeroDivisionError OK!")
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