def prep(*ini_list): target_sector = 'target_sector' target_sector = globals().get(target_sector, '_') risk = {} sharpe = {} stock_list = {} for path, section, key, value in INI.loadList(*ini_list): if target_sector in key: pass else: continue if section not in risk: risk[section] = {} sharpe[section] = {} if key.endswith(target_sector): key = key.replace(section, '') key = key.replace(target_sector, '') stock_list[section] = value else: key = key.replace(section, '') key = key.replace(target_sector, '') if 'risk' in key: key = key.replace('risk_', '') key = key.replace('_', '') risk[section][key] = value[0] if 'sharpe' in key: key = key.replace('sharpe_', '') key = key.replace('_', '') sharpe[section][key] = value[0] for key in risk: yield key, stock_list[key], risk[key], sharpe[key]
def prep(*ini_list) : ini_list = filter(lambda x : "benchmark" in x , ini_list) print (ini_list) for path, section, key, stock_list in INI.loadList(*ini_list) : if section == 'Index' : pass else : continue yield key, stock_list
def prep(*ini_list): ret = {} for path, section, key, weight in INI.loadList(*ini_list): if section not in ret: ret[section] = {} ret[section][key] = float(weight[0]) return ret
def filterByNasdaq(*ini_list): performers = {} stability = {} Sector = {} Industry = {} FundFamily = {} Category = {} ini_list = filter(lambda file: "nasdaq_quarterly.ini" in file, ini_list) for file_name, section, key, value in INI.loadList(*ini_list): if section == "Stability": config = stability elif section == "Performance": config = performers elif section == "Sector": config = Sector elif section == "Industry": config = Industry elif section == "Category": config = Category elif section == "FundFamily": config = FundFamily else: continue config[key] = value ret = performers.get('Q1234', []) stock_list = _combineNasdaq(Sector, Industry) fund_list = _combineNasdaq(Category, FundFamily) ret_stock = sorted(list(set(ret) & set(stock_list))) ret_fund = sorted(list(set(ret) & set(fund_list))) return stock_list, fund_list, ret_stock, ret_fund
def config(cls): ini_list = cls.instance().input_file logging.info('Reading input file {}'.format(ini_list)) ret = {} for path, section, key, value_list in INI.loadList(*[ini_list]): if section not in ret: ret[section] = {} ret[section][key] = value_list return ret
def background(cls): load_file = cls.instance().background if not (cls._background_cache is None): logging.info('reading cache {}'.format(load_file)) return cls._background_cache logging.info('reading file {}'.format(load_file)) ret = {} for path, key, stock, value in INI.loadList(*load_file): if "File Creation Time" in stock: continue if stock not in ret: ret[stock] = {} if key in cls._floats_in_summary: value = float(value[0]) else: value = ', '.join(value) ret[stock][key] = value load_file = cls.instance().sector logging.info('reading file {}'.format(load_file)) for path, section, key, ticker_list in INI.loadList(*load_file): entity = 'stock' if 'fund' in path: key = '{} ({})'.format(section, key) entity = 'fund' for ticker in ticker_list: if ticker not in ret: ret[ticker] = {} ret[ticker]['SECTOR'] = key ret[ticker]['ENTITY'] = entity ret = pd.DataFrame(ret).T ret['SECTOR'] = ret['SECTOR'].fillna("Unknown") ret['NAME'] = ret['NAME'].fillna("Unavailable") ret.fillna(0.0, inplace=True) for sector, category in TRANSFORM.by_sector(ret): pass ret = cls.filterBackground(ret) for sector, category in TRANSFORM.by_sector(ret): pass cls._background_cache = ret return ret
def enrich(*ini_list): ret = {} ini_list = filter(lambda x: 'background' in x, ini_list) for path, section, key, stock_list in INI.loadList(*ini_list): if section == 'Sector': pass elif section == 'Industry': pass else: continue for stock in stock_list: if stock not in ret: ret[stock] = {} ret[stock][section] = key return ret
def readPortfolio(cls): portfolio = EXTRACT.instance().input_file logging.info('reading file {}'.format(portfolio)) ret = {} for path, section, key, weight in INI.loadList(*[portfolio]): if section.startswith('dep_'): continue if section not in ret: ret[section] = {} ret[section][key] = float(weight[0]) return ret
def benchmark(*ini_list): ret = {} ini_list = filter(lambda x: 'benchmark' in x, ini_list) for path, section, key, stock_list in INI.loadList(*ini_list): if section not in ['MOTLEYFOOL', 'Index']: continue if section == 'MOTLEYFOOL': if 'NASDAQ' not in key: continue if 'FUND' not in key: continue if section == 'Index': if '500' not in key: continue ret[key] = stock_list return ret
def init(*ini_list): performers = {} stability = {} for file_name, name, key, value in INI.loadList(*ini_list): config = None if name == "Stability": config = stability if name == "Performance": config = performers config[key] = value ret = performers.get('Q1234', []) return ret
def merge(cls) : config = EXTRACT.instance().draft logging.info('loading config files {}'.format(config)) ret = DICT_HELPER.init() for path, section, key, stock in INI.loadList(*[config]) : ret.append(key,*stock) omit_list = ['ACT Symbol', 'CQS Symbol', 'alias', 'unknown'] for omit in omit_list : ret.data.pop(omit,None) stock_list = ret.values() return ret.data, stock_list
def read(cls): data = cls.singleton().input_file logging.info('reading file {}'.format([data])) ret = {} for path, section, stock_sector, stock_list in INI.loadList(*[data]): if section not in ret: ret[section] = {} ret[section][stock_sector] = stock_list target = 'MERGED' ret = ret.get(target, ret) stock_list = cls.flatten(ret.values()) return ret, stock_list
def read(cls): benchmark = EXTRACT.instance()._benchmark logging.info('reading file {}'.format(benchmark)) ret = {} for path, section, key, stock_list in INI.loadList(*benchmark): #if section not in ['MOTLEYFOOL', 'Index'] : continue if section not in ['PERSONAL', 'Index']: continue if section == 'MOTLEYFOOL': if 'NASDAQ' not in key: continue if 'FUND' not in key: continue if section == 'Index': if '500' not in key: continue ret[key] = stock_list logging.debug(ret) return ret
def prep(*ini_list): ret, overflow, default = prep_init() for path, section, key, value in INI.loadList(*ini_list): curr = lambdaFindSector(overflow, ret, section) curr[section][key] = float(value[0]) default_keys = sorted(default) for key in sorted(ret): portfolio_list = ret[key] portfolio_keys = sorted(portfolio_list) _list = [] for portfolio_name in portfolio_keys: portfolio = portfolio_list[portfolio_name] value = map(lambda x: portfolio[x], default_keys) _list.append(dict(zip(default_keys, value))) yield key, dict(zip(portfolio_keys, _list))
def _readSector(cls): load_file = EXTRACT.instance().category if not (cls._cache is None): logging.info('reading cache {}'.format(load_file)) return cls._cache logging.info('reading file {}'.format(load_file)) ret = {} for path, section, key, ticker_list in INI.loadList(*load_file): if 'fund' in path: key = '{} ({})'.format(section, key) for ticker in ticker_list: if ticker not in ret: ret[ticker] = {} ret[ticker]['Sector'] = key cls._cache = ret return ret
def _prep(*ini_list) : ret = {} target = "stocks" for path, section, key, value in INI.loadList(*ini_list) : key = key.replace(section+"_","") ret[key] = value if target not in key : continue logging.debug(key) prefix = key.replace(target,"") ret = HELPER2.transform(prefix,ret) if HELPER2.sharpe_cap(ret) : ret = {} continue logging.info((section,ret)) yield section, ret.get(target,[]) ret = {}
def prep(target, *ini_list) : Stock = {} Fund = {} ini_list = filter(lambda x : "sharpe_nasdaq" in x , ini_list) ini_list = filter(lambda x : "fact" not in x , ini_list) ini_list = filter(lambda x : target in x , ini_list) logging.debug(ini_list) for path, section, key, stock_list in INI.loadList(*ini_list) : if section == 'Sector' : config = Stock elif section == 'Industry' : config = Stock elif section == 'Fund' : config = Fund else : continue config[key] = stock_list Stock = reduce(lambda a, b : a + b, Stock.values()) Stock = list(set(Stock)) Fund = reduce(lambda a, b : a + b, Fund.values()) Fund = list(set(Fund)) return sorted(Stock), sorted(Fund)
def prep(target, *ini_list): Stock = {} Fund = {} ini_list = filter(lambda x: target in x, ini_list) for path, section, key, stock_list in INI.loadList(*ini_list): if section == 'Sector': config = Stock elif section == 'Industry': config = Stock elif section == 'Fund': config = Fund else: continue config[key] = stock_list stock_list = Stock.values() if len(stock_list) > 0: Stock = reduce(lambda a, b: a + b, stock_list) Stock = list(set(Stock)) fund_list = Fund.values() if len(fund_list) > 0: Fund = reduce(lambda a, b: a + b, fund_list) Fund = list(set(Fund)) return sorted(Stock), sorted(Fund)
def read(cls): load_file = EXTRACT.instance().category if not (cls._cache is None): return cls._cache logging.info('reading file {}'.format(load_file)) ret = {} for path, section, key, ticker_list in INI.loadList(*load_file): entity = 'Stock' if 'fund' in path: key = '{} ({})'.format(section, key) entity = 'Fund' for ticker in ticker_list: if ticker not in ret: ret[ticker] = {} ret[ticker]['SECTOR'] = key ret[ticker]['ENTITY'] = entity ret = pd.DataFrame(ret).T logging.info(ret) cls._cache = ret return ret
def prep() : target = "portfolio_list" portfolio_list = globals().get(target,[]) target = "sector_list" sector_list = globals().get(target,[]) not_stock = ['returns', 'risk', 'sharpe'] ret = [] for sector in sector_list : temp = filter(lambda x : sector in x, portfolio_list) ret += temp portfolio_list = ret logging.info(portfolio_list) ret = {} for path, sector_enum, key, value in INI.loadList(*portfolio_list) : logging.info(sector_enum) if key in not_stock : continue sector = filter(lambda x : x in sector_enum, key_list) logging.info(sector) if isinstance(sector,list) and len(sector) > 0 : sector = sector[0] if sector not in ret : ret[sector] = {} curr = ret[sector] if key not in curr : curr[key] = [] curr = curr[key] value = float(value[0]) curr.append(value) sector_list = sorted(ret) for sector in sector_list : curr = ret[sector] stock_list = sorted(curr) for stock in stock_list : data = curr[stock] data = pd.DataFrame(data).describe() curr[stock] = data logging.debug(ret) return ret
def read(cls): load_file = EXTRACT.instance().background if not (cls._background_cache is None): logging.info('reading cache {}'.format(load_file)) return cls._background_cache logging.info('reading file {}'.format(load_file)) ret = {} for path, key, stock, value in INI.loadList(*load_file): if "File Creation Time" in stock: continue if stock not in ret: ret[stock] = {} if key in cls._floats_in_summary: if '=' in value[0]: value = [0] value = float(value[0]) else: value = ', '.join(value) ret[stock][key] = value ret = pd.DataFrame(ret).T cls._background_cache = ret return ret
def prep(*ini_list) : risk = {} sharpe = {} stock_list = {} for path, section, key, value in INI.loadList(*ini_list) : if section not in risk : risk[section] = {} sharpe[section] = {} if section == key : value = sorted(list(set(value))) stock_list[key] = value key = key.replace(section,'') if 'risk' in key : key = key.replace('risk_','') key = key.replace('_','') risk[section][key] = value[0] elif 'sharpe' in key : key = key.replace('sharpe_','') key = key.replace('_','') sharpe[section][key] = value[0] for key in risk : yield key, stock_list[key], risk[key], sharpe[key]
def getByNasdaq(*ini_list): Sector = {} Industry = {} FundFamily = {} Category = {} ini_list = filter(lambda file: "yahoo" in file, ini_list) ini_list = filter(lambda file: "background" in file, ini_list) logging.debug(ini_list) for file_name, section, key, value in INI.loadList(*ini_list): if section == "Sector": config = Sector elif section == "Industry": config = Industry elif section == "Category": config = Category elif section == "FundFamily": config = FundFamily else: continue config[key] = value return Sector, Industry, Category, FundFamily
def prep(ini_list) : key_list = ['Basic_Materials','Communication_Services','Consumer_Cyclical','Consumer_Defensive','Empty','Energy','Financial_Services','Healthcare','Industrials','Real_Estate','Technology','Utilities'] not_stock = ['returns', 'risk', 'sharpe'] portfolio_list = filter(lambda x : 'portfolio' in x, ini_list) ret = [] for key in key_list : temp = filter(lambda x : key in x, portfolio_list) ret += temp portfolio_list = ret logging.info(portfolio_list) ret = {} for path, sector_enum, key, value in INI.loadList(*portfolio_list) : if key in not_stock : continue if key not in ret : ret[key] = [] value = float(value[0]) ret[key].append(value) stock_list = sorted(ret) for stock in stock_list : data = ret[stock] data = pd.DataFrame(data).describe() ret[stock] = data return ret
def config(cls) : config = cls.instance().config_file logging.info("loading results {}".format(config)) for path, section, key, stock_list in INI.loadList(*config) : yield path, section, key, stock_list
def prep(*ini_list): for path, section, key, stock in INI.loadList(*ini_list): yield key, stock
def config() : ini_list = EXTRACT.instance().config_list logging.info("loading results {}".format(ini_list)) for path, section, key, stock_list in INI.loadList(*ini_list) : yield path, section, key, stock_list
def prep(): ini_list = PREP.singleton().data logging.info("loading results {}".format(ini_list)) for path, section, key, stock_list in INI.loadList(*ini_list): yield key, stock_list