But again I have ti construct indicator using bt.Indicator. Which is ok for one indicator, but I need to construct more than 300 indicators (features). Usually, using pd.DataFrame I just have to call one function which automatically produces all features... For example backetsting.py package has this option but it is really slow and have other problems.
Mislav Šagovac
@Mislav Šagovac
Posts made by Mislav Šagovac

RE: How to use pandas series with time index as in backtrader?

RE: How to use pandas series with time index as in backtrader?
That's the main problem. I have ML model that have hundred of inputs (say indicators) plus some variables like the one in the question.
If I have to define every indicator with backtrader indicator function I would need 10 times more time do define the indicators for backtrader than anything else in the pipeline.
I am afraid backtrader is not suitable for that kind of investing, when you have hundreds of inputs, sampling of bars when to trade etc. At least I didn't find solution after reading the documentation. The backtesting can be done through big pandas df which already contains all necessary columns but not sure for live trading.

How to use pandas series with time index as in backtrader?
I want to trade only on time (minute) when daily volatility is higher than some number. There is a nice function in mlfinlab package to calculate daily volatility:
https://github.com/hudsonandthames/mlfinlab/blob/master/mlfinlab/util/volatility.pyThe problem is that function has pd series as first argument where index is time (in my case every minute). Backtrader as default track close as 'lines'. It doesn't track time index. I ma not sure how can pass close with time index as argument to get_daily_vol function.
Here is the simple sample code:
import backtrader as bt import pandas as pd import mlfinlab as ml class RandomForestStrategy(bt.Strategy): def __init__(self): self.closes = np.array([]) self.daily_vol = None def next(self): close_ = data["SPY"].Close self.closes = np.append(self.closes, close_) self.daily_vol = ml.util.get_daily_vol( pd.Series(self.closes, # index=how t odefine this?, lookback=50)
Probably one solution is to somehow rewrite the original function from mlfinlab but I would like to original function if possible because I will use more functions from this repo.

RE: Use function that takes pandas series as argument i __init__ part
It seems to me that all backtesting framework I tried don't have some easy way to include ML models inside them. It's not rare that we need hundreds of variables in ML, based on OHLC data. I don't understand how to construct this data inside backtrader. I know I can write function for generating every indicator by hand, but that's probably the last option. I have function that generate fatures on padans df using talib package and some my own calculations. Nut I cant just call this function in backtrader.

Use function that takes pandas series as argument i __init__ part
Hello,
First, I want to say I read a plenty of docs (no all, there lot of stuff:)) and successfully implemented some simple strategies like buy if price is above VIX, vice versa.
Now I wanted to backtest my ML strategy with backtrader.
In nutshell, I have saved sklearn model in pkl format. I want to make predictions using this model every "event" and buy if the prediction is 1, and sell if 1. If position is already 1, than keep holding, vice versa.
Now I don't want to make predictions every minute. On contrary, I want to make prediction only when event happens. The event is calculated using CUSUM filter. In pandas data.frame world, I would calculate trading events using function from mlfinlab package:
# Compute volatility daily_vol = mlfinlab.util.get_daily_vol( close, lookback=self.volatility_lookback)
But to calculate daily volatility using this function we have to set pandas serires (close prices) as frist argument.
My first naive approach to implement this in backtrader was to define daily_vol as I would do that in pandas world:
class RandomForestStrategy(bt.Strategy): params = ( ('volatility_scaler', 1), ('volatility_lookback', 50) ) def start(self): # get started value self.val_start = self.broker.get_cash() # keep the starting cash self.log(f"Type: {type(self.datas[0].close)}") def log(self, txt, dt=None): ''' Logging function for this strategy''' dt = dt or self.datas[0].datetime.datetime(0) print(f'{dt.isoformat()}, {txt}') def __init__(self): # Keep a reference to the "close" line in the data[0] dataseries self.dataclose = self.datas[0].close # load ml model clf = joblib.load("C:/Users/Mislav/Documents/GitHub/trademl/trademl/modeling/rf_model.pkl") # Compute volatility get CUSUM events self.daily_vol = ml.util.get_daily_vol( self.datas[0].close, lookback=self.params.volatility_lookback) # self.cusum_events = ml.filters.cusum_filter( # self.dataclose, # threshold=self.daily_vol.mean()*self.params.volatility_scaler) # To keep track of pending orders and buy price/commission self.order = None self.buyprice = None self.buycomm = None
There is also next part but not important for now. Backtrader can calculate daily volatility because
self.datas[0].close
is line buffer object, not pandas series (expected).My question is, is it possible to somehow provide pandas series as argument to
ml.util.get_daily_vol
or I have to rewrite the original function from mlfinlab?