Skip to content

Instantly share code, notes, and snippets.

@dyerrington
Created March 26, 2022 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyerrington/cc5c12f411421ebaad77c58f6a7672e5 to your computer and use it in GitHub Desktop.
Save dyerrington/cc5c12f411421ebaad77c58f6a7672e5 to your computer and use it in GitHub Desktop.
import finplot as fplt
import yfinance
df = yfinance.download('AAPL')
d = df[['Open', 'Close', 'High', 'Low']].reset_index(drop=True)
fplt.candlestick_ochl(d)
fplt.show()
@dyerrington
Copy link
Author

This default example creates the following result:

[*********************100%***********************]  1 of 1 completed
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [15], in <cell line: 6>()
      4 d = df[['Open', 'Close', 'High', 'Low']].reset_index(drop=True)
      5 fplt.candlestick_ochl(d)
----> 6 fplt.show()

File ~/anaconda3/envs/crypto/lib/python3.10/site-packages/finplot/__init__.py:1797, in show(qt_exec)
   1796 def show(qt_exec=True):
-> 1797     refresh()
   1798     for win in windows:
   1799         if isinstance(win, FinWindow) or qt_exec:

File ~/anaconda3/envs/crypto/lib/python3.10/site-packages/finplot/__init__.py:1790, in refresh()
   1788         datasrc = vb.datasrc_or_standalone
   1789         if datasrc and (vb.linkedView(0) is None or vb.linkedView(0).datasrc is None or vb.master_viewbox):
-> 1790             vb.update_y_zoom(datasrc.init_x0, datasrc.init_x1)
   1791 _repaint_candles()
   1792 for win in windows:

File ~/anaconda3/envs/crypto/lib/python3.10/site-packages/finplot/__init__.py:956, in FinViewBox.update_y_zoom(self, x0, x1)
    954 x1 = min(_round(x1-side_margin)+side_margin, datasrc.xlen+right_margin_candles-side_margin)
    955 # fetch hi-lo and set range
--> 956 _,_,hi,lo,cnt = datasrc.hilo(x0, x1)
    957 vr = self.viewRect()
    958 minlen = int((max_zoom_points-0.5) * self.max_zoom_points_f + 0.51)

File ~/anaconda3/envs/crypto/lib/python3.10/site-packages/finplot/__init__.py:395, in PandasDataSource.hilo(self, x0, x1)
    393 query = '%i,%i' % (x0,x1)
    394 if query not in self.cache_hilo:
--> 395     v = self.cache_hilo[query] = self._hilo(x0, x1)
    396 else:
    397     # re-insert to raise prio
    398     v = self.cache_hilo[query] = self.cache_hilo.pop(query)

File ~/anaconda3/envs/crypto/lib/python3.10/site-packages/finplot/__init__.py:410, in PandasDataSource._hilo(self, x0, x1)
    408 t0 = df[timecol].iloc[0]
    409 t1 = df[timecol].iloc[-1]
--> 410 valcols = df.columns[self.scale_cols]
    411 hi = df[valcols].max().max()
    412 lo = df[valcols].min().min()

File ~/anaconda3/envs/crypto/lib/python3.10/site-packages/pandas/core/indexes/base.py:5055, in Index.__getitem__(self, key)
   5048 if com.is_bool_indexer(key):
   5049     # if we have list[bools, length=1e5] then doing this check+convert
   5050     #  takes 166 µs + 2.1 ms and cuts the ndarray.__getitem__
   5051     #  time below from 3.8 ms to 496 µs
   5052     # if we already have ndarray[bool], the overhead is 1.4 µs or .25%
   5053     key = np.asarray(key, dtype=bool)
-> 5055 result = getitem(key)
   5056 # Because we ruled out integer above, we always get an arraylike here
   5057 if result.ndim > 1:

IndexError: index 4 is out of bounds for axis 0 with size 4

This is in a fresh Python environment.

@highfestiva
Copy link

Don't reset your index.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment