def compute_underlying_security(portfolios, tracks, spots, portfolio, operation, inner_security, key_date, work_date=None): portfolio_id = str(portfolio.id) inner_container = SecurityContainer.objects.get(id=inner_security) if not tracks.has_key(inner_security): track = get_main_track_content(inner_container) tracks[inner_security] = track value = get_closest_value(tracks[inner_security], operation.value_date if work_date==None else work_date) if value==None: LOGGER.error("No NAV available for " + inner_container.name + " as of " + str(from_epoch(long(key_date)))) return None divisor = get_price_divisor(inner_container) portfolios[portfolio_id][key_date][inner_security]['price'] = value['value'] portfolios[portfolio_id][key_date][inner_security]['price_divisor'] = divisor spot_pf = 1.0 if inner_container.currency.short_name!=portfolio.currency.short_name and (not spots.has_key(inner_container.currency.short_name) or not spots[inner_container.currency.short_name].has_key(portfolio.currency.short_name)): spot_track = get_exchange_rate(inner_container.currency.short_name, portfolio.currency.short_name) if not spots.has_key(inner_container.currency.short_name): spots[inner_container.currency.short_name] = {} spots[inner_container.currency.short_name][portfolio.currency.short_name] = spot_track if inner_container.currency.short_name!=portfolio.currency.short_name and spots.has_key(inner_container.currency.short_name) and spots[inner_container.currency.short_name].has_key(portfolio.currency.short_name): value = get_closest_value(spots[inner_container.currency.short_name][portfolio.currency.short_name], operation.value_date if work_date==None else work_date) if value!=None: spot_pf = value['value'] else: LOGGER.error("No SPOT available for " + inner_container.currency.short_name + '/' + portfolio.currency.short_name + " as of " + str(from_epoch(long(key_date)))) return None portfolios[portfolio_id][key_date][inner_security]['price_pf'] = portfolios[portfolio_id][key_date][inner_security]['price'] * spot_pf portfolios[portfolio_id][key_date][inner_security]['price_pf_divisor'] = portfolios[portfolio_id][key_date][inner_security]['price'] * spot_pf / divisor portfolios[portfolio_id][key_date][inner_security]['spot_pf'] = spot_pf portfolios[portfolio_id][key_date][inner_security]['price_date'] = value['date'].strftime('%Y-%m-%d')
def universe_details(request): if request.POST.has_key('universe_id'): universe_id = request.POST['universe_id'] else: universe_id = request.GET['universe_id'] # TODO: Check user user = User.objects.get(id=request.user.id) try: source = Universe.objects.get(Q(id=universe_id),Q(public=True)|Q(owner__id=request.user.id)) except: # TODO: Return error message return redirect('universes.html') context = {'universe': source, 'tracks': {}} for member in source.members.all(): if member.type.identifier not in ['CONT_COMPANY', 'CONT_BACKTEST', 'CONT_PORTFOLIO', 'CONT_COMPANY', 'CONT_OPERATION', 'CONT_PERSON', 'CONT_UNIVERSE']: effective_class_name = Attributes.objects.get(identifier=member.type.identifier + '_CLASS', active=True).name effective_class = classes.my_class_import(effective_class_name) member = effective_class.objects.get(id=member.id) content = get_main_track_content(member, True, True) if content!=None: context['tracks']['track_' + str(member.id)] = content else: context['tracks']['track_' + str(member.id)] = [] return render(request, 'universe_details.html', context)
def get_exchange_rate_price(source_currency, destination_currency, value_date): currency = SecurityContainer.objects.filter(name__startswith=source_currency + destination_currency) if not currency.exists(): threaded.bloomberg_data_query('NO_NEED', [source_currency + destination_currency + ' Curncy'], True) currency = SecurityContainer.objects.filter(name__startswith=source_currency + destination_currency) if currency.exists(): value = get_closest_value(get_main_track_content(currency[0]), dt.combine(value_date, dt.min.time())) if value==None: return 1.0 else: return value['value']
def create_security_movement(container, source, target, details, label): current_account_type = Attributes.objects.get(identifier='ACC_CURRENT', active=True) security_account_type = Attributes.objects.get(identifier='ACC_SECURITY', active=True) operation = FinancialOperation() operation.name = label if label!=None else generate_security_movement_label(target, details) operation.short_name = generate_security_movement_short_label(target, details) operation.status = Attributes.objects.get(identifier='OPE_STATUS_EXECUTED', active=True) if not details.has_key('status') else details['status'] operation.operation_type = generate_security_movement_operation_type(details) operation.creation_date = details['operation_date'] operation.operator = None operation.validator = None operation.source = get_account(container, details, current_account_type) if details['impact_pnl'] else None operation.target = target['security'] if operation.target.status.identifer!='STATUS_ACTIVE': # Status cannot be executed on a pending security operation.status = Attributes.objects.get(identifier='OPE_STATUS_PENDING', active=True) if not details.has_key('status') else details['status'] operation.spot = details['spot_rate'] operation.repository = get_account(container, details, security_account_type) operation.quantity = target['quantity'] used_price = target['price'] if not details['impact_pnl'] and (used_price==None or used_price==0.0): track = get_main_track_content(target['security']) value = get_closest_value(track, dt.strptime(details['trade_date'], '%Y-%m-%d')) if value==None: LOGGER.warn(target['security'].name + " - Transaction does not have a correct price but no valid price could be found.") used_price = 0.0 else: LOGGER.warn(target['security'].name + " - Transaction does not have a correct price but a valid price could be found.") used_price = value['value'] divisor = get_price_divisor(target['security']) operation.amount = (target['quantity'] * used_price * details['spot_rate']) / divisor operation.price = target['price'] operation.operation_date = details['trade_date'] operation.operation_pnl = 0.0 operation.value_date = details['value_date'] operation.termination_date = details['value_date'] operation.associated_operation = None operation.save() if details['impact_pnl']: create_expenses(operation, operation.source, details['source_expenses']) create_expenses(operation, operation.source, details['target_expenses']) if details.has_key('accrued_interest'): if details['accrued_interest'].has_key('source') and details['accrued_interest']['source']!=None and details['accrued_interest']['source']!=0.0: create_accrued(operation, operation.source, details['accrued_interest']['source']) if details['accrued_interest'].has_key('target') and details['accrued_interest']['target']!=None and details['accrued_interest']['target']!=0.0: create_accrued(operation, operation.source, details['accrued_interest']['target']) return operation
def get_security_price(isin_code,bloomberg_code, value_date): securities = [] if isin_code!=None and isin_code!='': securities = SecurityContainer.objects.filter(aliases__alias_type__name='ISIN', aliases__alias_value=isin_code) elif bloomberg_code!=None and bloomberg_code!='': securities = SecurityContainer.objects.filter(aliases__alias_type__name='BLOOMBERG', aliases__alias_value=bloomberg_code) if len(securities)==0 and ((bloomberg_code!=None and bloomberg_code!='') or (isin_code!=None and isin_code!='')): threaded.bloomberg_data_query('NO_NEED', [isin_code if bloomberg_code==None or bloomberg_code=='' else bloomberg_code], True) if isin_code!=None and isin_code!='': securities = SecurityContainer.objects.filter(aliases__alias_type__name='ISIN', aliases__alias_value=isin_code) elif bloomberg_code!=None and bloomberg_code!='': securities = SecurityContainer.objects.filter(aliases__alias_type__name='BLOOMBERG', aliases__alias_value=bloomberg_code) if len(securities)==0: LOGGER.warn("Security cannot be found:" + str(isin_code) + "/" + str(bloomberg_code)) return 1.0 else: value = get_closest_value(get_main_track_content(securities[0]), dt.combine(value_date, dt.min.time())) if value==None: return 1.0 else: return value['value']