Ejemplo n.º 1
0
def create_expenses(parent_operation, account, information):
    expenses = ['fees', 'tax', 'commission']
    for expense in expenses:
        if information[expense]!=0.0:
            operation = FinancialOperation()
    
            operation.name = parent_operation.name + ' (' + expense + ')'
            operation.short_name = parent_operation.name + ' (' + expense + ')'
            operation.status = parent_operation.status
            operation.operation_type = Attributes.objects.get(identifier='OPE_TYPE_' + expense.upper(), active=True)
            operation.creation_date = parent_operation.creation_date
            operation.operator = None
            operation.validator = None
            operation.source = account
            operation.target = None
            operation.spot = 1.0
            operation.repository = None
            operation.quantity = None
            operation.amount = -abs(information[expense])
            operation.price = None
            operation.operation_date = parent_operation.operation_date
            operation.operation_pnl = 0.0
            operation.value_date = parent_operation.value_date
            operation.termination_date = parent_operation.termination_date
            operation.associated_operation = operation
    
            operation.save()
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
def create_accrued(parent_operation, account, information):
    operation = FinancialOperation()

    operation.name = parent_operation.name + ' (interest re-payment)'
    operation.short_name = parent_operation.name + ' (interest re-payment)'
    operation.status = parent_operation.status
    operation.operation_type = Attributes.objects.get(identifier='OPE_TYPE_ACCRUED_PAYMENT', active=True)
    operation.creation_date = parent_operation.creation_date
    operation.operator = None
    operation.validator = None
    operation.source = account
    operation.target = None
    operation.spot = 1.0
    operation.repository = None
    operation.quantity = None
    operation.amount = information
    operation.price = None
    operation.operation_date = parent_operation.operation_date
    operation.operation_pnl = 0.0
    operation.value_date = parent_operation.value_date
    operation.termination_date = parent_operation.termination_date
    operation.associated_operation = operation

    operation.save()
