Exemplo n.º 1
0
 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')
Exemplo n.º 2
0
 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)
Exemplo n.º 3
0
 def test_bars_since_action(self):
     crit = criteria.BarsSinceAction(self.one, Long(), 2)
     self.assertEquals(str(crit), 'BarsSinceAction(symbol=ONE, action=1, periods=2, condition=NONE)')
     crit = criteria.BarsSinceLongExit(self.one, 3)
     self.assertTrue(crit.apply(self.data))
     crit = criteria.BarsSinceShortExit(self.one, 2)
     self.assertFalse(crit.apply(self.data))
     crit = criteria.BarsSinceShortExit(self.one, 1)
     self.assertFalse(crit.apply(self.data))
     crit = criteria.BarsSinceShortExit(self.one, 0)
     self.assertTrue(crit.apply(self.data))
     crit = criteria.BarsSinceLong(self.one, 4)
     self.assertFalse(crit.apply(self.data))
     crit = criteria.BarsSinceLong(self.one, 5)
     self.assertTrue(crit.apply(self.data))
     crit = criteria.BarsSinceLong(self.one, 6)
     self.assertFalse(crit.apply(self.data))
     crit = criteria.BarsSinceShortExit(self.one, 0, 'under')
     self.assertFalse(crit.apply(self.data))
     crit = criteria.BarsSinceShortExit(self.one, 1, 'under')
     self.assertTrue(crit.apply(self.data))
     crit = criteria.BarsSinceShortExit(self.one, 2, 'under')
     self.assertTrue(crit.apply(self.data))
     crit = criteria.BarsSinceLong(self.one, 5, 'under')
     self.assertFalse(crit.apply(self.data))
     crit = criteria.BarsSinceLong(self.one, 6, 'under')
     self.assertTrue(crit.apply(self.data))
     crit = criteria.BarsSinceLong(self.one, 7, 'under')
     self.assertTrue(crit.apply(self.data))
     crit = criteria.BarsSinceShortExit(self.one, 1, 'over')
     self.assertFalse(crit.apply(self.data))
     crit = criteria.BarsSinceShortExit(self.one, 0, 'over')
     self.assertFalse(crit.apply(self.data))
     crit = criteria.BarsSinceShort(self.one, 0, 'over')
     self.assertTrue(crit.apply(self.data))
     crit = criteria.BarsSinceLong(self.one, 4, 'over')
     self.assertTrue(crit.apply(self.data))
     crit = criteria.BarsSinceLong(self.one, 5, 'over')
     self.assertFalse(crit.apply(self.data))
     crit = criteria.BarsSinceLong(self.one, 6, 'over')
     self.assertFalse(crit.apply(self.data))
Exemplo n.º 4
0
# Our random forest will spit out a prediction price for 5 bars in the future
# We still need a threshold to determine whether or not we want to enter the trade
# Let's define a TI that is $5 higher than the random forest's prediction
threshold_above = technical_indicator.Addition(random_forest.value, 5)
test_dataset.add_technical_indicator(threshold_above)
# 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,