Files
lightweight-charts-python/docs/source/tutorials/getting_started.md
louisnw f72baf95ba Polygon:
- Added async methods to polygon.
- The `requests` library is no longer required, with `urllib` being used instead.
- Added the `get_bar_data` function, which returns a dataframe of aggregate data from polygon.
- Opened up the `subscribe` and `unsubscribe` functions

Enhancements:
- Tables will now scroll when the rows exceed table height.

Bugs:
- Fixed a bug preventing async functions being used with horizontal line event.
- Fixed a bug causing the legend to show duplicate lines if the line was created after the legend.
- Fixed a bug causing the line hide icon to persist within the legend after deletion (#75)
- Fixed a bug causing the search box to be unfocused when the chart is loaded.
2023-08-27 00:20:05 +01:00

1.9 KiB

Getting Started

Installation

To install the library, use pip:

pip install lightweight-charts

Pywebview's installation can differ depending on OS. Please refer to their documentation.


A simple static chart

import pandas as pd
from lightweight_charts import Chart

Download this ohlcv.csv file for this tutorial.

In this example, we are reading a csv file using pandas:

           date    open    high     low   close       volume
0    2010-06-29  1.2667  1.6667  1.1693  1.5927  277519500.0
1    2010-06-30  1.6713  2.0280  1.5533  1.5887  253039500.0
2    2010-07-01  1.6627  1.7280  1.3513  1.4640  121461000.0
3    2010-07-02  1.4700  1.5500  1.2473  1.2800   75871500.0
4..

..which can be used as data for the Chart object:

if __name__ == '__main__':
    chart = Chart()
    
    df = pd.read_csv('ohlcv.csv')
    chart.set(df)
    
    chart.show(block=True)

The block parameter is set to True in this case, as we do not want the program to exit.

Due to the library's use of multiprocessing, instantiations of `Chart` should be encapsulated within an   `if __name__ == '__main__'` block.

Adding a line

Now lets add a moving average to the chart using the following function:

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

calculate_sma derives the data column from f'SMA {period}', which we will use as the name of our line:

if __name__ == '__main__':
    chart = Chart()
    line = chart.create_line(name='SMA 50')

    df = pd.read_csv('ohlcv.csv')
    sma_df = calculate_sma(df, period=50)

    chart.set(df)
    line.set(sma_df)
    
    chart.show(block=True)