Exemple #1
0
def update_cash_holding(portfolio, amount):
    """
    update cash holdings after security buy/sell
    """
    #print "updating portfolioholding with amount:", amount
    # amount in cents. need to store $.
    amount = dollarize(amount)
    print 'amount to update in $:', amount
    cash_obj, created = Security.objects.get_or_create(symbol='CASH')
    try:
        cash_holding = PortfolioHolding.objects.get(portfolio=portfolio,
                        security=cash_obj)
        print 'held' + str(cash_holding.held)
        print 'add' + str(amount)
        print 'new held' + str(cash_holding.held + amount)
        cash_holding.held = cash_holding.held + amount
        print 'cash_holding.held', cash_holding.held
        cash_holding.save()
        
    except PortfolioHolding.DoesNotExist:
        print 'cash holding does not exists.'
        new_holding = PortfolioHolding()
        new_holding.portfolio = portfolio
        new_holding.security = cash_obj
        new_holding.held = amount
        new_holding.average_price = 1
        new_holding.last_price = 1
        new_holding.save()
Exemple #2
0
def update_holdings(portfolio, security):
    """
    takes portfolio and security and updates for that security.
    add to held if not present
    update security holding if held.
    """
    holding = None
    try:
        holding = PortfolioHolding.objects.get(portfolio=portfolio, security=security)
    except PortfolioHolding.DoesNotExist:
        # no holdings row exists for this security.
        new_holding = PortfolioHolding()
        new_holding.portfolio = portfolio
        new_holding.security = security
        new_holding.held = 0
        new_holding.average_price = 0
        new_holding.last_price = 0
        new_holding.save()
        holding = new_holding
        
    transactions = Transaction.objects.all().filter(portfolio=portfolio, security=security)
    held = 0;
    if security.symbol == 'CASH':
        for tran in transactions:
            if tran.type.name == 'Deposit':
                held = held + tran.amount
            elif tran.type.name == 'Withdraw':
                held = held - tran.amount
        
        print 'found it: ', held
        held = held / 100.00
        print 'after: ', held
    else:
        for tran in transactions:
            if tran.type.name == 'Buy':
                held = held + tran.units
            elif tran.type.name == 'Sell':
                held = held - tran.units
    
    holding.held = str(held)
    holding.save()