lightweight-charts-python icon indicating copy to clipboard operation
lightweight-charts-python copied to clipboard

[BUG] `chart.exit()` not end program

Open knight1972001 opened this issue 1 year ago • 0 comments

Expected Behavior

chart.exit() should end program immediately.

Current Behaviour

chart.exit() is not exit program.

` File "C:\Python312\Lib\asyncio\runners.py", line 118, in run return self._loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Python312\Lib\asyncio\base_events.py", line 664, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "C:\Python312\Lib\site-packages\lightweight_charts\chart.py", line 135, in show_async await asyncio.sleep(0.05) File "C:\Python312\Lib\asyncio\tasks.py", line 655, in sleep return await future ^^^^^^^^^^^^ asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Crypto_ML\test-lw.py", line 51, in chart.show(block=True) File "C:\Python312\Lib\site-packages\lightweight_charts\chart.py", line 123, in show asyncio.run(self.show_async(block=True)) File "C:\Python312\Lib\asyncio\runners.py", line 194, in run return runner.run(main) ^^^^^^^^^^^^^^^^ File "C:\Python312\Lib\asyncio\runners.py", line 123, in run raise KeyboardInterrupt() KeyboardInterrupt`

Reproducible Example

`import pandas as pd
from lightweight_charts import Chart


def exit_graph(key):
    print("Exiting...")
    chart.exit()


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


if __name__ == "__main__":

    # chart = Chart(toolbox=True)
    chart = Chart()
    # Columns: time | open | high | low | close | volume
    df_bin = pd.read_csv("ex_data.csv")
    df_bin["time"] = pd.to_datetime(df_bin["time"], unit="ms")

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

    chart.legend(
        visible=True,
        font_size=14,
        percent=True,
        color_based_on_candle=True,
        text="Chart info: ",
    )

    # chart.resize(width=900, height=600)
    chart.set(df_bin, render_drawings=True)

    line = chart.create_line("SMA 50")
    sma_data = calculate_sma(df_bin, period=50)
    line.set(sma_data)
    # chart.horizontal_line(200, func=on_horizontal_line_move)

    chart.hotkey("shift", "X", exit_graph)

    chart.show(block=True)
`

Environment

- OS:Windows
- Library: latest

knight1972001 avatar May 19 '24 00:05 knight1972001