(01.18) 수정됨.
Donation
Bitcoin: 1MKCHW8smDZv5DFMiVkA5G3DeXcMn871ZX
Ethereum: 0x1c5fb8a5e0b1153cd4116c91736bd16fabf83520
한국어 설명서
https://white.seolpyo.com/entry/147/
How to install
> pip install seolpyo-mplchart
※ I personally confirmed that it works with 32-bit Python 3.11, pandas 2.0.0, and matplotlib 3.7.0.
introduction
Introduction Click to expand
seolpyo-mplchart is a Python package (module) that utilizes matplotlib.
In fact, representative Python packages for creating candlestick charts include finplot and plotly.
Nevertheless, the reason why we created a new stock chart package with matplotlib is because finplot is mandatory to use pyqt, and plotly can only be used through a web browser.
Key Features
- A slider is provided to display the viewing area.
- You can change or move the viewing area using the slider.
- If the search area changes, ytick and ytick label change in real time accordingly.
- If the mouse cursor is over a candle or volume bar, the relevant information is displayed as text.
- Connectable gui is not forced into one thing. You can connect to any GUI supported by matplotlib (tkinter, wxpython, pyqt, etc.).
Below is a sample image that connects to tkinter to draw a candlestick chart.
Quick Start
Quick Start Click to expand
You can create a chart object through seolpyo_mplchart.SliderChart(), and receive stock price data through the set_data() method.
At this time, the data received must be pandas.DataFrame.
Afterwards, when you run seolpyo_mplchart.show(), the chart will open.
The seolpyo_mplchart.show() function calls the matploib.pyplot.show() function.
import json
import seolpyo_mplchart as mc
import pandas as pd
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
c = mc.SliderChart()
c.set_data(df)
mc.show() # same as matplotlib.pyplot.show()
data needed
The chart object basically receives a DataFrame containing [base time, opening price, high price, low price, closing price, transaction volume] columns.
Change data key
Each column key is [date, open, high, low, close, volume].
If the key value used in the data frame is different from this, you must change the class variable as follows.
import json
import seolpyo_mplchart as mc
import pandas as pd
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
c = mc.SliderChart()
c.date= '시간'
c.Open = '시가'
c.high = '고가'
c.low = '저가'
c.close = '종가'
c.volume = '거래량'
c.set_data(df)
mc.show() # same as matplotlib.pyplot.show()
### OR ###
import json
import seolpyo_mplchart as mc
import pandas as pd
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
class Chart(mc.SliderChart):
date = '시간'
Open, high, low, close = ('시가', '고가', '저가', '종가')
volume = '거래량'
c = Chart()'
c.set_data(df)
mc.show() # same as matplotlib.pyplot.show()
provide three charts.
Click to expand 3 charts
seolpyo-mplchart provides a total of 3 types of candlestick charts.
Normal Chart (OnlyChart)
A regular chart like mplfinance.
It only draws a candlestick chart, and there is no interaction such as moving the area or displaying information when the mouse moves.
import json
import seolpyo_mplchart as mc
import pandas as pd
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
c = mc.OnlyChart()
c.set_data(df)
mc.show() # same as matplotlib.pyplot.show()
CursorChart
This is a regular chart with mouse interaction added.
Informational text is displayed when the mouse is hovered over a candle or volume bar.
As with regular charts, there is no area movement function.
import json
import seolpyo_mplchart as mc
import pandas as pd
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
c = mc.CursorChart()
c.set_data(df)
mc.show() # same as matplotlib.pyplot.show()
SliderChart
This chart has a slide function added to the cursor chart.
The ability to change the viewing area through the provided slider is added.
import json
import seolpyo_mplchart as mc
import pandas as pd
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
c = mc.SliderChart()
c.set_data(df)
mc.show() # same as matplotlib.pyplot.show()
Exclude trading volume chart
How to exclude trading volume chart Click to expand
If the value of "if self.volume" is false, the volume chart will not be drawn.
This option applies to Chart, CursorChart, and SliderChart.
import json
import seolpyo_mplchart as mc
import pandas as pd
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
c = mc.SliderChart()
c.volume = None
c.set_data(df)
mc.show() # same as matplotlib.pyplot.show()
watermark
Click to expand watermark content
Upgrading to version 1.0.0, the watermark has been changed to display by default.
Why do I see a watermark?
I looked up information using a candlestick chart, but I felt that it was cumbersome to check what data I was looking at.
Therefore, we added this function because it is easier to check what information you are looking at if it is displayed in the center of the chart.
If you do not want the watermark to be exposed, you can set the value of "if watermark" to false.
If you want to change the watermark text, just change the watermark value of the Chart object.
import json
import seolpyo_mplchart as mc
import pandas as pd
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
c = mc.SliderChart()
c.watermark = '' # watermark not display
c.watermark = 'This is watermark text' # display text
c.set_data(df)
mc.show() # same as matplotlib.pyplot.show()
How to change language and text format
How to change language and text format Click to expand
The language used is Hangul by default.
If necessary, you can change the price and volume units, and change the format of the displayed text to your desired format.
How to change display units
Price and trading volume units are managed by the following two variables.
- unit_price: Units used in prices.
- unit_volume: Unit used for trading volume.
If desired, you can also choose how many decimal places to display price and volume.
- digit_price: The number of decimal points displayed in the price.
- digit_volume: Number of decimal points displayed in volume.
import json
import seolpyo_mplchart as mc
import pandas as pd
class Chart(mc.SliderChart):
unit_price = '$'
unit_volume = 'Vol'
digit_price = 3
digit_volume = 1
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
c = Chart()
c.set_data(df)
mc.show()
How to change information text format
You can change the name of the price moving average line and the text in the displayed text box.
The three formats are stock price, trading volume, and price moving average line.
The price moving average format receives *args, but the stock price and volume text format receives **kwargs.
For your reference, basic English formats are provided as mc.format_candleinfo_en and mc.format_volumeinfo_en, so you can use them if necessary.
- format_candleinfo: Text format of information displayed when the mouse is placed over a candle
- format_volumeinfo: Text format of information displayed when the mouse is placed over the trading volume bar
- format_ma: Text format used for the price moving average legend located above the stock price chart
import json
import seolpyo_mplchart as mc
import pandas as pd
class Chart(mc.SliderChart):
unit_price = '$'
format_ma = 'ma{}'
format_candleinfo = '{dt}\n\nclose: {close}\nrate: {rate}\ncompare: {compare}\nopen: {open}({rate_open})\nhigh: {high}({rate_high})\nlow: {low}({rate_low})\nvolume: {volume}({rate_volume})'
format_volumeinfo = '{dt}\n\nvolume: {volume}\nvolume rate: {rate_volume}\ncompare: {compare}'
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
c = Chart()
c.set_data(df)
mc.show()
Change chart style
Change chart style Click to expand
The default style for the chart is light because I like white backgrounds.
If you want to change the chart style, you can adjust each value to create a chart of the desired style.
The settings changed in the following code are as follows.
- color_background: background color
- gridKwargs: Options applied to the grid drawn in the background
- color_tick: Color applied to ticks
- color_tick_label: Color applied to tick labels
- color_up: Color applied to candles whose closing price is “higher” than the opening price
- color_down: Color applied to candles whose closing price is “lower” than the opening price
- color_flat: Color applied to candles whose closing price is “same” as the opening price
- color_up_down: The color applied to candles whose closing price is “higher” than the opening price, but whose closing price is “lower” than the previous day’s closing price.
- color_down_up: The color applied to candles whose closing price is “lower” than the opening price, but whose closing price is “higher” than the previous day’s closing price.
- color_priceline: Colors applied to closing price line charts and uncolored moving averages
- facecolor_volume: Body color of volume bars
- edgecolor_volume: Outline color for volume bars
- list_macolor: Color applied to moving average line
- lineKwargs: Options applied to lines created when the mouse is positioned over the chart
- color_box: When the mouse hovers over a candle or volume, the color applied to the box that highlights that candle or volume.
- textboxKwargs: Options that apply to information text boxes
- textKwargs: Options that apply to infotext objects
- color_navigator_color: Cover color applied to unselected areas
- color_navigator_line: Color of the vertical lines displayed on the left and right sides of the selection sequence
- generate_data(class method): A class method that operates before creating a segment. By overriding this method, we changed the color of the volume bar corresponding to certain conditions.
import json
import seolpyo_mplchart as mc
import pandas as pd
class Chart(mc.SliderChart):
digit_price = 3
digit_volume = 1
unit_price = '$'
unit_volume = 'Vol'
format_ma = 'ma{}'
format_candleinfo = format_candleinfo_en
format_volumeinfo = format_volumeinfo_en
color_background = (0, 0, 0, 1)
gridKwargs = dict(linestyle='-', color='darkorange')
color_tick = 'w'
color_tick_label = 'w'
color_up = 'aqua'
color_down = 'w'
color_flat = 'w'
color_down_up = 'w'
color_up_down = 'aqua'
color_priceline = 'w'
facecolor_volume = 'greenyellow'
edgecolor_volume = 'blue'
list_macolor = ['lime', 'cyan', 'fuchsia', 'yellow', 'blueviolet']
lineKwargs = dict(edgecolor='w', linestyle=':')
color_box = 'w'
textboxKwargs = dict(edgecolor='w', facecolor='k')
textKwargs = dict(color='w')
color_navigator_cover = 'w'
color_navigator_line = 'w'
def generate_data(self):
self.df.loc[self.df[self.volume].shift(1) < self.df[self.volume], ['volumefacecolor', 'volumeedgecolor']] = ('red', 'yellow')
return
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
c = Chart()
c.set_data(df)
mc.show()
How to change chart theme
How to change chart theme Click to expand
You can change the theme of the chart object through the built-in function called set_theme.
You can choose one of two themes: light and dark.
import json
import seolpyo_mplchart as mc
import pandas as pd
class Chart(mc.SliderChart):
digit_price = 3
digit_volume = 1
unit_price = '$'
unit_volume = 'Vol'
format_ma = 'ma{}'
format_candleinfo = format_candleinfo_en
format_volumeinfo = format_volumeinfo_en
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
c = Chart()
mc.set_theme(c, 'dark')
c.set_data(df)
mc.show()
How to display fractions
How to display fractions Click to expand
If "fraction" is true, displays fractions with decimal places. The fraction represented is affected by "digit_price" and "digit_volume".
import json
import seolpyo_mplchart as mc
import pandas as pd
class Chart(mc.SliderChart):
unit_price = '$'
unit_volume = 'Vol'
digit_price = 3
digit_volume = 1
fraction = True
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
c = Chart()
c.set_data(df)
mc.show()
Options of set_data method
Set_data method options Click to expand
The "set_data" method accepts the following arguments in addition to the data frame.
- sort_df: If true, sorts the DataFrame. The sorting standard is the standard time column and is sorted in ascending order.
- calc_ma: If true, calculates data to create price moving average line. If false, pre-calculated price moving average line information must exist in the DataFrame. The key of each column must be in the "ma{n}" format and match list_ma 1:1.
- change_lim: If true, changes the initial lookup area. If false, no changes are made to the lookup area.
- calc_info: If true, calculates the data used in the text box. If false, each pre-calculated information must exist in the DataFrame. The key for each column is ('rate', 'compare', 'rate_open', 'rate_high', 'rate_low', 'rate_volume').
- set_candlecolor: If false, the candle color is not set randomly. An error occurs if the “facecolor” and “edgecolor” columns do not exist in the passed data frame.
-
set_volumecolor: If false, do not set the volume bar color randomly. An error occurs if the "volumefacecolor" and "volumeedgecolor" columns do not exist in the received data frame.
If you feel that the operations described above are unnecessary, you can skip the operations by setting each arg to False.
import json
import seolpyo_mplchart as mc
import pandas as pd
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
c = mc.SliderChart()
c.set_data(df, sort_df=True, calc_ma=True, change_lim=True, calc_info=True, set_candlecolor=True, set_volumecolor=True)
mc.show()
Simple expression based on query scope
Simple expression based on query scope Click to expand
If there is a lot of data, seolpyo-mplchart draws only part of it, not all of it.
Here's why:
- This is to prevent a decrease in screen switching speed. The more data you draw, the slower the conversion becomes.
- If you draw a lot, it may not be recognizable even if you draw it in detail. This reduces unnecessary work.
Depending on your computer specifications, you may experience stuttering when switching screens using sliders or moving charts.
In that case, you should simplify the objects drawn on the chart by lowering the values of the following Variables.
반If your computer specifications are good enough to not slow down even if you draw a little more, you can set the following values larger than the default.
- limit_candle: Maximum number of candles drawn on the screen. If you deviate from this, only the wick will be drawn, not the candle. The default is 800.
- limit_wick: Maximum number of wicks drawn on screen. If you deviate from this, a line chart of closing prices is drawn instead of a wick. The default is 4,000.
- limit_volume: The maximum number of volume bars drawn on the screen when drawing a line chart at the opening or closing price. The default is 200.
※ limit_volume only applies to SliderCharts. - limit_ma: If the number of data displayed on the closing price line chart is greater than this value, the price moving average line is not drawn. The default is 8,000.
※ limit_ma only applies to SliderCharts.
Switch from candle to wick
Switch from starting price to closing price line chart
import json
import seolpyo_mplchart as mc
import pandas as pd
class Chart(mc.SliderChart):
limit_candle = 800
limit_volume = 800
limit_wick = 4000
limit_ma = 8000
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
c = Chart()
c.set_data(df)
mc.show()
Minimum viewing area
Minimum viewing area Click to expand
If the number of candles displayed in the area selectable by the navigator is less than “min_distance”, the search area does not change.
If you want to change the minimum query range, you must change the value. The default value is 30.
import json
import seolpyo_mplchart as mc
import pandas as pd
class Chart(mc.SliderChart):
min_distance = 30
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
c = Chart()
c.set_data(df)
mc.show()
How to change the slider position
How to change the slider position Click to expand
If the value of "slider_top" is not true, the slider will be positioned at the bottom of the chart. The default is True.
import json
import seolpyo_mplchart as mc
import pandas as pd
class Chart(mc.SliderChart):
slider_top = False
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
c = Chart()
c.set_data(df)
mc.show()
Change chart proportions
Change chart proportions Click to expand
You can change the ratio of each chart area through the values of "ratio_ax_slider", "ratio_ax_legend", "ratio_ax_price", "ratio_ax_volume", and "ratio_ax_none".
"ratio_ax_none" is only used when the value of "slider_top" is false.
import json
import seolpyo_mplchart as mc
import pandas as pd
class Chart(mc.SliderChart):
ratio_ax_slider, ratio_ax_legend, ratio_ax_price, ratio_ax_volume = (3, 1, 3, 3)
ax_none = 2
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
c = Chart()
c.set_data(df)
mc.show()
Drawing a price moving average line on a candle
Draw a price moving average line on a candle and click to expand
If the value of "candle_on_ma" is false, a price moving average line is drawn on the candle.
import json
import seolpyo_mplchart as mc
import pandas as pd
class Chart(mc.SliderChart):
candle_on_ma = False
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
c = Chart()
c.set_data(df)
mc.show()
Event control method
Event control method Click to expand
seolpyo-mplchart receives pick event, draw event, and mouse click/release/move event.
When each event occurs, the next method is called.
If there is a necessary action when an event occurs, you can control it by overriding the following methods. Even if you override each method, it does not affect the event control of the module.
You can also add artist tasks that need to be drawn before blit through the draw_artist method.
import json
import seolpyo_mplchart as mc
import pandas as pd
class Chart(mc.SliderChart):
def on_draw(self, e):
return
def on_pick(self, e):
return
def draw_artist(self):
return
def on_move(self, e):
return
def on_click(self, e):
return
def on_release(self, e):
return
file = {stock price data path}
with open(file, 'r', encoding='utf-8') as txt:
data = json.load(txt)
df = pd.DataFrame(data)
c = Chart()
c.set_data(df)
mc.show()