【问题标题】:how to convert any forex pair quote to usd value?如何将任何外汇对报价转换为美元价值?
【发布时间】:2020-02-21 12:40:40
【问题描述】:

是否可以将任何外汇对报价转换为美元价值?

我尝试使用以下代码,但结果不正确:

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 

【问题讨论】:

  • 什么是外汇对报价?请提供示例输入。
  • 首先显示你在变量中拥有什么——你使用什么数据并不明显。您可以使用print() 查看不同时刻变量中的内容,并将其与纸上的计算进行比较。也许您应该将数据保存在一个列表或字典中,而不是使用分隔的 currency, current_prices, instruments 可能具有不同顺序的元素。

标签: python forex


【解决方案1】:

这基本上是一个寻路问题:每种货币都是一个节点,每一对都是(有向的)可遍历链接,您正在寻找每种货币与目标之间的最短路径。由于您的“边缘成本”是常数 1,您可以使用 Dijkstra 的算法来解决它。或者只是将它委托给一个标准的包,比如 networkx。

一旦你定义了你的寻路图,就可以创建一个地图,给出从其他货币到你的共同货币的路径。

【讨论】:

  • 我正在训练一个用于投资组合优化的机器学习模型。我想将货币对的价值转换为一种通用货币。
  • 运行上述函数:
  • instruments = ['EUR/USD', 'USD/JPY', 'AUD/USD', 'USD/CAD', 'GBP/USD', 'NZD/USD', 'GBP/ JPY', 'EUR/JPY', 'AUD/JPY', 'EUR/GBP', 'USD/CHF'] current_prices = [1.10270,109.27500,0.68263,1.31496,1.30712,0.66076,142.82700,120.46800,74.58800,0.84334 0.97120]
  • instrument_values_calculated = calculate_instrument_value_in_account_currency('USD', current_prices, instruments) print(instruments) print(current_prices) print(instrument_values_calculated)
  • ['EUR/USD', 'USD/JPY', 'AUD/USD', 'USD/CAD', 'GBP/USD', 'NZD/USD', 'GBP/JPY' , 'EUR/JPY', 'AUD/JPY', 'EUR/GBP', 'USD/CHF'] [1.1027, 109.275, 0.68263, 1.31496, 1.30712, 0.66076, 142.827, 120.468, 74.588, 0.84343, 27.911. ,0.009151223976206817,0.68263,0.7604794062176797,1.30712,0.66076,1.307041866849691,1.102429649965683,0.682571493937314,1.1024642216,1.0296540362438222] 跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-29
  • 2019-08-30
  • 2021-11-08
  • 1970-01-01
  • 1970-01-01
  • 2021-09-23
相关资源
最近更新 更多