def main(plot): instrument = "yhoo" bBandsPeriod = 40 # Download the bars. feed = quandl.build_feed("WIKI", [instrument], 2011, 2012, ".") strat = BBands(feed, instrument, bBandsPeriod) sharpeRatioAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(sharpeRatioAnalyzer) if plot: plt = plotter.StrategyPlotter(strat, True, True, True) plt.getInstrumentSubplot(instrument).addDataSeries( "upper", strat.getBollingerBands().getUpperBand()) plt.getInstrumentSubplot(instrument).addDataSeries( "middle", strat.getBollingerBands().getMiddleBand()) plt.getInstrumentSubplot(instrument).addDataSeries( "lower", strat.getBollingerBands().getLowerBand()) strat.run() print("Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05)) if plot: plt.plot()
def main(plot): instruments = ["GORO"] # Download GORO bars using WIKI source code. feed = quandl.build_feed("WIKI", instruments, 2006, 2012, ".") # Load Quandl CSV downloaded from http://www.quandl.com/OFDP-Open-Financial-Data-Project/GOLD_2-LBMA-Gold-Price-London-Fixings-P-M quandlFeed = csvfeed.Feed("Date", "%Y-%m-%d") quandlFeed.setDateRange(datetime.datetime(2006, 1, 1), datetime.datetime(2012, 12, 31)) quandlFeed.addValuesFromCSV("quandl_gold_2.csv") myStrategy = MyStrategy(feed, quandlFeed, instruments[0]) if plot: plt = plotter.StrategyPlotter(myStrategy, True, False, False) plt.getOrCreateSubplot("quandl").addDataSeries("USD", quandlFeed["USD"]) plt.getOrCreateSubplot("quandl").addDataSeries("EUR", quandlFeed["EUR"]) plt.getOrCreateSubplot("quandl").addDataSeries("GBP", quandlFeed["GBP"]) myStrategy.run() if plot: plt.plot()
def testInvalidInstrument(self): instrument = "inexistent" # Don't skip errors. with self.assertRaisesRegex(Exception, "404 Client Error: Not Found"): with common.TmpDir() as tmpPath: quandl.build_feed( instrument, [instrument], 2010, 2010, tmpPath, bar.Frequency.WEEK, authToken=QUANDL_API_KEY ) # Skip errors. with common.TmpDir() as tmpPath: bf = quandl.build_feed( instrument, [instrument], 2010, 2010, tmpPath, bar.Frequency.WEEK, skipErrors=True, authToken=QUANDL_API_KEY ) bf.loadAll() self.assertNotIn(instrument, bf)
def main(plot): instruments = ["IBM", "AES", "AIG"] feed = quandl.build_feed("WIKI", instruments, 2008, 2009, ".") predicate = BuyOnGap(feed) eventProfiler = eventprofiler.Profiler(predicate, 5, 5) eventProfiler.run(feed, True) results = eventProfiler.getResults() print("%d events found" % (results.getEventCount())) if plot: eventprofiler.plot(results)
def testNoAdjClose(self): with common.TmpDir() as tmpPath: columnNames = { "open": "Last", "close": "Last", "adj_close": None } bf = quandl.build_feed( "BITSTAMP", ["USD"], 2014, 2014, tmpPath, columnNames=columnNames, authToken=QUANDL_API_KEY ) bf.loadAll() self.assertFalse(bf.barsHaveAdjClose()) self.assertEqual(bf["USD"][-1].getAdjClose(), None)
def testBuildFeedDaily(self): with common.TmpDir() as tmpPath: instrument = "ORCL" bf = quandl.build_feed("WIKI", [instrument], 2010, 2010, tmpPath, authToken=QUANDL_API_KEY) bf.loadAll() self.assertEqual(bf[instrument][-1].getDateTime(), datetime.datetime(2010, 12, 31)) self.assertEqual(bf[instrument][-1].getOpen(), 31.22) self.assertEqual(bf[instrument][-1].getHigh(), 31.33) self.assertEqual(bf[instrument][-1].getLow(), 30.93) self.assertEqual(bf[instrument][-1].getClose(), 31.3) self.assertEqual(bf[instrument][-1].getVolume(), 11716300) self.assertEqual(bf[instrument][-1].getPrice(), 31.3) # Not checking against a specific value since this is going to change # as time passes by. self.assertNotEqual(bf[instrument][-1].getAdjClose(), None)
def testExtraColumns(self): with common.TmpDir() as tmpPath: columnNames = { "open": "Last", "close": "Last" } bf = quandl.build_feed( "BITSTAMP", ["USD"], 2014, 2014, tmpPath, columnNames=columnNames, authToken=QUANDL_API_KEY ) bf.loadAll() self.assertEqual(len(bf["USD"][-1].getExtraColumns()), 3) self.assertEqual(bf["USD"][-1].getExtraColumns()["Bid"], 319.19) self.assertEqual(bf["USD"][-1].getExtraColumns()["Ask"], 319.63) bids = bf["USD"].getExtraDataSeries("Bid") self.assertEqual(bids[-1], 319.19)
def testBuildFeedDailyCreatingDir(self): tmpPath = tempfile.mkdtemp() shutil.rmtree(tmpPath) try: instrument = "ORCL" bf = quandl.build_feed("WIKI", [instrument], 2010, 2010, tmpPath, authToken=QUANDL_API_KEY) bf.loadAll() self.assertEqual(bf[instrument][-1].getDateTime(), datetime.datetime(2010, 12, 31)) self.assertEqual(bf[instrument][-1].getOpen(), 31.22) self.assertEqual(bf[instrument][-1].getHigh(), 31.33) self.assertEqual(bf[instrument][-1].getLow(), 30.93) self.assertEqual(bf[instrument][-1].getClose(), 31.3) self.assertEqual(bf[instrument][-1].getVolume(), 11716300) self.assertEqual(bf[instrument][-1].getPrice(), 31.3) # Not checking against a specific value since this is going to change # as time passes by. self.assertNotEqual(bf[instrument][-1].getAdjClose(), None) finally: shutil.rmtree(tmpPath)
def main(plot): instrument = "AAPL" smaPeriod = 163 # Download the bars. feed = quandl.build_feed("WIKI", [instrument], 2011, 2012, ".") strat = sma_crossover.SMACrossOver(feed, instrument, smaPeriod) sharpeRatioAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(sharpeRatioAnalyzer) if plot: plt = plotter.StrategyPlotter(strat, True, False, True) plt.getInstrumentSubplot(instrument).addDataSeries( "sma", strat.getSMA()) strat.run() print("Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05)) if plot: plt.plot()
def main(plot): instrument = "AAPL" vwapWindowSize = 5 threshold = 0.01 # Download the bars. feed = quandl.build_feed("WIKI", [instrument], 2011, 2012, ".") strat = VWAPMomentum(feed, instrument, vwapWindowSize, threshold) sharpeRatioAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(sharpeRatioAnalyzer) if plot: plt = plotter.StrategyPlotter(strat, True, False, True) plt.getInstrumentSubplot(instrument).addDataSeries("vwap", strat.getVWAP()) strat.run() print("Sharpe ratio: %.2f" % sharpeRatioAnalyzer.getSharpeRatio(0.05)) if plot: plt.plot()
def testMapColumnNames(self): column_names = { "open": "Price", "close": "Price", } with common.TmpDir() as tmpPath: instrument = "IWG" year = 2017 bf = quandl.build_feed( "LSE", [instrument], year, year, tmpPath, columnNames=column_names, skipMalformedBars=True, authToken=QUANDL_API_KEY ) bf.setNoAdjClose() bf.loadAll() self.assertEqual(bf[instrument][0].getDateTime(), datetime.datetime(year, 1, 3)) self.assertEqual(bf[instrument][0].getOpen(), 237.80) self.assertEqual(bf[instrument][0].getHigh(), 247.00) self.assertEqual(bf[instrument][0].getLow(), 236.30) self.assertEqual(bf[instrument][0].getClose(), 237.80) self.assertEqual(bf[instrument][0].getVolume(), 3494173) self.assertEqual(bf[instrument][0].getAdjClose(), None) self.assertEqual(bf[instrument][0].getPrice(), 237.80)
def testBuildFeedWeekly(self): with common.TmpDir() as tmpPath: instrument = "AAPL" bf = quandl.build_feed( "WIKI", [instrument], 2010, 2010, tmpPath, bar.Frequency.WEEK, authToken=QUANDL_API_KEY ) bf.loadAll() # Quandl used to report 2010-1-3 as the first week of 2010. self.assertTrue( bf[instrument][0].getDateTime() in [datetime.datetime(2010, 1, 3), datetime.datetime(2010, 1, 10)] ) self.assertEqual(bf[instrument][-1].getDateTime(), datetime.datetime(2010, 12, 26)) self.assertEqual(bf[instrument][-1].getOpen(), 325.0) self.assertEqual(bf[instrument][-1].getHigh(), 325.15) self.assertEqual(bf[instrument][-1].getLow(), 323.17) self.assertEqual(bf[instrument][-1].getClose(), 323.6) self.assertEqual(bf[instrument][-1].getVolume(), 7969900) self.assertEqual(bf[instrument][-1].getPrice(), 323.6) # Not checking against a specific value since this is going to change # as time passes by. self.assertNotEqual(bf[instrument][-1].getAdjClose(), None)