Skip to content
Jonte edited this page May 22, 2021 · 2 revisions

This is a beginner tutorial, focusing on getting the data downloaded, loading the cache and getting it into a window.

Install first

$ pip install -U finplot
Collecting finplot
  Using cached finplot-1.7-py3-none-any.whl (33 kB)
Requirement already satisfied: pandas in d:\prog\python39\lib\site-packages (from finplot) (1.2.3)
Requirement already satisfied: pyqtgraph>=0.11.1 in d:\prog\python39\lib\site-packages (from finplot) (0.12.0)
Requirement already satisfied: PyQt5 in d:\prog\python39\lib\site-packages (from finplot) (5.15.2)
Requirement already satisfied: numpy>=1.17.0 in d:\prog\python39\lib\site-packages (from pyqtgraph>=0.11.1->finplot) (1.20.2)
Requirement already satisfied: python-dateutil>=2.7.3 in d:\prog\python39\lib\site-packages (from pandas->finplot) (2.8.1)
Requirement already satisfied: pytz>=2017.3 in d:\prog\python39\lib\site-packages (from pandas->finplot) (2020.4)
Requirement already satisfied: six>=1.5 in d:\prog\python39\lib\site-packages (from python-dateutil>=2.7.3->pandas->finplot) (1.15.0)
Requirement already satisfied: PyQt5-sip<13,>=12.8 in d:\prog\python39\lib\site-packages (from PyQt5->finplot) (12.8.1)
Installing collected packages: finplot
Successfully installed finplot-1.7

Cache some data

Ok, so we start by caching some data on your local computer. This has nothing to do with finplot per se, but many seem to struggle with this. Create a script called download.py or something like that.

import yfinance as yf

df = yf.download('VWAGY', '2014-05-01') # Wolksvagen
df.to_csv('cache.csv')

Save and let's see what we've got:

$ python download.py
[*********************100%***********************]  1 of 1 completed
$ head cache.csv
Date,Open,High,Low,Close,Adj Close,Volume
2014-04-30,26.635000228881836,26.729999542236328,26.489999771118164,26.729999542236328,25.87717056274414,24800
2014-05-01,26.683500289916992,26.683500289916992,26.59000015258789,26.594999313354492,25.746477127075195,13800
2014-05-02,26.125,26.229999542236328,26.059999465942383,26.1200008392334,25.28663444519043,38600
2014-05-05,25.78499984741211,26.29400062561035,25.56999969482422,26.264999389648438,25.4270076751709,429000
2014-05-06,25.8799991607666,26.03499984741211,25.760000228881836,25.975000381469727,25.14626121520996,100200
2014-05-07,25.915000915527344,26.065000534057617,25.75749969482422,25.822500228881836,24.998626708984375,75800
2014-05-08,26.104999542236328,26.2450008392334,25.969999313354492,26.0,25.170461654663086,93000
2014-05-09,25.940000534057617,25.94499969482422,25.264999389648438,25.450000762939453,24.638011932373047,101200
2014-05-12,25.94499969482422,25.9950008392334,25.399999618530273,25.454999923706055,24.642850875854492,42800

So far so good. Note that the date format is ISO-8601.

Load data

Create a file called plot-downloaded.py:

import pandas as pd
import finplot as fplt

df = pd.read_csv('cache.csv')
print(df)

This gets us the flat, raw data. The print at the end looks like this:

$ python plot-downloaded.py
            Date       Open       High        Low      Close  Adj Close  Volume
0     2014-04-30  26.635000  26.730000  26.490000  26.730000  25.877171   24800
1     2014-05-01  26.683500  26.683500  26.590000  26.594999  25.746477   13800
2     2014-05-02  26.125000  26.230000  26.059999  26.120001  25.286634   38600
3     2014-05-05  25.785000  26.294001  25.570000  26.264999  25.427008  429000
4     2014-05-06  25.879999  26.035000  25.760000  25.975000  25.146261  100200
...          ...        ...        ...        ...        ...        ...     ...
1774  2021-05-17  32.290001  32.738998  32.290001  32.694000  32.694000  318100
1775  2021-05-18  32.599998  32.750000  32.360001  32.450001  32.450001  301800
1776  2021-05-19  32.220001  32.664001  31.879999  32.650002  32.650002  282700
1777  2021-05-20  32.993000  33.130001  32.840000  33.055000  33.055000  347200
1778  2021-05-21  33.150002  33.599998  32.950001  33.549999  33.549999  541800

[1779 rows x 7 columns]

Here, pandas has lost a piece of information. The Date column used to be the index, but now we've instead gotten a zero-based index, and Date has turned into a column. This is how Pandas works by default.

So for the moment we are particularly interested in the Date column, and how to turn it into a date index once again. Add another print statement at the end to show some more info on the Date column:

print(df['Date'])

Run again:

$ python plot-downloaded.py
...
0       2014-04-30
1       2014-05-01
2       2014-05-02
3       2014-05-05
4       2014-05-06
           ...
1774    2021-05-17
1775    2021-05-18
1776    2021-05-19
1777    2021-05-20
1778    2021-05-21
Name: Date, Length: 1779, dtype: object

Notice two things. The type of the column is object (in this case strings). And also, we would like to use date and time as index. To convert strings to dates and use as index:

df['Date'] = pd.to_datetime(df['Date'])
df = df.set_index('Date')
print(df)

The result:

$ python plot-downloaded.py
...
                 Open       High        Low      Close  Adj Close  Volume
Date
2014-04-30  26.635000  26.730000  26.490000  26.730000  25.877171   24800
2014-05-01  26.683500  26.683500  26.590000  26.594999  25.746477   13800
2014-05-02  26.125000  26.230000  26.059999  26.120001  25.286634   38600
2014-05-05  25.785000  26.294001  25.570000  26.264999  25.427008  429000
2014-05-06  25.879999  26.035000  25.760000  25.975000  25.146261  100200
...               ...        ...        ...        ...        ...     ...
2021-05-17  32.290001  32.738998  32.290001  32.694000  32.694000  318100
2021-05-18  32.599998  32.750000  32.360001  32.450001  32.450001  301800
2021-05-19  32.220001  32.664001  31.879999  32.650002  32.650002  282700
2021-05-20  32.993000  33.130001  32.840000  33.055000  33.055000  347200
2021-05-21  33.150002  33.599998  32.950001  33.549999  33.549999  541800

[1779 rows x 6 columns]

Notice that the Date label belongs to the index now. This is good news.

Plot

All we need to do now is create the plot and show the window:

fplt.candlestick_ochl(df[['Open', 'Close', 'High', 'Low']])
fplt.show()

The _ochl part is short for open, close, high, low — i.e. the order of columns to the candlestick plot function.