def get_valuation_content_display(content, start_date=None, end_date=None):
    key_start = 0
    if start_date!=None:
        key_start = epoch_time(start_date)
    key_end = float('inf')
    if end_date!=None:
        key_end = epoch_time(end_date)
    all_values = {from_epoch(long(key_date)).strftime('%Y-%m-%d'): content[key_date] for key_date in sorted(content.keys()) if key_start<=long(key_date) and key_end>=long(key_date)}
    return all_values
def set_data(collection_name, data, historize=True):
    if historize:
        data['_id'] = epoch_time(datetime.datetime.today())
        getattr(setup,collection_name).insert(data)
    else:
        if not data.has_key('_id'):
            data['_id'] = epoch_time(datetime.datetime.today())
        getattr(setup,collection_name).update({'_id': data['_id']}, data, True)
    if globals().has_key(collection_name + '_callback'):
        globals()[collection_name + '_callback'](data)
def import_external_grouped_data(data_source, data_type):
    LOGGER.info('Loading working data')
    query = QUERIES[data_source][data_type]['query']
    group_by = QUERIES[data_source][data_type]['group_by']
    
    LOGGER.info('Importing ' + str(data_type) + ' from ' + str(data_source))
    LOGGER.info('Using query:' + str(query))
    results = dbutils.query_to_dicts(query, data_source)
    database = getattr(client, data_source)
    
    all_values = {}
    
    for result in results:
        keys = []
        for key in group_by:
            if isinstance(result[key], datetime.date):
                keys.append(epoch_time(dt.combine(result[key],dt.min.time())))
            else:
                keys.append(result[key].replace('.','___'))
        # Clean the data        
        new_entry = convert_to_mongo(result)
        
        current_values = all_values
        for key in keys[:-1]:
            if not current_values.has_key(key):
                current_values[key] = {}
            current_values = current_values[key]
        if not current_values.has_key(keys[-1]):
            current_values[keys[-1]] = []
        current_values[keys[-1]].append(new_entry)
    
    for key in all_values.keys():
        LOGGER.info("Inserting " + data_type + " with _id: " + str(key))
        database[data_type].remove({'_id': key})
        database[data_type].insert({'_id': key, 'values': all_values[key]})   
Beispiel #4
0
def set_object_type_fields(values):
    values['_id'] = epoch_time(datetime.datetime.today())
    setup.object_type_fields.insert(values)
    all_types = Attributes.objects.filter(active=True, type='object_type')
    for a_type in all_types:
        if values.has_key(a_type.identifier):
            context = Context({"selection": values[a_type.identifier]})
            template = loader.get_template('rendition/object_type/lists/object_type_choices.html')
            rendition = template.render(context)
            # TODO Implement multi-langage
            outfile = os.path.join(STATICS_GLOBAL_PATH, a_type.identifier + '_en.html')
            with open(outfile,'w') as o:
                o.write(rendition.encode('utf-8'))
Beispiel #5
0
def send_bloomberg_get_history(tickers, fields=['PX_LAST'], ticker_type = 'ISIN', use_terminal = False, start_date = datetime.datetime(1972,1,1,0,0,0)):
    request = bloomberg_pb2.BloombergRequest()
    request.program = 'getHistory'
    request.rawmode = False
    request.tickertype = ticker_type
    request.startdate = dates.epoch_time(start_date)
    request.terminal = use_terminal
    [request.tickers.append(t) for t in tickers]
    [request.fields.append(f) for f in fields]
    
    sender = BloombergRequestSender(request)
    sender.start()
    time.sleep(1)
    LOGGER.info("Waiting for getHistory response")
    while not sender._stop.isSet():
        None
    return sender.response_object
Beispiel #6
0
def set_track_content(track, values, clean):
    LOGGER.info('Storing track content')
    if len(values)>0:
        container_id = 'trackscontainer_' + str(track.effective_container_id)
        track_id = 'track_' + str(track.id)
        proper_values = [{'_id': epoch_time(value['date']), 'value': value['value']} for value in values]
        if clean:
            client[container_id][track_id].drop()
            client[container_id][track_id].insert(proper_values)
        else:
            for value in proper_values:
                client[container_id][track_id].update({'_id': value['_id']}, value, True)
        LOGGER.info('Track content stored as ' + container_id + '.' + track_id + ' with ' + str(client[container_id][track_id].count()) + ' elements.')
        if len(values)>0:
            track.start_date = values[0]['date']
            track.end_date = values[len(values)-1]['date']
            track.save()
    else:
        LOGGER.warn('Cannot store empty tracks into MongoDB')
        
