示例#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)
示例#2
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')
示例#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)
示例#4
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)
示例#5
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')
示例#6
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))
示例#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)
示例#8
0
 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))
示例#9
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())))
示例#10
0
# 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,
示例#11
0
文件: adx.py 项目: ziptrade/NowTrade
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()
示例#12
0
from nowtrade.figures import Figure

# This is a standard crossover strategy that we won't get into here
stocks = StockList(['GOOGL'])
googl = stocks.get('GOOGL')
start = datetime.datetime(2013, 01, 01)
end = datetime.datetime(2015, 01, 01)
stock_data = Dataset(stocks, YahooConnection(), start, end)
stock_data.load_data()
sma10 = SMA(googl.close, 10)
sma25 = SMA(googl.close, 25)
stock_data.add_technical_indicator(sma10)
stock_data.add_technical_indicator(sma25)
enter_crit_long = CrossingAbove(sma10, sma25)
exit_crit_long = TrailingStop(googl, 0.05, percent=True)
enter_crit_group = CriteriaGroup([enter_crit_long], Long(), googl)
exit_crit_group = CriteriaGroup([exit_crit_long], LongExit(), googl)
profile = TradingProfile(100000, StaticAmount(10000), StaticFee(5))
strategy = Strategy(stock_data, [enter_crit_group, exit_crit_group], profile)
strategy.simulate()
# Show some charts
fig = Figure(strategy, rows=4)  # 4 charts in this figure
# Note that Figure() can be initialized with a dataset instead of a strategy object.
# In that case you would not see the enter/exits in the candlestick chart
# and you would not have access to the add_capital_chart() function.
fig.add_chart(googl, 1)  # First chart holds the googl candlesticks
fig.add_chart(googl.volume, 2, 'bar')  # Second chart holds the volume
fig.add_chart(sma10, 3)  # Third chart holds the simple moving averages
fig.add_chart(sma25, 3, color='red')  # SMA25 will have a red line
fig.add_capital_chart(4)  # Show the capital history on the last chart
fig.show()  # Show the figure
示例#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)
示例#14
0
 def __init__(self, symbol, periods, condition=None):
     BarsSinceAction.__init__(self, symbol, Long(), periods, condition)
     self.label = 'BarsSinceLong_%s_%s_%s' % (symbol, periods, condition)