| 10 | |
| 11 | @PLOTS.register_module(force=True) |
| 12 | class PlotsInterface(): |
| 13 | def __init__(self, |
| 14 | root = None, |
| 15 | workdir = None, |
| 16 | tag = None, |
| 17 | suffix = 'jpeg') -> None: |
| 18 | super(PlotsInterface, self).__init__() |
| 19 | self.root = root |
| 20 | self.workdir = workdir |
| 21 | self.tag = tag |
| 22 | self.suffix = suffix |
| 23 | |
| 24 | self.exp_path = init_path(os.path.join(self.root, self.workdir, self.tag)) |
| 25 | self.plot_path = init_path(os.path.join(self.exp_path, "plots")) |
| 26 | self.kline_plot_path = init_path(os.path.join(self.plot_path, "kline")) |
| 27 | self.trading_plot_path = init_path(os.path.join(self.plot_path, "trading")) |
| 28 | |
| 29 | self.echarts_js_path = os.path.join(self.root, "tools", "echarts-5.4.3" , "dist", "echarts.min.js") |
| 30 | |
| 31 | def plot_kline(self, state, info, save_dir, mode = "train"): |
| 32 | |
| 33 | try: |
| 34 | price = state["price"] |
| 35 | |
| 36 | kline_dir = init_path(os.path.join(self.kline_plot_path, save_dir)) |
| 37 | |
| 38 | if not os.path.exists(os.path.join(kline_dir, "echarts.min.js")): |
| 39 | shutil.copy(self.echarts_js_path, kline_dir) |
| 40 | |
| 41 | price = price[["open", "high", "low", "close", "volume"]] |
| 42 | price = price.reset_index(drop=False) |
| 43 | price = price.dropna(axis=0, how="any") |
| 44 | price = price.drop_duplicates(subset=["timestamp"], keep="first") |
| 45 | price = price.set_index("timestamp") |
| 46 | |
| 47 | title = "{} kline of {}".format(info["date"], info["symbol"]) |
| 48 | kline_path = os.path.join(kline_dir, "kline_{}.{}".format(info["date"], self.suffix)) |
| 49 | |
| 50 | now_date = pd.to_datetime(info["date"]) |
| 51 | now_date = min(price.index, key=lambda x: abs(x - now_date)) # find the nearest date before now_date |
| 52 | now_date = now_date.strftime("%Y-%m-%d") |
| 53 | |
| 54 | plot_kline(price, |
| 55 | title, |
| 56 | kline_path, |
| 57 | now_date=now_date, |
| 58 | path=os.path.join(kline_dir, f"{info['date']}_{self.suffix}_kline_render.html"), |
| 59 | mode=mode) |
| 60 | |
| 61 | except Exception as e: |
| 62 | print(e) |
| 63 | kline_path = None |
| 64 | return kline_path |
| 65 | |
| 66 | def plot_trading(self, records, info, save_dir): |
| 67 | try: |
| 68 | |
| 69 | trading_dir = init_path(os.path.join(self.trading_plot_path, save_dir)) |
nothing calls this directly
no outgoing calls
no test coverage detected