def get_closest_value(all_data, value_date, epoch_search=True):
    if value_date==None:
        return None
    previous = None
    if epoch_search:
        searched_date = str(epoch_time(value_date))
    elif not isinstance(value_date, basestring):
        searched_date = value_date.strftime('%Y-%m-%d')
    else:
        searched_date = value_date
    key_dates = sorted(all_data.keys())
    for key in key_dates:
        if key==searched_date:
            return all_data[key]
        elif key>searched_date:
            if previous==None:
                return None
            else:
                return all_data[previous]
        previous = key
    return None if previous==None else all_data[previous]
def set_container_history(container, field, value, provider = None):
    print field
    print value
    # TODO Handle Intraday
    collection_id = field + '_' + str(container.id)
    key_field = get_custom_historical_key(container.type.identifier, field)
    fields_info = setup_content.get_data('container_type_fields')[container.type.identifier]
    for info in fields_info:
        if info['name']==field and info['type']=='OBJECT_TYPE_HISTORICAL':
            break
    
    if key_field!=None:
        wrk_id = epoch_time(datetime.datetime.strptime(value[key_field], '%Y-%m-%d'))
    else:
        wrk_id = None
    if info['instanceType']=='FIELD_ASSIGNMENT_SINGLE' or (info['instanceType']=='FIELD_ASSIGNMENT_LIMITED' and int(info['repeat'])==1):
        value['_id'] = wrk_id
        historical[collection_id].update({'_id':wrk_id}, value, True)
    else:
        content = historical[collection_id].find_one({'_id':wrk_id})
        if content==None:
            content = []
        else:
            content = content['content']
        if info['instanceType']=='FIELD_ASSIGNMENT_MULTIPLE' or ((info['instanceType']=='FIELD_ASSIGNMENT_LIMITED' and len(content)<int(info['repeat'])) or value.has_key['index']):
            if value.has_key['index']:
                new_content = []
                for element in content:
                    if element['index']==value['index']:
                        new_content.append(value)
                    else:
                        new_content.append(element)
                content = new_content
            else:
                content.append(value)
        else:
            None
        historical[collection_id].update({'_id':wrk_id}, {'_id': wrk_id, 'content': content}, True)
def set_sequoia_map(values):
    values['_id'] = epoch_time(datetime.datetime.today())
    LOGGER.debug("Stored id [" + str(values['_id']) + "]" )
    custom.sequoia_map.insert(values)
def get_positions(data_source, working_date):
    database = getattr(client, data_source)
    key = epoch_time(dt.combine(working_date,dt.min.time()))
    LOGGER.info("Getting positions with _id: " + str(key))
    return database['positions'].find_one({'_id': key})
