def testResampleNinjaTraderDay(self): # Resample. feed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE) feed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv")) resampledBarDS = resampled.ResampledBarDataSeries(feed["spy"], barfeed.Frequency.DAY) resampledFile = os.path.join(common.get_temp_path(), "day-nt-spy-minute-2011.csv") resample.resample_to_csv(feed, barfeed.Frequency.DAY, resampledFile) resampledBarDS.pushLast() # Need to manually push the last stot since time didn't change. # Load the resampled file. feed = csvfeed.GenericBarFeed(barfeed.Frequency.DAY) feed.addBarsFromCSV("spy", resampledFile, marketsession.USEquities.getTimezone()) feed.loadAll() self.assertEqual(len(feed["spy"]), 25) self.assertEqual( feed["spy"][0].getDateTime(), dt.localize(datetime.datetime(2011, 1, 3), marketsession.USEquities.getTimezone()), ) self.assertEqual( feed["spy"][-1].getDateTime(), dt.localize(datetime.datetime(2011, 2, 1), marketsession.USEquities.getTimezone()), ) self.assertEqual(len(resampledBarDS), len(feed["spy"])) self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(datetime.datetime(2011, 1, 3))) self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(datetime.datetime(2011, 2, 1)))
def testResampleMtGoxDay(self): # Resample. feed = mtgoxfeed.CSVTradeFeed() feed.addBarsFromCSV(common.get_data_file_path("trades-mgtox-usd-2013-01-01.csv")) resampledBarDS = resampled.ResampledBarDataSeries(feed["BTC"], barfeed.Frequency.DAY) resampledFile = os.path.join(common.get_temp_path(), "day-mgtox-usd-2013-01-01.csv") resample.resample_to_csv(feed, barfeed.Frequency.DAY, resampledFile) resampledBarDS.pushLast() # Need to manually push the last stot since time didn't change. # Load the resampled file. feed = csvfeed.GenericBarFeed(barfeed.Frequency.DAY) feed.addBarsFromCSV("BTC", resampledFile) feed.loadAll() self.assertEqual(len(feed["BTC"]), 1) self.assertEqual(feed["BTC"][0].getDateTime(), datetime.datetime(2013, 1, 1)) self.assertEqual(feed["BTC"][0].getOpen(), 13.51001) self.assertEqual(feed["BTC"][0].getHigh(), 13.56) self.assertEqual(feed["BTC"][0].getLow(), 13.16123) self.assertEqual(feed["BTC"][0].getClose(), 13.30413) self.assertEqual(feed["BTC"][0].getVolume(), 28168.9114596) self.assertEqual(feed["BTC"][0].getAdjClose(), 13.30413) self.assertEqual(len(resampledBarDS), len(feed["BTC"])) self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(feed["BTC"][0].getDateTime())) self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(feed["BTC"][-1].getDateTime())) self.assertEqual(resampledBarDS[0].getOpen(), feed["BTC"][0].getOpen()) self.assertEqual(resampledBarDS[0].getHigh(), feed["BTC"][0].getHigh()) self.assertEqual(resampledBarDS[0].getLow(), feed["BTC"][0].getLow()) self.assertEqual(resampledBarDS[0].getClose(), feed["BTC"][0].getClose()) self.assertEqual(round(resampledBarDS[0].getVolume(), 5), round(feed["BTC"][0].getVolume(), 5)) self.assertEqual(resampledBarDS[0].getAdjClose(), feed["BTC"][0].getAdjClose())
def testResampleMtGoxMinute(self): # Resample. feed = mtgoxfeed.CSVTradeFeed() feed.addBarsFromCSV( common.get_data_file_path("trades-mgtox-usd-2013-01-01.csv")) resampledBarDS = resampled.ResampledBarDataSeries( feed["BTC"], barfeed.Frequency.MINUTE) resampledFile = os.path.join(common.get_temp_path(), "minute-mgtox-usd-2013-01-01.csv") resample.resample_to_csv(feed, barfeed.Frequency.MINUTE, resampledFile) resampledBarDS.pushLast( ) # Need to manually push the last stot since time didn't change. # Load the resampled file. feed = csvfeed.GenericBarFeed(barfeed.Frequency.MINUTE) feed.addBarsFromCSV("BTC", resampledFile) feed.loadAll() self.assertEqual(len(feed["BTC"]), 537) self.assertEqual(feed["BTC"][0].getDateTime(), datetime.datetime(2013, 01, 01, 00, 04, 59)) self.assertEqual(feed["BTC"][-1].getDateTime(), datetime.datetime(2013, 01, 01, 23, 58, 59)) self.assertEqual(len(resampledBarDS), len(feed["BTC"])) self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(feed["BTC"][0].getDateTime())) self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(feed["BTC"][-1].getDateTime()))
def testResampleNinjaTraderHour(self): # Resample. feed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE) feed.addBarsFromCSV("spy", common.get_data_file_path("nt-spy-minute-2011.csv")) resampledBarDS = resampled.ResampledBarDataSeries(feed["spy"], barfeed.Frequency.HOUR) resampledFile = os.path.join(common.get_temp_path(), "hour-nt-spy-minute-2011.csv") resample.resample_to_csv(feed, barfeed.Frequency.HOUR, resampledFile) resampledBarDS.pushLast() # Need to manually push the last stot since time didn't change. # Load the resampled file. feed = csvfeed.GenericBarFeed(barfeed.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 testResampleNinjaTraderDay(self): # Resample. feed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE) feed.addBarsFromCSV( "spy", common.get_data_file_path("nt-spy-minute-2011.csv")) resampledBarDS = resampled.ResampledBarDataSeries( feed["spy"], bar.Frequency.DAY) resampledFile = os.path.join(common.get_temp_path(), "day-nt-spy-minute-2011.csv") resample.resample_to_csv(feed, bar.Frequency.DAY, resampledFile) resampledBarDS.pushLast( ) # Need to manually push the last stot since time didn't change. # Load the resampled file. feed = csvfeed.GenericBarFeed(bar.Frequency.DAY) feed.addBarsFromCSV("spy", resampledFile, marketsession.USEquities.getTimezone()) feed.loadAll() self.assertEqual(len(feed["spy"]), 25) self.assertEqual( feed["spy"][0].getDateTime(), dt.localize(datetime.datetime(2011, 1, 3), marketsession.USEquities.getTimezone())) self.assertEqual( feed["spy"][-1].getDateTime(), dt.localize(datetime.datetime(2011, 2, 1), marketsession.USEquities.getTimezone())) self.assertEqual(len(resampledBarDS), len(feed["spy"])) self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(datetime.datetime(2011, 1, 3))) self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(datetime.datetime(2011, 2, 1)))
def testBaseFeedInterface(self): common.init_temp_path() path = os.path.join(common.get_temp_path(), "trades-mgtox-usd-2013-01-01.csv") tools.download_trades_by_day("usd", 2013, 1, 1, path) bf = barfeed.CSVTradeFeed() bf.addBarsFromCSV(path) feed_test.tstBaseFeedInterface(self, bf)
def testDownloadAndParseDaily(self): instrument = "orcl" common.init_temp_path() path = os.path.join(common.get_temp_path(), "orcl-2010.csv") googlefinance.download_daily_bars(instrument, 2010, path) bf = googlefeed.Feed() bf.addBarsFromCSV(instrument, path) bf.loadAll() self.assertEqual(bf[instrument][-1].getOpen(), 31.22) self.assertEqual(bf[instrument][-1].getClose(), 31.30)
def testDownloadAndParseDaily(self): instrument = "orcl" common.init_temp_path() path = os.path.join(common.get_temp_path(), "orcl-2010.csv") yahoofinance.download_daily_bars(instrument, 2010, path) bf = yahoofeed.Feed() bf.addBarsFromCSV(instrument, path) bf.loadAll() self.assertEqual(bf[instrument][-1].getOpen(), 31.22) self.assertEqual(bf[instrument][-1].getClose(), 31.30)
def testDownloadAndParseWeekly(self): instrument = "aapl" common.init_temp_path() path = os.path.join(common.get_temp_path(), "aapl-weekly-2013.csv") yahoofinance.download_weekly_bars(instrument, 2013, path) bf = yahoofeed.Feed(frequency=bar.Frequency.WEEK) bf.addBarsFromCSV(instrument, path) bf.loadAll() self.assertEqual(round(bf[instrument][-1].getOpen(), 2), 557.46) self.assertEqual(round(bf[instrument][-1].getHigh(), 2), 561.28) self.assertEqual(round(bf[instrument][-1].getLow(), 2), 540.43) self.assertEqual(round(bf[instrument][-1].getClose(), 2), 540.98) self.assertTrue(bf[instrument][-1].getVolume() in (9852500, 9855900, 68991600))
def testDownloadAndParseDaily(self): instrument = "ORCL" common.init_temp_path() path = os.path.join(common.get_temp_path(), "quandl-daily-orcl-2010.csv") quandl.download_daily_bars("WIKI", instrument, 2010, path, auth_token) bf = quandlfeed.Feed() bf.addBarsFromCSV(instrument, path) bf.loadAll() self.assertEquals(bf[instrument][-1].getDateTime(), datetime.datetime(2010, 12, 31)) self.assertEquals(bf[instrument][-1].getOpen(), 31.22) self.assertEquals(bf[instrument][-1].getHigh(), 31.33) self.assertEquals(bf[instrument][-1].getLow(), 30.93) self.assertEquals(bf[instrument][-1].getClose(), 31.3) self.assertEquals(bf[instrument][-1].getVolume(), 11716300) self.assertEquals(bf[instrument][-1].getAdjClose(), 30.23179912467581)
def testDownloadAndParseDailyNoAdjClose(self): instrument = "ORCL" common.init_temp_path() path = os.path.join(common.get_temp_path(), "quandl-daily-orcl-2013.csv") quandl.download_daily_bars("GOOG", "NASDAQ_ORCL", 2013, path, auth_token) bf = quandlfeed.Feed() bf.setNoAdjClose() bf.addBarsFromCSV(instrument, path) bf.loadAll() self.assertEquals(bf[instrument][-1].getDateTime(), datetime.datetime(2013, 12, 31)) self.assertEquals(bf[instrument][-1].getOpen(), 37.94) self.assertEquals(bf[instrument][-1].getHigh(), 38.34) self.assertEquals(bf[instrument][-1].getLow(), 37.88) self.assertEquals(bf[instrument][-1].getClose(), 38.26) self.assertEquals(bf[instrument][-1].getVolume(), 11747517) self.assertEquals(bf[instrument][-1].getAdjClose(), None)
def testDownloadAndParse(self): common.init_temp_path() path = os.path.join(common.get_temp_path(), "trades-mgtox-usd-2013-01-01.csv") tools.download_trades_by_day("usd", 2013, 1, 1, path) bf = barfeed.CSVTradeFeed() bf.addBarsFromCSV(path) bf.loadAll() ds = bf["BTC"] self.assertTrue(len(ds) > 0) self.assertEqual(ds[-1].getOpen(), ds[-1].getHigh()) self.assertEqual(ds[-1].getHigh(), ds[-1].getLow()) self.assertEqual(ds[-1].getLow(), ds[-1].getClose()) self.assertEqual(ds[-1].getClose(), 13.30413) self.assertEqual(ds[-1].getVolume(), 0.01) self.assertEqual(ds[-1].getTradeType(), "ask") self.assertEqual(ds[-1].getDateTime().date(), datetime.date(2013, 1, 1))
def testDownloadAndParseWeekly(self): instrument = "AAPL" common.init_temp_path() path = os.path.join(common.get_temp_path(), "quandl-aapl-weekly-2010.csv") quandl.download_weekly_bars("WIKI", instrument, 2010, path, auth_token) bf = quandlfeed.Feed(frequency=bar.Frequency.WEEK) bf.addBarsFromCSV(instrument, path) bf.loadAll() self.assertEquals(bf[instrument][0].getDateTime(), datetime.datetime(2010, 1, 3)) self.assertEquals(bf[instrument][-1].getDateTime(), datetime.datetime(2010, 12, 26)) self.assertEquals(bf[instrument][-1].getOpen(), 325.0) self.assertEquals(bf[instrument][-1].getHigh(), 325.15) self.assertEquals(bf[instrument][-1].getLow(), 323.17) self.assertEquals(bf[instrument][-1].getClose(), 323.6) self.assertEquals(bf[instrument][-1].getVolume(), 7969900) # Not checking against a specific value since this is going to change # as time passes by. self.assertNotEquals(bf[instrument][-1].getAdjClose(), None)
def testResampleNinjaTraderHour(self): common.init_temp_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(common.get_temp_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 testResampleMtGoxDay(self): # Resample. feed = mtgoxfeed.CSVTradeFeed() feed.addBarsFromCSV( common.get_data_file_path("trades-mgtox-usd-2013-01-01.csv")) resampledBarDS = resampled.ResampledBarDataSeries( feed["BTC"], barfeed.Frequency.DAY) resampledFile = os.path.join(common.get_temp_path(), "day-mgtox-usd-2013-01-01.csv") resample.resample_to_csv(feed, barfeed.Frequency.DAY, resampledFile) resampledBarDS.pushLast( ) # Need to manually push the last stot since time didn't change. # Load the resampled file. feed = csvfeed.GenericBarFeed(barfeed.Frequency.DAY) feed.addBarsFromCSV("BTC", resampledFile) feed.loadAll() self.assertEqual(len(feed["BTC"]), 1) self.assertEqual(feed["BTC"][0].getDateTime(), datetime.datetime(2013, 01, 01, 23, 59, 59)) self.assertEqual(feed["BTC"][0].getOpen(), 13.51001) self.assertEqual(feed["BTC"][0].getHigh(), 13.56) self.assertEqual(feed["BTC"][0].getLow(), 13.16123) self.assertEqual(feed["BTC"][0].getClose(), 13.30413) self.assertEqual(feed["BTC"][0].getVolume(), 28168.9114596) self.assertEqual(feed["BTC"][0].getAdjClose(), 13.30413) self.assertEqual(len(resampledBarDS), len(feed["BTC"])) self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(feed["BTC"][0].getDateTime())) self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(feed["BTC"][-1].getDateTime())) self.assertEqual(resampledBarDS[0].getOpen(), feed["BTC"][0].getOpen()) self.assertEqual(resampledBarDS[0].getHigh(), feed["BTC"][0].getHigh()) self.assertEqual(resampledBarDS[0].getLow(), feed["BTC"][0].getLow()) self.assertEqual(resampledBarDS[0].getClose(), feed["BTC"][0].getClose()) self.assertEqual(round(resampledBarDS[0].getVolume(), 5), round(feed["BTC"][0].getVolume(), 5)) self.assertEqual(resampledBarDS[0].getAdjClose(), feed["BTC"][0].getAdjClose())
def testDownloadAndParseDaily_UseAdjClose(self): instrument = "ORCL" common.init_temp_path() path = os.path.join(common.get_temp_path(), "quandl-daily-orcl-2010.csv") quandl.download_daily_bars("WIKI", instrument, 2010, path, auth_token) bf = quandlfeed.Feed() bf.addBarsFromCSV(instrument, path) # Need to setUseAdjustedValues(True) after loading the file because we # can't tell in advance if adjusted values are there or not. bf.setUseAdjustedValues(True) bf.loadAll() self.assertEquals(bf[instrument][-1].getDateTime(), datetime.datetime(2010, 12, 31)) self.assertEquals(bf[instrument][-1].getOpen(), 31.22) self.assertEquals(bf[instrument][-1].getHigh(), 31.33) self.assertEquals(bf[instrument][-1].getLow(), 30.93) self.assertEquals(bf[instrument][-1].getClose(), 31.3) self.assertEquals(bf[instrument][-1].getVolume(), 11716300) self.assertEquals(bf[instrument][-1].getAdjClose(), 30.23179912467581) self.assertEquals(bf[instrument][-1].getPrice(), 30.23179912467581)
def testResampleMtGoxHour(self): # Resample. feed = mtgoxfeed.CSVTradeFeed() feed.addBarsFromCSV(common.get_data_file_path("trades-mgtox-usd-2013-01-01.csv")) resampledBarDS = resampled.ResampledBarDataSeries(feed["BTC"], barfeed.Frequency.HOUR) resampledFile = os.path.join(common.get_temp_path(), "hour-mgtox-usd-2013-01-01.csv") resample.resample_to_csv(feed, barfeed.Frequency.HOUR, resampledFile) resampledBarDS.pushLast() # Need to manually push the last stot since time didn't change. # Load the resampled file. feed = csvfeed.GenericBarFeed(barfeed.Frequency.HOUR) feed.addBarsFromCSV("BTC", resampledFile) feed.loadAll() self.assertEqual(len(feed["BTC"]), 24) self.assertEqual(feed["BTC"][0].getDateTime(), datetime.datetime(2013, 1, 1)) self.assertEqual(feed["BTC"][-1].getDateTime(), datetime.datetime(2013, 1, 1, 23)) self.assertEqual(len(resampledBarDS), len(feed["BTC"])) self.assertEqual(resampledBarDS[0].getDateTime(), dt.as_utc(feed["BTC"][0].getDateTime())) self.assertEqual(resampledBarDS[-1].getDateTime(), dt.as_utc(feed["BTC"][-1].getDateTime()))