【发布时间】: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可能具有不同顺序的元素。