Beispiel #11
0
def compute_positions(container=None):
    LOGGER.info("Start computing securities positions")
    if container==None:
        all_operations = FinancialOperation.objects.filter(operation_type__identifier__in=['OPE_TYPE_BUY','OPE_TYPE_SELL','OPE_TYPE_BUY_FOP','OPE_TYPE_SELL_FOP']).order_by('value_date')
    else:
        accounts = container.accounts.all()
        accounts_ids = [account.id for account in accounts]
        all_operations = FinancialOperation.objects.filter(Q(repository__id__in=accounts_ids) | Q(source__id__in=accounts_ids) | Q(target__id__in=accounts_ids), operation_type__identifier__in=['OPE_TYPE_BUY','OPE_TYPE_SELL','OPE_TYPE_BUY_FOP','OPE_TYPE_SELL_FOP']).order_by('value_date')
        
    tracks = {}
    spots = {}
    
    portfolios = {}
    securities = {}
    previous_date = {}
    for operation in all_operations:
        LOGGER.info(operation.name)
        portfolio = PortfolioContainer.objects.filter(accounts__id=operation.repository.id)
        if portfolio.exists():
            portfolio = portfolio[0]
        else:
            LOGGER.error("No portfolio associated to account")
            continue
        key_date = str(epoch_time(operation.value_date))
        security_id = str(operation.target.id)
        
        portfolio_id = str(portfolio.id)
        if not securities.has_key(security_id):
            securities[security_id] = {}
        if not portfolios.has_key(portfolio_id):
            portfolios[portfolio_id] = {}
        if previous_date.has_key(security_id):
            securities[security_id][key_date] = copy.deepcopy(securities[security_id][previous_date[security_id]])
        if not securities[security_id].has_key(key_date):
            securities[security_id][key_date] = {'total': 0.0, 'name': operation.target.short_name, 'id': operation.target.id, 'type': operation.target.type.identifier}
        if not portfolios[portfolio_id].has_key(key_date):
            portfolios[portfolio_id][key_date] = {}
        if not securities[security_id][key_date].has_key(portfolio_id):
            securities[security_id][key_date][portfolio_id] = 0.0
        securities[security_id][key_date][portfolio_id] = securities[security_id][key_date][portfolio_id] + (operation.quantity * (1.0 if operation.operation_type.identifier in ['OPE_TYPE_BUY', 'OPE_TYPE_BUY_FOP'] else -1.0))
        securities[security_id][key_date]['total'] = securities[security_id][key_date]['total'] + (operation.quantity * (1.0 if operation.operation_type.identifier in ['OPE_TYPE_BUY', 'OPE_TYPE_BUY_FOP'] else -1.0))
        if previous_date.has_key(portfolio_id):
            portfolios[portfolio_id][key_date] = copy.deepcopy(portfolios[portfolio_id][previous_date[portfolio_id]])
            if previous_date[portfolio_id]!=key_date:
                if portfolios[portfolio_id][key_date].has_key('increase'):
                    del portfolios[portfolio_id][key_date]['increase']
                if portfolios[portfolio_id][key_date].has_key('decrease'):
                    del portfolios[portfolio_id][key_date]['decrease']
                if portfolios[portfolio_id][key_date].has_key('increase_fop'):
                    del portfolios[portfolio_id][key_date]['increase_fop']
                if portfolios[portfolio_id][key_date].has_key('decrease_fop'):
                    del portfolios[portfolio_id][key_date]['decrease_fop']
        if not portfolios[portfolio_id][key_date].has_key(security_id):
            portfolios[portfolio_id][key_date][security_id] = {'total': 0.0, 'name': operation.target.short_name, 'id': operation.target.id, 'type': operation.target.type.identifier, 'price': 0.0, 'price_pf': 0.0, 'price_date': None, 'buy_price': 0.0, 'buy_spot': 1.0}
        if not portfolios[portfolio_id][key_date].has_key('increase'):
            portfolios[portfolio_id][key_date]['increase'] = {'portfolio': 0.0}
        if not portfolios[portfolio_id][key_date].has_key('decrease'):
            portfolios[portfolio_id][key_date]['decrease'] = {'portfolio': 0.0}
        if not portfolios[portfolio_id][key_date].has_key('increase_fop'):
            portfolios[portfolio_id][key_date]['increase_fop'] = {'portfolio': 0.0}
        if not portfolios[portfolio_id][key_date].has_key('decrease_fop'):
            portfolios[portfolio_id][key_date]['decrease_fop'] = {'portfolio': 0.0}
        computed_amount = operation.quantity * (1.0 if operation.operation_type.identifier in ['OPE_TYPE_BUY', 'OPE_TYPE_BUY_FOP'] else -1.0)
        if (portfolios[portfolio_id][key_date][security_id]['total'] + computed_amount)!=0.0:
            portfolios[portfolio_id][key_date][security_id]['buy_price'] = computed_amount * (operation.price if operation.price!=None else 0.0) + portfolios[portfolio_id][key_date][security_id]['buy_price'] * portfolios[portfolio_id][key_date][security_id]['total']
            portfolios[portfolio_id][key_date][security_id]['buy_price'] = portfolios[portfolio_id][key_date][security_id]['buy_price']/(portfolios[portfolio_id][key_date][security_id]['total'] + computed_amount)
            portfolios[portfolio_id][key_date][security_id]['buy_spot'] = operation.spot if operation.spot!=None else 1.0
            
        portfolios[portfolio_id][key_date][security_id]['total'] += computed_amount
        
        portfolio_movement = portfolios[portfolio_id][key_date]['increase' if operation.operation_type.identifier in ['OPE_TYPE_BUY', 'OPE_TYPE_BUY_FOP'] else 'decrease']
        portfolio_movement_fop = portfolios[portfolio_id][key_date]['increase_fop' if operation.operation_type.identifier in ['OPE_TYPE_BUY', 'OPE_TYPE_BUY_FOP'] else 'decrease_fop']
        if not portfolio_movement.has_key(operation.target.currency.short_name):
            portfolio_movement[operation.target.currency.short_name] = 0.0
        if not portfolio_movement_fop.has_key(operation.target.currency.short_name):
            portfolio_movement_fop[operation.target.currency.short_name] = 0.0
        spot_pf = 1.0
        if operation.target.currency.short_name!=portfolio.currency.short_name and (not spots.has_key(operation.target.currency.short_name) or not spots[operation.target.currency.short_name].has_key(portfolio.currency.short_name)):
            spot_track = get_exchange_rate(operation.target.currency.short_name, portfolio.currency.short_name)
            if not spots.has_key(operation.target.currency.short_name):
                spots[operation.target.currency.short_name] = {}
            spots[operation.target.currency.short_name][portfolio.currency.short_name] = spot_track
        if operation.target.currency.short_name!=portfolio.currency.short_name and spots.has_key(operation.target.currency.short_name) and spots[operation.target.currency.short_name].has_key(portfolio.currency.short_name):
            value = get_closest_value(spots[operation.target.currency.short_name][portfolio.currency.short_name], operation.value_date)
            if value!=None:
                spot_pf = value['value']
        portfolios[portfolio_id][key_date][security_id]['buy_spot'] = spot_pf
        portfolio_movement[operation.target.currency.short_name] += operation.amount
        portfolio_movement['portfolio'] += operation.amount * spot_pf
        portfolio_movement_fop[operation.target.currency.short_name] += operation.amount if operation.operation_type.identifier in ['OPE_TYPE_SELL_FOP', 'OPE_TYPE_BUY_FOP'] else 0.0
        portfolio_movement_fop['portfolio'] += (operation.amount if operation.operation_type.identifier in ['OPE_TYPE_SELL_FOP', 'OPE_TYPE_BUY_FOP'] else 0.0) * spot_pf
        for inner_security in portfolios[portfolio_id][key_date].keys():
            if inner_security not in ['increase', 'decrease', 'increase_fop', 'decrease_fop']:
                compute_underlying_security(portfolios, tracks, spots, portfolio, operation, inner_security, key_date)
        previous_date[portfolio_id] = key_date
        previous_date[security_id] = key_date
    LOGGER.info("Completing securities positions")
    # Complete
    for portfolio_id in portfolios:
        movements = sorted(portfolios[portfolio_id].keys(), reverse=True)
        portfolio = PortfolioContainer.objects.get(id=portfolio_id)
        start_date = portfolio.inception_date
        if start_date==None:
            start_date = datetime.date(2014,1,1)
        today = datetime.date.today()
        while start_date<today:
            work_date = dt.combine(start_date, dt.min.time())
            key_date = str(epoch_time(work_date))
            for position_date in movements:
                if key_date>=position_date:
                    break
                position_date = None
            if position_date!=None and key_date!=position_date:
                portfolios[portfolio_id][key_date] = copy.deepcopy(portfolios[portfolio_id][position_date])
                if portfolios[portfolio_id][key_date].has_key('increase'):
                    del portfolios[portfolio_id][key_date]['increase']
                if portfolios[portfolio_id][key_date].has_key('decrease'):
                    del portfolios[portfolio_id][key_date]['decrease']
                if portfolios[portfolio_id][key_date].has_key('increase_fop'):
                    del portfolios[portfolio_id][key_date]['increase_fop']
                if portfolios[portfolio_id][key_date].has_key('decrease_fop'):
                    del portfolios[portfolio_id][key_date]['decrease_fop']
                for inner_security in portfolios[portfolio_id][key_date].keys():
                    if portfolios[portfolio_id][key_date][inner_security]['total']!=0.0:
                        compute_underlying_security(portfolios, tracks, spots, portfolio, operation, inner_security, key_date, work_date)
                    else:
                        del portfolios[portfolio_id][key_date][inner_security]
            start_date = dates.AddDay(start_date, 1)
            
    LOGGER.info("Securities positions computed")
    set_positions_portfolios(portfolios)
    LOGGER.info("Securities positions (portfolio view) stored")
    set_positions_securities(securities)
    LOGGER.info("Securities positions (securities view) stored")
