achannarasappa / ticker

Terminal stock ticker with live updates and position tracking
GNU General Public License v3.0
4.94k stars 264 forks source link

Fix issue #223 #231

Closed deroshkin closed 2 years ago

deroshkin commented 2 years ago

Force currency code to be upper case to compensate for Yahoo improperly formatting some foreign currencies (at least GBP), which confused currency conversion.

Not sure whether the place I chose to force the upper case conversion is the best location, but it works.

achannarasappa commented 2 years ago

Great find! Thank you for contributing this fix

I was able to replicate the issue and the root cause seems be as you've said that Yahoo returns non-uppercase characters for some currency quotes but the currency conversion pair is always uppercase.

❯  http "https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US&region=EU&corsDomain=finance.yahoo.com&symbols=GBPUSD=X" | jq '.quoteResponse.result[0].symbol'
"GBPUSD=X"

❯  http "https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US&region=EU&corsDomain=finance.yahoo.com&symbols=BATS.L" | jq '.quoteResponse.result[0].currency'
"GBp"
RichTeaMan commented 1 year ago

Sorry to comment on a merged issue, but I don't think is correct.

For instance, GBP is not the same as GBp. GBP is British Pounds while GBp is British pennies (see https://en.wiktionary.org/wiki/GBP, https://en.wiktionary.org/wiki/GBp & this for a real example: https://finance.yahoo.com/quote/0P00013P6I.L). Some of my holdings are showing me being £10000s up but unfortunately this is not actually the case.

deroshkin commented 1 year ago

@RichTeaMan is right, I just checked yahoo finance site listings for some London stocks, and they are in fact in terms of pennies. Unfortunately, the yahoo exchange rate API if queried with GBp will return the exchange rate for GBP. IMO there are 2 ways of addressing this without changing the API used, and both are far from ideal:

  1. Convert all stock quotes from GBp to GBP when creating assetQuote in transformResponseQuote (quote.go)
  2. Hard code a 2-step conversion from GBp to any target currency in 2 steps (GBp -> GBP -> USD/EUR/...) somewhere in the currency conversion code.

If you are willing to switch APIs, there are some currency converters that do allow British pennies (some use GBX instead of GBp)