def main(): env = globals().get('env', None) nasdaq = '{}/local/{}'.format(env.pwd_parent, NASDAQ.path) finder = NASDAQ.init(filename=nasdaq) stock_list = map(lambda stock: PROFILE.get(stock), finder()) ini, empty_list = find(stock_list) stock_ini = '{pwd_parent}/local/yahoo_background.ini'.format(**vars(env)) config = INI.init() temp = ini['Background'] for key in sorted(temp.keys()): INI.write_section(config, key, **temp[key]) config.write(open(stock_ini, 'w')) stock_ini = '{pwd_parent}/local/yahoo_background_sector.ini'.format( **vars(env)) config = INI.init() temp = ini['SectorGroupBy'] for key in sorted(temp.keys()): INI.write_section(config, key, **temp[key]) config.write(open(stock_ini, 'w')) # At one time, industries contained different sectors, this is no longer the case stock_ini = '{pwd_parent}/local/yahoo_background_industry.ini'.format( **vars(env)) config = INI.init() temp = ini['IndustryGroupBy'] for key in sorted(temp.keys()): INI.write_section(config, key, **temp[key]) config.write(open(stock_ini, 'w')) return empty_list
def config(cls, save_file, **config) : logging.info("results saved to {}".format(save_file)) ret = INI.init() for key in sorted(config) : value = config.get(key,{}) INI.write_section(ret,key,**value) ret.write(open(save_file, 'w'))
def main(save_file) : ret = INI.init() for key, value in action() : logging.debug(value) INI.write_section(ret,key,**value) ret.write(open(save_file, 'w')) logging.info("results saved to {}".format(save_file))
def background(cls, **config): save_file = EXTRACT.instance().background_file ret = INI.init() for key in sorted(config): value = config.get(key, []) INI.write_section(ret, key, **value) ret.write(open(save_file, 'w')) logging.info("results saved to {}".format(save_file))
def main(file_list, ini_list, save_file): logging.info("loading results {}".format(ini_list)) ret = INI.init() for key, value in action(file_list, ini_list): logging.debug(value) INI.write_section(ret, key, **value) ret.write(open(save_file, 'w')) logging.info("results saved to {}".format(save_file))
def config(cls, data): save_file = EXTRACT.singleton().output_file ret = INI.init() for key in sorted(data): logging.info(key) value = data[key] INI.write_section(ret, key, **value) ret.write(open(save_file, 'w')) logging.info('writing to file {}'.format(save_file))
def portfolio(cls, save_file, **portfolio) : logging.info("saving results to file {}".format(save_file)) ret = INI.init() name_list = sorted(portfolio.keys()) value_list = map(lambda key : portfolio[key], name_list) for i, name in enumerate(name_list) : if not isinstance(value_list,list) : value_list = list(value_list) INI.write_section(ret,name,**value_list[i]) ret.write(open(save_file, 'w')) logging.info("results saved to file {}".format(save_file))
def main(save_file): stock, etf, alias = NASDAQ.init().stock_list() background = STOCKMONITOR.init() logging.info(background) stockmonitor = background() temp = reduce(lambda a, b: a + b, stockmonitor.values()) stock = set(stock) - set(temp) stock_list = sorted(list(stock)) FinanceModel = scrape_financemodelling(stock_list) temp = reduce(lambda a, b: a + b, FinanceModel.values()) stock = set(stock) - set(temp) stock_list = sorted(list(stock)) yahoo = scrape_yahoo(stock_list) temp = reduce(lambda a, b: a + b, yahoo.values()) stock = set(stock) - set(temp) stock_list = sorted(list(stock)) ''' Try a set of alternative names ''' _retry, stock_list = handle_alias(*stock_list, **alias) retry = map(lambda x: alias[x].values(), _retry) retry = reduce(lambda a, b: a + b, retry) FinanceModel2 = scrape_financemodelling(retry) temp_fm = reduce(lambda a, b: a + b, FinanceModel2.values()) retry = set(retry) - set(temp_fm) retry = sorted(list(retry)) yahoo2 = scrape_yahoo(retry) temp_y = reduce(lambda a, b: a + b, yahoo2.values()) retry = set(retry) - set(temp_y) retry = sorted(list(retry)) logging.info(retry) logging.info(temp_fm) logging.info(temp_y) logging.info(_retry) config = INI.init() INI.write_section(config, "STOCKMONITOR", **stockmonitor) INI.write_section(config, "FINANCEMODEL", **FinanceModel) INI.write_section(config, "YAHOO", **yahoo) INI.write_section(config, "FINANCEMODEL2", **FinanceModel2) INI.write_section(config, "YAHOO2", **yahoo2) INI.write_section(config, "NASDAQTRADER", **{ 'unkown': stock_list, 'alias': retry }) for name in sorted(alias): INI.write_section(config, name, **alias[name]) config.write(open(save_file, 'w'))
def config(cls, summary, portfolio): save_file = EXTRACT.instance().output_file ret = INI.init() INI.write_section(ret, "summary", **summary) for key in portfolio.keys(): values = portfolio[key] if not isinstance(values, dict): values = values.to_dict() INI.write_section(ret, key, **values) ret.write(open(save_file, 'w')) logging.info('saving file {}'.format(save_file))
def main(local_dir) : for name, section_list in action() : output_file = "{}/portfolio_{}.ini".format(local_dir, name) output_file = output_file.replace(" ", "_") ret = INI.init() name_list = sorted(section_list.keys()) value_list = map(lambda key : section_list[key], name_list) for i, name in enumerate(name_list) : if not isinstance(value_list,list) : value_list = list(value_list) INI.write_section(ret,name,**value_list[i]) logging.info("saving results to file {}".format(output_file)) ret.write(open(output_file, 'w'))
def draft(cls,data) : save_file = EXTRACT.instance().draft logging.info('Loading results : {}'.format(save_file)) config = INI.init() for i, SECTION in enumerate(sorted(data)) : target = 'alias' if SECTION == target : alias = data.get(SECTION) continue logging.info((i,SECTION)) value = data.get(SECTION) INI.write_section(config, SECTION, **value) for name in sorted(alias) : INI.write_section(config,name,**alias[name]) config.write(open(save_file, 'w'))
ret = pd.DataFrame(ret).T # screen potential under valued ret = ret[ret['returns'] < 0.15] if len(ret) == 0: return [], None _len = len(ret) size = int(_len * .1) # filter riskier ret = ret.sort_values(['risk']).head(_len - size) return list(ret.T.columns), ret if __name__ == '__main__': from glob import glob import os, sys pwd = os.getcwd() pwd = pwd.replace('bin', 'local') ini_list = glob('{}/*.ini'.format(pwd)) file_list = glob('{}/historical_prices/*pkl'.format(pwd)) Sector_Top, Industry_Top, Fund_Top = main(file_list, ini_list) config = INI.init() INI.write_section(config, 'Sector', **Sector_Top) INI.write_section(config, 'Industry', **Industry_Top) INI.write_section(config, 'Fund', **Fund_Top) stock_ini = "{}/yahoo_sharpe_undervalued.ini".format(pwd) config.write(open(stock_ini, 'w'))
def final(cls,data) : save_file = EXTRACT.instance().final logging.info('Loading results : {}'.format(save_file)) config = INI.init() INI.write_section(config,"MERGED",**data) config.write(open(save_file, 'w'))
def main(input_file, file_list, ini_list,output_file) : ret = INI.init() for name, section_list in action(input_file, file_list, ini_list) : INI.write_section(ret,name,**section_list) ret.write(open(output_file, 'w'))