def test_or(): target = mock.MagicMock() target.temp = {} #run on the 1/2/18 runOnDateAlgo = algos.RunOnDate(pd.to_datetime('2018-01-02')) runOnDateAlgo2 = algos.RunOnDate(pd.to_datetime('2018-01-03')) runOnDateAlgo3 = algos.RunOnDate(pd.to_datetime('2018-01-04')) runOnDateAlgo4 = algos.RunOnDate(pd.to_datetime('2018-01-04')) orAlgo = algos.Or( [runOnDateAlgo, runOnDateAlgo2, runOnDateAlgo3, runOnDateAlgo4]) #verify it returns false when neither is true target.now = pd.to_datetime('2018-01-01') assert not orAlgo(target) # verify it returns true when the first is true target.now = pd.to_datetime('2018-01-02') assert orAlgo(target) # verify it returns true when the second is true target.now = pd.to_datetime('2018-01-03') assert orAlgo(target) # verify it returns true when both algos return true target.now = pd.to_datetime('2018-01-04') assert orAlgo(target)
def test_run_on_date(): target = mock.MagicMock() target.now = pd.to_datetime('2010-01-01') algo = algos.RunOnDate('2010-01-01', '2010-01-02') assert algo(target) target.now = pd.to_datetime('2010-01-02') assert algo(target) target.now = pd.to_datetime('2010-01-03') assert not algo(target)
def bt_strategy(self, data, cats, graph_path): rsmpl = { 'days': 'B', 'weeks': 'W-Fri', 'months': 'BM', 'years': 'BY' }[self.roll_ptype] first_date = data.index[0] + relativedelta(**{self.est_ptype: self.est_plen}) run_dates = asfreq_actual(data[data.columns[0]], str(self.roll_plen) + rsmpl) run_dates = data.loc[run_dates.index] run_dates = run_dates.loc[run_dates.index > first_date] run_dates = run_dates.iloc[:-1] run_dates.loc[data.index[-1]] = data.iloc[-1] fee_func_parial = partial(fee_func, ff=self.fix_fee, pf=self.prc_fee) algo_stack = [ algos.RunOnDate(*run_dates.index.tolist()), algos.SelectAll(), SaveWeights(), WeightHRP(plen=self.est_plen, ptype=self.est_ptype, robust=self.robust, ddev=self.ddev, cats=cats, graph_path=graph_path), GapWeights(self.reb_gap), ] if self.is_tvol: algo_stack.append(WeightTargetVol(self.tvol, plen=self.est_plen, ptype=self.est_ptype, kmax=self.leverage)) algo_stack.extend([ WeightAdjust(self.leverage, self.weight_round), LimitWeights(self.min_weight, self.max_weight, self.leverage), WeightsToPerm(), CheckFeeBankrupt(fee_func_parial), algos.Rebalance() ]) strategy = bt.Strategy(self.name(), algo_stack) return bt.Backtest(strategy, data.copy(), initial_capital=self.init_balance, commissions=fee_func_parial, integer_positions=self.int_pos, progress_bar=True)