lightweight-charts


Namelightweight-charts JSON
Version 2.1 PyPI version JSON
download
home_pagehttps://github.com/louisnw01/lightweight-charts-python
SummaryPython framework for TradingView's Lightweight Charts JavaScript library.
upload_time2024-09-28 14:22:05
maintainerNone
docs_urlNone
authorlouisnw
requires_python>=3.8
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <div align="center">

# lightweight-charts-python

[![PyPi Release](https://img.shields.io/pypi/v/lightweight-charts?color=32a852&label=PyPi)](https://pypi.org/project/lightweight-charts/)
[![Made with Python](https://img.shields.io/badge/Python-3.8+-c7a002?logo=python&logoColor=white)](https://python.org "Go to Python homepage")
[![License](https://img.shields.io/github/license/louisnw01/lightweight-charts-python?color=9c2400)](https://github.com/louisnw01/lightweight-charts-python/blob/main/LICENSE)
[![Documentation](https://img.shields.io/badge/documentation-006ee3)](https://lightweight-charts-python.readthedocs.io/en/latest/index.html)

![cover](https://raw.githubusercontent.com/louisnw01/lightweight-charts-python/main/cover.png)

lightweight-charts-python aims to provide a simple and pythonic way to access and implement [TradingView's Lightweight Charts](https://www.tradingview.com/lightweight-charts/).
</div>


## Installation
```
pip install lightweight-charts
```
___

## Features
1. Streamlined for live data, with methods for updating directly from tick data.
2. Multi-pane charts using [Subcharts](https://lightweight-charts-python.readthedocs.io/en/latest/reference/abstract_chart.html#AbstractChart.create_subchart).
3. The [Toolbox](https://lightweight-charts-python.readthedocs.io/en/latest/reference/toolbox.html), allowing for trendlines, rectangles, rays and horizontal lines to be drawn directly onto charts.
4. [Events](https://lightweight-charts-python.readthedocs.io/en/latest/tutorials/events.html) allowing for timeframe selectors (1min, 5min, 30min etc.), searching, hotkeys, and more.
5. [Tables](https://lightweight-charts-python.readthedocs.io/en/latest/reference/tables.html) for watchlists, order entry, and trade management.
6. Direct integration of market data through [Polygon.io's](https://polygon.io/?utm_source=affiliate&utm_campaign=pythonlwcharts) market data API.

__Supports:__ Jupyter Notebooks, PyQt6, PyQt5, PySide6, wxPython, Streamlit, and asyncio.

PartTimeLarry: [Interactive Brokers API and TradingView Charts in Python](https://www.youtube.com/watch?v=TlhDI3PforA)
___

### 1. Display data from a csv:

```python
import pandas as pd
from lightweight_charts import Chart


if __name__ == '__main__':
    
    chart = Chart()
    
    # Columns: time | open | high | low | close | volume 
    df = pd.read_csv('ohlcv.csv')
    chart.set(df)
    
    chart.show(block=True)

```
![setting_data image](https://raw.githubusercontent.com/louisnw01/lightweight-charts-python/main/examples/1_setting_data/setting_data.png)
___

### 2. Updating bars in real-time:

```python
import pandas as pd
from time import sleep
from lightweight_charts import Chart

if __name__ == '__main__':

    chart = Chart()

    df1 = pd.read_csv('ohlcv.csv')
    df2 = pd.read_csv('next_ohlcv.csv')

    chart.set(df1)

    chart.show()

    last_close = df1.iloc[-1]['close']
    
    for i, series in df2.iterrows():
        chart.update(series)

        if series['close'] > 20 and last_close < 20:
            chart.marker(text='The price crossed $20!')
            
        last_close = series['close']
        sleep(0.1)

```

![live data gif](https://github.com/louisnw01/lightweight-charts-python/blob/main/examples/2_live_data/live_data.gif?raw=true)
___

### 3. Updating bars from tick data in real-time:

```python
import pandas as pd
from time import sleep
from lightweight_charts import Chart


if __name__ == '__main__':
    
    df1 = pd.read_csv('ohlc.csv')
    
    # Columns: time | price 
    df2 = pd.read_csv('ticks.csv')
    
    chart = Chart()
    
    chart.set(df1)
    
    chart.show()
    
    for i, tick in df2.iterrows():
        chart.update_from_tick(tick)
            
        sleep(0.03)

```
![tick data gif](https://raw.githubusercontent.com/louisnw01/lightweight-charts-python/main/examples/3_tick_data/tick_data.gif)
___

### 4. Line Indicators:

```python
import pandas as pd
from lightweight_charts import Chart


def calculate_sma(df, period: int = 50):
    return pd.DataFrame({
        'time': df['date'],
        f'SMA {period}': df['close'].rolling(window=period).mean()
    }).dropna()


if __name__ == '__main__':
    chart = Chart()
    chart.legend(visible=True)

    df = pd.read_csv('ohlcv.csv')
    chart.set(df)

    line = chart.create_line('SMA 50')
    sma_data = calculate_sma(df, period=50)
    line.set(sma_data)

    chart.show(block=True)

```
![line indicators image](https://raw.githubusercontent.com/louisnw01/lightweight-charts-python/main/examples/4_line_indicators/line_indicators.png)
___

### 5. Styling:

```python
import pandas as pd
from lightweight_charts import Chart


if __name__ == '__main__':
    
    chart = Chart()

    df = pd.read_csv('ohlcv.csv')

    chart.layout(background_color='#090008', text_color='#FFFFFF', font_size=16,
                 font_family='Helvetica')

    chart.candle_style(up_color='#00ff55', down_color='#ed4807',
                       border_up_color='#FFFFFF', border_down_color='#FFFFFF',
                       wick_up_color='#FFFFFF', wick_down_color='#FFFFFF')

    chart.volume_config(up_color='#00ff55', down_color='#ed4807')

    chart.watermark('1D', color='rgba(180, 180, 240, 0.7)')

    chart.crosshair(mode='normal', vert_color='#FFFFFF', vert_style='dotted',
                    horz_color='#FFFFFF', horz_style='dotted')

    chart.legend(visible=True, font_size=14)

    chart.set(df)

    chart.show(block=True)

```
![styling image](https://raw.githubusercontent.com/louisnw01/lightweight-charts-python/main/examples/5_styling/styling.png)
___

### 6. Callbacks:

```python
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')


def on_search(chart, searched_string):  # Called when the user searches.
    new_data = get_bar_data(searched_string, chart.topbar['timeframe'].value)
    if new_data.empty:
        return
    chart.topbar['symbol'].set(searched_string)
    chart.set(new_data)


def on_timeframe_selection(chart):  # Called when the user changes the timeframe.
    new_data = get_bar_data(chart.topbar['symbol'].value, chart.topbar['timeframe'].value)
    if new_data.empty:
        return
    chart.set(new_data, True)


def on_horizontal_line_move(chart, line):
    print(f'Horizontal line moved to: {line.price}')


if __name__ == '__main__':
    chart = Chart(toolbox=True)
    chart.legend(True)

    chart.events.search += on_search

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

    df = get_bar_data('TSLA', '5min')
    chart.set(df)

    chart.horizontal_line(200, func=on_horizontal_line_move)

    chart.show(block=True)

```
![callbacks gif](https://raw.githubusercontent.com/louisnw01/lightweight-charts-python/main/examples/6_callbacks/callbacks.gif)
___

<div align="center">

[![Documentation](https://img.shields.io/badge/documentation-006ee3)](https://lightweight-charts-python.readthedocs.io/en/latest/index.html)

Inquiries: [shaders_worker_0e@icloud.com](mailto:shaders_worker_0e@icloud.com)
___

_This package is an independent creation and has not been endorsed, sponsored, or approved by TradingView. The author of this package does not have any official relationship with TradingView, and the package does not represent the views or opinions of TradingView._
</div>

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/louisnw01/lightweight-charts-python",
    "name": "lightweight-charts",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "louisnw",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/e0/9e/70868fb8614f02c1320798917bd5f6f9278e8471c0c459d4a87b50879bb3/lightweight_charts-2.1.tar.gz",
    "platform": null,
    "description": "<div align=\"center\">\n\n# lightweight-charts-python\n\n[![PyPi Release](https://img.shields.io/pypi/v/lightweight-charts?color=32a852&label=PyPi)](https://pypi.org/project/lightweight-charts/)\n[![Made with Python](https://img.shields.io/badge/Python-3.8+-c7a002?logo=python&logoColor=white)](https://python.org \"Go to Python homepage\")\n[![License](https://img.shields.io/github/license/louisnw01/lightweight-charts-python?color=9c2400)](https://github.com/louisnw01/lightweight-charts-python/blob/main/LICENSE)\n[![Documentation](https://img.shields.io/badge/documentation-006ee3)](https://lightweight-charts-python.readthedocs.io/en/latest/index.html)\n\n![cover](https://raw.githubusercontent.com/louisnw01/lightweight-charts-python/main/cover.png)\n\nlightweight-charts-python aims to provide a simple and pythonic way to access and implement [TradingView's Lightweight Charts](https://www.tradingview.com/lightweight-charts/).\n</div>\n\n\n## Installation\n```\npip install lightweight-charts\n```\n___\n\n## Features\n1. Streamlined for live data, with methods for updating directly from tick data.\n2. Multi-pane charts using [Subcharts](https://lightweight-charts-python.readthedocs.io/en/latest/reference/abstract_chart.html#AbstractChart.create_subchart).\n3. The [Toolbox](https://lightweight-charts-python.readthedocs.io/en/latest/reference/toolbox.html), allowing for trendlines, rectangles, rays and horizontal lines to be drawn directly onto charts.\n4. [Events](https://lightweight-charts-python.readthedocs.io/en/latest/tutorials/events.html) allowing for timeframe selectors (1min, 5min, 30min etc.), searching, hotkeys, and more.\n5. [Tables](https://lightweight-charts-python.readthedocs.io/en/latest/reference/tables.html) for watchlists, order entry, and trade management.\n6. Direct integration of market data through [Polygon.io's](https://polygon.io/?utm_source=affiliate&utm_campaign=pythonlwcharts) market data API.\n\n__Supports:__ Jupyter Notebooks, PyQt6, PyQt5, PySide6, wxPython, Streamlit, and asyncio.\n\nPartTimeLarry: [Interactive Brokers API and TradingView Charts in Python](https://www.youtube.com/watch?v=TlhDI3PforA)\n___\n\n### 1. Display data from a csv:\n\n```python\nimport pandas as pd\nfrom lightweight_charts import Chart\n\n\nif __name__ == '__main__':\n    \n    chart = Chart()\n    \n    # Columns: time | open | high | low | close | volume \n    df = pd.read_csv('ohlcv.csv')\n    chart.set(df)\n    \n    chart.show(block=True)\n\n```\n![setting_data image](https://raw.githubusercontent.com/louisnw01/lightweight-charts-python/main/examples/1_setting_data/setting_data.png)\n___\n\n### 2. Updating bars in real-time:\n\n```python\nimport pandas as pd\nfrom time import sleep\nfrom lightweight_charts import Chart\n\nif __name__ == '__main__':\n\n    chart = Chart()\n\n    df1 = pd.read_csv('ohlcv.csv')\n    df2 = pd.read_csv('next_ohlcv.csv')\n\n    chart.set(df1)\n\n    chart.show()\n\n    last_close = df1.iloc[-1]['close']\n    \n    for i, series in df2.iterrows():\n        chart.update(series)\n\n        if series['close'] > 20 and last_close < 20:\n            chart.marker(text='The price crossed $20!')\n            \n        last_close = series['close']\n        sleep(0.1)\n\n```\n\n![live data gif](https://github.com/louisnw01/lightweight-charts-python/blob/main/examples/2_live_data/live_data.gif?raw=true)\n___\n\n### 3. Updating bars from tick data in real-time:\n\n```python\nimport pandas as pd\nfrom time import sleep\nfrom lightweight_charts import Chart\n\n\nif __name__ == '__main__':\n    \n    df1 = pd.read_csv('ohlc.csv')\n    \n    # Columns: time | price \n    df2 = pd.read_csv('ticks.csv')\n    \n    chart = Chart()\n    \n    chart.set(df1)\n    \n    chart.show()\n    \n    for i, tick in df2.iterrows():\n        chart.update_from_tick(tick)\n            \n        sleep(0.03)\n\n```\n![tick data gif](https://raw.githubusercontent.com/louisnw01/lightweight-charts-python/main/examples/3_tick_data/tick_data.gif)\n___\n\n### 4. Line Indicators:\n\n```python\nimport pandas as pd\nfrom lightweight_charts import Chart\n\n\ndef calculate_sma(df, period: int = 50):\n    return pd.DataFrame({\n        'time': df['date'],\n        f'SMA {period}': df['close'].rolling(window=period).mean()\n    }).dropna()\n\n\nif __name__ == '__main__':\n    chart = Chart()\n    chart.legend(visible=True)\n\n    df = pd.read_csv('ohlcv.csv')\n    chart.set(df)\n\n    line = chart.create_line('SMA 50')\n    sma_data = calculate_sma(df, period=50)\n    line.set(sma_data)\n\n    chart.show(block=True)\n\n```\n![line indicators image](https://raw.githubusercontent.com/louisnw01/lightweight-charts-python/main/examples/4_line_indicators/line_indicators.png)\n___\n\n### 5. Styling:\n\n```python\nimport pandas as pd\nfrom lightweight_charts import Chart\n\n\nif __name__ == '__main__':\n    \n    chart = Chart()\n\n    df = pd.read_csv('ohlcv.csv')\n\n    chart.layout(background_color='#090008', text_color='#FFFFFF', font_size=16,\n                 font_family='Helvetica')\n\n    chart.candle_style(up_color='#00ff55', down_color='#ed4807',\n                       border_up_color='#FFFFFF', border_down_color='#FFFFFF',\n                       wick_up_color='#FFFFFF', wick_down_color='#FFFFFF')\n\n    chart.volume_config(up_color='#00ff55', down_color='#ed4807')\n\n    chart.watermark('1D', color='rgba(180, 180, 240, 0.7)')\n\n    chart.crosshair(mode='normal', vert_color='#FFFFFF', vert_style='dotted',\n                    horz_color='#FFFFFF', horz_style='dotted')\n\n    chart.legend(visible=True, font_size=14)\n\n    chart.set(df)\n\n    chart.show(block=True)\n\n```\n![styling image](https://raw.githubusercontent.com/louisnw01/lightweight-charts-python/main/examples/5_styling/styling.png)\n___\n\n### 6. Callbacks:\n\n```python\nimport pandas as pd\nfrom lightweight_charts import Chart\n\n\ndef get_bar_data(symbol, timeframe):\n    if symbol not in ('AAPL', 'GOOGL', 'TSLA'):\n        print(f'No data for \"{symbol}\"')\n        return pd.DataFrame()\n    return pd.read_csv(f'bar_data/{symbol}_{timeframe}.csv')\n\n\ndef on_search(chart, searched_string):  # Called when the user searches.\n    new_data = get_bar_data(searched_string, chart.topbar['timeframe'].value)\n    if new_data.empty:\n        return\n    chart.topbar['symbol'].set(searched_string)\n    chart.set(new_data)\n\n\ndef on_timeframe_selection(chart):  # Called when the user changes the timeframe.\n    new_data = get_bar_data(chart.topbar['symbol'].value, chart.topbar['timeframe'].value)\n    if new_data.empty:\n        return\n    chart.set(new_data, True)\n\n\ndef on_horizontal_line_move(chart, line):\n    print(f'Horizontal line moved to: {line.price}')\n\n\nif __name__ == '__main__':\n    chart = Chart(toolbox=True)\n    chart.legend(True)\n\n    chart.events.search += on_search\n\n    chart.topbar.textbox('symbol', 'TSLA')\n    chart.topbar.switcher('timeframe', ('1min', '5min', '30min'), default='5min',\n                          func=on_timeframe_selection)\n\n    df = get_bar_data('TSLA', '5min')\n    chart.set(df)\n\n    chart.horizontal_line(200, func=on_horizontal_line_move)\n\n    chart.show(block=True)\n\n```\n![callbacks gif](https://raw.githubusercontent.com/louisnw01/lightweight-charts-python/main/examples/6_callbacks/callbacks.gif)\n___\n\n<div align=\"center\">\n\n[![Documentation](https://img.shields.io/badge/documentation-006ee3)](https://lightweight-charts-python.readthedocs.io/en/latest/index.html)\n\nInquiries: [shaders_worker_0e@icloud.com](mailto:shaders_worker_0e@icloud.com)\n___\n\n_This package is an independent creation and has not been endorsed, sponsored, or approved by TradingView. The author of this package does not have any official relationship with TradingView, and the package does not represent the views or opinions of TradingView._\n</div>\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Python framework for TradingView's Lightweight Charts JavaScript library.",
    "version": "2.1",
    "project_urls": {
        "Homepage": "https://github.com/louisnw01/lightweight-charts-python"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f212f225e2140d49bdb4f43c5bd7b766ad6b52dc1c1c93f9c8f81700330cc69a",
                "md5": "1b86aa054a38d944e8c1136fe794a4dc",
                "sha256": "4c5598fd57ac7569c3fed733727e2033190ca2f6e2460223d27e0926be7bff4f"
            },
            "downloads": -1,
            "filename": "lightweight_charts-2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1b86aa054a38d944e8c1136fe794a4dc",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 96281,
            "upload_time": "2024-09-28T14:22:02",
            "upload_time_iso_8601": "2024-09-28T14:22:02.610942Z",
            "url": "https://files.pythonhosted.org/packages/f2/12/f225e2140d49bdb4f43c5bd7b766ad6b52dc1c1c93f9c8f81700330cc69a/lightweight_charts-2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e09e70868fb8614f02c1320798917bd5f6f9278e8471c0c459d4a87b50879bb3",
                "md5": "6da0e687bb5d5e9e585a5efc251f5bb0",
                "sha256": "ea269a57d1a5a638999378b0e291f471c0642ae85f55d32f03eb6a85b96d0f7f"
            },
            "downloads": -1,
            "filename": "lightweight_charts-2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "6da0e687bb5d5e9e585a5efc251f5bb0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 94907,
            "upload_time": "2024-09-28T14:22:05",
            "upload_time_iso_8601": "2024-09-28T14:22:05.182535Z",
            "url": "https://files.pythonhosted.org/packages/e0/9e/70868fb8614f02c1320798917bd5f6f9278e8471c0c459d4a87b50879bb3/lightweight_charts-2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-28 14:22:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "louisnw01",
    "github_project": "lightweight-charts-python",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "lightweight-charts"
}
        
Elapsed time: 0.82454s