date = models.DateTimeField()
ticker = models.ForeignKey(Ticker)
shares = models.IntegerField('number of shares')
- value = models.DecimalField(
- 'value in US dollars', max_digits=20, decimal_places=2)
+
# If you want to track values in several currencies, check out the
# python-money package: http://code.google.com/p/python-money/
+ value = models.DecimalField(
+ 'value in US dollars', max_digits=20, decimal_places=2)
+
+ source = models.CharField(max_length=200) # ideally a permalink
def __unicode__(self):
return (u'{0.person} traded {0.shares} shares (${0.value}) '
'of {0.ticker} on {0.date}').format(self)
+
+
+def add_transaction(person, date, exchange, exchange_symbol, company,
+ company_symbol, shares, value, source):
+ if not value and not shares:
+ return # not a transaction (insider just declaring holdings?)
+ try:
+ person = Person.objects.get(name__iexact=person)
+ except Person.DoesNotExist:
+ person = Person(name=person)
+ print('adding new person {}'.format(person))
+ person.save()
+ try:
+ exchange = Exchange.objects.get(symbol__iexact=exchange_symbol)
+ except Exchange.DoesNotExist:
+ exchange = Exchange(name=exchange, symbol=exchange_symbol)
+ print('adding new exchange {}'.format(exchange))
+ exchange.save()
+ try:
+ tickers = Ticker.objects.filter(symbol__iexact=company_symbol)
+ ticker = tickers.get(exchange__id=exchange.id)
+ except Ticker.DoesNotExist:
+ try:
+ company = Company.objects.get(name__iexact=company)
+ except Company.DoesNotExist:
+ company = Company(name=company)
+ print('adding new company {}'.format(company))
+ company.save()
+ ticker = Ticker(exchange=exchange, company=company,
+ symbol=company_symbol)
+ print('adding new ticker {}'.format(ticker))
+ ticker.save()
+ company = ticker.company
+ try:
+ transactions = Transaction.objects.filter(person__id=person.id)
+ transactions = transactions.filter(ticker__id=ticker.id)
+ transactions = transactions.filter(date=date)
+ transactions = transactions.filter(shares=shares)
+ transaction = transactions.get(value=value)
+ except Transaction.DoesNotExist:
+ transaction = Transaction(
+ person=person,
+ date=date,
+ ticker=ticker,
+ shares=shares,
+ value=value,
+ source=source)
+ print('adding new transaction {}'.format(transaction))
+ transaction.save()