def get(self, section_name, key): if os.path.exists(self.file_path): try: content = read_file_to_string(self.file_path) json_data = json.loads(content) return json_data[section_name][key] except Exception as e: return None else: return None
def read_portfolio_info(strategy_name): file_path = PathMgr.get_strategies_portfolio_file(strategy_name) if os.path.exists(file_path): content = read_file_to_string(file_path) dic = json.loads(content) # for key in dic.keys(): # dic[key] = Portfolio.from_dict(dic[key]) return dic else: return {}
def load_log(self, symbol, date): file_name = '%s%s.log' % (symbol, date.strftime('%Y%m%d')) path = PathMgr.get_data_path('quantopian_daily_min/%s' % file_name) content = read_file_to_string(path) lines = content.split('\n') filtered_lines = filter(lambda x: len(x) > 100, lines) lines = map(lambda x: string_fetch(x, 'PRINT ', ''), filtered_lines) close_list_str = ','.join(lines) prices_list = map(float, close_list_str.split(',')) datetimes = TradeTime.generate_datetimes(date, date) equities = map(lambda x, y: Equity(symbol, x, y, y, y, y), datetimes, prices_list) EquityMinDAO().insert(equities)
def set(self, section_name, key, value): dic = {} if os.path.exists(self.file_path): try: content = read_file_to_string(self.file_path) dic = json.loads(content) except Exception as e: pass if section_name in dic.keys(): dic[section_name][key] = value else: section_dic = {key: value} dic[section_name] = section_dic write_to_file(self.file_path, json.dumps(dic))
def parse_raw_file(raw_file): content = read_file_to_string(raw_file) underling_symbol = string_fetch(content, '<title>', ' Option') sub_content = string_fetch( content, '"underlyingSymbol\":\"%s\"},\"contracts\":' % underling_symbol, ',\"displayed\"') json_content = sub_content + '}' json_obj = json.loads(json_content) objs = json_obj['calls'] put_objs = json_obj['puts'] objs.extend(put_objs) for obj in objs: yield YahooOptionParser.json_obj_to_option(obj, underling_symbol)
def to_csv(self, symbol, date): file_name = '%s%s.log' % (symbol, date.strftime('%Y%m%d')) path = PathMgr.get_data_path('quantopian_daily_min/%s' % file_name) content = read_file_to_string(path) lines = content.split('\n') filtered_lines = filter(lambda x: len(x) > 100, lines) lines = map(lambda x: string_fetch(x, 'PRINT ', ''), filtered_lines) close_list_str = ','.join(lines) # print close_list_str prices_list = map(float, close_list_str.split(',')) datetimes = TradeTime.generate_datetimes(date, date) new_lines = map(lambda x, y: '%s,%s' % (x, y), datetimes, prices_list) new_content = '\n'.join(new_lines) write_path = PathMgr.get_data_path('quantopian_daily_min/%s%s.csv' % (symbol, date.strftime('%Y%m%d'))) write_to_file(write_path, new_content)
def get_order_id(): order_file_path = PathMgr.get_data_file_path('orderid.txt') order_id = int(read_file_to_string(order_file_path)) write_to_file(order_file_path, str(order_id + 1)) return order_id
def load_csv(self, symbol): path = PathMgr.get_data_path('1mincsv/%s.txt' % symbol) content = read_file_to_string(path) lines = content.split('\n') return map(lambda x: self.try_parse_line(x, symbol), lines)
def read_trade_trace(strategy_name): file_path = PathMgr.get_strategies_tradetrace_file(strategy_name) content = read_file_to_string(file_path) lines = content.split('\r\n')[:-1] return map(TradeRecord.read_from_line, lines)
def load_log(self, code): path = PathMgr.get_data_path('joinquant_min/%s_2018.log'%code) content = read_file_to_string(path) lines = content.split('\n') filtered_lines = filter(lambda x: 'INFO' not in x and x != '', lines) return map(lambda x: self.parse_line(x, code), filtered_lines)