- Moved ChartAsync’s methods into the Chart object.
- Removed ChartAsync.
- Added the `show_async` method to `Chart`.
- Refactored how the TopBar is used. The docs explain this in detail, but a basic rundown is:
- `corner_text` and `create_switcher` are no longer methods. The `topbar` attribute of `chart` should be used instead.
- switchers and textboxes, now created with `chart.topbar.textbox` and `chart.topbar.switcher` require a name to be passed to them, which is used to access its instance (e.g `chart.topbar[‘timeframe’]`)
- If you have any questions about these changes, or potential enhancements, feel free to raise an issue and I will get back to you ASAP :)
- PtQt and Wx can now use either synchronous or asynchronous callback functions
- BETA: Support for Jupyter Notebooks
- Fixed a bug causing the ‘date’ column of DataFrames passed to `set`, `update`, and `update_from_tick` to be modified.
|
Before Width: | Height: | Size: 6.6 MiB After Width: | Height: | Size: 5.0 MiB |
|
Before Width: | Height: | Size: 712 KiB After Width: | Height: | Size: 349 KiB |
|
Before Width: | Height: | Size: 2.7 MiB |
|
Before Width: | Height: | Size: 504 KiB |
@ -1,56 +0,0 @@
|
||||
import asyncio
|
||||
import pandas as pd
|
||||
|
||||
from lightweight_charts import ChartAsync
|
||||
|
||||
|
||||
def get_bar_data(symbol, timeframe):
|
||||
return pd.read_csv(f'bar_data/{symbol}_{timeframe}.csv')
|
||||
|
||||
|
||||
class API:
|
||||
def __init__(self):
|
||||
self.chart = None # Changes after each callback.
|
||||
self.symbol = 'TSLA'
|
||||
self.timeframe = '5min'
|
||||
|
||||
async def on_search(self, searched_string): # Called when the user searches.
|
||||
self.symbol = searched_string
|
||||
new_data = await self.get_data()
|
||||
if new_data.empty:
|
||||
return
|
||||
self.chart.set(new_data)
|
||||
self.chart.corner_text(searched_string)
|
||||
|
||||
async def on_timeframe_selection(self, timeframe): # Called when the user changes the timeframe.
|
||||
self.timeframe = timeframe
|
||||
new_data = await self.get_data()
|
||||
if new_data.empty:
|
||||
return
|
||||
self.chart.set(new_data)
|
||||
|
||||
async def get_data(self):
|
||||
if self.symbol not in ('AAPL', 'GOOGL', 'TSLA'):
|
||||
print(f'No data for "{self.symbol}"')
|
||||
return pd.DataFrame()
|
||||
data = get_bar_data(self.symbol, self.timeframe)
|
||||
return data
|
||||
|
||||
|
||||
async def main():
|
||||
api = API()
|
||||
|
||||
chart = ChartAsync(api=api, debug=True)
|
||||
chart.legend(True)
|
||||
|
||||
chart.create_switcher(api.on_timeframe_selection, '1min', '5min', '30min', default='5min')
|
||||
chart.corner_text(api.symbol)
|
||||
|
||||
df = get_bar_data(api.symbol, api.timeframe)
|
||||
chart.set(df)
|
||||
|
||||
await chart.show(block=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
BIN
examples/6_callbacks/callbacks.gif
Normal file
|
After Width: | Height: | Size: 475 KiB |
48
examples/6_callbacks/callbacks.py
Normal file
@ -0,0 +1,48 @@
|
||||
import asyncio
|
||||
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 = None # Changes after each callback.
|
||||
|
||||
async def on_search(self, searched_string): # Called when the user searches.
|
||||
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)
|
||||
|
||||
async def on_timeframe_selection(self): # Called when the user changes the timeframe.
|
||||
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)
|
||||
|
||||
|
||||
async def main():
|
||||
api = API()
|
||||
|
||||
chart = Chart(api=api, topbar=True, searchbox=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)
|
||||
|
||||
await chart.show_async(block=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||