Files
lightweight-charts-python/docs/source/toolbox.md
louisnw 06b605d3a7 Enhancements and Bug Fixes
Tables Feature
- Added the `create_table` method, which returns a `Table` object. This can be used to display watchlists, order windows, position windows and more.
- See the new page on the docs for more information!

Bugs
- Fixed a bug preventing the named column of a line to not work as a label of the series.
- Fixed a bug causing drawings loaded from the minute timeframe to not show on a daily timeframe.
- Fixed a bug causing `chart.exit` to not work.
- Fixed a bug preventing the chart from being moved after placing a ray.
- Fixed the ‘price in hoveringOver’ web console error.

Enhancements
- The date/time column can also be the `name` of the passed series object.
- Added the `label` method to `HorizontalLine`, allowing for the price line label of horizontal lines to be updated.
- `None` or an empty DataFrame can now be passed to `line.set` as a means to clear it.
- Seperate Chart objects will now run on the same pywebview instance. This means that any Chart objects created after the first will inherit the first Chart’s API.
- Reorganized the documentation for clarity.
2023-08-02 13:47:52 +01:00

3.0 KiB

Toolbox

The Toolbox allows for trendlines, ray lines and horizontal lines to be drawn and edited directly on the chart.

It can be used within any Chart object, and is enabled by setting the toolbox parameter to True upon Chart declaration.

The Toolbox should only be accessed from the toolbox attribute of the chart object. (chart.toolbox.<method>)

The following hotkeys can also be used when the Toolbox is enabled:

  • Alt+T: Trendline
  • Alt+H: Horizontal Line
  • Alt+R: Ray Line
  • Meta+Z or Ctrl+Z: Undo

Drawings can also be deleted by right-clicking on them, which brings up a context menu.


save_drawings_under

widget: Widget

Saves drawings under a specific topbar text widget. For example:

chart.toolbox.save_drawings_under(chart.topbar['symbol'])

load_drawings

tag: str

Loads and displays drawings stored under the tag given.


import_drawings

file_path: str

Imports the drawings stored at the JSON file given in file_path.


export_drawings

file_path: str

Exports all currently saved drawings to the JSON file given in file_path.


Example:

To get started, create a file called drawings.json, which should only contain {}.

import pandas as pd
from lightweight_charts import Chart


def get_bar_data(symbol, timeframe):
    if symbol not in ('AAPL', 'GOOGL', 'TSLA'):
        print(f'No data for "{symbol}"')
        return pd.DataFrame()
    return pd.read_csv(f'bar_data/{symbol}_{timeframe}.csv')


class API:
    def __init__(self):
        self.chart: Chart = None

    def on_search(self, searched_string): 
        new_data = get_bar_data(searched_string, self.chart.topbar['timeframe'].value)
        if new_data.empty:
            return
        self.chart.topbar['symbol'].set(searched_string)
        self.chart.set(new_data)
        self.chart.toolbox.load_drawings(searched_string)  # Loads the drawings saved under the symbol.

    def on_timeframe_selection(self):
        new_data = get_bar_data(self.chart.topbar['symbol'].value, self.chart.topbar['timeframe'].value)
        if new_data.empty:
            return
        self.chart.set(new_data, render_drawings=True)  # The symbol has not changed, so we want to re-render the drawings.
    
        
if __name__ == '__main__':
    api = API()

    chart = Chart(api=api, topbar=True, searchbox=True, toolbox=True)
    chart.legend(True)

    chart.topbar.textbox('symbol', 'TSLA')
    chart.topbar.switcher('timeframe', api.on_timeframe_selection, '1min', '5min', '30min', default='5min')

    df = get_bar_data('TSLA', '5min')

    chart.set(df)

    chart.toolbox.import_drawings('drawings.json')  # Imports the drawings saved in the JSON file.
    chart.toolbox.load_drawings(chart.topbar['symbol'].value)  # Loads the drawings under the default symbol.

    chart.toolbox.save_drawings_under(chart.topbar['symbol'])  # Saves drawings based on the symbol.
    
    chart.show(block=True)

    chart.toolbox.export_drawings('drawings.json')  # Exports the drawings to the JSON file.