From 41e54ab9c508bd7f2f3bc7838d33109bb2504fd5 Mon Sep 17 00:00:00 2001 From: David Brazda Date: Fri, 14 Jun 2024 09:20:11 +0200 Subject: [PATCH] precision parameter added on chart and panel level on helpers --- lightweight_charts/abstract.py | 2 +- lightweight_charts/helpers.py | 22 ++++++++++++++++------ setup.py | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lightweight_charts/abstract.py b/lightweight_charts/abstract.py index 4e0bf89..d10afde 100644 --- a/lightweight_charts/abstract.py +++ b/lightweight_charts/abstract.py @@ -42,7 +42,7 @@ INDEX = os.path.join(current_dir, 'js', 'index.html') # Predefined pool of colors COLORS = [ - "#63AA57", "#8F8AB0", "#4CAA4E", "#E24AEE", "#D06AA6", "#7891BA", "#A39A34", "#8A94A2", "#61BB2F", + "#63AA57", "#8F8AB0", "#E24AEE", "#D06AA6", "#7891BA", "#A39A34", "#8A94A2", "#61BB2F", "#FD569D", "#1EB6E1", "#379AC9", "#FD6F2E", "#8C9858", "#39A4A3", "#6D97F4", "#1ECB01", "#FA5B16", "#A6891C", "#48CF10", "#D27B26", "#D56B55", "#FE3AB8", "#E35C51", "#EC4FE6", "#E250A3", "#BA618E", "#1BC074", "#C57784", "#888BC5", "#4FA452", "#80885C", "#B97272", "#33BF98", "#B7961D", "#A07284", "#02E54E", "#AF7F35", "#F852EF", diff --git a/lightweight_charts/helpers.py b/lightweight_charts/helpers.py index 6f6ccbb..9e25775 100644 --- a/lightweight_charts/helpers.py +++ b/lightweight_charts/helpers.py @@ -22,6 +22,7 @@ class Panel: * middle1 : list of tuples, optional * middle2 : list of tuples, optional * xloc : str or slice, optional. Vectorbt indexing. Default is None. + * precision: int, optional. The number of digits after the decimal point. Apply to all lines on this pane. Default is None. Examples ------- @@ -46,7 +47,8 @@ class Panel: left=[(sma, "sma", short_signals, short_exits)], middle1=[], middle2=[], - xloc="2024-02-12 09:30" + xloc="2024-02-12 09:30", + precision=3 ) pane2 = Panel( @@ -61,7 +63,7 @@ class Panel: ch = chart([pane1, pane2], sync=True, title="neco", size="m", xloc=slice("2024-02-12 09:30","2024-02-12 16:00")) ``` """ - def __init__(self, ohlcv=None, right=None, left=None, middle1=None, middle2=None, histogram=None, title=None, xloc=None): + def __init__(self, ohlcv=None, right=None, left=None, middle1=None, middle2=None, histogram=None, title=None, xloc=None, precision=None): self.ohlcv = ohlcv if ohlcv is not None else [] self.right = right if right is not None else [] self.left = left if left is not None else [] @@ -70,9 +72,10 @@ class Panel: self.histogram = histogram if histogram is not None else [] self.title = title self.xloc = xloc + self.precision = precision -def chart(panes: list[Panel], sync=False, title='', size="m", xloc=None, session: str="9:30"): +def chart(panes: list[Panel], sync=False, title='', size="m", xloc=None, session: str="9:30", precision=None): """ Function to fast render a chart with multiple panes. This function manipulates graphical output or interfaces with an external framework to display charts with synchronized @@ -95,6 +98,8 @@ def chart(panes: list[Panel], sync=False, title='', size="m", xloc=None, session * session (str): Draw session vertical divider at that time. Defaults to '9:30'. Can be any time in 24-hour format or xloc slice + * precision (int): The number of digits after the decimal point. Defaults to None. Applies to lines on all panes, if not overriden by pane-specific precision. + * xloc (str): xloc advanced filtering of vbt.xloc accessor. Defaults to None. Applies to all panes. Might be overriden by pane-specific xloc. @@ -123,7 +128,8 @@ def chart(panes: list[Panel], sync=False, title='', size="m", xloc=None, session left=[(sma, "sma", short_signals, short_exits)], middle1=[], middle2=[], - xloc=slice("2024-02-12 09:30","2024-02-12 16:00") + xloc=slice("2024-02-12 09:30","2024-02-12 16:00"), + precision=4 ) pane2 = Pane( ohlcv=(t1data.data["BAC"],), @@ -162,6 +168,7 @@ def chart(panes: list[Panel], sync=False, title='', size="m", xloc=None, session else: subchartX = chartX.create_subchart(position='right', width=1, height=height_ratio, sync=sync, leftScale=bool(pane.left)) active_chart = subchartX + xloc = pane.xloc if pane.xloc is not None else xloc if pane.ohlcv is not None: @@ -193,7 +200,7 @@ def chart(panes: list[Panel], sync=False, title='', size="m", xloc=None, session #iterate over keys - they are all priceScaleId except of these for att_name, att_value_tuple in vars(pane).items(): - if att_name in ["ohlcv","histogram","title","xloc"]: + if att_name in ["ohlcv","histogram","title","xloc","precision"]: continue for tup in att_value_tuple: series, name, entries, exits, markers = (tup + (None, None, None, None, None))[:5] @@ -214,6 +221,9 @@ def chart(panes: list[Panel], sync=False, title='', size="m", xloc=None, session tmp = active_chart.create_line(name=name, priceScaleId=att_name)#, color="blue") tmp.set(xloc_me(series, xloc)) + if pane.precision is not None or precision is not None: + tmp.precision(pane.precision if pane.precision is not None else precision) + if entries is not None: tmp.markers_set(xloc_me(entries, xloc), "entries") if exits is not None: @@ -224,7 +234,7 @@ def chart(panes: list[Panel], sync=False, title='', size="m", xloc=None, session active_chart.legend(True) active_chart.fit() if session is not None and session: - active_chart.vertical_span(start_time=xloc_me(series, xloc).vbt.xloc[session].obj.index.to_list(), color="rgba(252, 219, 3, 0.4)") + active_chart.vertical_span(start_time=xloc_me(series, xloc).vbt.xloc[session].obj.index.to_list(), color="rgba(252, 255, 187, 0.42)") if not main_title_set: chartX.topbar.textbox("title",title) diff --git a/setup.py b/setup.py index b484fb8..6f21265 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open('README.md', 'r', encoding='utf-8') as f: setup( name='lightweight_charts', - version='2.1.0', + version='2.1.1', packages=find_packages(), python_requires='>=3.8', install_requires=[