Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import division
- import datetime
- from django.core.management.base import BaseCommand, CommandError
- import os
- import urllib2
- from backend.models import *
- from phpserialize import *
- import datetime
- from api.utils.NotificationManager import NotificationManager
- import commands
- import simplejson
- import gc
- def getClosestPrice(ticker, date):
- price = None
- iterations = 0
- iterations2 = 0
- dateor = date
- while not price and iterations < 7:
- _price = Price.objects.filter(security__ticker=ticker, date=date)
- if len(_price)>0:
- price = _price[0]
- else:
- iterations = iterations + 1
- date = date - datetime.timedelta(days=1)
- return price
- import math
- millnames=['', 'K', 'M', 'B', 'T']
- def convert_to_s(n):
- n = float(n)
- millidx=max(0,min(len(millnames)-1, int(math.floor(math.log10(abs(n))/3))))
- return '%.f%s'%(n/10**(3*millidx),millnames[millidx])
- class Command(BaseCommand):
- help = ("")
- def handle(self, *args, **options):
- try:
- securities = Security.objects.filter(ticker=args[0])
- except:
- securities = Security.objects.all()
- date_yesterday = datetime.date.today() - datetime.timedelta(days = 1)
- for security in securities:
- gc.collect()
- try: #TRY GIGANTE (MAL)
- q_cfs = CardsForSecurity.objects.filter(security=security)[0]
- if q_cfs.date > datetime.datetime.now() - datetime.timedelta(days = 1):
- continue
- CardsForSecurity.objects.filter(security=security).delete()
- try:
- bhi = int(BillionHoldingItem.objects.get(security=security))
- except:
- bhi = None
- if bhi:
- cfs_bhi = CardsForSecurity()
- cfs_bhi.security = security
- c_price = (((getClosestPrice(security.ticker, date_yesterday).adj_close_price / getClosestPrice(security.ticker, bhi.date_of_inclusion).adj_close_price) - 1) * 100)
- cfs_bhi.text = '%(company_name)s has been part of the iBillionaire Index since %(date_since)s and %(percentage)s since.' % {'company_name': security.name, 'date_since': bhi.date_of_inclusion.strftime("%B %d, %Y"), 'percentage': c_price }
- cfs_bhi.c_type = 1
- cfs_bhi.graph_date = bhi.date_of_inclusion
- cfs_bhi.save()
- del cfs_bhi
- del c_price
- funds = security.get_funds2()
- if funds:
- sum_sharres_amount = 0
- for fund in funds:
- try:
- report = Report.objects.get(pk=fund['fund_last_report_id'])
- report_item = ReportItem.objects.get(security=security, report=report)
- cfs = CardsForSecurity()
- cfs.security = security
- cfs.fund = Fund.objects.get(pk=fund['fund_id'])
- cfs.graph_date = report_item.updated_at
- cfs.shares_amount = fund['total_shares_amount']
- sum_sharres_amount = sum_sharres_amount + fund['total_shares_amount']
- cfs.c_type = 2
- v_str = {}
- v_str['manager_name'] = fund['fund_manager_name'].split(' ')[1]
- v_str['invested'] = convert_to_s((report_item.value * 1000))
- v_str['price'] = '%0.1f' % fund['average']
- #v_str['date'] = report_item.updated_at.strftime("%B %d, %Y")
- cfs.text = '%(manager_name)s has a $%(invested)s stake, acquired at an average price of $%(price)s per share' % v_str
- cfs.save()
- del cfs
- del v_str
- del report_item
- del report
- except Exception,e:
- print "EXCEPTIOOOOOOOOOOOOOOOOOONNNN: " + str(e)
- pass
- hh = security.get_all_holding_histories()
- sh_a = hh[-2]['shares'] - hh[-3]['shares']
- if sh_a > 0:
- cfs_ahh = CardsForSecurity()
- cfs_ahh.security = security
- cfs_ahh.graph_date = hh[-2]['date']
- cfs_ahh.shares_amount = hh[-2]['shares']
- cfs_ahh.c_type = 3
- cfs_ahh.text = 'Billionaires are buying %(sec_name)s. Last quarter, they bought %(shares_amount)s shares' % {'shares_amount': convert_to_s(sh_a), 'sec_name': security.name}
- cfs_ahh.save()
- del cfs_ahh
- else:
- cfs_ahh = CardsForSecurity()
- cfs_ahh.security = security
- cfs_ahh.graph_date = hh[-2]['date']
- cfs_ahh.shares_amount = hh[-2]['shares']
- cfs_ahh.c_type = 3
- cfs_ahh.text = 'Billionaires are selling %(sec_name)s. Last quarter, they sold %(shares_amount)s shares' % {'shares_amount': convert_to_s(sh_a), 'sec_name': security.name}
- cfs_ahh.save()
- del cfs_ahh
- else:
- cfs = CardsForSecurity()
- cfs.security = security
- cfs.text = 'No Billionaire owns this stock.'
- cfs.c_type = 4
- cfs.save()
- del cfs
- del funds
- except Exception,e:
- print str(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement