maread99 / market_prices

Get meaningful OHLCV datasets

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

404 on Non-US IPs

elementace opened this issue · comments

It looks like Yahoo Finance blocked all non-US IPs on certain endpoints as of a few days ago.

For example, attempting to hit
https://query2.finance.yahoo.com/v6/finance/quoteSummary/AMD?modules=summaryProfile
from a non-US IP will result in a 404 error.

Hitting it using a US VPN provides the expected result.

Follow along here:
ranaroussi/yfinance#1729

Thanks for raising this @elementace.

market_prices uses the Yahoo API (via the yahooquery package) primarily for price data. It doesn't appear that the availability of price data has been affected, however, market_prices does access some of the affected endpoints in order to map symbols to exchanges and price delays. Rather than rely on the Yahoo API, these mappings can be provided manually by passing the calendars and delays arguments to PricesYahoo. I've made some changes to raise errors providing advices to this effect if the calendars/delays aren't passed and the required Yahoo API endpoints are unavailable (changes included to release 0.11.1).

So, appears that the options are either:

  • Use a US server via a VPN (works for me, at least at the moment)
  • Pass the calendars and delays arguments to PricesYahoo to manually map symbols to exchanges and delays...

Example for a single symbol:

from market_prices import PricesYahoo
prices = PricesYahoo("MSFT", calendars="XNYS", delays=0)

# Or, if you are going to be requesting price data earlier than 20 years old (the calendar default):
import exchange_calendars as xcals
xnys = xcals.get_calendar("XNYS", start="1990-01-01")
prices = PricesYahoo("MSFT", calendars=xnys, delays=0)

Example for multiple symbols:

symbols = (
    "GOOG GEN.L 9988.HK QAN.AX CA.PA BAS.DE FER.MC IT4500.MI ^IBEX"
    " ^FTMC GBPEUR=X BTC-GBP GC=F CL=F ES=F ZB=F HG=F SPY QQQ ARKQ"
)

calendars = {
    "GOOG": "XNYS",
    "GEN.L": "XLON",
    "9988.HK": "XHKG",
    "QAN.AX": "XASX",
    "CA.PA": "XPAR",
    "BAS.DE": "XFRA",
    "FER.MC": "XMAD",
    "IT4500.MI": "XMIL",
    "^IBEX": "XMAD",
    "^FTMC": "XLON",
    "GBPEUR=X": "24/5",
    "BTC-GBP": "24/7",
    "GC=F": "CMES",
    "CL=F": "us_futures",
    "ES=F": "CMES",
    "ZB=F": "CMES",
    "HG=F": "CMES",
    "SPY": "XNYS",
    "QQQ": "XNYS",
    "ARKQ": "XNYS",
}

delays = {
    "GOOG": 0,
    "GEN.L": 15,
    "9988.HK": 15,
    "QAN.AX": 20,
    "CA.PA": 15,
    "BAS.DE": 15,
    "FER.MC": 15,
    "IT4500.MI": 15,
    "^IBEX": 15,
    "^FTMC": 15,
    "GBPEUR=X": 0,
    "BTC-GBP": 0,
    "GC=F": 10,
    "CL=F": 10,
    "ES=F": 10,
    "ZB=F": 10,
    "HG=F": 10,
    "SPY": 0,
    "QQQ": 0,
    "ARKQ": 0,
}

prices = PricesYahoo(symbols, calendars=calendars, delays=delays)

You're a lifesaver @maread99! This works perfectly!