Пример #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')
Пример #2
0
 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)
Пример #3
0
 def test_simple_long_strategy(self):
     enter_crit = criteria.Above(self.symbol.close, 25.88)
     exit_crit = criteria.BarsSinceLong(self.symbol, 2)
     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)
     repr_string = 'Strategy(dataset=Dataset(symbol_list=[MSFT], data_connection=DummyDataConnection(), start_datetime=None, end_datetime=None, periods=0, granularity=None), criteria_groups=[CriteriaGroup(criteria_list=[Above_MSFT_Close_25.88_1, Not_InMarket(symbol=MSFT)], action=long, symbol=MSFT), CriteriaGroup(criteria_list=[BarsSinceLong_MSFT_2_None, IsLong_MSFT], action=longexit, symbol=MSFT)], trading_profile=TradingProfile(capital=10000, trading_amount=StaticAmount(amount=5000, round_up=False), trading_fee=StaticFee(fee=0), slippage=0.0)'
     self.assertEquals(strat.__repr__(), repr_string)
     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'], 1)
     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'], -1)
     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'], 0)
     self.assertEqual(report_overview['losing_trades'], 1)
     self.assertEqual(report_overview['lacking_capital'], 0)
     self.assertEqual(report_overview['gross_profit'], 0)
     self.assertEqual(report_overview['gross_loss'],
                      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'], 0)
     pretty_overview_string = 'Trades:\nMSFT\nTrade(datetime=2010-06-02 00:00:00, action=LONG, symbol=MSFT, price=26.06, shares=192.0, money=5003.52, fee=0, slippage=0.0)\nTrade(datetime=2010-06-07 00:00:00, action=LONG_EXIT, symbol=MSFT, price=25.82, shares=192.0, money=4957.44, fee=0, slippage=0.0)\nProfitability: 0.0\n# Trades: 1\nNet Profit: -46.08\nGross Profit: 0.0\nGross Loss: -46.08\nWinning Trades: 0\nLosing Trades: 1\nSharpe Ratio: -6.0\nAvg. Trading Amount: 5003.52\nAvg. Fees: 0.0\nAvg. Slippage: 0.0\nAvg. Gains: -0.929512006197\nAvg. Winner: 0.0\nAvg. Loser: -0.929512006197\nAvg. Bars: 3.0\nTotal Fees: 0.0\nTotal Slippage: 0.0\nTrades Lacking Capital: 0\nOngoing Trades: 0'
     self.assertEqual(strat.report.pretty_overview(),
                      pretty_overview_string)
     with self.assertRaises(report.InvalidExit):
         strat.report.long_exit(None, None, 'MSFT')
     with self.assertRaises(report.InvalidExit):
         strat.report.short_exit(None, None, 'MSFT')
Пример #4
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))
Пример #5
0
import datetime
from nowtrade import symbol_list, data_connection, dataset, technical_indicator, \
                     criteria, criteria_group, trading_profile, trading_amount, \
                     trading_fee, strategy, figures
from nowtrade.action import Long, Short, LongExit, ShortExit

dc = data_connection.YahooConnection()
sl = symbol_list.SymbolList(['AAPL'])
symbol = sl.get('AAPL')
start = datetime.datetime(2010, 01, 01)
end = datetime.datetime(2015, 01, 01)
d = dataset.Dataset(sl, dc, start, end)
d.load_data()
adx28 = technical_indicator.ADX(symbol, 28)
d.add_technical_indicator(adx28)
# Enter Long
enter_crit_long1 = criteria.Above(adx28.value, 30)
enter_crit_long2 = criteria.Above(adx28.minus_di, 30)
enter_crit_long3 = criteria.Below(adx28.plus_di, 20)
# Exit Long
exit_crit_long = criteria.BarsSinceLong(symbol, 10) # Exit 10 days later
# Criteria Groups
enter_crit_group = criteria_group.CriteriaGroup([enter_crit_long1, enter_crit_long2, enter_crit_long3], Long(), symbol)
exit_crit_group = criteria_group.CriteriaGroup([exit_crit_long], LongExit(), symbol)
# Strategy
tp = trading_profile.TradingProfile(100000, trading_amount.StaticAmount(20000), trading_fee.StaticFee(10))
strat = strategy.Strategy(d, [enter_crit_group, exit_crit_group], tp)
strat.simulate()
print strat.report.pretty_overview()
Пример #6
0
test_dataset.add_technical_indicator(random_forest)
# 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(