Advertisement
Guest User

Untitled

a guest
Nov 16th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. print timezone.now()
  2. deltaHours = float(options.get('deltaHours'))
  3. env = options.get('BFSEnv')
  4. if options.get('blmbg') is not None:
  5. recentlyChanged = Prices.objects.filter(blmbg__iexact = options.get('blmbg')).order_by('date').values()
  6. else:
  7. recentlyChanged = Prices.objects.filter(update_time__gte = timezone.now() - datetime.timedelta(hours=deltaHours)).order_by('date').values()
  8.  
  9. res = defaultdict(list)
  10. print recentlyChanged.count()
  11. for Price in recentlyChanged:
  12.  
  13. try:
  14. try:
  15. BFSObject = PricesToBFS.objects.get(blmbg__iexact=Price['blmbg'])
  16. except MultipleObjectsReturned:
  17. print "Multiple items with same blmbg code in pricesToBFS, blmbg: %s" % Price['blmbg']
  18. continue
  19.  
  20. multiplier = BFSObject.multiplier
  21. isin = BFSObject.isin
  22.  
  23.  
  24. price_to_bfs = Price['price'] * multiplier
  25.  
  26. try:
  27. #Check if is own product with page on homepage, thus having more fields to work with
  28. selected_product = product.objects.get(bloomberg_ticker__iexact=Price['blmbg'])
  29.  
  30. price_to_bfs = float(price_to_bfs * selected_product.nominal_amount) / 100.0
  31.  
  32. except ObjectDoesNotExist:
  33. None
  34. #If BFSObject has an exhangeblmng ticker, try to fetch price for that.
  35. #Check if there is a blomoberg ticker:
  36. try:
  37. if len(BFSObject.exchange_blmbg)>0:
  38. has_fx = True
  39. else:
  40. has_fx = False
  41. except:
  42. has_fx = False
  43.  
  44.  
  45. if has_fx:
  46. try:
  47. exchange_rate = Prices.objects.filter(blmbg__iexact = BFSObject.exchange_blmbg, date__lte = Price['date']).latest('date')
  48. price_to_bfs = price_to_bfs * exchange_rate.price
  49. except:
  50. print "Exhange rate on PricesToBFS is not NULL but can't find price in price table", BFSObject.exchange_blmbg, BFSObject.blmbg, Price['date']
  51.  
  52. res[Price['date']].append({"ISIN": isin, "Price" : price_to_bfs})
  53.  
  54. except ObjectDoesNotExist:
  55. logging.debug('Not uploaded since not found in blmbg list : ' + Price['blmbg'])
  56.  
  57. print timezone.now()
  58. for unique_date in sorted(res.keys()):
  59. for price in res[unique_date]:
  60. result = self.SetPrice( unique_date, price, env )
  61. BFSObjects = PricesToBFS.objects.filter(isin__iexact=price['ISIN'])
  62. for BFSObject in BFSObjects:
  63. BFSObject.last_sent_to_bfs = "Uploaded: %s, target date: %s , %s %s" % (timezone.now(), unique_date, BFSObject.isin, price['Price'])
  64. BFSObject.save()
  65. time.sleep(.0625)
  66.  
  67. print timezone.now()
  68.  
  69. @retry(IOError, tries=40, delay=1, backoff=1)
  70. def SetPrice( self, target_date, prices, env):
  71.  
  72. setPrices = self.client.factory.create('SetHistoricPrices2')
  73. setPrices.request.Credentials.UserName = self.user
  74. setPrices.request.Credentials.Password = self.password
  75. setPrices.request.identify = self.identify
  76. setPrices.request.PriceDate = datetime.datetime(target_date.year,target_date.month,target_date.day,2,0,0,0)
  77. setPrices.request.Prices.PriceTuple = prices
  78. return self.client.service.SetHistoricPrices2(setPrices.request)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement