-
Notifications
You must be signed in to change notification settings - Fork 367
Description
I figured i will share some thoughts i have after trying PyBroker a bit.
Writing indicators could be more convenient i think. For example, in freqtrade it works like this:
Bot calls populate_indicators() that we implement and passes entire dataframe to it. There we can do things like this:
df['spread'] = df['high'] - df['low']
df['spread_sma20'] = ta.SMA(df['spread'], 20)
df['spread_sma40'] = ta.SMA(df['spread'], 40)This looks trivial on surface and of course is nothing PyBroker can not do, but actually this is very powerful.
To achieve something like this in PyBroker we would have to create a custom indicator functions for spread_sma20 and spread_sma40. But here we waste calculation of the spread column as it is done twice now.
It also is rather cumbersome to use indicator libraries like lib-ta or pandas_ta. These libraries already provide one-func-call indicators that we now must wrap in another function to acquaint them with PyBroker.
Normally i would just say "whatever, ill do it on symbol dataframe", however datasource.query() merges all symbols into one dataframe and thats the only place where it seems to make sense to insert custom indicators for backtesting.
What would be convenient
First of all it seems to me it would make more sense if data_source.query() returned a list of dataframes instead of one dataframe with all symbols. This dataframe need to be split anyway, besides merging dataframes of different symbols puts a burden on the user to make sure that dataframes of all queried symbols are of equal length and user must properly merge them in case there are missing candles. If everyone has to do it - might as well do it in the library.
Then, if dataframes were separate, we could also have a user-implemented indicators_fn(df) in the same spirit as exec_fn, which would allow massaging dataframe in any way we see necessary and utilizing all power of pandas.
This approach should be future-proof as well as adding support for multiple timeframes could be implemented as specifying indicator_fn for each timeframe. It should play well into live trading as well, since indicator_fn could be called once every new bar comes in.
Multi-symbol indicators
There is one special case where my proposed approach is not good enough: pairs trading. We need price data of two symbols in order to calculate necessary metrics. Maybe a way to get raw (just OHLC data, no indicators) symbol dataframe in indicator_fn could be an option. On same accord order entry for pairs trading is also bit unintuitive as entire process is split over two execute_fn iterations, but thats another topic..
Anyhow, by no means a request, just some food for thought and discussions. My proposition may have shortcomings that are unobvious to me.