def testBounded(self): tmpFeed = TemporarySQLiteFeed(SQLiteFeedTestCase.dbName, bar.Frequency.DAY, maxLen=2) with tmpFeed: # Load bars using a Yahoo! feed. yahooFeed = yahoofeed.Feed(maxLen=1) yahooFeed.addBarsFromCSV( "orcl", common.get_data_file_path("orcl-2000-yahoofinance.csv"), marketsession.USEquities.timezone) yahooFeed.addBarsFromCSV( "orcl", common.get_data_file_path("orcl-2001-yahoofinance.csv"), marketsession.USEquities.timezone) # Fill the database using the bars from the Yahoo! feed. sqliteFeed = tmpFeed.getFeed() sqliteFeed.getDatabase().addBarsFromFeed(yahooFeed) # Load the SQLite feed and process all bars. sqliteFeed.loadBars("orcl") for bars in sqliteFeed: pass barDS = sqliteFeed["orcl"] self.assertEqual(len(barDS), 2) self.assertEqual(len(barDS.getDateTimes()), 2) self.assertEqual(len(barDS.getCloseDataSeries()), 2) self.assertEqual(len(barDS.getCloseDataSeries().getDateTimes()), 2) self.assertEqual(len(barDS.getOpenDataSeries()), 2) self.assertEqual(len(barDS.getHighDataSeries()), 2) self.assertEqual(len(barDS.getLowDataSeries()), 2) self.assertEqual(len(barDS.getAdjCloseDataSeries()), 2)
def testDuplicateBars(self): barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV( FeedTestCase.TestInstrument, common.get_data_file_path("orcl-2000-yahoofinance.csv")) barFeed.addBarsFromCSV( FeedTestCase.TestInstrument, common.get_data_file_path("orcl-2000-yahoofinance.csv")) with self.assertRaisesRegexp(Exception, "Duplicate bars found for.*"): barFeed.loadAll()
def testDifferentTimezones(self): # Market times in UTC: # - TSE: 0hs ~ 6hs # - US: 14:30hs ~ 21hs feed = yahoofeed.Feed() for year in [2010, 2011]: feed.addBarsFromCSV("^n225", common.get_data_file_path("nikkei-%d-yahoofinance.csv" % year), marketsession.TSE.getTimezone()) feed.addBarsFromCSV("spy", common.get_data_file_path("spy-%d-yahoofinance.csv" % year), marketsession.USEquities.getTimezone()) self.__testDifferentTimezonesImpl(feed)
def testWithoutTimezone(self): barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV( FeedTestCase.TestInstrument, common.get_data_file_path("orcl-2000-yahoofinance.csv")) barFeed.addBarsFromCSV( FeedTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv")) for dateTime, bars in barFeed: bar = bars.getBar(FeedTestCase.TestInstrument) self.assertTrue(dt.datetime_is_naive(bar.getDateTime()))
def testWithDefaultTimezone(self): barFeed = yahoofeed.Feed( timezone=marketsession.USEquities.getTimezone()) barFeed.addBarsFromCSV( FeedTestCase.TestInstrument, common.get_data_file_path("orcl-2000-yahoofinance.csv")) barFeed.addBarsFromCSV( FeedTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv")) for dateTime, bars in barFeed: bar = bars.getBar(FeedTestCase.TestInstrument) self.assertFalse(dt.datetime_is_naive(bar.getDateTime()))
def testMultipleInstrumentsInterleaved(self): barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV("spy", common.get_data_file_path("spy-2010-yahoofinance.csv"), marketsession.NYSE.getTimezone()) barFeed.addBarsFromCSV("nikkei", common.get_data_file_path("nikkei-2010-yahoofinance.csv"), marketsession.TSE.getTimezone()) strat = strategy_test.TestStrategy(barFeed, 1000) stratAnalyzer = returns.Returns() strat.attachAnalyzer(stratAnalyzer) strat.marketOrder("spy", 1) strat.run() # The cumulative return should be the same if we load nikkei or not. self.assertEqual(round(stratAnalyzer.getCumulativeReturns()[-1], 5), 0.01338)
def __loadBarFeed(self): ret = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE) barFilter = csvfeed.USEquitiesRTH() ret.setBarFilter(barFilter) ret.addBarsFromCSV(TradesAnalyzerTestCase.TestInstrument, common.get_data_file_path("nt-spy-minute-2011.csv")) return ret
def __getFeed(self): # Load the feed and process all bars. barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV( VWAPTestCase.Instrument, common.get_data_file_path("orcl-2001-yahoofinance.csv")) return barFeed
def testResampleNinjaTraderHour(self): with common.TmpDir() as tmp_path: # Resample. feed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE) feed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv")) resampledBarDS = resampled_ds.ResampledBarDataSeries(feed["spy"], bar.Frequency.HOUR) resampledFile = os.path.join(tmp_path, "hour-nt-spy-minute-2011.csv") resample.resample_to_csv(feed, bar.Frequency.HOUR, resampledFile) resampledBarDS.pushLast() # Need to manually push the last stot since time didn't change. # Load the resampled file. feed = csvfeed.GenericBarFeed(bar.Frequency.HOUR, marketsession.USEquities.getTimezone()) feed.addBarsFromCSV("spy", resampledFile) feed.loadAll() self.assertEqual(len(feed["spy"]), 340) self.assertEqual(feed["spy"][0].getDateTime(), dt.localize(datetime.datetime(2011, 1, 3, 9), marketsession.USEquities.getTimezone())) self.assertEqual(feed["spy"][-1].getDateTime(), dt.localize(datetime.datetime(2011, 2, 1, 1), marketsession.USEquities.getTimezone())) self.assertEqual(feed["spy"][0].getOpen(), 126.35) self.assertEqual(feed["spy"][0].getHigh(), 126.45) self.assertEqual(feed["spy"][0].getLow(), 126.3) self.assertEqual(feed["spy"][0].getClose(), 126.4) self.assertEqual(feed["spy"][0].getVolume(), 3397.0) self.assertEqual(feed["spy"][0].getAdjClose(), None) self.assertEqual(len(resampledBarDS), len(feed["spy"])) self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(datetime.datetime(2011, 1, 3, 9))) self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(datetime.datetime(2011, 2, 1, 1)))
def testBaseBarFeed(self): barFeed = yahoofeed.Feed() barFeed.sanitizeBars(True) barFeed.addBarsFromCSV( FeedTestCase.TestInstrument, common.get_data_file_path("orcl-2000-yahoofinance.csv")) barfeed_test.check_base_barfeed(self, barFeed, True)
def testCSVFeedLoadOrder(self): barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV( FeedTestCase.TestInstrument, common.get_data_file_path("orcl-2000-yahoofinance.csv")) barFeed.addBarsFromCSV( FeedTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv")) # Dispatch and handle events. handler = BarFeedEventHandler_TestLoadOrder( self, barFeed, FeedTestCase.TestInstrument) barFeed.getNewValuesEvent().subscribe(handler.onBars) while not barFeed.eof(): barFeed.dispatch() self.assertTrue(handler.getEventCount() > 0)
def __testIGE_BrokerImpl(self, quantity): initialCash = 42.09 * quantity # This testcase is based on an example from Ernie Chan's book: # 'Quantitative Trading: How to Build Your Own Algorithmic Trading Business' barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV( "ige", common.get_data_file_path("sharpe-ratio-test-ige.csv")) strat = strategy_test.TestStrategy(barFeed, initialCash) strat.setUseAdjustedValues(True) strat.setBrokerOrdersGTC(True) stratAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(stratAnalyzer) # Disable volume checks to match book results. strat.getBroker().getFillStrategy().setVolumeLimit(None) # Manually place the order to get it filled on the first bar. order = strat.getBroker().createMarketOrder(broker.Order.Action.BUY, "ige", quantity, True) # Adj. Close: 42.09 order.setGoodTillCanceled(True) strat.getBroker().submitOrder(order) strat.addOrder(datetime.datetime(2007, 11, 13), strat.getBroker().createMarketOrder, broker.Order.Action.SELL, "ige", quantity, True) # Adj. Close: 127.64 strat.run() self.assertEqual(round(strat.getBroker().getCash(), 2), initialCash + (127.64 - 42.09) * quantity) self.assertEqual(strat.orderUpdatedCalls, 6) # The results are slightly different only because I'm taking into account the first bar as well. self.assertEqual(round(stratAnalyzer.getSharpeRatio(0.04, True), 4), 0.7889) self.assertEqual(round(stratAnalyzer.getSharpeRatio(0.04, False), 4), 0.0497)
def testIntraDay(self): barFeed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE, marketsession.USEquities.getTimezone()) barFeed.setBarFilter(csvfeed.USEquitiesRTH()) barFeed.addBarsFromCSV( "spy", common.get_data_file_path("nt-spy-minute-2011.csv")) strat = strategy_test.TestStrategy(barFeed, 1000) stratAnalyzer = sharpe.SharpeRatio(False) strat.attachAnalyzer(stratAnalyzer) strat.marketOrder("spy", 1) strat.run() tradingPeriods = 252 * 6.5 * 60 manualAnnualized = sharpe.sharpe_ratio(stratAnalyzer.getReturns(), 0.04, tradingPeriods, True) manualNotAnnualized = sharpe.sharpe_ratio(stratAnalyzer.getReturns(), 0.04, tradingPeriods, False) analyzerAnnualized = stratAnalyzer.getSharpeRatio(0.04) analyzerNotAnnualized = stratAnalyzer.getSharpeRatio(0.04, False) self.assertEqual(round(analyzerAnnualized, 10), -1.1814830854) self.assertEqual(round(analyzerNotAnnualized, 10), -0.0037659686) # They should be similar, but not identical because the analyzer uses 365 days/year # when useDailyReturns is set to False. self.assertEqual(round(analyzerAnnualized, 1), round(manualAnnualized, 1)) self.assertEqual(round(analyzerNotAnnualized, 3), round(manualNotAnnualized, 3))
def __testFilteredRangeImpl(self, fromDate, toDate): barFeed = yahoofeed.Feed() barFeed.setBarFilter(csvfeed.DateRangeFilter(fromDate, toDate)) barFeed.addBarsFromCSV( FeedTestCase.TestInstrument, common.get_data_file_path("orcl-2000-yahoofinance.csv")) barFeed.addBarsFromCSV( FeedTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv")) # Dispatch and handle events. handler = BarFeedEventHandler_TestFilterRange( self, FeedTestCase.TestInstrument, fromDate, toDate) barFeed.getNewValuesEvent().subscribe(handler.onBars) while not barFeed.eof(): barFeed.dispatch() self.assertTrue(handler.getEventCount() > 0)
def testDefaultInstrument(self): barFeed = yahoofeed.Feed() self.assertEquals(barFeed.getDefaultInstrument(), None) barFeed.addBarsFromCSV( FeedTestCase.TestInstrument, common.get_data_file_path("orcl-2000-yahoofinance.csv")) self.assertEquals(barFeed.getDefaultInstrument(), FeedTestCase.TestInstrument)
def testMaxRecursion(self): barFeed = ninjatraderfeed.Feed(bar.Frequency.MINUTE) barFeed.addBarsFromCSV("any", common.get_data_file_path("nt-spy-minute-2011.csv")) ema = ma.EMA(barFeed["any"].getPriceDataSeries(), 10) # Load all the feed. barFeed.loadAll() # Check that the max recursion limit bug is not hit when generating the last value first. self.assertEqual(round(ema[-1], 2), 128.81)
def testLocal(self): barFeed = yahoofeed.Feed() instrument = "orcl" barFeed.addBarsFromCSV(instrument, common.get_data_file_path("orcl-2000-yahoofinance.csv")) res = local.run( sma_crossover.SMACrossOver, barFeed, parameters_generator(instrument, 5, 100), logLevel=logging.DEBUG ) self.assertEquals(round(res.getResult(), 2), 1295462.6) self.assertEquals(res.getParameters()[1], 20)
def testNoTrades(self): barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV( "ige", common.get_data_file_path("sharpe-ratio-test-ige.csv")) barFeed.addBarsFromCSV( "spy", common.get_data_file_path("sharpe-ratio-test-spy.csv")) strat = strategy_test.TestStrategy(barFeed, 1000) strat.setBrokerOrdersGTC(True) strat.setUseAdjustedValues(True) stratAnalyzer = drawdown.DrawDown() strat.attachAnalyzer(stratAnalyzer) strat.run() self.assertTrue(strat.getBroker().getCash() == 1000) self.assertEqual(strat.orderUpdatedCalls, 0) self.assertTrue(stratAnalyzer.getMaxDrawDown() == 0) self.assertTrue( stratAnalyzer.getLongestDrawDownDuration() == datetime.timedelta())
def testNoEvents(self): feed = yahoofeed.Feed() feed.addBarsFromCSV( "orcl", common.get_data_file_path("orcl-2000-yahoofinance.csv")) predicate = Predicate([]) eventProfiler = eventprofiler.Profiler(predicate, 5, 5) eventProfiler.run(feed, True) self.assertEqual(eventProfiler.getResults().getEventCount(), 0)
def __test(self, strategyClass, finalValue): feed = yahoofeed.Feed() feed.addBarsFromCSV( "orcl", common.get_data_file_path("orcl-2001-yahoofinance.csv")) myStrategy = strategyClass(feed, 10, 25) myStrategy.run() myStrategy.printDebug("Final result:", round(myStrategy.getFinalValue(), 2)) self.assertTrue(round(myStrategy.getFinalValue(), 2) == finalValue)
def testMapTypeOperations(self): barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV( FeedTestCase.TestInstrument, common.get_data_file_path("orcl-2000-yahoofinance.csv"), marketsession.USEquities.getTimezone()) for dateTime, bars in barFeed: self.assertTrue(FeedTestCase.TestInstrument in bars) self.assertFalse(FeedTestCase.TestInstrument not in bars) bars[FeedTestCase.TestInstrument] with self.assertRaises(KeyError): bars["pirulo"]
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 testOneEvent(self): feed = yahoofeed.Feed() feed.addBarsFromCSV( "orcl", common.get_data_file_path("orcl-2000-yahoofinance.csv")) predicate = Predicate([datetime.date(2000, 1, 11)]) eventProfiler = eventprofiler.Profiler(predicate, 5, 5) eventProfiler.run(feed, True) self.assertEqual(eventProfiler.getResults().getEventCount(), 1) self.assertEqual(eventProfiler.getResults().getValues(0)[0], 1.0) self.assertEqual(round(eventProfiler.getResults().getValues(5)[0], 5), round(1.016745541, 5))
def testNoTrades(self): barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV( "ige", common.get_data_file_path("sharpe-ratio-test-ige.csv")) strat = strategy_test.TestStrategy(barFeed, 1000) stratAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(stratAnalyzer) strat.run() self.assertTrue(strat.getBroker().getCash() == 1000) self.assertTrue(stratAnalyzer.getSharpeRatio(0.04, True) == 0) self.assertTrue(stratAnalyzer.getSharpeRatio(0) == 0) self.assertTrue(stratAnalyzer.getSharpeRatio(0, True) == 0)
def testBaseFeedInterface(self): tmpFeed = TemporarySQLiteFeed(SQLiteFeedTestCase.dbName, bar.Frequency.DAY) with tmpFeed: # Load bars using a Yahoo! feed. yahooFeed = yahoofeed.Feed() yahooFeed.addBarsFromCSV( "orcl", common.get_data_file_path("orcl-2000-yahoofinance.csv"), marketsession.USEquities.timezone) yahooFeed.addBarsFromCSV( "orcl", common.get_data_file_path("orcl-2001-yahoofinance.csv"), marketsession.USEquities.timezone) # Fill the database using the bars from the Yahoo! feed. sqliteFeed = tmpFeed.getFeed() sqliteFeed.getDatabase().addBarsFromFeed(yahooFeed) # Load the SQLite feed and process all bars. sqliteFeed.loadBars("orcl") feed_test.tstBaseFeedInterface(self, sqliteFeed)
def testWithIntegerTimezone(self): try: barFeed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE, -3) self.assertTrue(False, "Exception expected") except Exception as e: self.assertTrue(str(e).find("timezone as an int parameter is not supported anymore") == 0) try: barFeed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE) barFeed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv"), -5) self.assertTrue(False, "Exception expected") except Exception as e: self.assertTrue(str(e).find("timezone as an int parameter is not supported anymore") == 0)
def testBounded(self): barFeed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE, maxLen=2) barFeed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011-03.csv")) barFeed.loadAll() barDS = barFeed["spy"] self.assertEqual(len(barDS), 2) self.assertEqual(len(barDS.getDateTimes()), 2) self.assertEqual(len(barDS.getCloseDataSeries()), 2) self.assertEqual(len(barDS.getCloseDataSeries().getDateTimes()), 2) self.assertEqual(len(barDS.getOpenDataSeries()), 2) self.assertEqual(len(barDS.getHighDataSeries()), 2) self.assertEqual(len(barDS.getLowDataSeries()), 2) self.assertEqual(len(barDS.getAdjCloseDataSeries()), 2)
def testLoadDailyBars(self): tmpFeed = TemporarySQLiteFeed(SQLiteFeedTestCase.dbName, bar.Frequency.DAY) with tmpFeed: # Load bars using a Yahoo! feed. yahooFeed = yahoofeed.Feed() yahooFeed.addBarsFromCSV( "orcl", common.get_data_file_path("orcl-2000-yahoofinance.csv"), marketsession.USEquities.timezone) yahooFeed.addBarsFromCSV( "orcl", common.get_data_file_path("orcl-2001-yahoofinance.csv"), marketsession.USEquities.timezone) # Fill the database using the bars from the Yahoo! feed. sqliteFeed = tmpFeed.getFeed() sqliteFeed.getDatabase().addBarsFromFeed(yahooFeed) # Load the SQLite feed and process all bars. sqliteFeed.loadBars("orcl") for bars in sqliteFeed: pass # Check that both dataseries have the same bars. yahooDS = yahooFeed["orcl"] sqliteDS = sqliteFeed["orcl"] self.assertEqual(len(yahooDS), len(sqliteDS)) for i in range(len(yahooDS)): self.assertEqual(yahooDS[i].getDateTime(), sqliteDS[i].getDateTime()) self.assertEqual(yahooDS[i].getOpen(), sqliteDS[i].getOpen()) self.assertEqual(yahooDS[i].getHigh(), sqliteDS[i].getHigh()) self.assertEqual(yahooDS[i].getLow(), sqliteDS[i].getLow()) self.assertEqual(yahooDS[i].getClose(), sqliteDS[i].getClose()) self.assertEqual(yahooDS[i].getAdjClose(), sqliteDS[i].getAdjClose())
def testCumulativeReturn(self): initialCash = 33.06 barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV(AnalyzerTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv")) strat = position_test.TestStrategy(barFeed, AnalyzerTestCase.TestInstrument, initialCash) strat.addPosEntry(datetime.datetime(2001, 1, 12), strat.enterLong, AnalyzerTestCase.TestInstrument, 1) # 33.06 strat.addPosExitMarket(datetime.datetime(2001, 11, 27)) # 14.32 stratAnalyzer = returns.Returns(maxLen=10) strat.attachAnalyzer(stratAnalyzer) strat.run() self.assertTrue(round(strat.getBroker().getCash(), 2) == round(initialCash + (14.32 - 33.06), 2)) self.assertTrue(round(33.06 * (1 + stratAnalyzer.getCumulativeReturns()[-1]), 2) == 14.32) self.assertEqual(len(stratAnalyzer.getCumulativeReturns()), 10) self.assertEqual(len(stratAnalyzer.getReturns()), 10)
def testLoadFilterFromAndTo(self): feed = barfeed.CSVTradeFeed() feed.addBarsFromCSV(common.get_data_file_path("bitstampUSD.csv"), "bitstampUSD", fromDateTime=dt.as_utc(datetime.datetime(2012, 5, 29)), toDateTime=datetime.datetime(2012, 5, 31)) loaded = [(dateTime, bars) for dateTime, bars in feed] self.assertEquals(len(loaded), 579) self.assertEquals(loaded[0][0], dt.as_utc(datetime.datetime(2012, 5, 29, 1, 47, 52))) self.assertEquals(loaded[0][1]["bitstampUSD"].getDateTime(), dt.as_utc(datetime.datetime(2012, 5, 29, 1, 47, 52))) self.assertEquals(loaded[0][1]["bitstampUSD"].getClose(), 5.07) self.assertEquals(loaded[0][1]["bitstampUSD"].getVolume(), 1.39081288) self.assertEquals(loaded[-1][0], dt.as_utc(datetime.datetime(2012, 5, 30, 23, 49, 21))) self.assertEquals(loaded[-1][1]["bitstampUSD"].getDateTime(), dt.as_utc(datetime.datetime(2012, 5, 30, 23, 49, 21))) self.assertEquals(loaded[-1][1]["bitstampUSD"].getClose(), 5.14) self.assertEquals(loaded[-1][1]["bitstampUSD"].getVolume(), 20)