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 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 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 testGoogle2011(self): initialValue = 1000000 barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV(AnalyzerTestCase.TestInstrument, common.get_data_file_path("goog-2011-yahoofinance.csv")) strat = strategy_test.TestStrategy(barFeed, initialValue) order = strat.getBroker().createMarketOrder(broker.Order.Action.BUY, AnalyzerTestCase.TestInstrument, 1654, True) # 2011-01-03 close: 604.35 strat.getBroker().submitOrder(order) stratAnalyzer = returns.Returns() strat.attachAnalyzer(stratAnalyzer) strat.run() finalValue = strat.getBroker().getEquity() self.assertEqual(round(stratAnalyzer.getCumulativeReturns()[-1], 4), round((finalValue - initialValue) / float(initialValue), 4))
def __testManualImpl(self, closingPrices, cash): barFeed = TestBarFeed(bar.Frequency.DAY) bars = build_bars_from_closing_prices(closingPrices) barFeed.addBarsFromSequence("orcl", bars) strat = strategy_test.TestStrategy(barFeed, cash) stratAnalyzer = drawdown.DrawDown() strat.attachAnalyzer(stratAnalyzer) # Manually place the order to get it filled on the first bar. order = strat.getBroker().createMarketOrder(broker.Order.Action.BUY, "orcl", 1, True) order.setGoodTillCanceled(True) strat.getBroker().submitOrder(order) strat.run() return stratAnalyzer
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 testFirstBar(self): initialCash = 1000 barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV(AnalyzerTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv")) strat = strategy_test.TestStrategy(barFeed, initialCash) strat.addOrder(datetime.datetime(2001, 1, 2), strat.getBroker().createMarketOrder, broker.Order.Action.BUY, AnalyzerTestCase.TestInstrument, 1, False) # 2001-01-03 Open: 25.25 Close: 32.00 stratAnalyzer = returns.Returns() strat.attachAnalyzer(stratAnalyzer) strat.run() self.assertEqual(stratAnalyzer.getReturns()[0], 0) self.assertEqual(stratAnalyzer.getReturns()[1], (32.00 - 25.25) / 1000) # Check date times. datetimes = barFeed[AnalyzerTestCase.TestInstrument].getDateTimes() for i in [0, -1]: self.assertEqual(stratAnalyzer.getReturns().getDateTimes()[i], datetimes[i]) self.assertEqual(stratAnalyzer.getCumulativeReturns().getDateTimes()[i], datetimes[i])
def testTwoBarReturns_CloseClose(self): initialCash = 15.90 barFeed = yahoofeed.Feed() barFeed.setBarFilter(csvfeed.DateRangeFilter(datetime.datetime(2001, 12, 6), datetime.datetime(2001, 12, 7))) barFeed.addBarsFromCSV(AnalyzerTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv")) strat = strategy_test.TestStrategy(barFeed, initialCash) # 2001-12-06,15.61,16.03,15.50,15.90,66944900,15.55 # 2001-12-07,15.74,15.95,15.55,15.91,42463200,15.56 # Manually place the entry order, to get it filled on the first bar. order = strat.getBroker().createMarketOrder(broker.Order.Action.BUY, AnalyzerTestCase.TestInstrument, 1, True) # Close: 15.90 strat.getBroker().submitOrder(order) strat.addOrder(datetime.datetime(2001, 12, 6), strat.getBroker().createMarketOrder, broker.Order.Action.SELL, AnalyzerTestCase.TestInstrument, 1, True) # Close: 15.91 stratAnalyzer = returns.Returns() strat.attachAnalyzer(stratAnalyzer) strat.run() self.assertTrue(strat.getBroker().getCash() == initialCash + (15.91 - 15.90)) # First day returns: 0 self.assertTrue(stratAnalyzer.getReturns()[0] == 0) # Second day returns: Open vs Prev. day's close self.assertTrue(stratAnalyzer.getReturns()[1] == (15.91 - 15.90) / 15.90)
def testOneBarReturn(self): initialCash = 1000 barFeed = yahoofeed.Feed() barFeed.setBarFilter(csvfeed.DateRangeFilter(datetime.datetime(2001, 12, 7), datetime.datetime(2001, 12, 7))) barFeed.addBarsFromCSV(AnalyzerTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv")) strat = strategy_test.TestStrategy(barFeed, initialCash) # 2001-12-07,15.74,15.95,15.55,15.91,42463200,15.56 # Manually place the orders to get them filled on the first (and only) bar. order = strat.getBroker().createMarketOrder(broker.Order.Action.BUY, AnalyzerTestCase.TestInstrument, 1, False) # Open: 15.74 strat.getBroker().submitOrder(order) order = strat.getBroker().createMarketOrder(broker.Order.Action.SELL, AnalyzerTestCase.TestInstrument, 1, True) # Close: 15.91 strat.getBroker().submitOrder(order) stratAnalyzer = returns.Returns() strat.attachAnalyzer(stratAnalyzer) strat.run() self.assertTrue(strat.getBroker().getCash() == initialCash + (15.91 - 15.74)) finalValue = 1000 - 15.74 + 15.91 rets = (finalValue - initialCash) / float(initialCash) self.assertEqual(stratAnalyzer.getReturns()[-1], rets)
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 = drawdown.DrawDown() 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.assertTrue( round(strat.getBroker().getCash(), 2) == initialCash + (127.64 - 42.09) * quantity) self.assertEqual(strat.orderUpdatedCalls, 6) self.assertTrue(round(stratAnalyzer.getMaxDrawDown(), 5) == 0.31178) self.assertTrue( stratAnalyzer.getLongestDrawDownDuration() == datetime.timedelta( days=623))
def __createStrategy(self): barFeed = self.__loadBarFeed() return strategy_test.TestStrategy(barFeed, 1000)