Exemplo n.º 1
0
def import_external_tracks(data_source):
    nav_value = Attributes.objects.get(identifier='NUM_TYPE_NAV', active=True)
    final_status = Attributes.objects.get(identifier='NUM_STATUS_FINAL', active=True)
    official_type = Attributes.objects.get(identifier='PRICE_TYPE_OFFICIAL', active=True)
    daily = Attributes.objects.get(identifier='FREQ_DAILY', active=True)
    data_provider = Attributes.objects.get(identifier='SCR_DP', active=True)

    provider = CompanyContainer.objects.get(short_name__icontains=data_source)

    external_provider = RelatedCompany.objects.get(company=provider, role=data_provider)

    query = QUERIES[data_source]['tracks']['query']

    securities = SecurityContainer.objects.filter(aliases__alias_type__short_name=data_source.upper()).distinct()
    for security in securities:
        LOGGER.info("Working on " + security.name)
        if not TrackContainer.objects.filter(effective_container_id = security.id).exists():
            # Switch to new data provider
            if security.associated_companies.filter(role=data_provider).exists():
                security.associated_companies.remove(security.associated_companies.get(role=data_provider))
                security.save()
                security.associated_companies.add(external_provider)
                security.save()
        try:
            track = TrackContainer.objects.get(
                                effective_container_id=security.id,
                                type__id=nav_value.id,
                                quality__id=official_type.id,
                                source__id=provider.id,
                                frequency__id=daily.id,
                                status__id=final_status.id)
            LOGGER.info("\tTrack already exists")
        except:
            track = TrackContainer()
            track.effective_container = security
            track.type = nav_value
            track.quality = official_type
            track.source = provider
            track.status = final_status
            track.frequency = daily
            track.frequency_reference = None
            track.save()
        all_values = []
        # TODO: Change while import of universe is correct
        alias = security.aliases.filter(alias_type__short_name=data_source.upper())
        alias = alias[0]
        results = dbutils.query_to_dicts(query%alias.alias_value, data_source)
        for result in results:
            if result['prezzo']!='' and result['prezzo']!=None:
                all_values.append({'date': dt.combine(result['data_ins'], dt.min.time()), 'value': float(result['prezzo'])})
            else:
                LOGGER.warning("\tInvalid price value " + unicode(result))
        set_track_content(track, all_values, True)
        if len(all_values)>0 and security.associated_companies.filter(company=provider, role=data_provider).exists():
            populate_perf(track.effective_container, track)
            populate_monthly_track_from_track(track.effective_container, track)
            populate_weekly_track_from_track(track.effective_container, track)
        LOGGER.info("\tFound " + str(len(all_values)) + " tokens in external track [" + str(alias.alias_value) + "]")
Exemplo n.º 2
0
def import_prices(container):
    guardian_alias = Attributes.objects.get(type='alias_type', short_name='GUARDIAN')

    nav_value = Attributes.objects.get(identifier='NUM_TYPE_NAV', active=True)
    final_status = Attributes.objects.get(identifier='NUM_STATUS_FINAL', active=True)
    official_type = Attributes.objects.get(identifier='PRICE_TYPE_OFFICIAL', active=True)
    daily = Attributes.objects.get(identifier='FREQ_DAILY', active=True)
    data_provider = Attributes.objects.get(identifier='SCR_DP', active=True)

    guardian = CompanyContainer.objects.get(short_name__icontains='GUARDIAN')

    guardian_provider = RelatedCompany.objects.filter(company=guardian, role=data_provider)
    if guardian_provider.exists():
        guardian_provider = guardian_provider[0]
    else:
        guardian_provider = RelatedCompany()
        guardian_provider.role = data_provider
        guardian_provider.company = guardian
        guardian_provider.save()

    if container.aliases.filter(alias_type=guardian_alias).exists():
        old_provider = container.associated_companies.filter(role=data_provider)
        if old_provider.exists():
            old_provider = old_provider[0]
            container.associated_companies.remove(old_provider)
            container.save()
            container.associated_companies.add(guardian_provider)
            container.save()
        try:
            track = TrackContainer.objects.get(
                                effective_container_id=container.id,
                                type__id=nav_value.id,
                                quality__id=official_type.id,
                                source__id=guardian.id,
                                frequency__id=daily.id,
                                status__id=final_status.id)
            LOGGER.info("\tTrack already exists")
        except:
            track = TrackContainer()
            track.effective_container = container
            track.type = nav_value
            track.quality = official_type
            track.source = guardian
            track.status = final_status
            track.frequency = daily
            track.frequency_reference = None
            track.save()
        all_prices = get_prices('guardian', container.aliases.get(alias_type=guardian_alias).alias_value)
        all_tokens = []
        for price in all_prices:
            if price['prezzo']!=None:
                all_tokens.append({'date': datetime.datetime.strptime(price['data_ins'], '%Y-%m-%d'), 'value': price['prezzo']})
        set_track_content(track, all_tokens, True)
        print all_tokens
Exemplo n.º 3
0
 def generate_tracks(self, container, frequency, valuation):
     LOGGER.info("Computing tracks of " + container.name + " with frequency " + frequency.name)
     perf_type = Attributes.objects.get(identifier='NUM_TYPE_PERF', active=True)
     nav_type = Attributes.objects.get(identifier='NUM_TYPE_NAV', active=True)
     final_status = Attributes.objects.get(identifier='NUM_STATUS_FINAL', active=True)
     official_type = Attributes.objects.get(identifier='PRICE_TYPE_OFFICIAL', active=True)
     finale_company = CompanyContainer.objects.get(name='FinaLE Engine')
     
     nav_track = get_track(container, {'track_type': 'NUM_TYPE_NAV', 'track_frequency':frequency.identifier, 'track_default': False, 'track_datasource': 'DATASOURCE_FINALE'})
     perf_track = get_track(container, {'track_type': 'NUM_TYPE_PERF', 'track_frequency':frequency.identifier, 'track_default': False, 'track_datasource': 'DATASOURCE_FINALE'})
     
     nav_content = []
     perf_content = []
     
     if nav_track==None:
         nav_track = TrackContainer()
         nav_track.effective_container = container
         nav_track.type = nav_type
         nav_track.quality = official_type
         nav_track.source = finale_company
         nav_track.status = final_status
         nav_track.frequency = frequency
         nav_track.frequency_reference = None
         nav_track.save()
     if perf_track==None:
         perf_track = TrackContainer()
         perf_track.effective_container = container
         perf_track.type = perf_type
         perf_track.quality = official_type
         perf_track.source = finale_company
         perf_track.status = final_status
         perf_track.frequency = frequency
         perf_track.frequency_reference = None
         perf_track.save()
     perf_key = PERF_MAPPING[frequency.identifier][0]
     if container.inception_date==None:
         nav_content.append({'date': datetime.datetime(2013,12,31), 'value': 100.0})
     else:
         nav_content.append({'date': dt.combine(dates.AddDay(container.inception_date, -1), dt.min.time()), 'value': 100.0})
         previous = 100.0
     for key_date in sorted(valuation.keys()):
         working_date = dt.combine(from_epoch(long(key_date)), dt.min.time())
         if get_work_date(working_date, frequency)==working_date:
             perf_content.append({'date': working_date, 'value': valuation[key_date]['performances']['mdietz'][perf_key]/100.0})
             previous = previous * (1.0 + valuation[key_date]['performances']['mdietz'][perf_key]/100.0)
             nav_content.append({'date': working_date, 'value':previous})
         
     set_track_content(perf_track, perf_content, True)
     set_track_content(nav_track, nav_content, True)