From d9cff93ba8deac0bf8cc514d0422e8b154ba6bc1 Mon Sep 17 00:00:00 2001 From: David Brazda Date: Wed, 12 Jun 2024 15:12:27 +0200 Subject: [PATCH] support for vbt indicators in helpers --- lightweight_charts/abstract.py | 12 +++--------- lightweight_charts/helpers.py | 20 +++++++++++++++++--- lightweight_charts/util.py | 6 ++++++ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/lightweight_charts/abstract.py b/lightweight_charts/abstract.py index b2a1b7e..89a617d 100644 --- a/lightweight_charts/abstract.py +++ b/lightweight_charts/abstract.py @@ -6,7 +6,7 @@ from datetime import datetime from typing import Callable, Union, Literal, List, Optional import pandas as pd import random -from vectorbtpro.indicators import IndicatorFactory +#from vectorbtpro.indicators import IndicatorFactory from .table import Table from .toolbox import ToolBox from .drawings import Box, HorizontalLine, RayLine, TrendLine, TwoPointDrawing, VerticalLine, VerticalSpan @@ -14,18 +14,12 @@ from .topbar import TopBar from .util import ( BulkRunScript, Pane, Events, IDGen, as_enum, jbool, js_json, TIME, NUM, FLOAT, LINE_STYLE, MARKER_POSITION, MARKER_SHAPE, CROSSHAIR_MODE, MARKER_TYPE, - PRICE_SCALE_MODE, marker_position, marker_shape, js_data, + PRICE_SCALE_MODE, marker_position, marker_shape, js_data, is_vbt_indicator ) current_dir = os.path.dirname(os.path.abspath(__file__)) INDEX = os.path.join(current_dir, 'js', 'index.html') -def is_indicator(variable): - # Get the module path of the variable's type - module_path = variable.__class__.__module__ - # Check if it starts with 'vectorbtpro.indicators' - return module_path.startswith('vectorbtpro.indicators') - # # Predefined colors that stand out well on dark backgrounds # COLORS = [ # 'rgba(255, 0, 0, 0.6)', # Red @@ -309,7 +303,7 @@ class SeriesCommon(Pane): self.run_script(f'{self.id}.series.setData([])') self.data = pd.DataFrame() return - if is_indicator(df): + if is_vbt_indicator(df): df = df.real #if df is pd.Series then convert to df if isinstance(df, pd.Series): diff --git a/lightweight_charts/helpers.py b/lightweight_charts/helpers.py index 50af0f4..e3e14af 100644 --- a/lightweight_charts/helpers.py +++ b/lightweight_charts/helpers.py @@ -1,4 +1,7 @@ -from lightweight_charts.widgets import JupyterChart +from .widgets import JupyterChart +from .util import ( + is_vbt_indicator +) class Panel: """ @@ -167,8 +170,19 @@ def chart(panes: list[Panel], sync=False, title='', size="m"): series, name, entries, exits, markers = (tup + (None, None, None, None, None))[:5] if series is None: continue - tmp = active_chart.create_line(name=name, priceScaleId=att_name)#, color="blue") - tmp.set(series) + + #pokud jde o vbt indikator vytahneme vsechny output a predpokladame, ze jde o lines a vykreslime je + if is_vbt_indicator(series): + for output in series.output_names: + output_series = getattr(series, output) + tmp = active_chart.create_line(name=output, priceScaleId=att_name)#, color="blue") + tmp.set(output_series) + else: + if name is None: + name = "no_name" + + tmp = active_chart.create_line(name=name, priceScaleId=att_name)#, color="blue") + tmp.set(series) if entries is not None: tmp.markers_set(entries, "entries") diff --git a/lightweight_charts/util.py b/lightweight_charts/util.py index 9933e52..edd01b3 100644 --- a/lightweight_charts/util.py +++ b/lightweight_charts/util.py @@ -7,6 +7,12 @@ from numpy import isin import pandas as pd +def is_vbt_indicator(variable): + # Get the module path of the variable's type + module_path = variable.__class__.__module__ + # Check if it starts with 'vectorbtpro.indicators' + return module_path.startswith('vectorbtpro.indicators') + class Pane: def __init__(self, window): from lightweight_charts import Window