Refactoring/Enhancements/Fixes
Breaking Changes:
- Removed the `api` parameter; callbacks no longer need to be in a specific class.
- Topbar callbacks now take a chart as an argument (see updated callback examples)
- Removed the `topbar` parameter from chart declaration. The Topbar will be automatically created upon declaration of a topbar widget.
- Removed the `searchbox` parameter from charts. It will be created upon subscribing to it in `chart.events`.
- Removed `dynamic_loading`.
- Removed ‘volume_enabled’ parameter. Volume will be enabled if the volumn column is present in the dataframe.
- Widgets’ `func` parameter is now declared last.
- Switchers take a tuple of options rather than a variable number of arguments.
- `add_hotkey` renamed to `hotkey`
- Horizontal lines now take a `func` argument rather than `interactive`. This event will emit the Line object that was moved.
- Removed the `name` parameter from `line.set`. Line object names are now declared upon creation.
Enhancements:
- Added the `button` widget to the Topbar.
- Added the color picker to the drawing context menu.
- Charts now have a `candle_data` method, which returns the current data displayed on the chart as a DataFrame.
- Fixed callbacks are now located in the `chart.events` object:
- search (e.g `chart.events.search += on_search`)
- new_bar
- range_change
- Added volume to the legend
- Drawings can now be accessed through `chart.toolbox.drawings`
- added the `style` and `name` parameters to `create_line`
Bug Fixes:
- Fixed a bug causing new charts not to load after `exit` was called.
- Refactored rayline placement to ensure they do not move the visible range.
- Fixed a bug causing the visible range to shift when trendlines are moved past the final candlestick.
- Fixed a bug preventing trendlines and raylines on irregular timeframes.
- Fixed a bug causing the legend to prevent mouse input into the chart.
This commit is contained in:
@ -2,10 +2,9 @@ import pandas as pd
|
||||
from lightweight_charts import Chart
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
chart = Chart()
|
||||
|
||||
# Columns: | time | open | high | low | close | volume (if volume is enabled) |
|
||||
# Columns: time | open | high | low | close | volume
|
||||
df = pd.read_csv('ohlcv.csv')
|
||||
chart.set(df)
|
||||
|
||||
|
||||
@ -2,15 +2,14 @@ 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 | volume (if volume is enabled) |
|
||||
# Columns: time | price
|
||||
df2 = pd.read_csv('ticks.csv')
|
||||
|
||||
chart = Chart(volume_enabled=False)
|
||||
chart = Chart()
|
||||
|
||||
chart.set(df1)
|
||||
|
||||
@ -18,6 +17,4 @@ if __name__ == '__main__':
|
||||
|
||||
for i, tick in df2.iterrows():
|
||||
chart.update_from_tick(tick)
|
||||
|
||||
sleep(0.03)
|
||||
|
||||
|
||||
@ -2,26 +2,22 @@ import pandas as pd
|
||||
from lightweight_charts import Chart
|
||||
|
||||
|
||||
def calculate_sma(data: pd.DataFrame, period: int = 50):
|
||||
def avg(d: pd.DataFrame):
|
||||
return d['close'].mean()
|
||||
result = []
|
||||
for i in range(period - 1, len(data)):
|
||||
val = avg(data.iloc[i - period + 1:i])
|
||||
result.append({'time': data.iloc[i]['date'], f'SMA {period}': val})
|
||||
return pd.DataFrame(result)
|
||||
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(debug=True)
|
||||
chart = Chart()
|
||||
chart.legend(visible=True)
|
||||
|
||||
df = pd.read_csv('ohlcv.csv')
|
||||
chart.set(df)
|
||||
|
||||
line = chart.create_line()
|
||||
line = chart.create_line('SMA 50')
|
||||
sma_data = calculate_sma(df, period=50)
|
||||
line.set(sma_data, name='SMA 50')
|
||||
line.set(sma_data)
|
||||
|
||||
chart.show(block=True)
|
||||
|
||||
@ -9,39 +9,38 @@ def get_bar_data(symbol, timeframe):
|
||||
return pd.read_csv(f'bar_data/{symbol}_{timeframe}.csv')
|
||||
|
||||
|
||||
class API:
|
||||
def __init__(self):
|
||||
self.chart: Chart = None # Changes after each callback.
|
||||
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_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)
|
||||
|
||||
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, True)
|
||||
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(self, line_id, price):
|
||||
print(f'Horizontal line moved to: {price}')
|
||||
|
||||
def on_horizontal_line_move(chart, line):
|
||||
print(f'Horizontal line moved to: {line.price}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
api = API()
|
||||
|
||||
chart = Chart(api=api, topbar=True, searchbox=True, toolbox=True)
|
||||
chart = Chart(toolbox=True)
|
||||
chart.legend(True)
|
||||
|
||||
chart.events.search += on_search
|
||||
|
||||
chart.topbar.textbox('symbol', 'TSLA')
|
||||
chart.topbar.switcher('timeframe', api.on_timeframe_selection, '1min', '5min', '30min', default='5min')
|
||||
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, interactive=True)
|
||||
chart.horizontal_line(200, func=on_horizontal_line_move)
|
||||
|
||||
chart.show(block=True)
|
||||
chart.show(block=True)
|
||||
|
||||
Reference in New Issue
Block a user