The personal finance app for everyone (by everyone)
Ruby
fix(yahoo): append the exchange suffix for European listings (#3769)
Daniel Garcia Sardina6e652b3ccfc4
diff --git a/app/models/provider/yahoo_finance.rb b/app/models/provider/yahoo_finance.rbindex 10ac95ac62..abf8cd9bf1 100644--- a/app/models/provider/yahoo_finance.rb+++ b/app/models/provider/yahoo_finance.rb@@ -573,11 +573,33 @@ class Provider::YahooFinance < Provider# MIC code to its Yahoo-specific symbol suffix, the default currency when# Yahoo omits one, and an optional dual-listing group with a preference# rank (lower = preferred). Adding a new market is a one-line hash entry.+ # A MIC missing here means the symbol is sent to Yahoo bare, which for any+ # non-US listing is a 404: the security then accumulates failed fetches and+ # never gets a price, with nothing telling the user why.EXCHANGE_CONFIG = {"XNSE" => { yahoo_suffix: ".NS", default_currency: "INR", dual_list_group: :india, preference_rank: 0 },"XBOM" => { yahoo_suffix: ".BO", default_currency: "INR", dual_list_group: :india, preference_rank: 1 },"XBOG" => { yahoo_suffix: ".CL", default_currency: "COP" },- "XIDX" => { yahoo_suffix: ".JK", default_currency: "IDR" }+ "XIDX" => { yahoo_suffix: ".JK", default_currency: "IDR" },+ # Europe. Yahoo quotes London in pence (GBp), which+ # normalize_currency_and_price already converts.+ "XETR" => { yahoo_suffix: ".DE", default_currency: "EUR" }, # Xetra+ "XFRA" => { yahoo_suffix: ".F", default_currency: "EUR" }, # Frankfurt floor+ "XMUN" => { yahoo_suffix: ".MU", default_currency: "EUR" }, # Munich+ "XSTU" => { yahoo_suffix: ".SG", default_currency: "EUR" }, # Stuttgart+ "XLON" => { yahoo_suffix: ".L", default_currency: "GBP" },+ "XPAR" => { yahoo_suffix: ".PA", default_currency: "EUR" },+ "XAMS" => { yahoo_suffix: ".AS", default_currency: "EUR" },+ "XBRU" => { yahoo_suffix: ".BR", default_currency: "EUR" },+ "XLIS" => { yahoo_suffix: ".LS", default_currency: "EUR" },+ "XMAD" => { yahoo_suffix: ".MC", default_currency: "EUR" },+ "XMIL" => { yahoo_suffix: ".MI", default_currency: "EUR" },+ "XSWX" => { yahoo_suffix: ".SW", default_currency: "CHF" },+ "XSTO" => { yahoo_suffix: ".ST", default_currency: "SEK" },+ "XCSE" => { yahoo_suffix: ".CO", default_currency: "DKK" },+ "XHEL" => { yahoo_suffix: ".HE", default_currency: "EUR" },+ "XOSL" => { yahoo_suffix: ".OL", default_currency: "NOK" },+ "XWBO" => { yahoo_suffix: ".VI", default_currency: "EUR" } # Vienna}.freeze# Yahoo Finance sometimes returns currencies in minor units (pence, cents)diff --git a/test/models/provider/yahoo_finance_test.rb b/test/models/provider/yahoo_finance_test.rbindex 107fa8bdb4..3693ce9e1a 100644--- a/test/models/provider/yahoo_finance_test.rb+++ b/test/models/provider/yahoo_finance_test.rb@@ -962,10 +962,35 @@ class Provider::YahooFinanceTest < ActiveSupport::TestCasetest "normalize_symbol leaves unconfigured MIC symbols unchanged" doassert_equal "AAPL", @provider.send(:normalize_symbol, "AAPL", "XNAS")- assert_equal "BARC", @provider.send(:normalize_symbol, "BARC", "XLON")+ assert_equal "SHOP", @provider.send(:normalize_symbol, "SHOP", "XTSE")assert_equal "AAPL", @provider.send(:normalize_symbol, "AAPL", nil)end+ # A European listing sent bare is a 404 at Yahoo; the security then fails+ # every fetch and silently never gets a price.+ test "normalize_symbol appends the Yahoo suffix for European exchanges" do+ assert_equal "IUSQ.DE", @provider.send(:normalize_symbol, "IUSQ", "XETR")+ assert_equal "SAP.F", @provider.send(:normalize_symbol, "SAP", "XFRA")+ assert_equal "BARC.L", @provider.send(:normalize_symbol, "BARC", "XLON")+ assert_equal "MC.PA", @provider.send(:normalize_symbol, "MC", "XPAR")+ assert_equal "ASML.AS", @provider.send(:normalize_symbol, "ASML", "XAMS")+ assert_equal "NESN.SW", @provider.send(:normalize_symbol, "NESN", "XSWX")+ assert_equal "VOLV-B.ST", @provider.send(:normalize_symbol, "VOLV-B", "XSTO")+ assert_equal "OMV.VI", @provider.send(:normalize_symbol, "OMV", "XWBO")+ end++ test "normalize_symbol does not double-suffix a European symbol stored with its suffix" do+ assert_equal "IUSQ.DE", @provider.send(:normalize_symbol, "IUSQ.DE", "XETR")+ assert_equal "BARC.L", @provider.send(:normalize_symbol, "BARC.L", "XLON")+ end++ test "default_currency_for_exchange knows the European venues Yahoo names" do+ assert_equal "GBP", @provider.send(:default_currency_for_exchange, "LSE")+ assert_equal "EUR", @provider.send(:default_currency_for_exchange, "FRA")+ assert_equal "EUR", @provider.send(:default_currency_for_exchange, "PAR")+ assert_equal "CHF", @provider.send(:default_currency_for_exchange, "SWX")+ end+test "normalize_symbol appends suffix to dotted symbols that do not already end with the configured suffix" doassert_equal "BRK.A.NS", @provider.send(:normalize_symbol, "BRK.A", "XNSE")assert_equal "BRK.B.BO", @provider.send(:normalize_symbol, "BRK.B", "XBOM")