support for vbt indicators in helpers

This commit is contained in:
David Brazda
2024-06-12 15:12:27 +02:00
parent c29b1c5535
commit d9cff93ba8
3 changed files with 26 additions and 12 deletions

View File

@ -6,7 +6,7 @@ from datetime import datetime
from typing import Callable, Union, Literal, List, Optional from typing import Callable, Union, Literal, List, Optional
import pandas as pd import pandas as pd
import random import random
from vectorbtpro.indicators import IndicatorFactory #from vectorbtpro.indicators import IndicatorFactory
from .table import Table from .table import Table
from .toolbox import ToolBox from .toolbox import ToolBox
from .drawings import Box, HorizontalLine, RayLine, TrendLine, TwoPointDrawing, VerticalLine, VerticalSpan from .drawings import Box, HorizontalLine, RayLine, TrendLine, TwoPointDrawing, VerticalLine, VerticalSpan
@ -14,18 +14,12 @@ from .topbar import TopBar
from .util import ( from .util import (
BulkRunScript, Pane, Events, IDGen, as_enum, jbool, js_json, TIME, NUM, FLOAT, BulkRunScript, Pane, Events, IDGen, as_enum, jbool, js_json, TIME, NUM, FLOAT,
LINE_STYLE, MARKER_POSITION, MARKER_SHAPE, CROSSHAIR_MODE, MARKER_TYPE, 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__)) current_dir = os.path.dirname(os.path.abspath(__file__))
INDEX = os.path.join(current_dir, 'js', 'index.html') 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 # # Predefined colors that stand out well on dark backgrounds
# COLORS = [ # COLORS = [
# 'rgba(255, 0, 0, 0.6)', # Red # 'rgba(255, 0, 0, 0.6)', # Red
@ -309,7 +303,7 @@ class SeriesCommon(Pane):
self.run_script(f'{self.id}.series.setData([])') self.run_script(f'{self.id}.series.setData([])')
self.data = pd.DataFrame() self.data = pd.DataFrame()
return return
if is_indicator(df): if is_vbt_indicator(df):
df = df.real df = df.real
#if df is pd.Series then convert to df #if df is pd.Series then convert to df
if isinstance(df, pd.Series): if isinstance(df, pd.Series):

View File

@ -1,4 +1,7 @@
from lightweight_charts.widgets import JupyterChart from .widgets import JupyterChart
from .util import (
is_vbt_indicator
)
class Panel: 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] 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.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: if entries is not None:
tmp.markers_set(entries, "entries") tmp.markers_set(entries, "entries")

View File

@ -7,6 +7,12 @@ from numpy import isin
import pandas as pd 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: class Pane:
def __init__(self, window): def __init__(self, window):
from lightweight_charts import Window from lightweight_charts import Window