def get_chart(request, identifiers, span='day'): span = str_to_duration(span) aggregator = Aggregator() indexes = get_indexes(aggregator, identifiers) # Hide the pricing information for a user index hide_value = any([p.pk for p in indexes]) title = ', '.join([p.name for p in indexes]) chart_data_sets = [ChartData(p) for p in indexes] indexes = [cd.index for cd in chart_data_sets] market = Market.get(MARKET) market_hours = market.hours() if not (market_hours.is_open and datetime.now() >= market_hours.extended_opens_at): # Get the most recent open market hours, and change the start/end time accordingly market_hours = market_hours.previous_open_hours() start_time, end_time = get_start_and_end_time(market_hours, span) quotes, historicals = aggregator.quotes_and_historicals( start_time, end_time) for chart_data in chart_data_sets: chart_data.load(quotes, historicals, start_time, end_time) chart = Chart(title, span, market.timezone, market_hours, hide_value) chart.plot(*chart_data_sets) chart_img_data = chart.get_img_data() return HttpResponse(chart_img_data, content_type="image/png")
def check_all_present(self, results, items): identifiers = set() for i in items: identifiers.add(Aggregator.get_identifier(i)) for identifier in identifiers: identifier = Aggregator.get_identifier(identifier) self.assertTrue(identifier in results) instrument = self.instruments[identifier] self.assertTrue(results[identifier].instrument == instrument.url)
def test_quotes_with_indexes(self): index = Index.objects.create(user_id='testuser', name='TEST') [ index.asset_set.create(instrument=i) for i in self.instruments.values() ] aggregator = Aggregator(index) quotes = aggregator.quotes() self.check_all_present(quotes, index.assets())
def display_index(request, index_name=None): user = get_or_create_user(request) index = None if index_name: try: index = Index.objects.get(name=index_name.upper()) except Index.DoesNotExist: raise BadRequestException( "Index does not exist: '{}'".format(index_name)) else: indexes = Index.objects.filter(user=user) if not indexes: return mattermost_text("You do not have any indexes.\n\t" + "\n\t".join([p.name for p in indexes])) elif len(indexes) == 1: index = indexes[0] else: return mattermost_text( "You have multiple indexes. Specify the index you want to view.\n\t" + "\n\t".join([p.name for p in indexes])) aggregator = Aggregator(index) return print_index(index, aggregator, index.user == user)
def ready(self): if {'runserver', 'uwsgi'}.intersection(set(sys.argv)): from indexes.models import Index DATABASE_PRESENT = bool(connection.settings_dict['NAME']) if DATABASE_PRESENT and Index._meta.db_table in connection.introspection.table_names( ): # Preload instruments in users' indexes from quotes.aggregator import Aggregator import logging logger = logging.getLogger('stockbot') logger.info("Preloading index instruments") aggregator = Aggregator(*Index.objects.all())
def mattermost_chart(request, identifiers, span): aggregator = Aggregator() indexes = get_indexes(aggregator, identifiers) ids = identifiers.upper() # Replace slashes with hyphens for safety # Slashes could be present in date-formatted string ids = ids.replace('/', '-') chart_name = ', '.join([p.name for p in indexes]) # Add a timestamp to the image name to avoid caching future charts timestamp = datetime.now().strftime("%H%M%S") img_file_name = "{}_{}_{}".format(ids, timestamp, span) # Generate the image and cache it in advance get_chart_img(request, img_file_name) image_path = reverse('quote_img', args=[img_file_name]) image_url = request.build_absolute_uri(image_path) update_url = request.build_absolute_uri(reverse('quote_update')) actions = [ mattermost_action(update_url, 'refresh', identifiers=ids, span=span), mattermost_action(update_url, 'day', identifiers=ids, span='day'), mattermost_action(update_url, 'week', identifiers=ids, span='week'), mattermost_action(update_url, 'month', identifiers=ids, span='month'), mattermost_action(update_url, '3 months', identifiers=ids, span='3month'), mattermost_action(update_url, 'year', identifiers=ids, span='year'), mattermost_action(update_url, '5 years', identifiers=ids, span='5year'), ] response = { "response_type": "in_channel", "attachments": [{ "fallback": "{} Chart".format(chart_name), "text": chart_name, "image_url": image_url, "actions": actions }] } return response
def update_index(index, asset_defs, **opts): aggregator = Aggregator() process_assets(index, aggregator, asset_defs, **opts) return print_index(index, aggregator)
def test_quotes_with_assets(self): assets = [Asset(instrument=i) for i in self.instruments.values()] aggregator = Aggregator(*assets) quotes = aggregator.quotes() self.check_all_present(quotes, assets)
def test_quotes_with_instruments(self): aggregator = Aggregator(*self.instruments.values()) quotes = aggregator.quotes() self.check_all_present(quotes, self.instruments.values())
def test_quotes_with_identifiers(self): aggregator = Aggregator(*self.identifiers) quotes = aggregator.quotes() self.check_all_present(quotes, self.identifiers)