async def private_(self, type_: str, base_endpoint: str, url: str, params: dict = {}) -> dict: try: timestamp = int(time() * 1000) params.update({"timestamp": timestamp, "recvWindow": 5000}) for k in params: if type(params[k]) == bool: params[k] = "true" if params[k] else "false" elif type(params[k]) == float: params[k] = format_float(params[k]) params = sort_dict_keys(params) params["signature"] = hmac.new( self.secret.encode("utf-8"), urlencode(params).encode("utf-8"), hashlib.sha256, ).hexdigest() async with getattr(self.session, type_)(base_endpoint + url, params=params, headers=self.headers) as response: result = await response.text() return json.loads(result) except Exception as e: print(f"error with private {type_} {base_endpoint} {url} {params}") traceback.print_exc() return {}
async def private_(self, type_: str, base_endpoint: str, url: str, params: dict = {}) -> dict: timestamp = int(time() * 1000) params.update({'api_key': self.key, 'timestamp': timestamp}) for k in params: if type(params[k]) == bool: params[k] = 'true' if params[k] else 'false' elif type(params[k]) == float: params[k] = str(params[k]) params['sign'] = hmac.new( self.secret.encode('utf-8'), urlencode(sort_dict_keys(params)).encode('utf-8'), hashlib.sha256).hexdigest() async with getattr(self.session, type_)(base_endpoint + url, params=params) as response: result = await response.text() return json.loads(result)
def __init__(self, config: dict): self.config = config self.do_long = config["long"]["enabled"] self.do_short = config["short"]["enabled"] self.n_harmonies = max(config["n_harmonies"], len(config["starting_configs"])) self.starting_configs = config["starting_configs"] self.hm_considering_rate = config["hm_considering_rate"] self.bandwidth = config["bandwidth"] self.pitch_adjusting_rate = config["pitch_adjusting_rate"] self.iters = config["iters"] self.n_cpus = config["n_cpus"] self.pool = Pool(processes=config["n_cpus"]) self.long_bounds = sort_dict_keys( config[f"bounds_{self.config['passivbot_mode']}"]["long"]) self.short_bounds = sort_dict_keys( config[f"bounds_{self.config['passivbot_mode']}"]["short"]) self.symbols = config["symbols"] self.identifying_name = (f"{len(self.symbols)}_symbols" if len(self.symbols) > 1 else self.symbols[0]) self.now_date = ts_to_date(time())[:19].replace(":", "-") self.results_fpath = make_get_filepath( f"results_harmony_search_{self.config['passivbot_mode']}/{self.now_date}_{self.identifying_name}/" ) self.exchange_name = config["exchange"] + ( "_spot" if config["market_type"] == "spot" else "") self.market_specific_settings = { s: json.load( open( f"backtests/{self.exchange_name}/{s}/caches/market_specific_settings.json" )) for s in self.symbols } self.date_range = f"{self.config['start_date']}_{self.config['end_date']}" self.bt_dir = f"backtests/{self.exchange_name}" self.ticks_cache_fname = ( f"caches/{self.date_range}{'_ohlcv_cache.npy' if config['ohlcv'] else '_ticks_cache.npy'}" ) """ self.ticks_caches = ( {s: np.load(f"{self.bt_dir}/{s}/{self.ticks_cache_fname}") for s in self.symbols} if self.n_harmonies > len(self.symbols) else {} ) """ self.ticks_caches = {} self.shms = {} # shared memories self.current_best_config = None # [{'config': dict, 'task': process, 'id_key': tuple}] self.workers = [None for _ in range(self.n_cpus)] # hm = {hm_key: str: {'long': {'score': float, 'config': dict}, 'short': {...}}} self.hm = {} # {identifier: {'config': dict, # 'single_results': {symbol_finished: single_backtest_result}, # 'in_progress': set({symbol_in_progress}))} self.unfinished_evals = {} self.iter_counter = 0
def load_live_config(live_config_path: str) -> dict: try: live_config = json.load(open(live_config_path)) return sort_dict_keys(numpyize(make_compatible(live_config))) except Exception as e: raise Exception(f"failed to load live config {live_config_path} {e}")