Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print timezone.now()
- deltaHours = float(options.get('deltaHours'))
- env = options.get('BFSEnv')
- if options.get('blmbg') is not None:
- recentlyChanged = Prices.objects.filter(blmbg__iexact = options.get('blmbg')).order_by('date').values()
- else:
- recentlyChanged = Prices.objects.filter(update_time__gte = timezone.now() - datetime.timedelta(hours=deltaHours)).order_by('date').values()
- res = defaultdict(list)
- print recentlyChanged.count()
- for Price in recentlyChanged:
- try:
- try:
- BFSObject = PricesToBFS.objects.get(blmbg__iexact=Price['blmbg'])
- except MultipleObjectsReturned:
- print "Multiple items with same blmbg code in pricesToBFS, blmbg: %s" % Price['blmbg']
- continue
- multiplier = BFSObject.multiplier
- isin = BFSObject.isin
- price_to_bfs = Price['price'] * multiplier
- try:
- #Check if is own product with page on homepage, thus having more fields to work with
- selected_product = product.objects.get(bloomberg_ticker__iexact=Price['blmbg'])
- price_to_bfs = float(price_to_bfs * selected_product.nominal_amount) / 100.0
- except ObjectDoesNotExist:
- None
- #If BFSObject has an exhangeblmng ticker, try to fetch price for that.
- #Check if there is a blomoberg ticker:
- try:
- if len(BFSObject.exchange_blmbg)>0:
- has_fx = True
- else:
- has_fx = False
- except:
- has_fx = False
- if has_fx:
- try:
- exchange_rate = Prices.objects.filter(blmbg__iexact = BFSObject.exchange_blmbg, date__lte = Price['date']).latest('date')
- price_to_bfs = price_to_bfs * exchange_rate.price
- except:
- print "Exhange rate on PricesToBFS is not NULL but can't find price in price table", BFSObject.exchange_blmbg, BFSObject.blmbg, Price['date']
- res[Price['date']].append({"ISIN": isin, "Price" : price_to_bfs})
- except ObjectDoesNotExist:
- logging.debug('Not uploaded since not found in blmbg list : ' + Price['blmbg'])
- print timezone.now()
- for unique_date in sorted(res.keys()):
- for price in res[unique_date]:
- result = self.SetPrice( unique_date, price, env )
- BFSObjects = PricesToBFS.objects.filter(isin__iexact=price['ISIN'])
- for BFSObject in BFSObjects:
- BFSObject.last_sent_to_bfs = "Uploaded: %s, target date: %s , %s %s" % (timezone.now(), unique_date, BFSObject.isin, price['Price'])
- BFSObject.save()
- time.sleep(.0625)
- print timezone.now()
- @retry(IOError, tries=40, delay=1, backoff=1)
- def SetPrice( self, target_date, prices, env):
- setPrices = self.client.factory.create('SetHistoricPrices2')
- setPrices.request.Credentials.UserName = self.user
- setPrices.request.Credentials.Password = self.password
- setPrices.request.identify = self.identify
- setPrices.request.PriceDate = datetime.datetime(target_date.year,target_date.month,target_date.day,2,0,0,0)
- setPrices.request.Prices.PriceTuple = prices
- return self.client.service.SetHistoricPrices2(setPrices.request)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement