def _init_driver(self): if self.driver: return home_dir = env.get_home_dir() if env.is_posix(): default_profile_dir = '{}/.config/chromium'.format(home_dir) elif env.is_windows(): default_profile_dir = '{}/AppData/Local/Google/Chrome/User Data'.format( home_dir) else: raise Exception('unknown os.name {}'.format(os.name)) profile_dir = os.path.join(env.get_desktop_dir(), 'chrome_profiles', self.name) if not os.path.exists(profile_dir): shutil.copytree(default_profile_dir, profile_dir) self.logger.info('PROFILE_DIR {}'.format(profile_dir)) self.logger.info('connect to chrome port {}'.format(self.chrome_port)) self.logger.info('conn_factory:{}'.format(self.conn_factory)) self.driver = ChromeDriver(logger=self.logger, profile_dir=profile_dir, remote_port=self.chrome_port, connection_factory=self.conn_factory) gevent.sleep(1) self.logger.info('ChromeDriver launched')
def dir_path(self): if self.chrome: path = self.name + '_chrome' else: path = self.name path = os.path.join(env.get_desktop_dir(), 'model_data', path) os.makedirs(path, exist_ok=True) return path
def update_prices(self, prices: List[dict]): disabled = self.root_config.setdefault('disabled_services', set()) instrument_service_prices = defaultdict(lambda: defaultdict(list)) for price in prices: instrument_service_prices[price['instrument']][price['service']].append(price) signals = [] # type:List[dict] import csv import os home_dir = env.get_desktop_dir() with open(os.path.join(home_dir, 'pyapp.csv'), 'a') as f: csv_writer = csv.writer(f) for instrument, service_prices in instrument_service_prices.items(): service_pairs = sorted(itertools.permutations(service_prices.keys(), 2), key=str) for b, a in service_pairs: bidder, asker = service_prices[b][0], service_prices[a][0] signaler = self.signalers[(instrument, b, a)] d = signaler.judge(instrument=instrument, bid_data=bidder, ask_data=asker, duration_threshold=(2 if a == b else None)) if d: # minus spread keys = ['time', 'instrument', 'bidder', 'asker', 'bid', 'ask', 'sp'] values = [d[0][k] for k in keys] values[-1] = '{:.2f}'.format(values[-1]) csv_writer.writerow(values) if d and d[1]: d = d[1] d['signaler'] = signaler d['bg'] = None d['time'] = d['time'].strftime('%Y-%m-%d %H:%M:%S%Z') d['sp'] = '{:.2f}'.format(d['sp']) with open(self.signal_log_file, 'a') as f: f.write('{time},{instrument},{bidder},{asker},{bid},{ask},{sp},{not_closed}\n'.format(**d)) if d['bidder'] in disabled or d['asker'] in disabled: continue opposite = self.signalers[(instrument, a, b)] opposite.clear() if instrument in self.enabled_instruments: if signaler.enabled: if signaler.not_closed: self.sound_player.play('signal_close') d['bg'] = QBrush(Qt.green) elif not self.root_config.get('not_closed_only_check'): self.sound_player.play('signal_open') self.add_signal(d) return signals
def init_driver(self): if self.driver: return home_dir = env.get_home_dir() if env.is_posix(): default_profile_dir = '{}/.config/chromium'.format(home_dir) elif env.is_windows(): default_profile_dir = '{}/AppData/Local/Google/Chrome/User Data'.format(home_dir) else: raise Exception('unknown os.name {}'.format(os.name)) profile_dir = os.path.join(env.get_desktop_dir(), 'chrome_profiles', self.name) if not os.path.exists(profile_dir): shutil.copytree(default_profile_dir, profile_dir) self.logger.info('PROFILE_DIR {}'.format(profile_dir)) self.logger.info('connect to driver port {}'.format(self.driver_port)) self.driver = selepy.Chrome(args=['--user-data-dir={}'.format(profile_dir)], port=self.driver_port) time.sleep(5)
def main(): logging.basicConfig( level=logging.INFO, format='%(asctime)s|%(name)s|%(levelname)s| %(message)s') args = docopt(""" Usage: {f} [options] NAME Options: --master IP_PORT [default: 127.0.0.1:10000] --bind IP_PORT [default: 127.0.0.1:0] --make-train-data N [default: 0] --train EPOCH [default: 0] --snap N [default: 0] --snap-interval SEC [default: 1] --chrome --title TITLE_RE """.format(f=sys.argv[0])) name = args['NAME'] name = 'gaitame' l = args['--master'].split(':') master_address = (l[0], int(l[1])) l = args['--bind'].split(':') bind_address = (l[0], int(l[1])) train_data_n = int(args['--make-train-data']) epoch = int(args['--train']) snap_n = int(args['--snap']) snap_interval = float(args['--snap-interval']) chrome = args['--chrome'] if args['--title']: title_re = args['--title'] for i in range(snap_n): for handle in CaptureServer.iter_windows(title_re): title = CaptureServer.get_window_title(handle) save_dir = os.path.join(env.get_desktop_dir(), 'market_snap', name, title) os.makedirs(save_dir, exist_ok=True) img = CaptureServer.capture_window(handle) f_name = os.path.join( save_dir, '{}.png'.format( timeutil.jst_now().strftime('%Y%m%dT%H%M%S'))) img.save(f_name) print('#{}/{} {}'.format(i + 1, snap_n, f_name)) time.sleep(snap_interval) return server = GaitameServer(name, bind_address, master_address=master_address, chrome=chrome, **server_options(name, chrome=chrome)) if snap_n > 0: for i in range(snap_n): img = server.capture() f = os.path.join(server.dir_path, timeutil.jst_now().strftime('%Y%m%dT%H%M%S.png')) img.save(f) print('#SNAP {}/{} file:{}'.format(i + 1, snap_n, f)) time.sleep(snap_interval) return if not epoch and train_data_n <= 0: server.load_model() server.start() return server.load_model() server.load_train_data() if train_data_n: server.make_train_data(server.gen_image(crop=True, n=train_data_n)) server.save_train_data() if epoch: server.train(epoch=epoch) server.save_model()