Beispiel #12
0
def compute_accounts(container=None):
    LOGGER.info("Start computing accounts positions")
    spots = {}
    if container==None:
        all_operations = FinancialOperation.objects.filter(~Q(operation_type__identifier__in=['OPE_TYPE_BUY_FOP', 'OPE_TYPE_SELL_FOP']), Q(status__identifier__in=['OPE_STATUS_EXECUTED','OPE_STATUS_CONFIRMED'])).order_by('value_date')
    else:
        accounts = container.accounts.all()
        accounts_ids = [account.id for account in accounts]
        all_operations = FinancialOperation.objects.filter(~Q(operation_type__identifier__in=['OPE_TYPE_BUY_FOP', 'OPE_TYPE_SELL_FOP']), Q(repository__id__in=accounts_ids) | Q(source__id__in=accounts_ids) | Q(target__id__in=accounts_ids), Q(status__identifier__in=['OPE_STATUS_EXECUTED','OPE_STATUS_CONFIRMED'])).order_by('value_date')
    accounts_history = {}
    previous_date = {}
    for operation in all_operations:
        target_account_used = False
        source_account_used = True
        try:
            portfolio = PortfolioContainer.objects.filter(accounts__id=operation.source.id)
            source_key = str(operation.source.id)
        except:
            source_account_used = False
            portfolio = PortfolioContainer.objects.filter(accounts__id=operation.target.id)
        if portfolio.exists():
            portfolio = portfolio[0]
           
            key_date = str(epoch_time(operation.value_date))
            if operation.target!=None and operation.operation_type.identifier not in ['OPE_TYPE_BUY', 'OPE_TYPE_SELL', 'OPE_TYPE_BUY_FOP', 'OPE_TYPE_SELL_FOP', 'OPE_TYPE_DIVIDEND', 'OPE_TYPE_COUPON']:
                target_key = str(operation.target.id)
                if not accounts_history.has_key(target_key):
                    accounts_history[target_key] = {}
                target_account_used = True
                
            if source_account_used:
                compute_account_details(portfolio, operation, spots, accounts_history, source_key, previous_date, key_date, True, target_account_used)
                
            if target_account_used:
                compute_account_details(portfolio, operation, spots, accounts_history, target_key, previous_date, key_date, False, target_account_used)
        else:
            LOGGER.error("No portfolio associated to account")
            continue
    LOGGER.info("Completing accounts positions")
    # Complete
    for account_id in accounts_history.keys():
        portfolio = PortfolioContainer.objects.filter(accounts__id=account_id)
        account = AccountContainer.objects.get(id=account_id)
        portfolio = portfolio[0]
        
        start_date = portfolio.inception_date
        if start_date==None:
            start_date = datetime.date(2014,1,1)
        today = datetime.date.today()
        
        movements = sorted(accounts_history[account_id].keys(), reverse=True)
        while start_date<today:
            work_date = dt.combine(start_date, dt.min.time())
            key_date = str(epoch_time(work_date))
            for account_date in movements:
                if key_date>=account_date:
                    break
                account_date = None
            if account_date!=None and key_date!=account_date:
                spot_pf = 1.0
                wrk_currency = account.currency.short_name
                if wrk_currency!=portfolio.currency.short_name and not spots.has_key(wrk_currency):
                    spot_track = get_exchange_rate(wrk_currency, portfolio.currency.short_name)
                    if not spots.has_key(wrk_currency):
                        spots[wrk_currency] = {}
                    spots[wrk_currency][portfolio.currency.short_name] = spot_track
                if wrk_currency!=portfolio.currency.short_name and spots.has_key(wrk_currency) and spots[wrk_currency].has_key(portfolio.currency.short_name):
                    value = get_closest_value(spots[wrk_currency][portfolio.currency.short_name], work_date)
                    if value!=None:
                        spot_pf = value['value']
                accounts_history[account_id][key_date] = copy.deepcopy(accounts_history[account_id][account_date])
                accounts_history[account_id][key_date]['assets_pf'] = accounts_history[account_id][key_date]['assets'] * spot_pf
                accounts_history[account_id][key_date]['mvt_no_pnl_pf'] = 0.0
                accounts_history[account_id][key_date]['mvt_pnl_pf'] = 0.0
                accounts_history[account_id][key_date]['mvt_no_pnl'] = 0.0
                accounts_history[account_id][key_date]['mvt_pnl'] = 0.0
                accounts_history[account_id][key_date]['spot_pf'] = spot_pf
            start_date = dates.AddDay(start_date, 1)
    LOGGER.info("Accounts positions computed")
    set_accounts_history(accounts_history)
    LOGGER.info("Accounts positions stored")
