Beispiel #1
0
 def test_dataset(self):
     d = dataset.Dataset(self.sl, self.dc, None, None, 0)
     d.load_data()
     sanity = msft_data[[
         'MSFT_Open', 'MSFT_High', 'MSFT_Low', 'MSFT_Close', 'MSFT_Volume',
         'MSFT_Adj Close'
     ]] == d.data_frame[[
         u'MSFT_Open', u'MSFT_High', u'MSFT_Low', u'MSFT_Close',
         u'MSFT_Volume', u'MSFT_Adj Close'
     ]]
     self.assertTrue(sanity.all().all())
     addition = technical_indicator.Addition(self.symbol.close, 1)
     d.add_technical_indicator(addition)
     self.assertIn(addition.value, d.data_frame.columns)
     self.assertEqual(len(d.data_frame[addition.value]), len(msft_data))
     self.assertEqual(len(d.technical_indicators), 1)
     self.assertEqual(d.technical_indicators[0], addition)
Beispiel #2
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()
Beispiel #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()
Beispiel #4
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
Beispiel #5
0
import datetime
from nowtrade import symbol_list, data_connection, dataset, technical_indicator, \
                     criteria, criteria_group, trading_profile, trading_amount, \
                     trading_fee, strategy, neural_network
from nowtrade.action import Long, Short, LongExit, ShortExit

dc = data_connection.YahooConnection()
sl = symbol_list.SymbolList(['GOOGL'])
symbol = sl.get('GOOGL')
# Train the network using 5 years of data
train_start = datetime.datetime(2010, 01, 01)
train_end = datetime.datetime(2015, 01, 01)
train_dataset = dataset.Dataset(sl, dc, train_start, train_end)
train_dataset.load_data()
# Use the OHLCV data of the stock to predict the C in the future
nn = neural_network.NeuralNetwork([symbol.open, symbol.high, symbol.low, symbol.close, symbol.volume], [symbol.close])
# 3 hidden layers, 1 day prediction window for our close, learning rate of 0.001, momentum of 0.1
nn.build_network(train_dataset, hidden_layers=3, prediction_window=1, learning_rate=0.001, momentum=0.1)
# Let's train our network for 10 iterations
train_intervals = 10
for i in range(train_intervals): nn.train()
# We save the network to disk to be able to load it back up later
nn.save_to_file('test.nn')
# We can load the network using the following line of code
nn = neural_network.load_from_file('test.nn')
# Let's create our testing dataset
# It's important we don't test our network on the data it was trained with
test_start = train_end
test_end = datetime.datetime(2016, 01, 01)
test_dataset = dataset.Dataset(sl, dc, test_start, test_end)
test_dataset.load_data()