Ejemplo n.º 4
0
def create_transfer(container, source, target, details, label):
    current_account_type = Attributes.objects.get(identifier='ACC_CURRENT', active=True)
    operation = FinancialOperation()
    operation.name = label if label!=None else generate_spot_movement_label(source, target, details)
    operation.short_name = generate_spot_movement_short_label(source, 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 = Attributes.objects.get(identifier='OPE_TYPE_INTERNAL_TRANSFER')
    operation.creation_date = details['operation_date']
    operation.operator = None
    operation.validator = None
    operation.source = get_account(container, source, current_account_type)
    operation.target = get_account(container, target, current_account_type)
    operation.spot = None
    operation.repository = None
    operation.quantity = None
    operation.amount = get_cash_movement_amount(source, target, details)
    operation.price = None
    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()
    create_expenses(operation, operation.source, details['source_expenses'])
    create_expenses(operation, operation.target, details['target_expenses'])
    return operation
Ejemplo n.º 5
0
def create_forward_operation(container, source, target, details, label):
    current_account_type = Attributes.objects.get(identifier='ACC_CURRENT', active=True)
    forward_account_type = Attributes.objects.get(identifier='ACC_FORWARD', active=True)
    
    open_operation = FinancialOperation()
    open_operation.name = "[OPEN] " + label if label!=None else generate_forward_open_label(source, target, details)
    open_operation.short_name = generate_forward_open_short_label(source, target, details)
    open_operation.status = Attributes.objects.get(identifier='OPE_STATUS_EXECUTED', active=True) if not details.has_key('status') else details['status']
    open_operation.operation_type = generate_spot_movement_operation_type(source, target, details)
    open_operation.creation_date = details['operation_date']
    open_operation.operator = None
    open_operation.validator = None
    open_operation.source = get_account(container, source, forward_account_type)
    open_operation.target = get_account(container, target, forward_account_type)
    open_operation.spot = details['spot_rate']
    open_operation.repository = None
    open_operation.quantity = None
    open_operation.amount = get_forward_amount(source, details)
    open_operation.price = None
    open_operation.operation_date = details['trade_date']
    open_operation.operation_pnl = 0.0
    open_operation.value_date = details['trade_date']
    open_operation.termination_date = details['trade_date']
    open_operation.associated_operation = None
    open_operation.save()
    create_expenses(open_operation, open_operation.source, details['source_expenses'])
    create_expenses(open_operation, open_operation.target, details['target_expenses'])
    
    close_operation = FinancialOperation()
    close_operation.name = "[CLOSE] " + label if label!=None else generate_forward_close_label(source, target, details)
    close_operation.short_name = generate_forward_close_short_label(source, target, details)
    close_operation.status = Attributes.objects.get(identifier='OPE_STATUS_EXECUTED', active=True) if not details.has_key('status') else details['status']
    close_operation.operation_type = generate_spot_movement_operation_type(target, source, details)
    close_operation.creation_date = details['operation_date']
    close_operation.operator = None
    close_operation.validator = None
    close_operation.source = get_account(container, source, forward_account_type)
    close_operation.target = get_account(container, target, forward_account_type)
    close_operation.spot = details['spot_rate']
    close_operation.repository = None
    close_operation.quantity = None
    close_operation.amount = -get_forward_amount(source, details)
    close_operation.price = None
    close_operation.operation_date = details['value_date']
    close_operation.operation_pnl = 0.0
    close_operation.value_date = details['value_date']
    close_operation.termination_date = details['value_date']
    close_operation.associated_operation = None
    close_operation.save()
    
    spot_operation = FinancialOperation()
    spot_operation.name = "[FWD EXEC] " + label if label!=None else generate_forward_open_label(source, target, details)
    spot_operation.short_name = generate_forward_open_short_label(source, target, details)
    spot_operation.status = Attributes.objects.get(identifier='OPE_STATUS_EXECUTED', active=True) if not details.has_key('status') else details['status']
    spot_operation.operation_type = generate_spot_movement_operation_type(source, target, details)
    spot_operation.creation_date = details['operation_date']
    spot_operation.operator = None
    spot_operation.validator = None
    # Clean account ids
    source['account_id'] = None
    target['account_id'] = None 
    spot_operation.source = get_account(container, source, current_account_type)
    spot_operation.target = get_account(container, target, current_account_type)
    spot_operation.spot = details['spot_rate'] if details['operation']=='BUY' else 1.0/details['spot_rate']
    spot_operation.repository = None
    spot_operation.quantity = None
    spot_operation.amount = -get_forward_amount(source, details)
    spot_operation.price = None
    spot_operation.operation_date = details['trade_date']
    spot_operation.operation_pnl = 0.0
    spot_operation.value_date = details['value_date']
    spot_operation.termination_date = details['value_date']
    spot_operation.associated_operation = None
    spot_operation.save()
Ejemplo n.º 6
0
def create_investment(container, source, target, details, label):
    current_account_type = Attributes.objects.get(identifier='ACC_CURRENT', active=True)
    
    operation = FinancialOperation()
    operation.name = label if label!=None else generate_investment_label(target, details)
    operation.short_name = generate_investment_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_investment_type(source, target, details)
    operation.creation_date = details['operation_date']
    operation.operator = None
    operation.validator = None
    operation.source = None
    operation.target = get_account(container, target, current_account_type) if target!=None else None
    operation.spot = None
    operation.repository = None
    operation.quantity = None
    operation.amount = target['quantity'] * target['price'] * (1.0 if details['operation']=='BUY' else -1.0)
    operation.price = None
    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()
    create_expenses(operation, operation.target, details['target_expenses'])
    return operation
Ejemplo n.º 7
0
def create_security_credit(container, source, target, details, label, is_dividend=True):
    current_account_type = Attributes.objects.get(identifier='ACC_CURRENT', active=True)
    operation_type = Attributes.objects.get(identifier='OPE_TYPE_DIVIDEND' if is_dividend else 'OPE_TYPE_COUPON', active=True)
    
    operation = FinancialOperation()
    operation.name = ('Dividends' if operation_type.identifier=='OPE_TYPE_DIVIDEND'  else 'Coupon') + ' on security ' + target['security'].name + ' of ' + str(target['price']) + ' per share'
    operation.short_name =('Dividends' if operation_type.identifier=='OPE_TYPE_DIVIDEND'  else 'Coupon') + ' on security ' + target['security'].name
    operation.status = Attributes.objects.get(identifier='OPE_STATUS_EXECUTED', active=True) if not details.has_key('status') else details['status']
    operation.operation_type = operation_type
    operation.creation_date = details['operation_date']
    operation.operator = None
    operation.validator = None
    operation.source = get_account(container, details, current_account_type)
    operation.target = target['security']
    operation.spot = details['spot_rate']
    operation.repository = None
    
    divisor = get_price_divisor(target['security'])

    operation.quantity = target['quantity']
    operation.amount = target['quantity'] * target['price'] * details['spot_rate'] / divisor
    operation.price = target['price']
    operation.operation_date = details['trade_date']
    operation.operation_pnl = operation.amount
    operation.value_date = details['value_date']
    operation.termination_date = details['value_date']
    operation.associated_operation = None
    operation.save()
    create_expenses(operation, operation.source, details['source_expenses'])
    create_expenses(operation, operation.source, details['target_expenses'])
    return operation