Skip to content

Commit

Permalink
Enhancements:
Browse files Browse the repository at this point in the history
- Added the `title` parameter to `Chart`.

Bug Fixes:
- When passing a timestamp format to `set`, `update`, or `update_from_tick`, the unit is now by default milliseconds rather than nanoseconds.
- When using `update`, if a time is passed that occurs before the current last datapoint, an error will be raised.
  • Loading branch information
louisnw01 committed Nov 22, 2023
1 parent c3645ef commit 631afd4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/source/reference/charts.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ They inherit from [AbstractChart](#AbstractChart).

___

`````{py:class} Chart(width: int, height: int, x: int, y: int, screen: int, on_top: bool, maximize: bool, debug: bool, toolbox: bool, inner_width: float, inner_height: float, scale_candles_only: bool)
`````{py:class} Chart(width: int, height: int, x: int, y: int, title: str, screen: int, on_top: bool, maximize: bool, debug: bool, toolbox: bool, inner_width: float, inner_height: float, scale_candles_only: bool)
The main object used for the normal functionality of lightweight-charts-python, built on the pywebview library.
Expand Down
7 changes: 6 additions & 1 deletion lightweight_charts/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def _series_datetime_format(self, series: pd.Series, exclude_lowercase=None):

def _single_datetime_format(self, arg):
if isinstance(arg, (str, int, float)) or not pd.api.types.is_datetime64_any_dtype(arg):
arg = pd.to_datetime(arg)
arg = pd.to_datetime(arg, unit='ms')
arg = self._interval * (arg.timestamp() // self._interval)+self.offset
return arg

Expand Down Expand Up @@ -620,6 +620,11 @@ def update_from_tick(self, series: pd.Series, cumulative_volume: bool = False):
:param cumulative_volume: Adds the given volume onto the latest bar.
"""
series = self._series_datetime_format(series)
if series['time'] < self._last_bar['time']:
raise ValueError(
f'Trying to update tick of time "{pd.to_datetime(series["time"])}", '
f'which occurs before the last bar time of '
f'"{pd.to_datetime(self._last_bar["time"])}".')
bar = pd.Series()
if series['time'] == self._last_bar['time']:
bar = self._last_bar
Expand Down
19 changes: 10 additions & 9 deletions lightweight_charts/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def callback(self, message: str):

class PyWV:
def __init__(self, q, start_ev, exit_ev, loaded, emit_queue, return_queue, html, debug,
width, height, x, y, screen, on_top, maximize):
width, height, x, y, screen, on_top, maximize, title):
self.queue = q
self.return_queue = return_queue
self.exit = exit_ev
Expand All @@ -25,13 +25,13 @@ def __init__(self, q, start_ev, exit_ev, loaded, emit_queue, return_queue, html,
self.html = html

self.windows = []
self.create_window(width, height, x, y, screen, on_top, maximize)
self.create_window(width, height, x, y, screen, on_top, maximize, title)

start_ev.wait()
webview.start(debug=debug)
self.exit.set()

def create_window(self, width, height, x, y, screen=None, on_top=False, maximize=False):
def create_window(self, width, height, x, y, screen=None, on_top=False, maximize=False, title=''):
screen = webview.screens[screen] if screen is not None else None
if maximize:
if screen is None:
Expand All @@ -40,7 +40,7 @@ def create_window(self, width, height, x, y, screen=None, on_top=False, maximize
else:
width, height = screen.width, screen.height
self.windows.append(webview.create_window(
'', html=self.html, js_api=self.callback_api,
title, html=self.html, js_api=self.callback_api,
width=width, height=height, x=x, y=y, screen=screen,
on_top=on_top, background_color='#000000'))
self.windows[-1].events.loaded += lambda: self.loop(self.loaded[len(self.windows)-1])
Expand Down Expand Up @@ -73,9 +73,10 @@ class Chart(abstract.AbstractChart):
_q, _emit_q, _return_q = (mp.Queue() for _ in range(3))
_loaded_list = [mp.Event() for _ in range(MAX_WINDOWS)]

def __init__(self, width: int = 800, height: int = 600, x: int = None, y: int = None, screen: int = None,
on_top: bool = False, maximize: bool = False, debug: bool = False, toolbox: bool = False,
inner_width: float = 1.0, inner_height: float = 1.0, scale_candles_only: bool = False):
def __init__(self, width: int = 800, height: int = 600, x: int = None, y: int = None, title: str = '',
screen: int = None, on_top: bool = False, maximize: bool = False, debug: bool = False,
toolbox: bool = False, inner_width: float = 1.0, inner_height: float = 1.0,
scale_candles_only: bool = False):
self._i = Chart._window_num
self._loaded = Chart._loaded_list[self._i]
abstract.Window._return_q = Chart._return_q
Expand All @@ -89,13 +90,13 @@ def __init__(self, width: int = 800, height: int = 600, x: int = None, y: int =
self._process = mp.Process(target=PyWV, args=(
self._q, self._start, self._exit, Chart._loaded_list,
self._emit_q, self._return_q, abstract.TEMPLATE, debug,
width, height, x, y, screen, on_top, maximize,
width, height, x, y, screen, on_top, maximize, title
), daemon=True)
self._process.start()
else:
window.handlers = Chart._main_window_handlers
super().__init__(window, inner_width, inner_height, scale_candles_only, toolbox)
self._q.put(('create_window', (width, height, x, y, screen, on_top, maximize)))
self._q.put(('create_window', (width, height, x, y, screen, on_top, maximize, title)))

def show(self, block: bool = False):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='lightweight_charts',
version='1.0.18.5',
version='1.0.18.6',
packages=find_packages(),
python_requires='>=3.8',
install_requires=[
Expand Down

0 comments on commit 631afd4

Please sign in to comment.