def test_in_range(self): crit = criteria.InRange(str(self.one), 10, 20) ret = crit.apply(self.data.head(2)) self.assertFalse(ret) ret = crit.apply(self.data.head(3)) self.assertTrue(ret) ret = crit.apply(self.data.head(4)) self.assertTrue(ret) ret = crit.apply(self.data.head(5)) self.assertTrue(ret) ret = crit.apply(self.data.head(6)) self.assertFalse(ret) crit = criteria.InRange(str(self.one), str(self.two), str(self.three)) ret = crit.apply(self.data.head(1)) self.assertFalse(ret) ret = crit.apply(self.data.head(2)) self.assertTrue(ret) ret = crit.apply(self.data.head(3)) self.assertFalse(ret) crit = criteria.InRange(str(self.one), -1, 1) ret = crit.apply(self.data.head(1)) self.assertTrue(ret) crit = criteria.InRange(str(self.two), 4, str(self.three)) ret = crit.apply(self.data.head(1)) self.assertTrue(ret) crit = criteria.InRange(str(self.two), str(self.one), 6) ret = crit.apply(self.data.head(1)) self.assertTrue(ret)
def test_simple_ti_crit_strategy(self): sma2 = technical_indicator.SMA(self.symbol.close, 2) sma3 = technical_indicator.SMA(self.symbol.close, 3) self.d.add_technical_indicator(sma2) self.d.add_technical_indicator(sma3) enter_crit1 = criteria.Above(sma2, sma3) enter_crit2 = criteria.Below(sma3, sma2) enter_crit3 = criteria.InRange(sma2, 25, 26) enter_crit4 = criteria.CrossingAbove(sma2, sma3) enter_crit5 = criteria.CrossingBelow(sma2, sma3) exit_crit1 = criteria.BarsSinceLong(self.symbol, 2) exit_crit2 = criteria.Equals(sma2, sma3) enter_crit_group1 = criteria_group.CriteriaGroup( [enter_crit1, enter_crit2], Long(), self.symbol) enter_crit_group2 = criteria_group.CriteriaGroup( [enter_crit1, enter_crit2], Short(), self.symbol) enter_crit_group3 = criteria_group.CriteriaGroup( [enter_crit3, enter_crit4, enter_crit5], Long(), self.symbol) exit_crit_group1 = criteria_group.CriteriaGroup([exit_crit1], LongExit(), self.symbol) exit_crit_group2 = criteria_group.CriteriaGroup([exit_crit2], LongExit(), self.symbol) tp = trading_profile.TradingProfile(10000, trading_amount.StaticAmount(5000), trading_fee.StaticFee(0)) strat = strategy.Strategy(self.d, [ enter_crit_group1, enter_crit_group2, enter_crit_group3, exit_crit_group1, exit_crit_group2 ], tp) strat.simulate() overview = strat.report.overview() self.assertEqual(overview['trades'], 0)
def test_not(self): inRangeCriteria = criteria.InRange(str(self.one), 10, 20) crit = criteria.Not(inRangeCriteria) ret = crit.apply(self.data.head(2)) self.assertTrue(ret) ret = crit.apply(self.data.head(3)) self.assertFalse(ret) ret = crit.apply(self.data.head(4)) self.assertFalse(ret) ret = crit.apply(self.data.head(5)) self.assertFalse(ret) ret = crit.apply(self.data.head(6)) self.assertTrue(ret)