def calculate(stock_no): ''' calculate accrual for last year and last 4Q calculate roa for every finance report ''' stock_data = _prepare(stock_no) finance = stock_data[common.FINANCE] key_net_income_after_tax = _field_name(u'本期稅後淨利') key_cash_flow_of_investment = _field_name(u'投資活動之現金流量') key_net_income_before_tax = _field_name(u'稅前淨利') key_total_assets = _field_name(u'資產總額') meta = stock_data[common.META] for y in (common.LAST_4Q_YEAR, meta[common.LAST_YEAR]): try: f = None if y: f = finance[y] if f and len(meta[common.LAST_4Q]) == 4: # accrual 權責發生額 = 本期稅後淨利 - 來自營運之現金流量 accrual = f[_field_name(u'本期稅後淨利')] - \ f[_field_name(u'來自營運之現金流量')] f[_field_name(u'權責發生額')] = accrual except Exception as e: msg = '%s: %s, accrual failed: %s %s' % (stock_no, y, type(e), e.message) common.report_error(msg) for period, f in finance.items(): try: # ROA = 稅前純利 / 資產總額 var1 = _field_name(u'稅前淨利') var2 = _field_name(u'資產總額') if var1 in f and var2 in f: roa = f[var1] / f[var2] f[_field_name(u'總資產報酬率')] = roa except Exception as e: msg = '%s: %s, ROA failed: %s %s' % (stock_no, period, type(e), e.message) common.report_error(msg) common.save_stock(stock_no, stock_data)
def calculate_latest_day(stock_no, average_data): ''' calculate p/e ratio for last year and last 4Q ''' stock_data = common.load_stock(stock_no) finance = stock_data.get(common.FINANCE, None) if not finance: common.report_error('%s does not have finance report!!' % stock_no) # XXX new stock no, trigger parse finance report and calculate return daily_prices = stock_data[common.DAILY] # day format exampe: 101/10/28 latest_day = sorted((k for k in daily_prices.keys() if k[0].isdigit()), reverse=True)[0] calculate_day(latest_day, stock_data, average_data, stock_no=stock_no) common.save_stock(stock_no, stock_data) return latest_day
def main(): catalog = {} curr_data_date = None # Add some more to prevent error when new stocks found total = _total_stocks() + 10 widgets = [ FormatLabel( 'Processed: %(value)d / {0} (in: %(elapsed)s)'.format(total)) ] pbar = ProgressBar(widgets=widgets, maxval=total) count = 0 pbar.start() state = common.load_state() for catalog_key, url in CATELOG.items(): data_date, result = get_category_stock_info(url) if not result: raise Exception('Empty parsing result, key: {}, url: {}'.foramt( catalog_key, url)) if curr_data_date is None: curr_data_date = data_date elif curr_data_date != data_date: msg = 'Data date is not the same!'\ ' curr_data_date: %s, data_date: %s, url: %s'\ % (curr_data_date, data_date, url) common.report_error(msg) raise Exception(msg) stype, category = catalog_key for stock_no, data in result.items(): stock_data = common.load_stock(stock_no) daily_report = stock_data.setdefault(common.DAILY, {}) meta = stock_data.setdefault(common.META, {}) daily_report[data_date] = data category_key = SEPARATOR.join(catalog_key) meta.update({ common.META_STOCK_NO: stock_no, common.META_COMPANY_TYPE: stype, common.META_COMPANY_CATEGORY: category, common.META_CATEGORY_KEY: category_key, common.META_NAME: data.pop('name'), common.META_DAYS: sorted(daily_report.keys(), reverse=True), }) stock_data.setdefault(common.META, {}).update(meta) common.save_stock(stock_no, stock_data) catalog.setdefault(category_key, []).append(stock_no) pbar.update(count) count += 1 if not catalog.setdefault(SEPARATOR.join(catalog_key), []): common.report_error('NO STOCK FOUND!!!! %s, %s' % (catalog_key, url)) common.save_catalog(catalog) state[common.CURRENT_DATA_DATE] = curr_data_date common.save_state(state) pbar.finish()
def fix_it(stock_no): data = common.load_stock(stock_no) for k, f in data[common.FINANCE].items(): if 'cash_flow_operatinh' in f: f['cash_flow_operating'] = f.pop('cash_flow_operatinh') common.save_stock(stock_no, data)
def transfer(read_from, save_to): click.echo('%s --> %s' % (read_from, save_to)) if read_from not in OPTIONS or save_to not in OPTIONS: print 'Should be %s or %s' % (LOCAL, FIREBASE) sys.exit(-1) if read_from == save_to: print 'Saving data to where it is from does not make sense.' sys.exit(-2) click.echo('This will OVERWRITE data in "%s". Are you sure? [y/N]' % save_to) confirm = sys.stdin.readline() if confirm.strip() != 'y': print 'byebye~' return common.READ_FROM = common.LOCAL if read_from == LOCAL else common.FIREBASE common.SAVE_TO = (common.LOCAL,)\ if save_to == LOCAL else (common.FIREBASE,) print 'Transfering catalog...' catalog = common.load_catalog() common.save_catalog(catalog) print 'Transfering categories...' catalog = common.load_catalog() categories = common.load_categories() common.save_categories(categories) print 'Transfering filter results...' f_results = common.load_filter_results() common.save_filter_results(f_results) print 'Transfering indicator results...' i_results = common.load_indicator_results() common.save_indicator_results(i_results) print 'Transfering config...' config = common.load_config() common.save_config(config) todo = [] for stocks in catalog.values(): todo.extend(stocks) total = len(todo) print 'Transfering sotcks...' widgets = [ FormatLabel( 'Processed: %(value)d / {0} (in: %(elapsed)s)'.format(total)) ] pbar = ProgressBar(widgets=widgets, maxval=total) count = 0 pbar.start() for s in todo: data = common.load_stock(s) common.save_stock(s, data) pbar.update(count) count += 1 pbar.finish() print 'Transfering state...' catalog = common.load_catalog() state = common.load_state() common.save_state(state)