def test_position(self): crit = criteria.Above('ONE', 5) value = crit.apply(pd.DataFrame()) self.assertEqual(value, False) value = crit.apply(self.data.head(2)) self.assertEqual(value, False) value = crit.apply(self.data.head(3)) self.assertEqual(value, True) crit = criteria.Above('TWO', 4, 3) value = crit.apply(self.data.head(3)) self.assertEqual(value, True) crit = criteria.Below('TWO', 6, 2) value = crit.apply(pd.DataFrame()) self.assertFalse(value) value = crit.apply(self.data) self.assertEqual(value, True) crit = criteria.Below('ONE', 5) value = crit.apply(self.data.head(1)) self.assertEqual(value, True) value = crit.apply(self.data.head(2)) self.assertEqual(value, False) value = crit.apply(self.data.head(3)) self.assertEqual(value, False) crit = criteria.Above('ONE', 'TWO') value = crit.apply(self.data.head(1)) self.assertEqual(value, False) value = crit.apply(self.data.head(2)) self.assertEqual(value, True) crit = criteria.Below('ONE', 'TWO') value = crit.apply(self.data.head(1)) self.assertEqual(value, True) value = crit.apply(self.data.head(2)) self.assertEqual(value, False) crit = criteria.Equals('ONE', 10) value = crit.apply(pd.DataFrame()) self.assertEqual(value, False) value = crit.apply(self.data.head(2)) self.assertEqual(value, False) crit = criteria.Equal('ONE', 10) value = crit.apply(self.data.head(2)) self.assertEqual(value, False) value = crit.apply(self.data.head(3)) self.assertEqual(value, True) value = crit.apply(self.data.head(4)) self.assertEqual(value, False) crit = criteria.Above('ONE', 10, 1) value = crit.apply(self.data.head(4)) self.assertEqual(value, True) crit = criteria.Equals('ONE', 12, 3) self.assertEqual(value, True) crit = criteria.Equals('ONE', 'ONE_CLONE', 2) value = crit.apply(self.data) self.assertEqual(value, True)
def test_upcoming_action(self): enter_crit = criteria.Above(self.symbol.close, 25.88) exit_crit = criteria.Equals(self.symbol.close, 25.00) enter_crit_group = criteria_group.CriteriaGroup([enter_crit], Short(), self.symbol) exit_crit_group = criteria_group.CriteriaGroup([exit_crit], ShortExit(), self.symbol) tp = trading_profile.TradingProfile(10000, trading_amount.StaticAmount(5000), trading_fee.StaticFee(0)) strat = strategy.Strategy(self.d, [enter_crit_group, exit_crit_group], tp) strat.simulate() next_action = strat.get_next_action()[self.symbol] self.assertTrue(self.symbol in strat.upcoming_actions) self.assertEqual(strat.upcoming_actions[self.symbol], SHORT_EXIT) self.assertEqual(next_action['estimated_money_required'], 5000.8699999999999) self.assertEqual(next_action['estimated_enter_value'], 25.129999999999999) self.assertEqual(next_action['action_name'], 'SHORT_EXIT') self.assertEqual(next_action['estimated_shares'], 199.0) self.assertEqual(next_action['action'], SHORT_EXIT) self.assertEqual(next_action['enter_on'], 'OPEN')
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)
dc = data_connection.YahooConnection() sl = symbol_list.SymbolList(['GOOGL']) symbol = sl.get('GOOGL') start = datetime.datetime(2010, 01, 01) end = datetime.datetime(2015, 01, 01) d = dataset.Dataset(sl, dc, start, end) d.load_data() sma100 = technical_indicator.SMA(symbol.close, 100) d.add_technical_indicator(sma100) high7 = technical_indicator.Max(symbol.close, 7) d.add_technical_indicator(high7) low7 = technical_indicator.Min(symbol.close, 7) d.add_technical_indicator(low7) # Enter Long enter_crit_long1 = criteria.Above(symbol.close, sma100.value) enter_crit_long2 = criteria.Equals(symbol.close, low7.value) enter_crit_short1 = criteria.Below(symbol.close, sma100.value) enter_crit_short2 = criteria.Equals(symbol.close, high7.value) # Exit Long exit_crit_long1 = criteria.StopLoss(symbol, 5) exit_crit_long2 = criteria.TakeProfit(symbol, 10) exit_crit_short1 = criteria.StopLoss(symbol, 5) exit_crit_short2 = criteria.TakeProfit(symbol, 10) # Criteria Groups enter_crit_group1 = criteria_group.CriteriaGroup( [enter_crit_long1, enter_crit_long2], Long(), symbol) enter_crit_group2 = criteria_group.CriteriaGroup( [enter_crit_short1, enter_crit_short2], Short(), symbol) exit_crit_group1 = criteria_group.CriteriaGroup([exit_crit_long1], LongExit(), symbol) exit_crit_group2 = criteria_group.CriteriaGroup([exit_crit_long2], LongExit(),