Coder Social home page Coder Social logo

happydasch / btplotting Goto Github PK

View Code? Open in Web Editor NEW
307.0 15.0 76.0 919 KB

btplotting provides plotting for backtests, optimization results and live data from backtrader.

License: GNU General Public License v3.0

Python 97.76% JavaScript 0.49% Jinja 1.75%
backtrader backtrader-plotting live-plotting livedata backtest bokeh visualization plotting

btplotting's Introduction

btplotting

Library to add extended plotting capabilities to backtrader (https://www.backtrader.com/) using bokeh.

btplotting is based on the awesome backtrader_plotting (https://github.com/verybadsoldier/backtrader_plotting)

btplotting is a complete rework of backtrader_plotting with the live client in focus. Besides this, a lot of issues are fixed and new functionality is added. See the list below for differences.

What is different:

Basic:

  • No need for custom backtrader
  • Different naming / structure
  • Data alignment which allows to generate data for different data sources. This is useful when replaying or resampling data, for example to remove gaps.
  • Support for replay data
  • Different filtering of plot objects
  • Every figure has its own ColumnDataSource, so the live client can patch without having issues with nan values, every figure is updated individually
  • Plotting looks very similar to backtraders own plotting (order, heights, etc.)
  • Allows to generate custom columns, which don't have to be hardcoded.
  • This is being used to generate color for candles, varea values, etc.
  • Save images of strategy or a single data (for example save an image of data when a trade is happening)

Plotting:

  • Datas, Indicators, Observer and Volume have own aspect ratios, which can be configured in live client or scheme
  • Only one axis for volume will be added when using multiple data sources on one figure
  • Volume axis position is configureable in scheme, by default it is being plotted on the right side
  • Linked Crosshair across all figures
  • _skipnan, fill_gt, fill_lt, fill support
  • Plot objects can be filtered by one or more datanames or by plot group
  • Custom plot group, which can be configured in app or in live client by providing all plotids in a comma-separated list or by selecting the parts of the plot to display

Tabs:

  • Default tabs can be completely removed
  • New log panel to also include logging information
  • Can be extended with custom tabs (for example order execution with live client, custom analysis, etc.)

Live plotting:

(Live plotting is broken at the moment)

  • Navigation in live client (Pause, Backward, Forward)
  • Live plotting is done using an analyzer, so there is no need to use custom backtrader
  • Live plotting data update works in a single thread and is done by a DataHandler
  • Data update is being done every n seconds, which is configureable

Features

  • Interactive plots
  • Interactive backtrader optimization result browser (only supported for single-strategy runs)
  • Highly configurable
  • Different skinnable themes
  • Easy to use

Python >= 3.6 is required.

How to use

  • Add to cerebro as an analyzer:
from btplotting import BacktraderPlottingLive
  ...
  ...

cerebro = bt.Cerebro()
cerebro.addstrategy(MyStrategy)
cerebro.adddata(LiveDataStream())
cerebro.addanalyzer(BacktraderPlottingLive)
cerebro.run()
cerebro.plot()
  • If you need to change the default port or share the plotting to public:
cerebro.addanalyzer(BacktraderPlottingLive, address="*", port=8889)

Jupyter

In Jupyter you can plot to a single browser tab with iplot=False:

plot = btplotting.BacktraderPlotting()
cerebro.plot(plot, iplot=False)

You may encounters TypeError: <class '__main__.YourStrategyClass'> is a built-in class error.

To remove the source code tab use:

plot = btplotting.BacktraderPlotting()
plot.tabs.remove(btplotting.tabs.SourceTab)
cerebro.plot(plot, iplot=False)

Demos

https://happydasch.github.io/btplotting/

Installation

pip install git+https://github.com/happydasch/btplotting

Sponsoring

If you want to support the development of btplotting, consider to support this project.

btplotting's People

Contributors

delta575 avatar eltociear avatar gf-huang avatar happydasch avatar huuquynh avatar jhogendorn avatar jiaccopu-ford avatar konmeo avatar leiguorui avatar mircuz avatar raptoravis avatar sohailsomani avatar verybadsoldier avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

btplotting's Issues

Turn off debug messages from Bokeh

Firstly - what an absolutely great library! Does exactly what I was looking for and more.

One slight niggle is that I seem to see all the Bokeh Debug messages at the command line output. Any easy way to turn these off would be great. I had a go myself but failed.

500:Internal Server Error

When using btplotting for online data visualization, a 500: Internal Server Error error is reported, and the error code is as follows:

HTTPServerRequest(protocol='http', host='localhost:8888', method='GET', uri='/', version='HTTP/1.1', remote_ip='::1')
Traceback (most recent call last):
File "C:\Python\python3.8.9\lib\site-packages\tornado\web.py", line 1786, in _execute
result = await result
File "C:\Python\python3.8.9\lib\site-packages\bokeh-3.2.0.dev1-py3.8.egg\bokeh\server\views\doc_handler.py", line 54, in get
session = await self.get_session()
File "C:\Python\python3.8.9\lib\site-packages\bokeh-3.2.0.dev1-py3.8.egg\bokeh\server\views\session_handler.py", line 145, in get_session
session = await self.application_context.create_session_if_needed(session_id, self.request, token)
File "C:\Python\python3.8.9\lib\site-packages\bokeh-3.2.0.dev1-py3.8.egg\bokeh\server\contexts.py", line 242, in create_session_if_needed
self._application.initialize_document(doc)
File "C:\Python\python3.8.9\lib\site-packages\bokeh-3.2.0.dev1-py3.8.egg\bokeh\application\application.py", line 192, in initialize_document
h.modify_document(doc)
File "C:\Python\python3.8.9\lib\site-packages\bokeh-3.2.0.dev1-py3.8.egg\bokeh\application\handlers\function.py", line 142, in modify_document
self._func(doc)
File "C:\Python\python3.8.9\lib\site-packages\btplotting-0.2.1-py3.8.egg\btplotting\webapp.py", line 54, in make_document
model = self._model_factory_fnc(doc)
File "C:\Python\python3.8.9\lib\site-packages\btplotting-0.2.1-py3.8.egg\btplotting\analyzers\plot.py", line 64, in _app_cb_build_root_model
client = LiveClient(doc,
File "C:\Python\python3.8.9\lib\site-packages\btplotting-0.2.1-py3.8.egg\btplotting\live\client.py", line 55, in init
self.model, self._refresh_fnc = self._createmodel()
File "C:\Python\python3.8.9\lib\site-packages\btplotting-0.2.1-py3.8.egg\btplotting\live\client.py", line 177, in _createmodel
tabs = Tabs(
File "C:\Python\python3.8.9\lib\site-packages\bokeh-3.2.0.dev1-py3.8.egg\bokeh\model\model.py", line 97, in new
raise ValueError("'id' cannot be used together with property initializers")
ValueError: 'id' cannot be used together with property initializers

Tried python 3.8.9, 3.8.6, but it didn't work
Use btplotting version 0.2.1

How to set the default plot group?

How can we set what's the default value of this dropdown? Right now it defaults to the first dataset and I would want to see all the datas at once (Strategy option).

Screenshot from 2021-12-23 18-43-34

Got this warning:DataFrame is highly fragmented

I have 86 symbols which symbol has 8640 rows of kline data(using bt.feeds.PandasData)
When i run backtest, I got this warning:
C:\Users\63036\Anaconda3\lib\site-packages\btplotting\app.py:514: PerformanceWarning: DataFrame is highly fragmented. This is usually the result of calling frame.insertmany times, which has poor performance. Consider joining all columns at once using pd.concat(axis=1) instead. To get a de-fragmented frame, usenewframe = frame.copy()df[source_id] = new_line C:\Users\63036\Anaconda3\lib\site-packages\btplotting\app.py:524: PerformanceWarning: DataFrame is highly fragmented. This is usually the result of callingframe.insertmany times, which has poor performance. Consider joining all columns at once using pd.concat(axis=1) instead. To get a de-fragmented frame, usenewframe = frame.copy()df['index'] = indices C:\Users\63036\Anaconda3\lib\site-packages\btplotting\app.py:525: PerformanceWarning: DataFrame is highly fragmented. This is usually the result of callingframe.insertmany times, which has poor performance. Consider joining all columns at once using pd.concat(axis=1) instead. To get a de-fragmented frame, usenewframe = frame.copy()df['datetime'] = clkidx
image

ValueError: 'id' cannot be used together with property initializers in Live plotting.

Uncaught exception GET / (::1)
HTTPServerRequest(protocol='http', host='localhost:8065', method='GET', uri='/', version='HTTP/1.1', remote_ip='::1')
Traceback (most recent call last):
  File "D:\VScodeProjects\dangu\venv_310\lib\site-packages\btplotting\analyzers\plot.py", line 64, in _app_cb_build_root_model   
    client = LiveClient(doc,
  File "D:\VScodeProjects\dangu\venv_310\lib\site-packages\btplotting\live\client.py", line 55, in __init__
    self.model, self._refresh_fnc = self._createmodel()
  File "D:\VScodeProjects\dangu\venv_310\lib\site-packages\btplotting\live\client.py", line 177, in _createmodel
    tabs = Tabs(
  File "D:\VScodeProjects\dangu\venv_310\lib\site-packages\bokeh\model\model.py", line 97, in __new__
    raise ValueError("'id' cannot be used together with property initializers")
ValueError: 'id' cannot be used together with property initializers
500 GET / (::1) 150.34ms

Exception: Sorry, unsupported marker: "8". Please report to GitHub.`

Backtest successfully completes but won't plot.
How to reproduce

  1. Using ibstore
  2. Have multi data (2 is enough)
  3. Have strategy that uses the psar indicator
  4. Backtest or run live

Full stacktrace:
Traceback (most recent call last): File "C:\Users\Jacob\Desktop\Epicrise\cerebroHelper.py", line 262, in cerebro_helper_backtest self.cerebro.plot(p) File "C:\Users\Jacob\AppData\Roaming\Python\Python37\site-packages\backtrader\cerebro.py", line 991, in plot start=start, end=end, use=use) File "C:\Users\Jacob\AppData\Roaming\Python\Python37\site-packages\btplotting\app.py", line 567, in plot filter=filter) File "C:\Users\Jacob\AppData\Roaming\Python\Python37\site-packages\btplotting\app.py", line 318, in create_figurepage self._blueprint_strategy(figid, filter) File "C:\Users\Jacob\AppData\Roaming\Python\Python37\site-packages\btplotting\app.py", line 207, in _blueprint_strategy figure.plot(parent) File "C:\Users\Jacob\AppData\Roaming\Python\Python37\site-packages\btplotting\figure.py", line 649, in plot self.plot_observer(obj) File "C:\Users\Jacob\AppData\Roaming\Python\Python37\site-packages\btplotting\figure.py", line 828, in plot_observer self._plot_indicator_observer(obj) File "C:\Users\Jacob\AppData\Roaming\Python\Python37\site-packages\btplotting\figure.py", line 456, in _plot_indicator_observer + f' "{marker}". Please report to GitHub.') Exception: Sorry, unsupported marker: "8". Please report to GitHub.

Issue: float() argument must be a string or a number, not 'Timestamp'

Hi happydasch,
When I plot, I always have following issue:
image

Could you please help? Many thanks!

` data = store.getdata(
dataname='%s/%s' % (COIN_TARGET, COIN_REFER),
name='%s%s' % (COIN_TARGET, COIN_REFER),
timeframe=bt.TimeFrame.Minutes,
fromdate=hist_start_date,
compression=1,
ohlcv_limit=99999,
drop_newest = True,
nullvalue=0.0
)

    cerebro.adddata(data)
    cerebro.replaydata(data,timeframe=bt.TimeFrame.Minutes,compression=30,name='30_min')`

Live trading ploting got some issuse

Uncaught exception GET / (::1) HTTPServerRequest(protocol='http', host='localhost', method='GET', uri='/', version='HTTP/1.1', remote_ip='::1') Traceback (most recent call last): File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\tornado\web.py", line 1713, in _execute result = await result File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\bokeh\server\views\doc_handler.py", line 52, in get session = await self.get_session() File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\bokeh\server\views\session_handler.py", line 120, in get_session session = await self.application_context.create_session_if_needed(session_id, self.request, token) File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\bokeh\server\contexts.py", line 218, in create_session_if_needed self._application.initialize_document(doc) File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\bokeh\application\application.py", line 171, in initialize_document h.modify_document(doc) File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\bokeh\application\handlers\function.py", line 132, in modify_document self._func(doc) File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\btplotting\webapp.py", line 54, in make_document model = self._model_factory_fnc(doc) File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\btplotting\analyzers\plot.py", line 64, in _app_cb_build_root_model client = LiveClient(doc, File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\btplotting\live\client.py", line 56, in __init__ self.refreshmodel() File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\btplotting\live\client.py", line 255, in refreshmodel self._datahandler = LiveDataHandler(self) File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\btplotting\live\datahandler.py", line 22, in __init__ self._fill() File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\btplotting\live\datahandler.py", line 96, in _fill df = app.get_data(figid=figid, back=lookback) File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\btplotting\app.py", line 448, in get_data dt_idx = data_clock.get_dt_list(startidx, endidx) File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\btplotting\clock.py", line 214, in get_dt_list val = bt.num2date(val, tz=None if not localized else self._tz) File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\backtrader\utils\dateintern.py", line 165, in num2date ix = int(x) ValueError: cannot convert float NaN to integer
and
WARNING:bokeh.server.views.ws:Failed sending message as connection was closed Exception in callback functools.partial(<bound method IOLoop._discard_future_result of <tornado.platform.asyncio.AsyncIOMainLoop object at 0x000001578A5C6A00>>, <Task finished name='Task-10353' coro=<_needs_document_lock.<locals>._needs_document_lock_wrapper() done, defined at C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\bokeh\server\session.py:51> exception=KeyError('index')>) Traceback (most recent call last): File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\tornado\ioloop.py", line 740, in _run_callback ret = callback() File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\tornado\ioloop.py", line 764, in _discard_future_result future.result() File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\bokeh\server\session.py", line 71, in _needs_document_lock_wrapper result = await result File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\tornado\gen.py", line 216, in wrapper result = ctx_run(func, *args, **kwargs) File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\btplotting\live\datahandler.py", line 42, in _cb_push p_data, s_data = fp.get_cds_patchdata_from_series(idx, patch) File "C:\Users\Four\AppData\Local\Programs\Python\Python39\lib\site-packages\btplotting\cds.py", line 166, in get_cds_patchdata_from_series idx_map = {d: idx for idx, d in enumerate(self._cds.data['index'])} KeyError: 'index'
someone help me fix it

Run Live Socket Error

If I run a strategy live with the defaults and go to localhost:80, it works

self.cerebro.addanalyzer(BacktraderPlottingLive,barup='green')#, barup='green', volume=False, lookback=200, address="*", port=8889)

However if I want to change the port and work on any address, it doesn't. Work before the address argument was added.

self.cerebro.addanalyzer(BacktraderPlottingLive,barup='green',volume=False, address="*", port=5001)

I get error

Open browser at: http://localhost:5001
INFO:bokeh.server.server:Starting Bokeh server version 2.3.0 (running on Tornado 6.1)
Exception in thread Thread-13:
Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\threading.py", line 926, in _bootstrap_inner
    self.run()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\btplotting\analyzers\plot.py", line 59, in _t_server
    self._webapp.start(loop)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\btplotting\webapp.py", line 53, in start
    address=self._address, autostart=self._autostart)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\btplotting\webapp.py", line 82, in _run_server
    server = Server(apps, address=address, port=port, allow_websocket_origin=[address], io_loop=ioloop)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\bokeh\server\server.py", line 395, in __init__
    sockets, self._port = bind_sockets(opts.address, opts.port)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\bokeh\server\util.py", line 60, in bind_sockets
    ss = netutil.bind_sockets(port=port or 0, address=address)
  File "C:\Users\Jacob\AppData\Roaming\Python\Python37\site-packages\tornado\netutil.py", line 104, in bind_sockets
    socket.getaddrinfo(address, port, family, socket.SOCK_STREAM, 0, flags),
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\socket.py", line 752, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

Chat scaling seems to have a bug

New user, not sure what went wrong.

But by comparing below chart
Screenshot 2023-11-23 at 2 29 56 AM

versus the orignal from backtrader

Screenshot 2023-11-23 at 2 30 54 AM

Something regarding the chart scaling is having a problem.

Repo steps:
repo.zip

unzip repo.zip
cd repo
./source.py --plot

TypeError: unhashable type: 'slice'

This is The Error I get when running ( my pandas is 1.4.2 ):

Traceback (most recent call last):
File "/data/anaconda3/envs/forex/lib/python3.8/site-packages/pandas/core/frame.py", line 3868, in _set_value
series = self._get_item_cache(col)
File "/data/anaconda3/envs/forex/lib/python3.8/site-packages/pandas/core/frame.py", line 3926, in _get_item_cache
res = cache.get(item)
TypeError: unhashable type: 'slice'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/data/anaconda3/envs/forex/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/data/anaconda3/envs/forex/lib/python3.8/site-packages/btplotting/live/datahandler.py", line 222, in _t_thread
self._process(data)
File "/data/anaconda3/envs/forex/lib/python3.8/site-packages/btplotting/live/datahandler.py", line 191, in _process
self._set_data(row, ds_idx)
File "/data/anaconda3/envs/forex/lib/python3.8/site-packages/btplotting/live/datahandler.py", line 79, in _set_data
self._datastore.at[idx] = data
File "/data/anaconda3/envs/forex/lib/python3.8/site-packages/pandas/core/indexing.py", line 2281, in setitem
return super().setitem(key, value)
File "/data/anaconda3/envs/forex/lib/python3.8/site-packages/pandas/core/indexing.py", line 2236, in setitem
self.obj._set_value(*key, value=value, takeable=self._takeable)
File "/data/anaconda3/envs/forex/lib/python3.8/site-packages/pandas/core/frame.py", line 3881, in _set_value
self._item_cache.pop(col, None)
TypeError: unhashable type: 'slice'
Exception in thread Thread-11:
Traceback (most recent call last):
File "/data/anaconda3/envs/forex/lib/python3.8/site-packages/pandas/core/frame.py", line 3868, in _set_value
series = self._get_item_cache(col)
File "/data/anaconda3/envs/forex/lib/python3.8/site-packages/pandas/core/frame.py", line 3926, in _get_item_cache
res = cache.get(item)
TypeError: unhashable type: 'slice'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/data/anaconda3/envs/forex/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/data/anaconda3/envs/forex/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/data/anaconda3/envs/forex/lib/python3.8/site-packages/btplotting/live/datahandler.py", line 222, in _t_thread
self._process(data)
File "/data/anaconda3/envs/forex/lib/python3.8/site-packages/btplotting/live/datahandler.py", line 191, in _process
self._set_data(row, ds_idx)
File "/data/anaconda3/envs/forex/lib/python3.8/site-packages/btplotting/live/datahandler.py", line 79, in _set_data
self._datastore.at[idx] = data
File "/data/anaconda3/envs/forex/lib/python3.8/site-packages/pandas/core/indexing.py", line 2281, in setitem
return super().setitem(key, value)
File "/data/anaconda3/envs/forex/lib/python3.8/site-packages/pandas/core/indexing.py", line 2236, in setitem
self.obj._set_value(*key, value=value, takeable=self._takeable)
File "/data/anaconda3/envs/forex/lib/python3.8/site-packages/pandas/core/frame.py", line 3881, in _set_value
self._item_cache.pop(col, None)
TypeError: unhashable type: 'slice'

Replacing Bokeh with `pyecharts` for better performance

Reason

Web pages are very slow when zooming with long bars (greater than 3000).

I use pyecharts to draw the same bars, it's smooth.

Thanks

I would like to implement it, but I don't understand how it works yet.

Finally, thank you for your selfless dedication. Hope to hear from you.

Thank you

This is a great project btw and the work appreciated.

Custom indicator - lines connecting candles dont show up (eg zigzag indicator lines)

I am trying to plot a custom indicator. The lines dont show up.. For eg, can you try plotting this zigzag indicator coded by the creator of backtrader (https://community.backtrader.com/topic/773/zigzag-indicator). You will see that the lines show up intermittently. If you plot using the original backtrader plot, they work as expected
Here is the screenshot
Inkedbtplotting_LI
If you see above, only in few places do you see the zigzag lines.. But look at the original plot below.. THis is how it should be
Here is the original plot
Original_plot_backtrader

I also tried drawing horizontal lines.. They work perfectly.. So the issue is specific to bars connecting highs and lows
btplotting_horizontal_lines

Please check the requirement packages

Hi @happydasch ,

After make a fresh install computer. I got the error when plotting live, and stuck in find what is wrong. The plotting live got the error on browser console:

[bokeh]: could not set init ranges

and result is nothing plotting at all.

It throws an exception also:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/local/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/hq/.venv/lib/python3.8/site-packages/btplotting/live/datahandler.py", line 217, in _t_thread
    data = self._app.generate_data(
  File "/home/hq/.venv/lib/python3.8/site-packages/btplotting/app.py", line 451, in generate_data
    clock_values[smallest] = generator.get_clock(
  File "/home/hq/.venv/lib/python3.8/site-packages/btplotting/clock.py", line 91, in get_clock
    arr = get_slice_with_end(arr, start, end)
  File "/home/hq/.venv/lib/python3.8/site-packages/btplotting/clock.py", line 14, in get_slice_with_end
    slice = list(data)[start:end] + [data[end]]
TypeError: slice indices must be integers or None or have an __index__ method

The steps to make install as:

  1. Install backtrader
  2. Install btplotting
  3. Install another packages: websockets, TA-Lib, requests, schedule, ujson

Here is my pip freeze:

backtrader==1.9.76.123
bokeh==2.4.3
btplotting @ git+https://github.com/happydasch/btplotting@512780f8ab71395fef3667e872deca3a8a2f2e9a
certifi==2022.6.15
charset-normalizer==2.1.0
cycler==0.11.0
fonttools==4.33.3
idna==3.3
Jinja2==3.1.2
kiwisolver==1.4.3
MarkupSafe==2.1.1
matplotlib==3.5.2
numpy==1.23.0
packaging==21.3
pandas==1.4.3
Pillow==9.2.0
pyparsing==3.0.9
python-dateutil==2.8.2
pytz==2022.1
PyYAML==6.0
requests==2.28.1
schedule==1.1.0
six==1.16.0
TA-Lib==0.4.24
tornado==6.2
typing_extensions==4.3.0
ujson==5.4.0
urllib3==1.26.9
websockets==10.3

Customize webapp port

Is it possible to customize the port number used for the web app? Currently it seems to be hardcoded to port 80.

I've tried:
BacktraderPlottingLive.http_port=8080

ctype does not match ColumnDataType

Hi, I always receive an Unsupported ColumnDataType triggered by the _get_formatter() function within datable.py.
I've fixed moving from the old definition to something like ctype.name == ColummDataType.XXX.name.

I hope this could help you

Best regards

Problem with toolbox, the toolbar is not displayed on the chart

Hi,
in my strategy code I had a long line of code, something like this:
if self.position.size > 0 and self.position.size > 0 and self.position.size > 0 and self.position.size > 0 and self.position.size > 0 and self.position.size > 0 and self.position.size > 0 and self.position.size > 0 and self.position.size > 0 and self.position.size > 0 and self.position.size > 0 and self.position.size > 0 and self.position.size > 0 and self.position.size > 0 and
and due this tabs "source code" just crashes all html file
Screenshot_2

after i styled my strategy code i got the correct display of the toolbar. BINGO :)

Unable to run at all

Traceback (most recent call last):
File "main.py", line 19, in
from btplotting import BacktraderPlotting
File "/home/user/PycharmProjects/backtrader/venv/lib/python3.8/site-packages/btplotting/init.py", line 2, in
from .analyzers import LivePlotAnalyzer as BacktraderPlottingLive
File "/home/user/PycharmProjects/backtrader/venv/lib/python3.8/site-packages/btplotting/analyzers/init.py", line 1, in
from .plot import LivePlotAnalyzer
File "/home/user/PycharmProjects/backtrader/venv/lib/python3.8/site-packages/btplotting/analyzers/plot.py", line 13, in
from ..live.client import LiveClient
ModuleNotFoundError: No module named 'btplotting.live'

'DivByZero' object has no attribute 'size'

我自定义了一个指标

VAR1 / VAR2

    dpv = bt.DivByZero(self.data.pd1, self.data.pd2, 999999999)
    self.data.dpv = dpv

使用如上方式计算
当我启动时会报如下错误:
Traceback (most recent call last):
File "D:/pythonProject/test/ztbQuant.py", line 183, in
cerebro.plot(bh)
File "C:\Users\being\anaconda3\lib\site-packages\backtrader\cerebro.py", line 989, in plot
rfig = plotter.plot(strat, figid=si * 100,
File "C:\Users\being\anaconda3\lib\site-packages\backtrader_plotting\bokeh\bokeh.py", line 511, in plot
df: pd.DataFrame = self.build_strategy_data(obj, start, end)
File "C:\Users\being\anaconda3\lib\site-packages\backtrader_plotting\bokeh\bokeh.py", line 473, in build_strategy_data
for lineidx in range(obj.size()):
AttributeError: 'DivByZero' object has no attribute 'size'

Graphs oversized

I'm running a backtest with a period of 10 years and the graphs are just enormous.
How to resize/scale them properly ?

Y-axis auto scaling

Hello. Firstly, thanks for bringing such an amazing library to plot the graph!

I am currently using it to plot the live data.
However, the bar size looks very small in the graph. I think it's because the y-axis starts at 0.
I wonder is there any param I can set such that the y-axis can be auto-scaled?

image

Thank you in advance.

report error when I run `https://github.com/happydasch/btplotting/blob/master/demos/optimization.py`

report error when I run https://github.com/happydasch/btplotting/blob/master/demos/optimization.py

C:\Python311\Lib\site-packages\btplotting\clock.py:316: FutureWarning: Series.fillna with 'method' is deprecated and will raise in a future version. Use obj.ffill() or obj.bfill() instead.
  df[name] = df[name].fillna(method='ffill')
C:\Python311\Lib\site-packages\btplotting\clock.py:316: FutureWarning: Series.fillna with 'method' is deprecated and will raise in a future version. Use obj.ffill() or obj.bfill() instead.
  df[name] = df[name].fillna(method='ffill')
C:\Python311\Lib\site-packages\btplotting\clock.py:316: FutureWarning: Series.fillna with 'method' is deprecated and will raise in a future version. Use obj.ffill() or obj.bfill() instead.
  df[name] = df[name].fillna(method='ffill')
C:\Python311\Lib\site-packages\btplotting\clock.py:316: FutureWarning: Series.fillna with 'method' is deprecated and will raise in a future version. Use obj.ffill() or obj.bfill() instead.
  df[name] = df[name].fillna(method='ffill')
C:\Python311\Lib\site-packages\btplotting\clock.py:316: FutureWarning: Series.fillna with 'method' is deprecated and will raise in a future version. Use obj.ffill() or obj.bfill() instead.
  df[name] = df[name].fillna(method='ffill')
C:\Python311\Lib\site-packages\btplotting\clock.py:316: FutureWarning: Series.fillna with 'method' is deprecated and will raise in a future version. Use obj.ffill() or obj.bfill() instead.
  df[name] = df[name].fillna(method='ffill')
C:\Python311\Lib\site-packages\btplotting\clock.py:316: FutureWarning: Series.fillna with 'method' is deprecated and will raise in a future version. Use obj.ffill() or obj.bfill() instead.
  df[name] = df[name].fillna(method='ffill')
Uncaught exception GET / (::1)
HTTPServerRequest(protocol='http', host='localhost:81', method='GET', uri='/', version='HTTP/1.1', remote_ip='::1')
Traceback (most recent call last):
  File "C:\Python311\Lib\site-packages\tornado\web.py", line 1786, in _execute
    result = await result
             ^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\bokeh\server\views\doc_handler.py", line 54, in get
    session = await self.get_session()
              ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\bokeh\server\views\session_handler.py", line 145, in get_session
    session = await self.application_context.create_session_if_needed(session_id, self.request, token)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\bokeh\server\contexts.py", line 240, in create_session_if_needed
    self._application.initialize_document(doc)
  File "C:\Python311\Lib\site-packages\bokeh\application\application.py", line 190, in initialize_document
    h.modify_document(doc)
  File "C:\Python311\Lib\site-packages\bokeh\application\handlers\function.py", line 140, in modify_document
    self._func(doc)
  File "C:\Python311\Lib\site-packages\btplotting\webapp.py", line 54, in make_document
    model = self._model_factory_fnc(doc)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\btplotting\optbrowser.py", line 130, in build_optresult_model
    [selector, _get_model(selector_cds, 0)],
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\btplotting\optbrowser.py", line 103, in _get_model
    return self._app.plot_optmodel(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\btplotting\app.py", line 511, in plot_optmodel
    return self.generate_bokeh_model(0)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\btplotting\app.py", line 343, in generate_bokeh_model
    tab_panels = self.generate_bokeh_model_tab_panels()
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\btplotting\app.py", line 413, in generate_bokeh_model_tab_panels
    g = gridplot(plot_figures,
        ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\bokeh\layouts.py", line 333, in gridplot
    toolbar = Toolbar(
              ^^^^^^^^
TypeError: bokeh.models.tools.Toolbar() got multiple values for keyword argument 'logo'
500 GET / (::1) 715.09ms

Error on subplot on indicator plotting

Hello,

I add a subplot on indicator plotting, but it always plotting on master windows - price and volume, not on the indicator windows.

For example, the fisherema is based on fisher, but fisherema is plotting on master windows, not fisher indicator windows.

`
self.fisher = Fisher(period=self.params.FISHER)

    self.fisherema = bt.indicators.ExponentialMovingAverage(self.fisher, period=3)
    self.fisherema.plotinfo.plotmaster = self.fisher

`
Is there any wrong here on my code?

How to plot bar changing with tick changes?

Hi,
My trade strategy used 30min BNB data to send signal, and with Tick level data for trailing.
With your great btplotting tool, I am able to do live trading plotting ( together with bt-ccxt-store), but my issue is the 30min bar is not changing according to the receiving of tick data. It receives the 30min bar data for once, and then no changes... ( please see attached pic)
image

here is the data I use:

hist_start_date = datetime.datetime.utcnow() - datetime.timedelta(hours=249)
data = store.getdata(
    dataname='BNB/USDT',
    name="BNB/USDT",
    dtformat=('%Y-%m-%d %H:%M:%S.%f'), 
    timeframe=bt.TimeFrame.Ticks,
    fromdate=hist_start_date,
    #ohlcv_limit=99999,
    nullvalue=0.0
)
tframes = dict(
    ticks=bt.TimeFrame.Ticks,
    microseconds=bt.TimeFrame.MicroSeconds,
    seconds=bt.TimeFrame.Seconds,
    minutes=bt.TimeFrame.Minutes,
    days=bt.TimeFrame.Days,
    weekly=bt.TimeFrame.Weeks,
    monthly=bt.TimeFrame.Months
)

cerebro.resampledata(
    data,
    timeframe=tframes['minutes'],
    compression=30
) #把行情数据对象注入引擎

So my question is can btplotting modifying the 30min bar as long as it receives tick data? Many thanks!

Getting an error trying to plot the PSAR indicator

Trying to plot the built-in indicator from backtrader PSAR and getting an error.

self.psar[i] = bt.ind.PSAR(self.datas[i],period=2,af=0.02,afmax=0.2) # parabolic sar

Error : `Exception: Sorry, unsupported marker: ".". Please report to GitHub.``

StackTrace :

File "C:\Users\Jacob\AppData\Roaming\Python\Python37\site-packages\btplotting\app.py", line 567, in plot
filter=filter)
File "C:\Users\Jacob\AppData\Roaming\Python\Python37\site-packages\btplotting\app.py", line 318, in create_figurepage
self._blueprint_strategy(figid, filter)
File "C:\Users\Jacob\AppData\Roaming\Python\Python37\site-packages\btplotting\app.py", line 209, in _blueprint_strategy
figure.plot(c)
File "C:\Users\Jacob\AppData\Roaming\Python\Python37\site-packages\btplotting\figure.py", line 671, in plot
self.plot_indicator(obj)
File "C:\Users\Jacob\AppData\Roaming\Python\Python37\site-packages\btplotting\figure.py", line 855, in plot_indicator
self._plot_indicator_observer(obj)
File "C:\Users\Jacob\AppData\Roaming\Python\Python37\site-packages\btplotting\figure.py", line 477, in _plot_indicator_observer
+ f' "{marker}". Please report to GitHub.')
Exception: Sorry, unsupported marker: ".". Please report to GitHub.

BacktraderPlottingOptBrowser doesn't work

Running the simple example from the deoms (demos/optimization.py, just changes the port), produces the following error:

Open browser at: http://localhost:8087
ERROR:tornado.application:Uncaught exception GET / (::1)
HTTPServerRequest(protocol='http', host='localhost:8087', method='GET', uri='/', version='HTTP/1.1', remote_ip='::1')
Traceback (most recent call last):
  File "...\AppData\Local\Programs\Python\Python37\lib\site-packages\tornado\web.py", line 1592, in _execute
    result = yield result
  File "...\AppData\Local\Programs\Python\Python37\lib\site-packages\tornado\gen.py", line 1133, in run
    value = future.result()
  File "...\AppData\Local\Programs\Python\Python37\lib\site-packages\bokeh\server\views\doc_handler.py", line 52, in get
    session = await self.get_session()
  File "...\AppData\Local\Programs\Python\Python37\lib\site-packages\bokeh\server\views\session_handler.py", line 120, in get_session
    session = await self.application_context.create_session_if_needed(session_id, self.request, token)
  File "...\AppData\Local\Programs\Python\Python37\lib\site-packages\bokeh\server\contexts.py", line 218, in create_session_if_needed
    self._application.initialize_document(doc)
  File "...\AppData\Local\Programs\Python\Python37\lib\site-packages\bokeh\application\application.py", line 171, in initialize_document
    h.modify_document(doc)
  File "...\AppData\Local\Programs\Python\Python37\lib\site-packages\bokeh\application\handlers\function.py", line 132, in modify_document
    self._func(doc)
  File "...\AppData\Local\Programs\Python\Python37\lib\site-packages\btplotting\webapp.py", line 49, in make_document
    model = self._model_factory_fnc(doc)
  File "...\AppData\Local\Programs\Python\Python37\lib\site-packages\btplotting\optbrowser.py", line 113, in build_optresult_model
    [selector, _get_model(selector_cds, 0)],
  File "...\AppData\Local\Programs\Python\Python37\lib\site-packages\btplotting\optbrowser.py", line 99, in _get_model
    self._optresults[selected][0])
  File "...\AppData\Local\Programs\Python\Python37\lib\site-packages\btplotting\app.py", line 543, in plot_optmodel
    return self.generate_model(0)
  File "...\AppData\Local\Programs\Python\Python37\lib\site-packages\btplotting\app.py", line 357, in generate_model
    panels.append(tab.get_panel())
  File "...\AppData\Local\Programs\Python\Python37\lib\site-packages\btplotting\tab.py", line 35, in get_panel
    child, title = self._get_panel()
  File "...\AppData\Local\Programs\Python\Python37\lib\site-packages\btplotting\tabs\metadata.py", line 138, in _get_panel
    self.content = self._create_content()
  File "...\AppData\Local\Programs\Python\Python37\lib\site-packages\btplotting\tabs\metadata.py", line 132, in _create_content
    [self._get_metadata_info()]
  File "...\AppData\Local\Programs\Python\Python37\lib\site-packages\btplotting\tabs\metadata.py", line 105, in _get_metadata_info
    acolumns = self._get_metadata_columns(self._figurepage.strategy)
  File "...\AppData\Local\Programs\Python\Python37\lib\site-packages\btplotting\tabs\metadata.py", line 99, in _get_metadata_columns
    acolumns.extend(self._get_strategy(strategy))
  File "...\AppData\Local\Programs\Python\Python37\lib\site-packages\btplotting\tabs\metadata.py", line 50, in _get_strategy
    childs.append(self._get_title(f'Strategy: {obj2label(strategy)}'))
  File "...\AppData\Local\Programs\Python\Python37\lib\site-packages\btplotting\helper\label.py", line 25, in obj2label
    raise RuntimeError(f'Unsupported type: {obj.__class__}')
RuntimeError: Unsupported type: <class 'NoneType'>
ERROR:tornado.access:500 GET / (::1) 171.54ms

When trying to set optreturn=False I get:

ERROR:bokeh.server.views.ws:Refusing websocket connection from Origin 'http://localhost:8081';                       use --allow-websocket-origin=localhost:8081 or set BOKEH_ALLOW_WS_ORIGIN=localhost:8081 to permit this; currently we allow origins {'localhost:80'}
WARNING:tornado.access:403 GET /ws (::1) 1.00ms

minutes level plotting issue

When I use 1 minute timeframe of 3 month data, the plotting result is not very responsive on the web page. The page is very lagging, and often took a long time to load the page. is there any way to improve the performance? i think 3 month data shall not be considerred large for plotting.

the candle is not updated in live plotting

The live plotting is not worked fine.
It's easy to reproduce. just run in "demos/date_live.py" or "demos/data_multi_live.py".
6~7 months ago, it worked. I don't know why....

when changing the filter data, the layout is totally wrong

image

when changing the filter data, the layout is totally wrong.
in the picture above, I added the secondary data feed and when I tried to see the secondary data feed.

I am looking at it but, it too many things are unclear..., if any clue?

Is there anyway to customize plotting to use tick data?

Hello,

This is not an issue at all, and maybe I should ask Backtrader's owner, but I think Daniel is not focus on plotting, he built the system. And you are a special one on plotting when fork to bokeh and make it more beautiful and useful when backtest or live trade.

The idea is when we got the tick data, could plotting it as footprint bar chart? If you don't have time, please talk to me the right way to implement this.

Thank you very much!

The buy/sell arrows are so far away from the candlesticks

The buy/sell arrows are so far away from the candlesticks.
This is different from the demo where the buy/sell arrows are directly above or below the candlesticks.
Is there anything I can do to get arrows style same as in demo? Thanks

data-live demo got "ZeroDivisionError: float division by zero"

I tried to use live demo (both 2 samples), and it runs for a short while (2-3 minutes), then this error happens.

I don't think that this error is from indicators, but the debug message looks like relatived indicator issues. I wonder because the sample runs with mature indicators, not a new one.

Traceback (most recent call last): File "/home/bt-learning/data-live.py", line 101, in <module> cerebro, strat = _run_resampler(data_timeframe=bt.TimeFrame.Ticks, File "/home/bt-learning/data-live.py", line 94, in _run_resampler res = cerebro.run() File "/home/bt-learning/venv/lib/python3.8/site-packages/backtrader/cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "/home/bt-learning/venv/lib/python3.8/site-packages/backtrader/cerebro.py", line 1298, in runstrategies self._runnext(runstrats) File "/home/bt-learning/venv/lib/python3.8/site-packages/backtrader/cerebro.py", line 1630, in _runnext strat._next() File "/home/bt-learning/venv/lib/python3.8/site-packages/backtrader/strategy.py", line 347, in _next super(Strategy, self)._next() File "/home/bt-learning/venv/lib/python3.8/site-packages/backtrader/lineiterator.py", line 263, in _next indicator._next() File "/home/bt-learning/venv/lib/python3.8/site-packages/backtrader/lineiterator.py", line 263, in _next indicator._next() File "/home/bt-learning/venv/lib/python3.8/site-packages/backtrader/linebuffer.py", line 621, in _next self.nextstart() File "/home/bt-learning/venv/lib/python3.8/site-packages/backtrader/lineroot.py", line 144, in nextstart self.next() File "/home/bt-learning/venv/lib/python3.8/site-packages/backtrader/linebuffer.py", line 744, in next self[0] = self.operation(self.a[0], self.b[0])

Or is there any misunderstood here?

Thank you very much!

Missing file, unable to run

Actually, the whole folder "templates" is missing.

Traceback (most recent call last):
File "main.py", line 494, in
start_test()
File "main.py", line 470, in start_test
cerebro.plot(p)
File "/home/user/PycharmProjects/backtrader/venv/lib/python3.8/site-packages/backtrader/cerebro.py", line 989, in plot
rfig = plotter.plot(strat, figid=si * 100,
File "/home/user/PycharmProjects/backtrader/venv/lib/python3.8/site-packages/btplotting-0.1.0-py3.8.egg/btplotting/app.py", line 563, in plot
self.create_figurepage(
File "/home/user/PycharmProjects/backtrader/venv/lib/python3.8/site-packages/btplotting-0.1.0-py3.8.egg/btplotting/app.py", line 319, in create_figurepage
self._blueprint_strategy(figid, filter)
File "/home/user/PycharmProjects/backtrader/venv/lib/python3.8/site-packages/btplotting-0.1.0-py3.8.egg/btplotting/app.py", line 203, in _blueprint_strategy
figure = Figure(
File "/home/user/PycharmProjects/backtrader/venv/lib/python3.8/site-packages/btplotting-0.1.0-py3.8.egg/btplotting/figure.py", line 245, in init
self._init_figure()
File "/home/user/PycharmProjects/backtrader/venv/lib/python3.8/site-packages/btplotting-0.1.0-py3.8.egg/btplotting/figure.py", line 335, in _init_figure
formatter_code = pkgutil.get_data(
File "/usr/lib/python3.8/pkgutil.py", line 637, in get_data
return loader.get_data(resource_name)
File "", line 972, in get_data
FileNotFoundError: [Errno 2] No such file or directory: '/home/user/PycharmProjects/backtrader/venv/lib/python3.8/site-packages/btplotting-0.1.0-py3.8.egg/btplotting/templates/js/tick_formatter.js'

500: Internal Server Error

I keep getting this error. "500: Internal Server Error"

I have done installing btplotting and calling it using:
cerebro.addanalyzer(BacktraderPlottingLive, address="*", port=7692)
I've also tried different port numbers
also googling didnt help.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.