Files
strategy-lab/research/chartMultipleMarkers.ipynb

3.0 KiB

In [ ]:
import pandas as pd
import vectorbtpro as vbt
from lightweight_charts import chart, Panel

# Pulling ETH-USD data
data = vbt.YFData.pull("ETH-USD")
close = data.close
high = data.high
low = data.low

# Define a simple moving average crossover strategy using EWM
short_ma = vbt.MA.run(close, window=6, wtype="exp").ma
long_ma = vbt.MA.run(close, window=73, wtype="exp").ma

# Generate signals
long_entries = short_ma > long_ma
long_exits = short_ma < long_ma
short_entries = short_ma < long_ma
short_exits = short_ma > long_ma

clean_long_entries, clean_long_exits = long_entries.vbt.signals.clean(long_exits)
clean_short_entries, clean_short_exits = short_entries.vbt.signals.clean(short_exits)

# ohlcv_df = data.ohlcv.get()

#assume i want to display simple entries or exits on series or ohlcv 
#based on tuple positions it determines entries or exits (and set colors and shape accordingly)
pane1 = Panel(
    ohlcv=(data.ohlcv.get(), clean_long_entries, clean_short_entries)
)
ch = chart([pane1], title="Chart with Entry/Exit Markers", session=None, size="s")

#if you want to display more entries or exits, use tuples with their colors
pane1 = Panel(
    ohlcv=(data.ohlcv.get(),
           [(clean_long_entries, "yellow"), (clean_short_entries, "pink")], #list of entries tuples with color
           [(clean_long_exits, "yellow"), (clean_short_exits, "pink")] #list of exits tuples with color
           ), 
)

# # Create the chart with the panel
ch = chart([pane1], title="Chart with EntryShort/ExitShort (yellow) and EntryLong/ExitLong markers (pink)", sync=True, session=None, size="s")
In [ ]:
# # Add the markers to the chart using markers_set method
# entry_signals = pd.DataFrame({
#     'time': clean_long_entries.index.astype(str),
#     'value': clean_long_entries.values
# }).dropna()
# entry_signals['value'] = entry_signals['value'].astype(bool)

# ch.markers_set(entry_signals, type='entries')