Skip to content

Commit

Permalink
Merge pull request #1244 from ranaroussi/fix/repair-100x
Browse files Browse the repository at this point in the history
Fix '100x price' repair
  • Loading branch information
ValueRaider committed Dec 16, 2022
2 parents e7bf360 + a13bf0c commit 88d21d7
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions yfinance/base.py
Expand Up @@ -559,12 +559,21 @@ def _reconstruct_intervals_batch(self, df, interval, tag=-1):
# Calibrate! Check whether 'df_fine' has different split-adjustment.
# If different, then adjust to match 'df'
df_block_calib = df_block[price_cols]
calib_filter = df_block_calib.to_numpy() != tag
calib_filter = (df_block_calib != tag).to_numpy()
if not calib_filter.any():
# Can't calibrate so don't attempt repair
continue
df_new_calib = df_new[df_new.index.isin(df_block_calib.index)][price_cols]
ratios = (df_block_calib[price_cols].to_numpy() / df_new_calib[price_cols].to_numpy())[calib_filter]
# Avoid divide-by-zero warnings printing:
df_new_calib = df_new_calib.to_numpy()
df_block_calib = df_block_calib.to_numpy()
for j in range(len(price_cols)):
c = price_cols[j]
f = ~calib_filter[:,j]
if f.any():
df_block_calib[f,j] = 1
df_new_calib[f,j] = 1
ratios = (df_block_calib / df_new_calib)[calib_filter]
ratio = _np.mean(ratios)
#
ratio_rcp = round(1.0 / ratio, 1)
Expand All @@ -591,7 +600,7 @@ def _reconstruct_intervals_batch(self, df, interval, tag=-1):
if not idx in df_new.index:
# Yahoo didn't return finer-grain data for this interval,
# so probably no trading happened.
print("no fine data")
# print("no fine data")
continue
df_new_row = df_new.loc[idx]

Expand Down Expand Up @@ -646,10 +655,15 @@ def _fix_unit_mixups(self, df, interval, tz_exchange):

data_cols = ["High", "Open", "Low", "Close"] # Order important, separate High from Low
data_cols = [c for c in data_cols if c in df2.columns]
f_zeroes = (df2[data_cols]==0).any(axis=1)
if f_zeroes.any():
df2_zeroes = df2[f_zeroes]
df2 = df2[~f_zeroes]
else:
df2_zeroes = None
if df2.shape[0] <= 1:
return df
median = _ndimage.median_filter(df2[data_cols].values, size=(3, 3), mode="wrap")

if (median == 0).any():
raise Exception("median contains zeroes, why?")
ratio = df2[data_cols].values / median
ratio_rounded = (ratio / 20).round() * 20 # round ratio to nearest 20
f = ratio_rounded == 100
Expand Down Expand Up @@ -715,6 +729,9 @@ def _fix_unit_mixups(self, df, interval, tz_exchange):
if fj.any():
c = data_cols[j]
df2.loc[fj, c] = df.loc[fj, c]
if df2_zeroes is not None:
df2 = _pd.concat([df2, df2_zeroes]).sort_index()
df2.index = _pd.to_datetime()

return df2

Expand Down Expand Up @@ -1167,4 +1184,4 @@ def get_history_metadata(self) -> dict:
if self._history_metadata is None:
raise RuntimeError("Metadata was never retrieved so far, "
"call history() to retrieve it")
return self._history_metadata
return self._history_metadata

0 comments on commit 88d21d7

Please sign in to comment.