Beispiel #13
0
def set_container_type_fields(values):
    values['_id'] = epoch_time(datetime.datetime.today())
    setup.container_type_fields.insert(values)
    
Beispiel #14
0
 def compute_valuation(self, container, frequency):
     #TODO Correct the bug with frequency different of daily
     LOGGER.info("Computing valuation of " + container.name + " with frequency " + frequency.name)
     start_date = datetime.date(2014,1,1) if container.inception_date==None else container.inception_date
     today = datetime.date.today()
     valuation = {}
     all_positions = get_positions_portfolio(container)
     all_positions = all_positions['data'] if all_positions!=None else {}
     previous_key = None
     previous_date = None
     while start_date<today:
         work_date = get_work_date(start_date, frequency)
         key_date = str(epoch_time(work_date))
         period_duration = float(get_current_period_duration(work_date, frequency))
         period_position = float(get_current_date_position(start_date, frequency))
         if not valuation.has_key(key_date):
             valuation[key_date] = {'total': {'portfolio': 0.0}, 'forward': {}, 'forward_pf': 0.0,'cash': {}, 'cash_pf': 0.0, 'spot_pf': {}, 'invested': {}, 'invested_fop':{}, 'pnl': {'portfolio': 0.0}, 'fx_pnl': {}, 'movement':{'portfolio': 0.0, 'portfolio_tw': 0.0}, 'performances': {'mdietz': {'day': 0.0, 'wtd': 0.0, 'mtd': 0.0, 'qtd': 0.0, 'std': 0.0, 'ytd': 0.0, 'si': 0.0}}}
         else:
             valuation[key_date]['cash'] = {}
             valuation[key_date]['forward'] = {}
             valuation[key_date]['cash_pf'] = 0.0
             valuation[key_date]['forward_pf'] = 0.0
             valuation[key_date]['spot_pf'] = {}
             valuation[key_date]['invested'] = {}
             valuation[key_date]['total'] = {}
         for account in container.accounts.filter(~Q(account_type__identifier='ACC_SECURITY')):
             history = get_account_history(account)
             if history!=None:
                 history = history['data']
                 if not valuation[key_date]['pnl'].has_key(account.currency.short_name):
                     valuation[key_date]['pnl'][account.currency.short_name] = 0.0
                 if not valuation[key_date]['movement'].has_key(account.currency.short_name):
                     valuation[key_date]['movement'][account.currency.short_name] = 0.0
                 if history!=None:
                     value = valuation_content.get_closest_value(history, dt.combine(start_date, dt.min.time()), True)
                     if account.account_type.identifier=='ACC_CURRENT':
                         account_key = 'cash'
                     elif account.account_type.identifier=='ACC_FORWARD':
                         account_key = 'forward'
                     if not valuation[key_date][account_key].has_key(account.currency.short_name):
                         valuation[key_date][account_key][account.currency.short_name] = {'portfolio': 0.0, 'account': 0.0}
                     if not valuation[key_date]['movement'].has_key(account.currency.short_name):
                         valuation[key_date]['movement'][account.currency.short_name] = 0.0
                     if value!=None:
                         valuation[key_date][account_key][account.currency.short_name]['account'] += value['assets']
                         if not valuation[key_date]['total'].has_key(account.currency.short_name):
                             valuation[key_date]['total'][account.currency.short_name] = 0.0
                         valuation[key_date]['total'][account.currency.short_name] += value['assets']
                         valuation[key_date][account_key][account.currency.short_name]['portfolio'] += value['assets_pf']
                         valuation[key_date][account_key + '_pf'] += value['assets_pf']
                         valuation[key_date]['spot_pf'][account.currency.short_name] = value['spot_pf']
                         valuation[key_date]['movement'][account.currency.short_name] += value['mvt_no_pnl']
                         valuation[key_date]['movement']['portfolio'] += value['mvt_no_pnl_pf']
                         valuation[key_date]['movement']['portfolio_tw'] += value['mvt_no_pnl_pf'] * ((period_duration - period_position + 1.0)/period_duration)
                         valuation[key_date]['pnl'][account.currency.short_name] += value['mvt_pnl']
                         valuation[key_date]['pnl']['portfolio'] += value['mvt_pnl_pf']
         if not valuation[key_date]['invested'].has_key('portfolio'):
             valuation[key_date]['invested']['portfolio'] = 0.0
         if not valuation[key_date]['invested_fop'].has_key('portfolio'):
             valuation[key_date]['invested_fop']['portfolio'] = 0.0
         current_positions = valuation_content.get_closest_value(all_positions, work_date, True)
         if current_positions!=None:
             for position in current_positions:
                 if position not in ['increase', 'decrease', 'increase_fop', 'decrease_fop']:
                     LOGGER.info("Operation on security with id [" + str(position) + "]")
                     security = SecurityContainer.objects.get(id=position)
                     divisor = get_price_divisor(security)
                     amount = current_positions[position]['total'] * current_positions[position]['price'] / divisor
                     amount_portfolio = current_positions[position]['total'] * current_positions[position]['price_pf'] / divisor
                     if not valuation[key_date]['invested'].has_key(security.currency.short_name):
                         valuation[key_date]['invested'][security.currency.short_name] = 0.0
                     valuation[key_date]['invested'][security.currency.short_name] += amount
                     valuation[key_date]['invested']['portfolio'] += amount_portfolio
                 elif position in ['increase_fop', 'decrease_fop']:
                     valuation[key_date]['invested_fop']['portfolio'] += (current_positions[position]['portfolio'] * (1.0 if position=='increase_fop' else -1.0))
         valuation[key_date]['total']['portfolio'] = valuation[key_date]['cash_pf'] + valuation[key_date]['invested']['portfolio']
         new_work_date = get_work_date(dates.AddDay(start_date, 1), frequency)
         new_key_date = str(epoch_time(new_work_date))
         # Modified dietz
         if key_date!=new_key_date or dates.AddDay(start_date, 1)>=today:
             if previous_key!=None:
                 movement_previous_spot = 0.0
                 for key_currency in valuation[key_date]['movement'].keys():
                     if key_currency!='portfolio' and key_currency!='portfolio_tw' and valuation[key_date]['movement'][key_currency]!=0.0:
                         movement_previous_spot += valuation[key_date]['movement'][key_currency] * (valuation[previous_key]['spot_pf'][key_currency] if valuation[previous_key]['spot_pf'].has_key(key_currency) else valuation[key_date]['spot_pf'][key_currency])
             else:
                 movement_previous_spot = valuation[key_date]['movement']['portfolio']
             mdietz_up = valuation[key_date]['total']['portfolio'] - valuation[key_date]['invested_fop']['portfolio'] - (valuation[previous_key]['total']['portfolio'] if previous_key!=None else 0.0) - movement_previous_spot
             mdietz_down = (valuation[previous_key]['total']['portfolio'] if previous_key!=None else 0.0) + valuation[key_date]['movement']['portfolio_tw']
             if mdietz_down!=0.0:
                 mdietz = mdietz_up / mdietz_down
             else:
                 mdietz = 0.0
             # First is used to determine if this is the reference frequency that is computed
             first = True
             for key_perf in PERF_MAPPING[frequency.identifier]:
                 if first:
                     valuation[key_date]['performances']['mdietz'][key_perf] = mdietz * 100.0
                 else:
                     if previous_date!=None:
                         if key_perf=='si' or (previous_key!=key_date and get_work_date(previous_date, DATE_MAPPING[key_perf])==get_work_date(start_date, DATE_MAPPING[key_perf])):
                             valuation[key_date]['performances']['mdietz'][key_perf] = (((valuation[previous_key]['performances']['mdietz'][key_perf]/100.0 + 1.0) * (mdietz + 1.0)) - 1.0) * 100.0
                         elif get_work_date(previous_date, DATE_MAPPING[key_perf])!=get_work_date(start_date, DATE_MAPPING[key_perf]):
                             valuation[key_date]['performances']['mdietz'][key_perf] = mdietz * 100.0
                     else:
                         valuation[key_date]['performances']['mdietz'][key_perf] = mdietz * 100.0
                 first = False
         if key_date!=new_key_date:
             previous_key = key_date
             previous_date = start_date
         start_date = dates.AddDay(start_date, 1)
     set_portfolios_valuation({str(container.id): valuation})
     for wrk_frequency in DATE_MAPPING.values():
         self.generate_tracks(container, wrk_frequency, valuation)