Ventures of an ex indie game developer

finplot v1.0 released!

Couldn't avoid feature creep for the release, but of the 32 public functions in the API, there's only 4-6 functions I never or rarely use myself. (Such as screenshot().)

Anyway, I've included some minor features, mostly fixing internal X-axis/time handling, which allow for using more of pyqtgraph's functionality. So now showing correlation between two stocks is easy as cake. First we download data:

import finplot as fplt
import scipy.optimize
import yfinance as yf

dfgo = yf.download('GOOG', '2020-01-01')
dfms = yf.download('MSFT', '2020-01-01')

Then we create our plot. Correlation charts are not related to time, and that we have to tell finplot explicitly as time normally links several stacked axes.

ax = fplt.create_plot('Microsoft over Alphabet price correlation 2020 to date')
ax.disable_x_index() # plot is not timebased

Ok, we have our window. Now we calculate daily returns for Alphabet Inc. and Microsoft Corp.

dfgo['ret_alphabet']  = dfgo.Close.pct_change() * 100
dfgo['ret_microsoft'] = dfms.Close.pct_change() * 100
# drop the time-based index, as we want to plot one asset against the other
dfcorr = dfgo[['ret_alphabet', 'ret_microsoft']].reset_index(drop=True)

Nice, let's plot.

fplt.plot(dfcorr, style='o', color='#a32', ax=ax)
fplt.show()

If you want to throw some linear regression on that, it goes something like this:

# 1. drop first row (% change on first day since previous day is not defined)
dfcorr = dfcorr.dropna()
# 2. error function is actual y (Microsoft daily return) minus k*x+m (x=Alphabet)
errfun = lambda arr: [y-arr[0]*x+arr[1] for x,y in dfcorr.values]
# 3. let scipy optimize it
line = scipy.optimize.least_squares(errfun, [0.01, 0.01]).x
# 4. line starting and ending X coordinates
linex = [dfcorr.ret_alphabet.min(), dfcorr.ret_alphabet.max()]
# 5. use those to calculate corresponding Y coordinates
liney = [linex[0]*line[0]+line[1], linex[1]*line[0]+line[1]]
# 6. plot (min_x, y0) -> (max_x, y2)
fplt.add_line((linex[0],liney[0]), (linex[1],liney[1]), color='#993')
# 7. show correlation coefficient
fplt.add_text((linex[1],liney[1]), 'k=%.2f'%line[0], color='#993', anchor=(1,-1.1))

There are some other new features, Renko plots for instance:

import finplot as fplt
import yfinance as yf
df = yf.download('BNO', '2014-01-01')
fplt.renko(df.Close)
fplt.show()

Given five lines of coloring and two lines of volume plotting you get something like this:

Anyway, finplot is great, great, great; and you should pip install -U finplot right now. For me at least, it's doing a great job of simplifying everyday cryptocurrency plotting and reducing the inception time from idea to visual by a magnitude or so.

About the author

Mitt foto
Gothenburg, Sweden