def post(self, instrument): """ Create/Update detail information of the given instrument. :param instrument: instrument name """ return run_func_against_dao(lambda dao: 201 if dao.update_instrument( instrument, api.payload) else 500)
def get(self): return run_func_against_dao(lambda dao: [{ 'ccy': x[0], 'broker': x[1], 'balance': x[2], 'xccy': x[3] } for x in dao.get_cash_balance()])
def get(self): """ Return list of countries """ return run_func_against_dao(lambda dao: [{ 'id': x[0], 'country': x[1] } for x in dao.get_countries()])
def post(self, instrument): """ Create/Update country allocations for the given instrument. :param instrument: instrument name """ return None, run_func_against_dao( lambda dao: 201 if dao.update_instrument_country_allocations( instrument, api.payload) else 500)
def post(self, stock): """ Create/Update a stock/ETF transaction. :param stock: instrument name """ return run_func_against_dao( lambda dao: 201 if dao.update_stock_transaction(stock, api.payload) else 500)
def get(self): """ Return list of instrument types """ return run_func_against_dao(lambda dao: [{ 'id': x[0], 'type': x[1] } for x in dao.get_instrument_types()])
def get(self): """ Return list of brokers """ return run_func_against_dao(lambda dao: [{ 'id': x[0], 'name': x[1], 'full_name': x[2] } for x in dao.get_brokers()])
def get(self): """ Return a list of portfolios """ portfolios = run_func_against_dao( lambda dao: get_portfolios(dao, date.today())) return [{ 'name': name, 'allocations': portfolio.to_dict('records') } for name, portfolio in portfolios]
def get(self, instrument): """ Returns list of region allocations of instruments. :param instrument: instrument name """ return run_func_against_dao(lambda dao: [{ 'regions': [{ 'Region': x[0], 'ratio': x[1] } for x in dao.get_region_allocation(instrument_name=instrument)] }])
def get(self, instrument): """ Returns list of asset allocations of the given instrument. :param instrument: instrument name """ return run_func_against_dao(lambda dao: [{ 'assets': [{ 'asset': x[0], 'ratio': x[1] } for x in dao.get_asset_allocation(instrument_name=instrument)] }])
def get(self, instrument): """ Returns list of country allocations of instruments. :param instrument: instrument name """ return run_func_against_dao(lambda dao: [{ 'countries': [{ 'country': x[0], 'ratio': x[1] } for x in dao.get_country_allocation(instrument_name=instrument)] }])
def get(self, name, new_fund=0): """ Return a list of portfolio rebalancing plans. """ r = run_func_against_dao( lambda dao: rebalance_portfolio(dao, date.today(), name, new_fund)) plans = r['plans'] if len(plans) == 0: return {} merged = r['merged'] rebalancing = { 'plans': [{ "new_funds": p.delta_funds.sum(), "allocations": p.to_dict('records') } for p in plans], 'merged': {} if merged is None else { "new_funds": merged.delta_funds.sum(), "allocations": merged.to_dict('records') } } return rebalancing
def post(self, ccy, broker): return None, run_func_against_dao( lambda dao: 201 if dao.update_cash_balance(ccy, broker, api.payload) else 500)
def get(self): """ Return list of stock/ETF quotes """ return run_func_against_dao(lambda dao: _get_stock_quote( dao, None, _get_int_from_query_param('max_days')))
def get(self, stock): """ Return all quotes of the given stock/ETF """ return run_func_against_dao(lambda dao: _get_stock_quote( dao, stock, _get_int_from_query_param('max_days')))
def get(self, stock): """ Return all transactions of the given stock/ETF """ return run_func_against_dao( lambda dao: _get_stock_transaction(dao, stock))
def get(self): """ Return list of detail information of instruments """ return run_func_against_dao(lambda dao: _get_instruments(dao))
def get(self, from_ccy, to_ccy): """ Return all quotes of the given currency pair """ return run_func_against_dao(lambda dao: _get_xccy_quote( dao, (from_ccy, to_ccy), _get_int_from_query_param('max_days')))
def get(self): """ Return list of stock/ETF transactions """ return run_func_against_dao(lambda dao: _get_stock_transaction(dao))
def get(self): """ Return list of the quotes of the all (known) currency pairs """ return run_func_against_dao(lambda dao: _get_xccy_quote( dao, None, _get_int_from_query_param('max_days')))
def post(self): return run_func_against_dao( lambda dao: 201 if dao.update_xccy_quotes(api.payload) else 500)
def post(self, fund): return run_func_against_dao( lambda dao: 201 if dao.update_fund_performance(fund, api.payload) else 500)
def get(self, instrument): """ Return detail information of the given instrument """ return run_func_against_dao( lambda dao: _get_instruments(dao, instrument))
def get(self): """ Returns list of fund performance. """ return run_func_against_dao( lambda dao: FundReport(dao, date.today()).positions)
def get(self, broker): return run_func_against_dao(lambda dao: _get_active_funds(dao, broker))
def post(self, broker): return run_func_against_dao(lambda dao: 201 if dao.mark_inactive_funds( broker, api.payload) else 500)
def get(self): """ Return a summary of all positions """ def _alloc_auto_other(allocations): alloc = [ AllocItem(x[0], x[1]) for x in allocations if x[0] != 'Other' ] total = reduce(lambda accu, x: accu + x.ratio, alloc, 0) if total > 100: log.error('Alloc over 100% : {}'.format(allocations)) else: alloc.append(AllocItem('Other', 100 - total)) for a in alloc: yield (a.alloc, a.ratio) def _get_position(dao, report): return [{ 'instrument': { 'id': p['instrument'], 'name': p['symbol'] }, 'asset_allocation': [{ 'asset': x[0], 'ratio': x[1] } for x in dao.get_asset_allocation( instrument_id=p['instrument'])], 'country_allocation': [{ 'country': x[0], 'ratio': x[1] } for x in _alloc_auto_other( dao.get_country_allocation(instrument_id=p['instrument'])) ], 'region_allocation': [{ 'region': x[0], 'ratio': x[1] } for x in _alloc_auto_other( dao.get_region_allocation(instrument_id=p['instrument']))], 'ccy': p['ccy'], 'xccy': p['xccy'], 'shares': p['shares'], 'price': p['price'], 'capital': -p['liquidated'] } for p in report if p['shares'] > 0] def _get_stock_etf_positions(dao): r = StockReport2(dao, date.today()).stock_positions() return _get_position(dao, r['ETF']), _get_position(dao, r['Stock']) def _get_all(dao): stocks = _get_stock_etf_positions(dao) # change to the same format as stock position # Japan mutual funds have different ways to calculate amount(口), # we simplify here by assigning total market value (scraped from broker's page) to price and keep share be 1 funds = [{ 'instrument': p['instrument_id'], 'symbol': p['name'], 'ccy': 'JPY', 'xccy': 1, 'shares': 1, 'price': p['value'], 'liquidated': -p['capital'] } for p in FundReport(dao, date.today()).positions] return { 'ETF': stocks[0], 'Stock': stocks[1], 'Funds': _get_position(dao, funds), 'Cash': [{ 'ccy': x[0], 'broker': x[1], 'balance': x[2], 'xccy': x[3] } for x in dao.get_cash_balance()] } return run_func_against_dao(lambda dao: _get_all(dao))
def post(self, stock): return run_func_against_dao(lambda dao: 201 if dao.update_stock_quotes( stock, api.payload) else 500)