dynamic sizing (size=s,m,l) and height ratio
This commit is contained in:
@ -10,103 +10,108 @@ class Panel:
|
|||||||
self.histogram = histogram if histogram is not None else []
|
self.histogram = histogram if histogram is not None else []
|
||||||
self.title = title
|
self.title = title
|
||||||
|
|
||||||
def chart(panes: list[Panel], sync=False, title=''):
|
def chart(panes: list[Panel], sync=False, title='', size="m"):
|
||||||
"""Function to fast render a chart with multiple panes.
|
"""Function to fast render a chart with multiple panes.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
panes (List[Pane]): A list of Pane instances to be rendered in the chart.
|
panes (List[Pane]): A list of Pane instances to be rendered in the chart.
|
||||||
sync (bool): If True, synchronize scales of all panes. Default is False.
|
sync (bool): If True, synchronize scales of all panes. Default is False.
|
||||||
title (str): Title of the chart. Default is an empty string.
|
title (str): Title of the chart. Default is an empty string.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
None. This function is expected to manipulate a graphical output or an external framework.
|
None. This function is expected to manipulate a graphical output or an external framework.
|
||||||
|
|
||||||
# Example usage
|
|
||||||
```
|
|
||||||
pane1 = Pane(
|
|
||||||
ohlcv=(t1data.data["BAC"],), #(series, entries, exits, other_markers)
|
|
||||||
histogram=[(order_imbalance_allvolume, "oivol")] # [(series, name, "rgba(53, 94, 59, 0.6)")]
|
|
||||||
#following attributes corresponds to different priceScaleId and allow to display
|
|
||||||
# line series on these scale
|
|
||||||
right=[], # [(series, name, entries, exits, other_markers)]
|
|
||||||
left=[(sma, "sma", short_signals, short_exits)],
|
|
||||||
middle1=[],
|
|
||||||
middle2=[],
|
|
||||||
)
|
|
||||||
|
|
||||||
pane2 = Pane(
|
# Example usage
|
||||||
ohlcv=(t1data.data["BAC"],),
|
```
|
||||||
right=[],
|
pane1 = Pane(
|
||||||
left=[(sma, "sma_below", short_signals, short_exits)],
|
ohlcv=(t1data.data["BAC"],), #(series, entries, exits, other_markers)
|
||||||
middle1=[],
|
histogram=[(order_imbalance_allvolume, "oivol")] # [(series, name, "rgba(53, 94, 59, 0.6)")]
|
||||||
middle2=[],
|
#following attributes corresponds to different priceScaleId and allow to display
|
||||||
histogram=[(order_imbalance_sma, "oisma")]
|
# line series on these scale
|
||||||
)
|
right=[], # [(series, name, entries, exits, other_markers)]
|
||||||
|
left=[(sma, "sma", short_signals, short_exits)],
|
||||||
|
middle1=[],
|
||||||
|
middle2=[],
|
||||||
|
)
|
||||||
|
|
||||||
ch = chart([pane1, pane2], sync=True)
|
pane2 = Pane(
|
||||||
```
|
ohlcv=(t1data.data["BAC"],),
|
||||||
|
right=[],
|
||||||
"""
|
left=[(sma, "sma_below", short_signals, short_exits)],
|
||||||
numpanes = len(panes)
|
middle1=[],
|
||||||
main_title_set = False
|
middle2=[],
|
||||||
for index, pane in enumerate(panes):
|
histogram=[(order_imbalance_sma, "oisma")]
|
||||||
subchartX = None
|
)
|
||||||
if index == 0:
|
|
||||||
chartX = JupyterChart(width=1000, height=600, inner_width=1, inner_height=0.5, leftScale=bool(pane.left))
|
|
||||||
active_chart = chartX
|
|
||||||
else:
|
|
||||||
subchartX = chartX.create_subchart(position='right', width=1, height=0.5, sync=sync, leftScale=bool(pane.left))
|
|
||||||
active_chart = subchartX
|
|
||||||
|
|
||||||
if pane.ohlcv is not None:
|
ch = chart([pane1, pane2], sync=True)
|
||||||
series, entries, exits, markers = (pane.ohlcv + (None,) * 4)[:4]
|
```
|
||||||
active_chart.set(series)
|
|
||||||
if entries is not None:
|
"""
|
||||||
|
size_to_dimensions = {
|
||||||
|
's': (800, 400),
|
||||||
|
'm': (1000, 600),
|
||||||
|
'l': (1300, 800)}
|
||||||
|
width, height = size_to_dimensions.get(size, (1000, 600))
|
||||||
|
height_ratio = 1 / len(panes)
|
||||||
|
main_title_set = False
|
||||||
|
for index, pane in enumerate(panes):
|
||||||
|
subchartX = None
|
||||||
|
if index == 0:
|
||||||
|
chartX = JupyterChart(width=width, height=height, inner_width=1, inner_height=height_ratio, leftScale=bool(pane.left))
|
||||||
|
active_chart = chartX
|
||||||
|
else:
|
||||||
|
subchartX = chartX.create_subchart(position='right', width=1, height=height_ratio, sync=sync, leftScale=bool(pane.left))
|
||||||
|
active_chart = subchartX
|
||||||
|
|
||||||
|
if pane.ohlcv is not None:
|
||||||
|
series, entries, exits, markers = (pane.ohlcv + (None,) * 4)[:4]
|
||||||
|
active_chart.set(series)
|
||||||
|
if entries is not None:
|
||||||
active_chart.markers_set(entries, "entries")
|
active_chart.markers_set(entries, "entries")
|
||||||
if exits is not None:
|
if exits is not None:
|
||||||
active_chart.markers_set(exits, "exits")
|
active_chart.markers_set(exits, "exits")
|
||||||
if markers is not None:
|
if markers is not None:
|
||||||
active_chart.markers_set(markers)
|
active_chart.markers_set(markers)
|
||||||
|
|
||||||
for tup in pane.histogram:
|
for tup in pane.histogram:
|
||||||
series, name, color, _, _ = (tup + (None, None, None, None, None))[:5]
|
series, name, color, _, _ = (tup + (None, None, None, None, None))[:5]
|
||||||
if series is None:
|
if series is None:
|
||||||
continue
|
continue
|
||||||
#conditionally include color
|
#conditionally include color
|
||||||
kwargs = {'name': name}
|
kwargs = {'name': name}
|
||||||
if color is not None:
|
if color is not None:
|
||||||
kwargs['color'] = color
|
kwargs['color'] = color
|
||||||
tmp = active_chart.create_histogram(**kwargs) #green transparent "rgba(53, 94, 59, 0.6)"
|
tmp = active_chart.create_histogram(**kwargs) #green transparent "rgba(53, 94, 59, 0.6)"
|
||||||
tmp.set(series)
|
tmp.set(series)
|
||||||
|
|
||||||
if pane.title is not None:
|
if pane.title is not None:
|
||||||
active_chart.topbar.textbox("title",pane.title)
|
active_chart.topbar.textbox("title",pane.title)
|
||||||
main_title_set = True if index==0 else False
|
main_title_set = True if index==0 else False
|
||||||
|
|
||||||
#iterate over keys - they are all priceScaleId except of histogram and ohlcv
|
#iterate over keys - they are all priceScaleId except of histogram and ohlcv
|
||||||
for att_name, att_value_tuple in vars(pane).items():
|
for att_name, att_value_tuple in vars(pane).items():
|
||||||
if att_name in ["ohlcv","histogram","title"]:
|
if att_name in ["ohlcv","histogram","title"]:
|
||||||
continue
|
continue
|
||||||
for tup in att_value_tuple:
|
for tup in att_value_tuple:
|
||||||
series, name, entries, exits, markers = (tup + (None, None, None, None, None))[:5]
|
series, name, entries, exits, markers = (tup + (None, None, None, None, None))[:5]
|
||||||
if series is None:
|
if series is None:
|
||||||
continue
|
continue
|
||||||
tmp = active_chart.create_line(name=name, priceScaleId=att_name)#, color="blue")
|
tmp = active_chart.create_line(name=name, priceScaleId=att_name)#, color="blue")
|
||||||
tmp.set(series)
|
tmp.set(series)
|
||||||
|
|
||||||
if entries is not None:
|
if entries is not None:
|
||||||
tmp.markers_set(entries, "entries")
|
tmp.markers_set(entries, "entries")
|
||||||
if exits is not None:
|
if exits is not None:
|
||||||
tmp.markers_set(exits, "exits")
|
tmp.markers_set(exits, "exits")
|
||||||
if markers is not None:
|
if markers is not None:
|
||||||
tmp.markers_set(markers)
|
tmp.markers_set(markers)
|
||||||
|
|
||||||
active_chart.legend(True)
|
active_chart.legend(True)
|
||||||
active_chart.fit()
|
active_chart.fit()
|
||||||
|
|
||||||
if not main_title_set:
|
if not main_title_set:
|
||||||
chartX.topbar.textbox("title",title)
|
chartX.topbar.textbox("title",title)
|
||||||
chartX.legend(True)
|
chartX.legend(True)
|
||||||
chartX.fit()
|
chartX.fit()
|
||||||
chartX.load()
|
chartX.load()
|
||||||
return chartX
|
return chartX
|
||||||
|
|||||||
2
setup.py
2
setup.py
@ -5,7 +5,7 @@ with open('README.md', 'r', encoding='utf-8') as f:
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='lightweight_charts',
|
name='lightweight_charts',
|
||||||
version='2.0.11',
|
version='2.0.12',
|
||||||
packages=find_packages(),
|
packages=find_packages(),
|
||||||
python_requires='>=3.8',
|
python_requires='>=3.8',
|
||||||
install_requires=[
|
install_requires=[
|
||||||
|
|||||||
Reference in New Issue
Block a user