[英]how to convert any forex pair quote to usd value?
Is it possible to convert any forex pair quote to usd value?是否可以将任何外汇对报价转换为美元价值?
I tried using following code but it gives incorrect results:我尝试使用以下代码,但它给出了不正确的结果:
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
That's basically a pathfinding problem: each currency is a node, each pair is a (directed) traversable link, and you're looking for the shortest path between each currency and your target.这基本上是一个寻路问题:每种货币都是一个节点,每一对都是一个(有向的)可遍历链接,您正在寻找每种货币和目标之间的最短路径。 Since your "edge cost" is a constant 1 you can just use Dijkstra's algorithm to resolve it.由于您的“边缘成本”是一个常数 1,您可以使用 Dijkstra 算法来解决它。 Or just delegate that to a standard package like networkx.或者只是将其委托给像 networkx 这样的标准包。
Once you've defined your pathfinding graph, can create a map giving the path to your common currency from each other currency.一旦你定义了你的寻路图,就可以创建一个地图,给出从其他货币到你的共同货币的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.