确定汇率的算法

发布于 2024-09-12 00:28:21 字数 1150 浏览 6 评论 0原文

给定各种货币对的数据集,如何有效计算数据集中未提供的货币对的隐含外汇汇率?

例如,假设我的数据库/表如下所示(此数据是捏造的):

GBP x USD = 1.5
USD x GBP = 0.64
GBP x EUR = 1.19
AUD x USD = 1.1  

请注意 (GBP,USD) != 1/(USD,GBP)。

我期望得到以下结果:

print rate('GBP','USD')
> 1.5

print rate('USD','GBP')
> 0.64

print rate('GBP','EUR')
> 1.19

#now in the absence of an explicit pair, we imply one using the inverse
print rate('EUR','GBP')
> 0.84

这些是简单的情况,它变得更有趣:

#this is the implied rate from (GBP,EUR) and (GBP,USD)
print rate('EUR','USD')
> 1.26

或者一个更复杂的例子是使用 3 个或更多对找到最有效的翻译:

print rate('EUR','AUD')
> 1.38

我认为详细介绍了这个问题的编程相关方面。我想这里可以完成一个高效或聪明的递归。唯一的要求是使用最少数量的对来达到所要求的对(这是为了减少错误)。如果没有给出显式的逆,那么反转一对就不需要任何成本。

动机
在理想的金融世界中,货币市场是高效的。事实上,这 99% 是正确的。很多时候,奇怪的货币对不会被报价或者很少被报价。如果存在明确的引用,我们必须在任意计算中使用它。如果不是,我们必须暗示最准确的对,尽可能保留小数位。此外,它们并不总是乘以 1(实际上,它们永远不会乘以 1);这反映了市场的买入/卖出价差。因此,我们在两个方向上保留尽可能多的货币对,但希望能够对所有货币进行通用编码。

我认为我已经实施了一个体面的、强力的解决方案。它有效,但我认为这个问题很有趣,并且想知道是否其他人认为它很有趣/具有挑战性。我个人使用Python工作,但它更多的是一种练习而不是实现,所以伪代码“足够好”。

Given a data set of various currency pairs, how do I efficiently compute the implied fx rate for a pair not supplied in the data set?

For example, say my database/table looks like this (this data is fudged):

GBP x USD = 1.5
USD x GBP = 0.64
GBP x EUR = 1.19
AUD x USD = 1.1  

Notice that (GBP,USD) != 1/(USD,GBP).

I would expect the following results:

print rate('GBP','USD')
> 1.5

print rate('USD','GBP')
> 0.64

print rate('GBP','EUR')
> 1.19

#now in the absence of an explicit pair, we imply one using the inverse
print rate('EUR','GBP')
> 0.84

These are the simple cases, it gets more interesting:

#this is the implied rate from (GBP,EUR) and (GBP,USD)
print rate('EUR','USD')
> 1.26

Or an even more complicated example is finding the most efficient translation using 3 or more pairs:

print rate('EUR','AUD')
> 1.38

I think that details the programming related aspects of this problem. I'd imagine there's an efficient or clever recursion that can be done here. The only requirement is that the least number of pairs are used to arrive at the asked for pair (this is to reduce error). If no explicit inverse is given, then inverting a pair costs you nothing.

Motivation
In the ideal financial world, currency markets are efficient. In reality, that's 99% true. Often times, odd currency pairs aren't quoted or they're quoted infrequently. If an explicit quote exists, we must use it in our arbitrary calculations. If not, we must imply the most accurate pair, out to as many decimal places as we can. Furthermore, they don't always multiply to 1 (actually, they never multiply to 1); this reflects the bid/ask spread in the market. So we keep as many pairs as we can in both directions, but would like to be able to code in general for all currencies.

I think I have a decent, brute force solution implemented. It works, but I thought the problem was interesting and was wondering if anyone else thought it was interesting/challenging. I'm personally working in Python but it's more an exercise than an implementation, so psuedo code is "good enough".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葮薆情 2024-09-19 00:28:22

您正在寻找有向图中的最短路径,其中货币是顶点,给定的汇率是边。
如果仅给出一个方向的汇率,您可以添加一个相反方向的汇率,但成本较高。

You're looking for the shortest path in a directed graph, where the currencies are the vertices and the given exchange rates are the edges.
If an exchange rate is given only for one direction, you can add one for the opposite direction with a higher cost.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文