как конвертировать любую пару форекс котировки в значение usd? - PullRequest
0 голосов
/ 21 февраля 2020

Возможно ли преобразовать любую пару валютных пар в значение usd?

Я пытался использовать следующий код, но он дает неверные результаты:

def calculate_instrument_value_in_account_currency(currency, current_prices, instruments):

    instrument_values = []

    #dictionary to keep prices for each currency, assuming that current_prices has prices in the same order as instruments list has instument names
    prices_for_currency = {}

    instrument_index = 0
    for instrument in instruments:
        prices_for_currency[instrument] = current_prices[instrument_index]
        instrument_index += 1

    #account currencu is USD
    if currency == 'USD':
        m = 0            
        for instrument in instruments:                                               
            first_currency = instrument[0:3]
            second_currency = instrument[4:7]

            #counter currency same as account currency
            if second_currency == 'USD':
                instrument_value = current_prices[m]
            #base currency same as account currency    
            elif first_currency == 'USD':
                instrument_value = 1 / current_prices[m]
            #none of the currency pair is the same as account currency
            #is needed the currency rate for the base currency/account currency
            else:
                if second_currency == 'JPY':
                    JPY_to_USD = prices_for_currency[currency+"/"+second_currency]
                    USD_to_JPY = 1 / JPY_to_USD
                    instrument_value = current_prices[m] * USD_to_JPY

                else: 
                    USD_to_GBP = prices_for_currency[second_currency+"/"+currency]
                    instrument_value =  current_prices[m] * USD_to_GBP

            instrument_values.append(instrument_value)
            m += 1    

    return instrument_values 

1 Ответ

0 голосов
/ 21 февраля 2020

Это в основном проблема поиска пути: каждая валюта - это узел, каждая пара - (направленная) пересекаемая ссылка, и вы ищете кратчайший путь между каждой валютой и вашей целью. Поскольку ваша «крайняя стоимость» является постоянной величиной 1, вы можете просто использовать алгоритм Дейкстры для ее решения. Или просто делегируйте это стандартному пакету, например networkx.

После того, как вы определили свой график поиска пути, можете создать карту, указывающую путь к вашей общей валюте из каждой другой валюты.

...