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

Fix prices 30m resampling #2027

Merged
merged 1 commit into from
Aug 16, 2024

Conversation

aleksfasting
Copy link
Contributor

Solves #2026 to fix interval offset of the indian stock exchange

Solution that checks the offset of the opening time of the stock exchange (mod 30 minutes) and adds it to the quotes

That issue addresses #1436 and #1447.


Solution

The only change lies in yfinance/scrapers/history.py.

If ìnterval='30m', we find the time that the stock exchange opens. If it opens at a time t.minutes % 30 != 0` we add that offset to the quote.

Solution that checks the offset of the opening time of the stock exchange (mod 30 minutes) and adds it to the quotes
@ValueRaider
Copy link
Collaborator

Hypothetical - can self._history_metadata["tradingPeriods"][0][0]["start"] throw a KeyError or IndexError?

@aleksfasting
Copy link
Contributor Author

The 'tradingPeriods' key

The data from Yahoo Finance will always come with metadata, and it will include the 'tradingPeriods' key as long as there was a ticker to be found. If the ticker wasn't found, that would have been handled before the call to this line.

The first 0 index

The entries of self._histoy_metadata['tradingPeriods'] will be a list of all the opening-times, end-times, etc. of each day succeding the start date.

If the start date is after the current date there will not have been generated any price data, and the code has handled this before this line of code. The same goes for any scenario where no price data has been generated, like weekends, holidays or such.

The second 0 index

I have no idea why Yahoo Finance supply their metadata with this wrapper list. The list always contains a single dict. Artistic choice by Yahoo finance. I could not find a counter example.

The 'start' index

This is a key in the standard dict in self._history_metadata["tradingPeriods][0][0]. I have not found a dict that does not contain this key.


So i think the keys and indexes will always exist if this line is being executed. No exceptions will be thrown

@ValueRaider ValueRaider merged commit 67d6859 into ranaroussi:dev Aug 16, 2024
1 check passed
@ValueRaider ValueRaider changed the title Solves #2026 Fix prices 30m resampling Aug 16, 2024
@aleksfasting aleksfasting deleted the fix/NSEI-quarter-offset branch August 17, 2024 14:50
@ValueRaider
Copy link
Collaborator

ValueRaider commented Aug 19, 2024

KeyError was possible, I encountered it:

    exchangeStartTime = pd.Timestamp(self._history_metadata["tradingPeriods"][0][0]["start"], unit='s')
                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^

KeyError: 0

I'll try to provide arguments to reproduce, but this is rare so could take a long time to repeat.

@ValueRaider
Copy link
Collaborator

Reproduce:

dat = yf.Ticker('0001.HK')
df = dat.history(start=1719763200, end=1720540800, interval='30m', prepost=True)

ValueRaider added a commit that referenced this pull request Aug 21, 2024
ValueRaider added a commit that referenced this pull request Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants