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_report(self): enter_crit = criteria.Above(self.symbol.close, 25.88) exit_crit = criteria.BarsSinceLong(self.symbol, 1) enter_crit_group = criteria_group.CriteriaGroup([enter_crit], Long(), self.symbol) exit_crit_group = criteria_group.CriteriaGroup([exit_crit], LongExit(), 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() report_overview = strat.report.overview() self.assertAlmostEqual(report_overview['net_profit'], 7.68) self.assertAlmostEqual(report_overview['average_gains'], 0.153256704981) enter_crit = criteria.Above(self.symbol.close, 25.88) exit_crit = criteria.BarsSinceShort(self.symbol, 1) 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() report_overview = strat.report.overview() self.assertAlmostEqual(report_overview['net_profit'], -7.68) self.assertAlmostEqual(report_overview['average_gains'], -0.15325670498086685) enter_crit = criteria.Above(self.symbol.close, 50) exit_crit = criteria.BarsSinceLong(self.symbol, 1) enter_crit_group = criteria_group.CriteriaGroup([enter_crit], Long(), self.symbol) exit_crit_group = criteria_group.CriteriaGroup([exit_crit], LongExit(), 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() pretty_overview = strat.report.pretty_overview() no_trades = pretty_overview.split('\n')[0] self.assertEqual(no_trades, 'No trades')
def test_simple_short_strategy(self): enter_crit = criteria.Above(self.symbol.close, 25.88) exit_crit = criteria.BarsSinceShort(self.symbol, 2) 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(5)) self.assertEquals( tp.__repr__(), 'TradingProfile(capital=10000, trading_amount=StaticAmount(amount=5000, round_up=False), trading_fee=StaticFee(fee=5), slippage=0.0' ) strat = strategy.Strategy(self.d, [enter_crit_group, exit_crit_group], tp) strat.simulate() report_overview = strat.report.overview() self.assertAlmostEqual(strat.realtime_data_frame.iloc[4]['PL_MSFT'], report_overview['net_profit']) self.assertTrue( np.isnan(strat.realtime_data_frame.iloc[0]['CHANGE_PERCENT_MSFT'])) self.assertTrue( np.isnan(strat.realtime_data_frame.iloc[5]['CHANGE_VALUE_MSFT'])) self.assertEqual(strat.realtime_data_frame.iloc[0]['ACTIONS_MSFT'], 0) self.assertEqual(strat.realtime_data_frame.iloc[1]['ACTIONS_MSFT'], 2) self.assertEqual(strat.realtime_data_frame.iloc[2]['ACTIONS_MSFT'], 0) self.assertEqual(strat.realtime_data_frame.iloc[3]['ACTIONS_MSFT'], 0) self.assertEqual(strat.realtime_data_frame.iloc[4]['ACTIONS_MSFT'], -2) self.assertEqual(strat.realtime_data_frame.iloc[5]['ACTIONS_MSFT'], 0) self.assertEqual(strat.realtime_data_frame.iloc[0]['STATUS_MSFT'], 0) self.assertEqual(strat.realtime_data_frame.iloc[1]['STATUS_MSFT'], -1) self.assertEqual(strat.realtime_data_frame.iloc[2]['STATUS_MSFT'], -1) self.assertEqual(strat.realtime_data_frame.iloc[3]['STATUS_MSFT'], -1) self.assertEqual(strat.realtime_data_frame.iloc[4]['STATUS_MSFT'], 0) self.assertEqual(report_overview['trades'], 1) self.assertEqual(report_overview['winning_trades'], 1) self.assertEqual(report_overview['losing_trades'], 0) self.assertEqual(report_overview['lacking_capital'], 0) self.assertEqual(report_overview['gross_loss'], 0) self.assertEqual(report_overview['gross_profit'], report_overview['net_profit']) self.assertEqual(report_overview['ongoing_trades'], 0) self.assertEqual(report_overview['average_trading_amount'], 5003.5199999999995) self.assertEqual(report_overview['profitability'], 100.00)
def __init__(self, criteria_list, action, symbol): self.criteria_list = criteria_list self.action = action self._action = 0 self.symbol = str(symbol) # Add Actions Criteria Automatically if self.action == Long(): self.criteria_list.append(Not(InMarket(self.symbol))) self._action = LONG elif self.action == LongExit(): self.criteria_list.append(IsLong(self.symbol)) self._action = LONG_EXIT elif self.action == Short(): self.criteria_list.append(Not(InMarket(self.symbol))) self._action = SHORT elif self.action == ShortExit(): self.criteria_list.append(IsShort(self.symbol)) self._action = SHORT_EXIT else: raise InvalidAction() self.logger = logger.Logger(self.__class__.__name__) self.logger.info('New Criteria Group - %s for %s (%s)' \ %(str(action).upper(), symbol, criteria_list))
# SMA100 slope is moving down enter_crit_short2 = criteria.Below(sma100.value, sma100_previous.value) # Stop loss and take profit exit criteria exit_crit_long1 = criteria.StopLoss(symbol, 0.01, percent=True) # 1% exit_crit_long2 = criteria.TakeProfit(symbol, 50) # $100 exit_crit_short1 = criteria.StopLoss(symbol, 0.01, short=True, percent=True) # 1% exit_crit_short2 = criteria.TakeProfit(symbol, 50, short=True) # $100 # 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(), symbol) exit_crit_group3 = criteria_group.CriteriaGroup([exit_crit_short1], ShortExit(), symbol) exit_crit_group4 = criteria_group.CriteriaGroup([exit_crit_short2], ShortExit(), symbol) # Strategy tp = trading_profile.TradingProfile(20000, trading_amount.StaticAmount(10000), trading_fee.StaticFee(5)) strat = strategy.Strategy(d, [ enter_crit_group1, enter_crit_group2, exit_crit_group1, exit_crit_group2, exit_crit_group3, exit_crit_group4 ], tp) strat.simulate() print strat.report.pretty_overview()
# And $5 lower than the neural network's prediction threshold_below = technical_indicator.Subtraction(random_forest.value, 5) test_dataset.add_technical_indicator(threshold_below) # Criteria # Current price is below the threshold of our random forest's prediction price enter_crit_long = criteria.Below(symbol.close, threshold_below.value) # Current price is above the threshold of our random forest's prediction price enter_crit_short = criteria.Above(symbol.close, threshold_above.value) # Exit after 5 days - as per the random forest's build parameters exit_crit_long = criteria.BarsSinceLong(symbol, 5) exit_crit_short = criteria.BarsSinceShort(symbol, 5) # Criteria Groups enter_crit_group1 = criteria_group.CriteriaGroup([enter_crit_long], Long(), symbol) enter_crit_group2 = criteria_group.CriteriaGroup([enter_crit_short], Short(), symbol) exit_crit_group1 = criteria_group.CriteriaGroup([exit_crit_long], LongExit(), symbol) exit_crit_group2 = criteria_group.CriteriaGroup([exit_crit_short], ShortExit(), symbol) # Trading Profile tp = trading_profile.TradingProfile(100000, trading_amount.StaticAmount(10000), trading_fee.StaticFee(5)) # Strategy strat = strategy.Strategy( test_dataset, [enter_crit_group1, enter_crit_group2, exit_crit_group1, exit_crit_group2], tp) strat.simulate() print strat.report.pretty_overview()
def __init__(self, symbol, periods, condition=None): BarsSinceAction.__init__(self, symbol, ShortExit(), periods, condition) self.label = 'BarsSinceShortExit_%s_%s_%s' % (symbol, periods, condition)