Displaying candlestick charts in Python (Plotly edition)

Display candlestick chart in Python (matplotlib edition) It is a continuation of.

Plotly is not a Python-only package, but also supports R, Matlab, JavaScript, etc. In online mode, you need to create an account, but if you want to use it offline, you don't need to create an account. The following is an offline explanation.

Preparation

Installation of Plotly's Python package can be found on this page (https://plot.ly/python/getting-started/)

$ pip install plotly

Is OK.

Then, like previous article, create fictitious 4-value data. However, this time, the data is for one year.

import numpy as np
import pandas as pd

idx = pd.date_range('2015/01/01', '2015/12/31 23:59', freq='T')
dn = np.random.randint(2, size=len(idx))*2-1
rnd_walk = np.cumprod(np.exp(dn*0.0002))*100
df = pd.Series(rnd_walk, index=idx).resample('B').ohlc()

Candlestick chart using Plotly

The reference page is here. Candlestick Charts in Python

First, display the candlestick chart with the standard settings.

from plotly.offline import init_notebook_mode, iplot
from plotly.tools import FigureFactory as FF

init_notebook_mode(connected=True) #Settings for Jupyter notebook

fig = FF.create_candlestick(df.open, df.high, df.low, df.close, dates=df.index)

iplot(fig)

Just pass Open data, High data, Low data, Close data, date and time data to the argument of create_candlestick.

The chart looks like this.

newplot.png

Actually, you can zoom and pan the chart freely, so you can enlarge it as shown below.

newplot.png

Candlestick chart for business days only

Plotly charts are very useful as interactive charts, but after all, there is space on Saturdays and Sundays, and they are not continuous on business days. Therefore, we will work on the x-axis data so that only business days are continuous. The procedure is the same as for matplotlib. If you omit the dates argument of create_candlestick, the x-axis data will be an integer index. Therefore, the date and time data corresponding to the index is displayed as the x-axis scale.

fig = FF.create_candlestick(df.open, df.high, df.low, df.close)

xtick0 = (5-df.index[0].weekday())%5 #First monday index
fig['layout'].update({
    'xaxis':{
        'showgrid': True,
        'ticktext': [x.strftime('%Y-%m-%d') for x in df.index][xtick0::5],
        'tickvals': np.arange(xtick0,len(df),5)
    }
})

iplot(fig)

The displayed chart is as follows.

newplot.png

The x-axis scale is displayed at weekly intervals, so it's a little fine, but it's easier to see if you zoom in.

newplot.png

You can see that this is a candlestick chart for business days only.

Recommended Posts

Displaying candlestick charts in Python (Plotly edition)
Displaying candlestick charts in Python (matplotlib edition)
Drawing candle charts in python
Draw knots interactively in Plotly (Python)
CGI server (1) python edition in one line
Get Started with TopCoder in Python (2020 Edition)
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Detect golden crosses on stock charts in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Plot Bitcoin candle charts and technical indicators in Python
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Implement Enigma in python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Candlestick with plotly + Jupyter
Edit fonts in Python
Singleton pattern in Python
File operations in Python
Read DXF in python
Daily AtCoder # 53 in Python
Key input in Python