// algo to set the weights
// it will only run when runMonthlyAlgo returns true
// which only happens on the first of every month
weights = pd.Series([0.6, 0.4], index=rdf.columns)
weighSpecifiedAlgo = bt.algos.WeighSpecified(**weights)
// algo to rebalance the current weights to weights set by weighSpecified
// will only run when weighSpecifiedAlgo returns true
After Change
progress_bar=False,
additional_data = {"mydata":pdf}
)
bidoffer = 1.
backtest2 = bt.Backtest(
strat,
pdf,
initial_capital = 0,
integer_positions=False,
progress_bar=False,
additional_data = {"mydata":pdf,
"bidoffer": pd.DataFrame( bidoffer, pdf.index, pdf.columns )}
)
random.seed(1234)
res = bt.run(backtest)
random.seed(1234)
res2 = bt.run(backtest2)
assert(type(res.get_security_weights()) is pd.DataFrame)
assert (type(res.get_transactions()) is pd.DataFrame)
assert len(res.get_transactions()) > 0
assert (type(res.get_weights()) is pd.DataFrame)
// Make sure the insertion of the first row applies to additional data as well
assert backtest.data.index.equals( backtest.additional_data["mydata"].index )
// Check that bid/offer is accounted for
transactions = res.get_transactions()
transactions["price"] = transactions["price"] + 0.5 * bidoffer
assert (res2.get_transactions().price - res2.get_transactions().price).abs().sum() == 0