Example #1
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)
Example #2
0
 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')
Example #3
0
 def test_trailing_stop_long_strategy(self):
     enter_crit = criteria.Above(self.symbol.close, 25.88)
     exit_crit = criteria.TrailingStop(self.symbol, -0.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)
     strat.simulate()
     report_overview = strat.report.overview()
     self.assertTrue(
         np.isnan(strat.realtime_data_frame.iloc[0]['CHANGE_PERCENT_MSFT']))
     self.assertEqual(
         strat.realtime_data_frame.iloc[-5]['CHANGE_VALUE_MSFT'],
         -0.26999999999999957)
     self.assertEqual(
         strat.realtime_data_frame.iloc[1]['CHANGE_VALUE_MSFT'],
         0.40000000000000213)
     self.assertEqual(strat.realtime_data_frame.iloc[2]['PL_MSFT'],
                      153.60000000000014)
     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[4]['ACTIONS_MSFT'], -1)
     self.assertEqual(strat.realtime_data_frame.iloc[5]['ACTIONS_MSFT'], 0)
Example #4
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')
Example #5
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)
Example #6
0
 def test_criteria_group(self):
     criteria_list = [DummyCriteria(True), DummyCriteria(True)]
     with self.assertRaises(criteria_group.InvalidAction):
         cg = criteria_group.CriteriaGroup(criteria_list, 'INVALID', 'MSFT')
     cg = criteria_group.CriteriaGroup(criteria_list, Long(), 'MSFT')
     self.assertEquals(
         str(cg),
         'CriteriaGroup(criteria_list=[DummyCriteria(), DummyCriteria(), Not_InMarket(symbol=MSFT)], action=long, symbol=MSFT)'
     )
     self.assertEqual(cg._action, 1)
     out = cg.get_result(msft_data)
     self.assertEqual(len(cg.criteria_list), 3)  # One extra: NotInMarket
     self.assertEqual(out, strategy.LONG)
     criteria_list = [DummyCriteria(True), DummyCriteria(False)]
     cg = criteria_group.CriteriaGroup(criteria_list, Long(), 'MSFT')
     out = cg.get_result(msft_data)
     self.assertEqual(out, strategy.NO_ACTION)
Example #7
0
 def test_stop_loss_strategy(self):
     enter_crit = criteria.Above(self.symbol.close, 25.88)
     exit_crit = criteria.StopLoss(self.symbol, -0.8)
     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(strat.realtime_data_frame.iloc[-2]['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[-1]['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'],
                      -1)
     self.assertEqual(strat.realtime_data_frame.iloc[-1]['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[-3]['STATUS_MSFT'], 1)
     self.assertEqual(strat.realtime_data_frame.iloc[-2]['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_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.0)
Example #8
0
 def test_trailing_stop_short_strategy(self):
     enter_crit = criteria.Above(self.symbol.close, 25.88)
     exit_crit = criteria.TrailingStop(self.symbol, -0.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)
     strat.simulate()
     self.assertTrue(
         np.isnan(strat.realtime_data_frame.iloc[0]['CHANGE_PERCENT_MSFT']))
     self.assertTrue(
         np.isnan(strat.realtime_data_frame.iloc[-3]['CHANGE_VALUE_MSFT']))
     self.assertEqual(
         strat.realtime_data_frame.iloc[-4]['CHANGE_VALUE_MSFT'],
         -0.23999999999999844)
     self.assertEqual(
         strat.realtime_data_frame.iloc[1]['CHANGE_VALUE_MSFT'],
         0.40000000000000213)
     self.assertEqual(strat.realtime_data_frame.iloc[2]['PL_MSFT'],
                      153.60000000000014)
     self.assertEqual(
         strat.realtime_data_frame.iloc[3]['CHANGE_PERCENT_MSFT'],
         -0.01036070606293168)
     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)
     # No properly implemented yet
     self.assertTrue(np.isnan(strat.report.get_sharpe_ratio(benchmark=5)))
     self.assertTrue(
         np.isnan(strat.report.get_sharpe_ratio(benchmark=pd.Series())))
Example #9
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')
Example #10
0
d.add_technical_indicator(sma100_previous)
# Criteria
enter_crit_long1 = criteria.Above(rsi28.value, 60)
# SMA100 slope is moving up
enter_crit_long2 = criteria.Above(sma100.value, sma100_previous.value)
enter_crit_short1 = criteria.Below(rsi28.value, 40)
# 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,
Example #11
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()
Example #12
0
import datetime
from nowtrade import symbol_list, data_connection, dataset, technical_indicator, \
                     criteria, criteria_group, trading_profile, trading_amount, \
                     trading_fee, strategy, action
dc = data_connection.YahooConnection()
sl = symbol_list.SymbolList(['^gspc'])  # S&P 500
snp500 = sl.get('^gspc')
start = datetime.datetime(1980, 01, 01)
end = datetime.datetime(2015, 01, 01)
d = dataset.Dataset(sl, dc, start, end)
d.load_data()
# Go Long in November , Exit in May, every year.
enter_crit = criteria.IsMonth(11)
exit_crit = criteria.IsMonth(5)
enter_crit_group = criteria_group.CriteriaGroup([enter_crit], action.Long(),
                                                snp500)
exit_crit_group = criteria_group.CriteriaGroup([exit_crit], action.LongExit(),
                                               snp500)
tp = trading_profile.TradingProfile(100000,
                                    trading_amount.CapitalPercentage(100),
                                    trading_fee.StaticFee(20))
strat = strategy.Strategy(d, [enter_crit_group, exit_crit_group], tp)
strat.simulate()
print strat.report.pretty_overview()
Example #13
0
# 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,
    [enter_crit_group1, enter_crit_group2, exit_crit_group1, exit_crit_group2],
    tp)
strat.simulate()
Example #14
0
from nowtrade import symbol_list, data_connection, dataset, technical_indicator, \
                     criteria, criteria_group, trading_profile, trading_amount, \
                     trading_fee, strategy, action

dc = data_connection.YahooConnection()
sl = symbol_list.SymbolList(['GOOGL'])
symbol = sl.get('GOOGL')
print symbol
d = dataset.Dataset(sl, dc, datetime.datetime(2010, 01, 01), datetime.datetime(2015, 01, 01))
d.load_data()
ultosc = technical_indicator.ULTOSC(symbol, 14, 28, 56)
d.add_technical_indicator(ultosc)
# Enter Long
enter_crit_long1 = criteria.Below(ultosc.value, 40)
# Enter Short
enter_crit_short1 = criteria.Above(ultosc.value, 60)
# Exit Long
exit_crit_long1 = criteria.BarsSinceAction(symbol, action.Long(), 10)
# Exit Short
exit_crit_short1 = criteria.BarsSinceAction(symbol, action.Short(), 10)
# Criteria Groups
enter_crit_group1 = criteria_group.CriteriaGroup([enter_crit_long1], action.Long(), symbol)
enter_crit_group2 = criteria_group.CriteriaGroup([enter_crit_short1], action.Short(), symbol)
exit_crit_group1 = criteria_group.CriteriaGroup([exit_crit_long1], action.LongExit(), symbol)
exit_crit_group2 = criteria_group.CriteriaGroup([exit_crit_short1], action.ShortExit(), symbol)
# Strategy
tp = trading_profile.TradingProfile(11000, trading_amount.StaticAmount(10000), trading_fee.StaticFee(0))
strat = strategy.Strategy(d, [enter_crit_group1, enter_crit_group2, exit_crit_group1, exit_crit_group2], tp)
strat.simulate()
print strat.report.pretty_overview()