def stocks_remove_filter_option(filter_id, filter_option_id): current_filter = db.filter_by_id(filter_id) remain = [o for o in current_filter.get('options', []) if o['_id'] != ObjectId(filter_option_id)] current_filter['options'] = remain db.save_filter(current_filter) return redirect(url_for('stocks', filter_id=current_filter['_id']))
def stocks_save_filter(filter_id): if request.method == 'POST': current_filter = db.filter_by_id(filter_id) name = request.form.get('filter_name', '') current_filter['name'] = name db.save_filter(current_filter) return redirect(url_for('stocks', filter_id=filter_id))
def stocks_add_filter_option(filter_id): if request.method == 'POST': name = request.form.get('filter_name') key = request.form.get('filter_option_key') morethan = request.form.get('filter_option_morethan') morethan = True if morethan == 'morethan' else False try: value = float(request.form.get('filter_option_value', 0)) except: value = 0 selected = [ filter_option for filter_option in db.available_filter_options if filter_option.key == key ][0] new_filter_option = db.FilterOption(key, selected.title, morethan, value, selected.is_boolean) current_filter = db.filter_by_id(filter_id) options = current_filter.get('options', []) filter_option_dict = new_filter_option._asdict() filter_option_dict['_id'] = ObjectId() options.append(filter_option_dict) current_filter['options'] = options current_filter['name'] = name db.save_filter(current_filter) return redirect(url_for('stocks', filter_id=current_filter['_id']))
def stocks_remove_rank_option(filter_id): rank_option_key = request.args.get('key', None) if not rank_option_key: return redirect(url_for('stocks', filter_id=filter_id)) current_filter = db.filter_by_id(filter_id) options = current_filter.get('options', []) options = [o for o in options if o['key'] != rank_option_key] current_filter['options'] = options db.save_filter(current_filter) return redirect( url_for('stocks', filter_id=filter_id, status='rank', alt='alt1'))
def stocks_add_rank_option(filter_id): rank_option_key = request.args.get('key', None) if not rank_option_key: return redirect(url_for('stocks', filter_id=filter_id)) options = [ r for r in db.available_rank_options if r.key == rank_option_key ] if options: rank_option_to_add = options[0] current_filter = db.filter_by_id(filter_id) options = current_filter.get('options', []) options.append(rank_option_to_add._asdict()) db.save_filter(current_filter) return redirect( url_for('stocks', filter_id=filter_id, status='rank', alt='alt1'))
def stocks(status=None, filter_id=None): find = None filter_fscore = False stat = {} if status == 'starred': find = {'starred': True} elif status == 'owned': find = {'owned': True} elif status == 'starredorowned': find = {'$or': [{'starred': True}, {'owned': True}]} elif status == 'doubtful': find = {'doubtful': True} elif status == 'fscore': filter_fscore = True order_by = request.args.get('order_by', 'expected_rate') ordering = request.args.get('ordering', 'desc') filters = db.all_filters() current_filter = None if filter_id: current_filter = db.filter_by_id(filter_id) stocks = db.all_stocks(order_by=order_by, ordering=ordering, find=find, filter_by_expected_rate=find==None, filter_bad=status!='bad', filter_fscore=filter_fscore, filter_options=(current_filter.filter_options if current_filter else [])) if status in ['owned', 'starred', 'starredorowned']: stat['low_pbr'] = len([stock for stock in stocks if stock.pbr <= 1]) stat['high_expected_rate'] = len([stock for stock in stocks if stock.expected_rate >= 15]) stat['fscore'] = len([stock for stock in stocks if stock.latest_fscore >= 3]) stat['mean_expected_rate'] = mean_or_zero([stock.expected_rate for stock in stocks]) stat['mean_expected_rate_by_low_pbr'] = mean_or_zero([stock.expected_rate_by_low_pbr for stock in stocks]) stat['mean_future_roe'] = mean_or_zero([stock.future_roe for stock in stocks]) qROE_numbers = [stock.QROEs[0][1] for stock in stocks if len(stock.QROEs) > 0] qROE_numbers = [float(roe_number) for roe_number in qROE_numbers if roe_number] stat['mean_qROEs'] = mean_or_zero(qROE_numbers) stat['qROEs_count'] = len(qROE_numbers) return render_template('stocks.html', stocks=stocks, order_by=order_by, ordering=ordering, status=status, available_filter_options=db.available_filter_options, filters=filters, current_filter=current_filter, stat=stat)
def stocks_remove_filter(filter_id): current_filter = db.filter_by_id(filter_id) db.remove_filter(filter_id) return redirect(url_for('stocks'))