Ejemplo n.º 1
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()
Ejemplo n.º 2
0
import datetime
from nowtrade import symbol_list, data_connection, dataset, technical_indicator, \
                     criteria, criteria_group, trading_profile, trading_amount, \
                     trading_fee, strategy
from nowtrade.action import Long, Short, LongExit, ShortExit

dc = data_connection.YahooConnection()
sl = symbol_list.SymbolList(['MSFT'])
symbol = sl.get('MSFT')
d = dataset.Dataset(sl, dc, datetime.datetime(2010, 01, 01),
                    datetime.datetime(2015, 01, 01))
d.load_data()
rsi28 = technical_indicator.RSI(symbol.close, 28)
d.add_technical_indicator(rsi28)
sma100 = technical_indicator.SMA(symbol.close, 100)
d.add_technical_indicator(sma100)
sma100_previous = technical_indicator.Shift(sma100.value, 1)
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
Ejemplo n.º 3
0
 def setUp(self):
     self.dc = DummyDataConnection()
     self.sl = symbol_list.SymbolList(['MSFT'])
     self.symbol = self.sl.get('msft')
     self.d = dataset.Dataset(self.sl, self.dc, None, None, 0)
     self.d.load_data()
Ejemplo n.º 4
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()
Ejemplo n.º 5
0
import datetime
from nowtrade import symbol_list, data_connection, dataset, technical_indicator, \
                     criteria, criteria_group, trading_profile, trading_amount, \
                     trading_fee, strategy, ensemble
from nowtrade.action import Long, Short, LongExit, ShortExit

stocks = symbol_list.SymbolList(['GOOGL'])
symbol = stocks.get('GOOGL')
# We will train the random forest ensemble using 2 years of data
train_start = datetime.datetime(2013, 01, 01)
train_end = datetime.datetime(2015, 01, 01)
train_dataset = dataset.Dataset(stocks, data_connection.YahooConnection(),
                                train_start, train_end)
train_dataset.load_data()
# Use the OHLCV data of the stock to predict the C in the future
rf = ensemble.Ensemble(
    [symbol.open, symbol.high, symbol.low, symbol.close, symbol.volume],
    [symbol.close])
# Our ensemble will attempt to predict 5 bars in the future and will use 25 historical bars to do so
# number_of_jobs refers to the number of CPU threads to use when training
rf.build_ensemble(train_dataset,
                  prediction_window=5,
                  look_back_window=25,
                  number_of_jobs=2)
# Let's start fitting the model
rf.fit()
# We can save our random forest ensemble for future use by using the save_to_file() function
rf.save_to_file('test.ens')
# This is how we would load it back up later
rf = ensemble.load_from_file('test.ens')
# Let's create our testing dataset
Ejemplo n.º 6
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(['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()
Ejemplo n.º 7
0
 def setUp(self):
     self.dc = DummyDataConnection()
     self.sl = symbol_list.SymbolList(['msft'])
     self.symbol = self.sl.get('msft')