def testReset(self): feed = csvfeed.Feed("Date", "%Y-%m-%d") feed.addValuesFromCSV(common.get_data_file_path("orcl-2000-yahoofinance.csv")) disp = dispatcher.Dispatcher() disp.addSubject(feed) disp.run() keys = list(feed.getKeys()) key = keys[0] values = feed[key] feed.reset() disp = dispatcher.Dispatcher() disp.addSubject(feed) disp.run() reloadedKeys = list(feed.getKeys()) reloadedValues = feed[key] self.assertEqual(keys.sort(), reloadedKeys.sort()) self.assertNotEqual(values, reloadedValues) self.assertEqual(len(values), len(reloadedValues)) for i in range(len(values)): self.assertEqual(values[i], reloadedValues[i])
def testReset(self): key = "i" values = [(datetime.datetime.now() + datetime.timedelta(seconds=i), {key: i}) for i in range(100)] feed = memfeed.MemFeed() feed.addValues(values) disp = dispatcher.Dispatcher() disp.addSubject(feed) disp.run() keys = feed.getKeys() values = feed[key] feed.reset() disp = dispatcher.Dispatcher() disp.addSubject(feed) disp.run() reloadedKeys = feed.getKeys() reloadedValues = feed[key] self.assertEqual(keys, reloadedKeys) self.assertNotEqual(values, reloadedValues) self.assertEqual(len(values), len(reloadedValues)) for i in range(len(values)): self.assertEqual(values[i], reloadedValues[i])
def testResampledBarFeed(self): barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV( "spy", common.get_data_file_path("spy-2010-yahoofinance.csv")) barFeed.addBarsFromCSV( "nikkei", common.get_data_file_path("nikkei-2010-yahoofinance.csv")) resampledBarFeed = resampled_bf.ResampledBarFeed( barFeed, bar.Frequency.MONTH) disp = dispatcher.Dispatcher() disp.addSubject(barFeed) disp.addSubject(resampledBarFeed) disp.run() weeklySpyBarDS = resampledBarFeed["spy"] weeklyNikkeiBarDS = resampledBarFeed["nikkei"] # Check first bar self.assertEqual(weeklySpyBarDS[0].getDateTime().date(), datetime.date(2010, 1, 1)) self.assertEqual(weeklyNikkeiBarDS[0].getDateTime().date(), datetime.date(2010, 1, 1)) # Check last bar self.assertEqual(weeklySpyBarDS[-1].getDateTime().date(), datetime.date(2010, 11, 1)) self.assertEqual(weeklyNikkeiBarDS[-1].getDateTime().date(), datetime.date(2010, 11, 1))
def run(self, feed, useAdjustedCloseForReturns=True): """Runs the analysis using the bars supplied by the feed. :param barFeed: The bar feed to use to run the analysis. :type barFeed: :class:`mooquant.barfeed.BarFeed`. :param useAdjustedCloseForReturns: True if adjusted close values should be used to calculate returns. :type useAdjustedCloseForReturns: boolean. """ if useAdjustedCloseForReturns: assert feed.barsHaveAdjClose( ), "Feed doesn't have adjusted close values" try: self.__feed = feed self.__rets = {} self.__futureRets = {} for instrument in feed.getRegisteredInstruments(): self.__events.setdefault(instrument, []) self.__futureRets[instrument] = [] if useAdjustedCloseForReturns: ds = feed[instrument].getAdjCloseDataSeries() else: ds = feed[instrument].getCloseDataSeries() self.__rets[instrument] = roc.RateOfChange(ds, 1) feed.getNewValuesEvent().subscribe(self.__onBars) disp = dispatcher.Dispatcher() disp.addSubject(feed) disp.run() finally: feed.getNewValuesEvent().unsubscribe(self.__onBars)
def testFeedWithQuandl(self): class RowFilter(csvfeed.RowFilter): def includeRow(self, dateTime, values): return dateTime.year == 2013 feed = csvfeed.Feed("Date", "%Y-%m-%d", maxLen=40, timezone=marketsession.USEquities.timezone) feed.setRowFilter(RowFilter()) feed.setTimeDelta(datetime.timedelta(hours=23, minutes=59, seconds=59)) feed.addValuesFromCSV(common.get_data_file_path("quandl_gold_2.csv")) for col in ["USD", "GBP", "EUR"]: self.assertEqual(len(feed[col]), 0) disp = dispatcher.Dispatcher() disp.addSubject(feed) disp.run() for col in ["USD", "GBP", "EUR"]: self.assertEqual(len(feed[col]), 39) self.assertEqual(feed["USD"][-1], 1333.0) self.assertEqual(feed["GBP"][-1], 831.203) self.assertEqual(feed["EUR"][-1], 986.75) self.assertFalse(dt.datetime_is_naive(feed["USD"].getDateTimes()[-1])) self.assertEqual( feed["USD"].getDateTimes()[-1], dt.localize(datetime.datetime(2013, 9, 29, 23, 59, 59), marketsession.USEquities.timezone) )
def tstBaseFeedInterface(testCase, feed): # This tests the observer.Subject interface. disp = dispatcher.Dispatcher() disp.addSubject(feed) disp.run() # This tests the feed.BaseFeed interface. feed.createDataSeries("any", 10) feed.getNextValues()
def test1NrtFeed(self): values = [] now = datetime.datetime.now() datetimes = [now + datetime.timedelta(seconds=i) for i in range(10)] nrtFeed = NonRealtimeFeed(copy.copy(datetimes)) nrtFeed.getEvent().subscribe(lambda x: values.append(x)) disp = dispatcher.Dispatcher() disp.addSubject(nrtFeed) disp.run() self.assertEqual(values, datetimes)
def testPriority(self): feed4 = RealtimeFeed([], None) feed3 = RealtimeFeed([], None) feed2 = RealtimeFeed([], 3) feed1 = RealtimeFeed([], 0) disp = dispatcher.Dispatcher() disp.addSubject(feed3) disp.addSubject(feed2) disp.addSubject(feed1) self.assertEqual(disp.getSubjects(), [feed1, feed2, feed3]) disp = dispatcher.Dispatcher() disp.addSubject(feed1) disp.addSubject(feed2) disp.addSubject(feed3) self.assertEqual(disp.getSubjects(), [feed1, feed2, feed3]) disp = dispatcher.Dispatcher() disp.addSubject(feed3) disp.addSubject(feed4) disp.addSubject(feed2) disp.addSubject(feed1) self.assertEqual(disp.getSubjects(), [feed1, feed2, feed3, feed4])
def testDispatchOrder(self): values = [] now = datetime.datetime.now() feed1 = NonRealtimeFeed([now], 0) feed2 = RealtimeFeed([now + datetime.timedelta(seconds=1)], None) feed1.getEvent().subscribe(lambda x: values.append(x)) feed2.getEvent().subscribe(lambda x: values.append(x)) disp = dispatcher.Dispatcher() disp.addSubject(feed2) disp.addSubject(feed1) self.assertEqual(disp.getSubjects(), [feed1, feed2]) disp.run() # Check that although feed2 is realtime, feed1 was dispatched before. self.assertTrue(values[0] < values[1])
def testFeed(self): values = [(datetime.datetime.now() + datetime.timedelta(seconds=i), {"i": i}) for i in range(100)] feed = memfeed.MemFeed() feed.addValues(values) # Check that the dataseries are available after adding values. self.assertTrue("i" in feed) self.assertEqual(len(feed["i"]), 0) self.assertFalse("dt" in feed) disp = dispatcher.Dispatcher() disp.addSubject(feed) disp.run() self.assertTrue("i" in feed) self.assertFalse("dt" in feed) self.assertEqual(feed["i"][0], 0) self.assertEqual(feed["i"][-1], 99)
def test2NrtFeeds(self): values = [] now = datetime.datetime.now() datetimes1 = [now + datetime.timedelta(seconds=i) for i in range(10)] datetimes2 = [ now + datetime.timedelta(seconds=i + len(datetimes1)) for i in range(10) ] nrtFeed1 = NonRealtimeFeed(copy.copy(datetimes1)) nrtFeed1.getEvent().subscribe(lambda x: values.append(x)) nrtFeed2 = NonRealtimeFeed(copy.copy(datetimes2)) nrtFeed2.getEvent().subscribe(lambda x: values.append(x)) disp = dispatcher.Dispatcher() disp.addSubject(nrtFeed1) disp.addSubject(nrtFeed2) disp.run() self.assertEqual(len(values), len(datetimes1) + len(datetimes2)) self.assertEqual(values[:len(datetimes1)], datetimes1) self.assertEqual(values[len(datetimes1):], datetimes2)
def __init__(self, barFeed, broker): self.__barFeed = barFeed self.__broker = broker self.__activePositions = set() self.__orderToPosition = {} self.__barsProcessedEvent = observer.Event() self.__analyzers = [] self.__namedAnalyzers = {} self.__resampledBarFeeds = [] self.__dispatcher = dispatcher.Dispatcher() self.__broker.getOrderUpdatedEvent().subscribe(self.__onOrderEvent) self.__barFeed.getNewValuesEvent().subscribe(self.__onBars) self.__dispatcher.getStartEvent().subscribe(self.onStart) self.__dispatcher.getIdleEvent().subscribe(self.__onIdle) # It is important to dispatch broker events before feed events, specially if we're backtesting. self.__dispatcher.addSubject(self.__broker) self.__dispatcher.addSubject(self.__barFeed) # Initialize logging. self.__logger = logger.getLogger(BaseStrategy.LOGGER_NAME)
def testBarFeed(self): events = { "break": False, "on_bars": False, "on_order_book_updated": False, "start": datetime.datetime.now() } disp = dispatcher.Dispatcher() barFeed = barfeed.LiveTradeFeed() disp.addSubject(barFeed) def on_bars(dateTime, bars): bars[common.btc_symbol] events["on_bars"] = True if events["on_order_book_updated"] is True: disp.stop() def on_order_book_updated(orderBookUpdate): events["on_order_book_updated"] = True if events["on_bars"] is True: disp.stop() def on_idle(): if (datetime.datetime.now() - events["start"]).seconds > 60 * 5: disp.stop() # Subscribe to events. barFeed.getNewValuesEvent().subscribe(on_bars) barFeed.getOrderBookUpdateEvent().subscribe(on_order_book_updated) disp.getIdleEvent().subscribe(on_idle) disp.run() # Check that we received both events. self.assertTrue(events["on_bars"]) self.assertTrue(events["on_order_book_updated"])
def testFeedWithBars(self): feed = csvfeed.Feed("Date", "%Y-%m-%d") feed.addValuesFromCSV(common.get_data_file_path("orcl-2000-yahoofinance.csv")) self.assertEqual(len(feed.getKeys()), 6) for col in ["Open", "High", "Low", "Close", "Volume", "Adj Close"]: self.assertEqual(len(feed[col]), 0) disp = dispatcher.Dispatcher() disp.addSubject(feed) disp.run() for col in ["Open", "High", "Low", "Close", "Volume", "Adj Close"]: self.assertEqual(len(feed[col]), 252) self.assertEqual(feed["Open"][-1], 30.87) self.assertEqual(feed["High"][-1], 31.31) self.assertEqual(feed["Low"][-1], 28.69) self.assertEqual(feed["Close"][-1], 29.06) self.assertEqual(feed["Volume"][-1], 31655500) self.assertEqual(feed["Adj Close"][-1], 28.41)
def check_base_barfeed(testCase, barFeed, barsHaveAdjClose): called = {"called": True} def callback(dateTime, bars): called["called"] = True testCase.assertEqual(barFeed.getCurrentDateTime(), dateTime) testCase.assertEqual(barFeed.getCurrentDateTime(), None) testCase.assertEqual(barFeed.barsHaveAdjClose(), barsHaveAdjClose) if not barsHaveAdjClose: with testCase.assertRaisesRegex( Exception, "The barfeed doesn't support adjusted close values.*"): barFeed.setUseAdjustedValues(True) d = dispatcher.Dispatcher() d.addSubject(barFeed) barFeed.getNewValuesEvent().subscribe(callback) d.run() testCase.assertEqual(called["called"], True)
def resample_impl(barFeed, frequency, csvFile): instruments = barFeed.getRegisteredInstruments() if len(instruments) != 1: raise Exception("Only barfeeds with 1 instrument can be resampled") csvWriter = CSVFileWriter(csvFile) def on_bar(ds, dateTime, value): csvWriter.writeBar(value) if not (type(instruments) == list): instruments = list(instruments) insrumentDS = barFeed[instruments[0]] resampledDS = resampled.ResampledBarDataSeries(insrumentDS, frequency) resampledDS.getNewValueEvent().subscribe(on_bar) # Process all bars. disp = dispatcher.Dispatcher() disp.addSubject(barFeed) disp.run() resampledDS.pushLast()