Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.92 KB | None | 0 0
  1. from __future__ import division
  2. import datetime
  3. from django.core.management.base import BaseCommand, CommandError
  4. import os
  5. import urllib2
  6.  
  7. from backend.models import *
  8. from phpserialize import *
  9. import datetime
  10. from api.utils.NotificationManager import NotificationManager
  11. import commands
  12. import simplejson
  13. import gc
  14.  
  15.  
  16.  
  17. def getClosestPrice(ticker, date):
  18.     price = None
  19.     iterations  = 0
  20.     iterations2  = 0
  21.     dateor = date
  22.     while not price and iterations < 7:
  23.         _price = Price.objects.filter(security__ticker=ticker, date=date)
  24.         if len(_price)>0:
  25.             price = _price[0]
  26.         else:
  27.             iterations = iterations + 1
  28.             date = date - datetime.timedelta(days=1)
  29.  
  30.     return price
  31.  
  32.  
  33. import math
  34. millnames=['', 'K', 'M', 'B', 'T']
  35. def convert_to_s(n):
  36.     n = float(n)
  37.     millidx=max(0,min(len(millnames)-1, int(math.floor(math.log10(abs(n))/3))))
  38.  
  39.     return '%.f%s'%(n/10**(3*millidx),millnames[millidx])
  40.  
  41.  
  42. class Command(BaseCommand):
  43.     help = ("")
  44.  
  45.     def handle(self, *args, **options):
  46.         try:
  47.             securities = Security.objects.filter(ticker=args[0])
  48.         except:
  49.             securities = Security.objects.all()
  50.  
  51.         date_yesterday = datetime.date.today() - datetime.timedelta(days = 1)
  52.  
  53.         for security in securities:
  54.             gc.collect()
  55.             try: #TRY GIGANTE (MAL)
  56.                 q_cfs = CardsForSecurity.objects.filter(security=security)[0]
  57.                 if q_cfs.date > datetime.datetime.now() - datetime.timedelta(days = 1):
  58.                     continue
  59.  
  60.                 CardsForSecurity.objects.filter(security=security).delete()
  61.  
  62.                 try:
  63.                     bhi = int(BillionHoldingItem.objects.get(security=security))
  64.                 except:
  65.                     bhi = None
  66.  
  67.                 if bhi:
  68.                     cfs_bhi = CardsForSecurity()
  69.                     cfs_bhi.security = security
  70.                     c_price = (((getClosestPrice(security.ticker, date_yesterday).adj_close_price / getClosestPrice(security.ticker, bhi.date_of_inclusion).adj_close_price) - 1) * 100)
  71.                     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 }
  72.                     cfs_bhi.c_type = 1
  73.                     cfs_bhi.graph_date = bhi.date_of_inclusion
  74.                     cfs_bhi.save()
  75.  
  76.                     del cfs_bhi
  77.                     del c_price
  78.  
  79.                 funds = security.get_funds2()
  80.                 if funds:
  81.                     sum_sharres_amount = 0
  82.                     for fund in funds:
  83.                         try:
  84.                             report = Report.objects.get(pk=fund['fund_last_report_id'])
  85.                             report_item = ReportItem.objects.get(security=security, report=report)
  86.                             cfs = CardsForSecurity()
  87.                             cfs.security = security
  88.                             cfs.fund = Fund.objects.get(pk=fund['fund_id'])
  89.                             cfs.graph_date = report_item.updated_at
  90.                             cfs.shares_amount = fund['total_shares_amount']
  91.                             sum_sharres_amount = sum_sharres_amount + fund['total_shares_amount']
  92.                             cfs.c_type = 2
  93.  
  94.                             v_str = {}
  95.                             v_str['manager_name'] = fund['fund_manager_name'].split(' ')[1]
  96.                             v_str['invested'] = convert_to_s((report_item.value * 1000))
  97.                             v_str['price'] = '%0.1f' % fund['average']
  98.                             #v_str['date'] = report_item.updated_at.strftime("%B %d, %Y")
  99.  
  100.                             cfs.text = '%(manager_name)s has a $%(invested)s stake, acquired at an average price of $%(price)s per share' % v_str
  101.                             cfs.save()
  102.  
  103.                             del cfs
  104.                             del v_str
  105.                             del report_item
  106.                             del report
  107.  
  108.                         except Exception,e:
  109.                             print "EXCEPTIOOOOOOOOOOOOOOOOOONNNN:  " + str(e)
  110.                             pass
  111.  
  112.                     hh = security.get_all_holding_histories()
  113.                     sh_a = hh[-2]['shares'] - hh[-3]['shares']
  114.                     if sh_a > 0:
  115.                         cfs_ahh = CardsForSecurity()
  116.                         cfs_ahh.security = security
  117.                         cfs_ahh.graph_date = hh[-2]['date']
  118.                         cfs_ahh.shares_amount = hh[-2]['shares']
  119.                         cfs_ahh.c_type = 3
  120.                         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}
  121.                         cfs_ahh.save()
  122.  
  123.                         del cfs_ahh
  124.                     else:
  125.                         cfs_ahh = CardsForSecurity()
  126.                         cfs_ahh.security = security
  127.                         cfs_ahh.graph_date = hh[-2]['date']
  128.                         cfs_ahh.shares_amount = hh[-2]['shares']
  129.                         cfs_ahh.c_type = 3
  130.                         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}
  131.                         cfs_ahh.save()
  132.  
  133.                         del cfs_ahh
  134.                 else:
  135.                     cfs = CardsForSecurity()
  136.                     cfs.security = security
  137.                     cfs.text = 'No Billionaire owns this stock.'
  138.                     cfs.c_type = 4
  139.                     cfs.save()
  140.  
  141.                     del cfs
  142.  
  143.                 del funds
  144.  
  145.             except Exception,e:
  146.                 print str(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement