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,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)
|
||||
|
||||
Reference in New Issue
Block a user