Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Currency conversion rates #228

Open
georgeef opened this issue Nov 10, 2024 · 1 comment
Open

Currency conversion rates #228

georgeef opened this issue Nov 10, 2024 · 1 comment
Labels
r: feature Request: New feature

Comments

@georgeef
Copy link
Contributor

georgeef commented Nov 10, 2024

Currency conversion rates are stored in:

  • CURRENCYFORMATS_V1.BASECONVRATE
  • CURRENCYHISTORY_V1.CURRVALUE

MMEX Desktop assumes that a base currency is always defined and it interprets the conversion rate of a currency C as the rate with respect to the base currency B, i.e.,

  • 1 C = (C.rate) B

This formula assumes that B.rate is always equal to 1.

Problems

If the base currency is changed from B1 to B2, all conversion rates must be scaled with 1/B2.rate, in order to maintain the invariant that the rate of the base currency is always equal to 1.

Depending on the source, conversion rates may not be available for all currency pairs. Typically they are readily available with respect to USD. If conversion rates with respect to the base currency B are not available, the rates of all wanted currencies can be downloaded with respect to USD, and then divided with the rate of B with respect to USD.

Proposed behavior of conversion rates

A base currency may or may not be defined. The conversion rates are interpreted as the rates with respect to an arbitrary (undefined) unit and only the ratio of two conversion rates is important. In practice the unit is the most convenient reference currency for the source of conversion rates, but this choice is not important.

Scaling all conversion rates with the same scale factor has no impact to balance and report calculations.

The exchange formula between two currencies C and B becomes:

  • (1/C.rate) C = (1/B.rate) B, or equivalently, 1 C = (C.rate/B.rate) B

The flow of a set of transactions, expressed in currency B, is defined as

  • flow = sum_t ( ± t.amount * t.a.c.rate / B.rate )

where t is one side of a transaction, ±t.amount is the signed amount (positive for deposit and for the "to" side of a transfer, negative for withdrawal and for the "from" side of a transfer), t.a is the account of t, and t.a.c is the currency of t.a.

The flow calculation may be optimized by factoring out common terms, e.g.,

  • flow = ( sum_C flow_C * C.rate ) / B.rate

where

  • flow_C = sum_{t | t.a.c = C} ( ±t.amount )

As seen in the above formulas, only the ratio C.rate/B.rate is significant.

@georgeef georgeef added the r: feature Request: New feature label Nov 10, 2024
@georgeef
Copy link
Contributor Author

@guanlisheng as FYI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
r: feature Request: New feature
Projects
None yet
Development

No branches or pull requests

1 participant