async def list_monitors(request): params = request.args code = params.get('code') frame_type = params.get('frame_type') plot = params.get('plot') flag = params.get('falg') try: monitors = await mm.list_monitors(code=code, frame_type=frame_type, plot=plot, flag=flag) result = {} for job_name, name, params, trigger in monitors: plot = create_plot(name) rows = result.get(plot.display_name, []) row = plot.translate_monitor(job_name, params, trigger) if row: rows.append(row) result[plot.display_name] = rows return response.json(result, status=200) except Exception as e: logger.exception(e) return response.text(e, status=500)
async def copy_mom(code: str, frame: str = None, frame_type: str = '1d', wins=None): frame = frame or arrow.now(tz=cfg.tz) mom = create_plot('momentum') wins = wins or [5, 10, 20] features = await mom.copy(code, frame_type, frame, wins) for win, feat in zip(wins, features): print(f"ma{win}特征:") print(round(feat[0], 4), np.round(feat[1], 4), round(feat[2], 1))
async def plot_command_handler(request, cmd): plot_name = request.json.get("plot") params = request.json del params['plot'] try: plot = create_plot(plot_name) func = getattr(plot, cmd) results = await func(**params) return response.json(results, status=200) except Exception as e: logger.exception(e) return response.json(e, status=500)
async def resume_monitors(self): """ resume monitors from database, in case of the process is restarted Returns: """ logger.info("(re)loading monitor...") jobs = await cache.sys.hgetall(self.monitor_key) for job_name, job_info in jobs.items(): job_info = json.loads(job_info.encode('utf-8')) plot_name = job_info.get("plot") plot = create_plot(plot_name) self._add_watch(plot, job_name, job_info) self.watch_list[job_name] = job_info logger.info("done with %s monitor loaded", len(self.watch_list)) return self.watch_list
async def get_stock_pool(request): args = request.args frames = int(args.get('frames')[0]) frame_types = args.getlist("frame_types") if frame_types: frame_types = [FrameType(frame_type) for frame_type in frame_types] else: frame_types = tf.day_level_frames frame_types.extend(tf.minute_level_frames) plots = args.getlist('plots') or ['momentum'] results = [] for plot_name in plots: plot = create_plot(plot_name) results.append(await plot.list_stock_pool(frames, frame_types)) return response.json(body=results)
def reschedule_job(self, start_time: datetime.datetime, job: Job): """ I don't know why, but it doesn't work if call job's reschedule with different start_date. So I have to re-create the job Args: start_time: job_id: Returns: """ job_info = self.watch_list.get(job.name) plot_name = job_info.get("plot") plot = create_plot(plot_name) self.sched.remove_job(job.id) self.sched.add_job(self._add_watch, 'date', next_run_time=start_time, args=(plot, job.name, job_info), misfire_grace_time=30)
async def add_monitor(self, plot_name: str, **kwargs): """ Args: plot_name: the name of plot kwargs: required by plot Returns: """ plot = create_plot(plot_name) title_keys, job_info = plot.parse_monitor_settings(**kwargs) job_name = self.make_job_name(title_keys, plot_name, **kwargs) # remove old ones first for job in self.sched.get_jobs(): if job.name == job_name: self.sched.remove_job(job.id) await cache.sys.hdel(self.monitor_key, job_name) del self.watch_list[job.name] self.watch_list[job_name] = job_info self._add_watch(plot, job_name, job_info) await cache.sys.hset(self.monitor_key, job_name, json.dumps(job_info))
async def visual_mom(code: str, frame: str = None, frame_type='1d'): frame = frame or arrow.now(tz=cfg.tz) mom = create_plot('momentum') await mom.visualize(code, frame, frame_type)