def testOneBarReturn(self): initialCash = 1000 barFeed = yahoofeed.Feed() barFeed.setBarFilter( csvfeed.DateRangeFilter( strategy_test.datetime_from_date(2001, 12, 07), strategy_test.datetime_from_date(2001, 12, 07))) barFeed.addBarsFromCSV( ReturnsTestCase.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, ReturnsTestCase.TestInstrument, 1, False) # Open: 15.74 strat.getBroker().placeOrder(order) order = strat.getBroker().createMarketOrder( broker.Order.Action.SELL, ReturnsTestCase.TestInstrument, 1, True) # Close: 15.91 strat.getBroker().placeOrder(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 testTwoBarReturns_CloseClose(self): initialCash = 15.90 barFeed = yahoofeed.Feed() barFeed.setBarFilter( csvfeed.DateRangeFilter( strategy_test.datetime_from_date(2001, 12, 06), strategy_test.datetime_from_date(2001, 12, 07))) barFeed.addBarsFromCSV( ReturnsTestCase.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, ReturnsTestCase.TestInstrument, 1, True) # Close: 15.90 strat.getBroker().placeOrder(order) strat.addOrder(strategy_test.datetime_from_date(2001, 12, 06), strat.getBroker().createMarketOrder, broker.Order.Action.SELL, ReturnsTestCase.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 __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.getBroker().setUseAdjustedValues(True) strat.setBrokerOrdersGTC(True) 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, "ige", quantity, True) # Adj. Close: 42.09 order.setGoodTillCanceled(True) strat.getBroker().placeOrder(order) strat.addOrder(strategy_test.datetime_from_date(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.assertTrue(strat.getOrderUpdatedEvents() == 4) self.assertTrue(round(stratAnalyzer.getMaxDrawDown(), 5) == 0.31178) self.assertTrue( stratAnalyzer.getLongestDrawDownDuration() == datetime.timedelta( days=623))
def testIGE_BrokerWithCommission(self): commision = 0.5 initialCash = 42.09 + commision # This testcase is based on an example from Ernie Chan's book: # 'Quantitative Trading: How to Build Your Own Algorithmic Trading Business' bar_feed = yahoofeed.Feed() bar_feed.add_bars_from_csv( "ige", common.get_data_file_path("sharpe-ratio-test-ige.csv")) brk = backtesting.Broker(initialCash, bar_feed, backtesting.FixedCommission(commision)) strat = strategy_test.TestStrategy(bar_feed, initialCash, brk) strat.get_broker().set_use_adj_values(True) strat.setBrokerOrdersGTC(True) stratAnalyzer = sharpe.SharpeRatio() strat.attach_analyzer(stratAnalyzer) # Manually place the order to get it filled on the first bar. order = strat.get_broker().create_market_order( broker.Order.Action.BUY, "ige", 1, True) # Adj. Close: 42.09 order.set_good_until_canceled(True) strat.get_broker().place_order(order) strat.addOrder(strategy_test.datetime_from_date(2007, 11, 13), strat.get_broker().create_market_order, broker.Order.Action.SELL, "ige", 1, True) # Adj. Close: 127.64 strat.run() self.assertTrue( round(strat.get_broker().get_cash(), 2) == initialCash + (127.64 - 42.09 - commision * 2)) self.assertTrue(strat.get_order_updated_events() == 2) # The results are slightly different only because I'm taking into account the first bar as well, # and I'm also adding commissions. self.assertEqual( round(stratAnalyzer.get_sharpe_ratio(0.04, 252, annualized=True), 6), 0.776443)
def testSharpeRatioIGE_SPY_Broker(self): # 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")) barFeed.addBarsFromCSV("spy", common.get_data_file_path("sharpe-ratio-test-spy.csv")) strat = strategy_test.TestStrategy(barFeed, 1000) strat.getBroker().setUseAdjustedValues(True) strat.setBrokerOrdersGTC(True) stratAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(stratAnalyzer) # Manually place IGE order to get it filled on the first bar. order = strat.getBroker().createMarketOrder(broker.Order.Action.BUY, "ige", 1, True) # Adj. Close: 42.09 order.setGoodTillCanceled(True) strat.getBroker().placeOrder(order) strat.addOrder(strategy_test.datetime_from_date(2007, 11, 13), strat.getBroker().createMarketOrder, broker.Order.Action.SELL, "ige", 1, True) # Adj. Close: 127.64 # Manually place SPY order to get it filled on the first bar. order = strat.getBroker().createMarketOrder(broker.Order.Action.SELL_SHORT, "spy", 1, True) # Adj. Close: 105.52 order.setGoodTillCanceled(True) strat.getBroker().placeOrder(order) strat.addOrder(strategy_test.datetime_from_date(2007, 11, 13), strat.getBroker().createMarketOrder, broker.Order.Action.BUY_TO_COVER, "spy", 1, True) # Adj. Close: 147.67 strat.run() self.assertTrue(round(strat.getBroker().getCash(), 2) == round(1000 + (127.64 - 42.09) + (105.52 - 147.67), 2)) self.assertTrue(strat.getOrderUpdatedEvents() == 4)
def testIGE_BrokerWithCommission(self): # 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")) brk = backtesting.Broker(1000, barFeed, broker.FixedCommission(0.5)) strat = strategy_test.TestStrategy(barFeed, 1000, brk) strat.getBroker().setUseAdjustedValues(True) strat.setBrokerOrdersGTC(True) stratAnalyzer = sharpe.SharpeRatio() strat.attachAnalyzer(stratAnalyzer) # Manually place the order to get it filled on the first bar. order = strat.getBroker().createMarketOrder(broker.Order.Action.BUY, "ige", 1, True) # Adj. Close: 42.09 order.setGoodTillCanceled(True) strat.getBroker().placeOrder(order) strat.addOrder(datetime.datetime(2007, 11, 13), strat.getBroker().createMarketOrder, broker.Order.Action.SELL, "ige", 1, True) # Adj. Close: 127.64 strat.run() self.assertTrue( round(strat.getBroker().getCash(), 2) == 1000 + (127.64 - 42.09 - 0.5 * 2)) self.assertTrue(strat.getOrderUpdatedEvents() == 2) # The results are slightly different different only because I'm taking into account the first bar as well, # and I'm also adding commissions. self.assertTrue( round(stratAnalyzer.getSharpeRatio(0.04, 252, annualized=True), 4) == 0.7763)
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 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, 01, 02), 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_CloseOpen(self): barFeed = yahoofeed.Feed() barFeed.setBarFilter( csvfeed.DateRangeFilter(datetime.datetime(2001, 12, 06), datetime.datetime(2001, 12, 07))) barFeed.addBarsFromCSV( ReturnsTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv")) strat = strategy_test.TestStrategy(barFeed, 1000) # 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, ReturnsTestCase.TestInstrument, 1, True) # Close: 15.90 strat.getBroker().placeOrder(order) strat.addOrder(datetime.datetime(2001, 12, 06), strat.getBroker().createMarketOrder, broker.Order.Action.SELL, ReturnsTestCase.TestInstrument, 1, False) # Open: 15.74 returnsDS = returns.ReturnsDataSeries(strat) strat.run() self.assertTrue(strat.getBroker().getCash() == 1000 + (15.74 - 15.90)) # First day returns: 0 self.assertTrue(returnsDS.getValueAbsolute(0) == 0) # Second day returns: Open vs Prev. day's close self.assertTrue( returnsDS.getValueAbsolute(1) == (15.74 - 15.90) / 15.90)
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 = googlefeed.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 testIGE_BrokerWithCommission(self): commision = 0.5 initialCash = 42.09 + commision # 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.getBroker().setCommission(backtesting.FixedPerTrade(commision)) 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", 1, True) # Adj. Close: 42.09 order.setGoodTillCanceled(True) strat.getBroker().placeOrder(order) strat.addOrder(datetime.datetime(2007, 11, 13), strat.getBroker().createMarketOrder, broker.Order.Action.SELL, "ige", 1, True) # Adj. Close: 127.64 strat.run() self.assertTrue(round(strat.getBroker().getCash(), 2) == initialCash + (127.64 - 42.09 - commision*2)) self.assertEqual(strat.orderUpdatedCalls, 4) # The results are slightly different only because I'm taking into account the first bar as well, # and I'm also adding commissions. self.assertEqual(round(stratAnalyzer.getSharpeRatio(0.04, True), 6), 0.776443)
def __createStrategy(self): bar_feed = ninjatraderfeed.Feed(ninjatraderfeed.Frequency.MINUTE) bar_filter = csvfeed.USEquitiesRTH() bar_feed.set_bar_filter(bar_filter) bar_feed.add_bars_from_csv( TradesAnalyzerTestCase.TestInstrument, common.get_data_file_path("nt-spy-minute-2011.csv")) return strategy_test.TestStrategy(bar_feed, 1000)
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.order("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(ReturnsTestCase.TestInstrument, common.get_data_file_path("goog-2011-yahoofinance.csv")) strat = strategy_test.TestStrategy(barFeed, initialValue) order = strat.getBroker().createMarketOrder(broker.Order.Action.BUY, ReturnsTestCase.TestInstrument, 1654, True) # 2011-01-03 close: 604.35 strat.getBroker().placeOrder(order) stratAnalyzer = returns.Returns() strat.attachAnalyzer(stratAnalyzer) strat.run() finalValue = strat.getBroker().getValue() self.assertEqual(round(stratAnalyzer.getCumulativeReturns()[-1], 4), round((finalValue - initialValue) / float(initialValue), 4))
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.getBroker().setUseAdjustedValues(True) stratAnalyzer = drawdown.DrawDown() strat.attachAnalyzer(stratAnalyzer) strat.run() self.assertTrue(strat.getBroker().getCash() == 1000) self.assertTrue(strat.getOrderUpdatedEvents() == 0) self.assertTrue(stratAnalyzer.getMaxDrawDown() == 0) self.assertTrue(stratAnalyzer.getMaxDrawDownDuration()== 0)
def testNoTrades(self): bar_feed = yahoofeed.Feed() bar_feed.add_bars_from_csv("ige", common.get_data_file_path("sharpe-ratio-test-ige.csv")) bar_feed.add_bars_from_csv("spy", common.get_data_file_path("sharpe-ratio-test-spy.csv")) strat = strategy_test.TestStrategy(bar_feed, 1000) strat.setBrokerOrdersGTC(True) strat.get_broker().set_use_adj_values(True) stratAnalyzer = drawdown.DrawDown() strat.attach_analyzer(stratAnalyzer) strat.run() self.assertTrue(strat.get_broker().get_cash() == 1000) self.assertTrue(strat.get_order_updated_events() == 0) self.assertTrue(stratAnalyzer.get_max_draw_down() == 0) self.assertTrue(stratAnalyzer.get_longest_draw_down_duration()== 0)
def testNoTrades(self): bar_feed = yahoofeed.Feed() bar_feed.add_bars_from_csv( "ige", common.get_data_file_path("sharpe-ratio-test-ige.csv")) strat = strategy_test.TestStrategy(bar_feed, 1000) stratAnalyzer = sharpe.SharpeRatio() strat.attach_analyzer(stratAnalyzer) strat.run() self.assertTrue(strat.get_broker().get_cash() == 1000) self.assertTrue( stratAnalyzer.get_sharpe_ratio(0.04, 252, annualized=True) == 0) self.assertTrue(stratAnalyzer.get_sharpe_ratio(0, 252) == 0) self.assertTrue( stratAnalyzer.get_sharpe_ratio(0, 252, annualized=True) == 0)
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 __testManualImpl(self, closingPrices, cash): bar_feed = membf.Feed(bar.Frequency.DAY) bars = build_bars_from_closing_prices(closingPrices) bar_feed.add_bars_from_sequence("orcl", bars) strat = strategy_test.TestStrategy(bar_feed, cash) stratAnalyzer = drawdown.DrawDown() strat.attach_analyzer(stratAnalyzer) # Manually place the order to get it filled on the first bar. order = strat.get_broker().create_market_order(broker.Order.Action.BUY, "orcl", 1, True) order.set_good_until_canceled(True) strat.get_broker().place_order(order) strat.run() return stratAnalyzer
def testSharpeRatioIGE_SPY_Broker(self): initialCash = 42.09 # This testcase is based on an example from Ernie Chan's book: # 'Quantitative Trading: How to Build Your Own Algorithmic Trading Business' bar_feed = yahoofeed.Feed() bar_feed.add_bars_from_csv( "ige", common.get_data_file_path("sharpe-ratio-test-ige.csv")) bar_feed.add_bars_from_csv( "spy", common.get_data_file_path("sharpe-ratio-test-spy.csv")) strat = strategy_test.TestStrategy(bar_feed, initialCash) strat.get_broker().set_use_adj_values(True) strat.setBrokerOrdersGTC(True) stratAnalyzer = sharpe.SharpeRatio() strat.attach_analyzer(stratAnalyzer) # Manually place IGE order to get it filled on the first bar. order = strat.get_broker().create_market_order( broker.Order.Action.BUY, "ige", 1, True) # Adj. Close: 42.09 order.set_good_until_canceled(True) strat.get_broker().place_order(order) # Manually place SPY order to get it filled on the first bar. order = strat.get_broker().create_market_order( broker.Order.Action.SELL_SHORT, "spy", 1, True) # Adj. Close: 105.52 order.set_good_until_canceled(True) strat.get_broker().place_order(order) strat.addOrder(strategy_test.datetime_from_date(2007, 11, 13), strat.get_broker().create_market_order, broker.Order.Action.SELL, "ige", 1, True) # Adj. Close: 127.64 strat.addOrder(strategy_test.datetime_from_date(2007, 11, 13), strat.get_broker().create_market_order, broker.Order.Action.BUY_TO_COVER, "spy", 1, True) # Adj. Close: 147.67 strat.run() self.assertTrue(strat.get_order_updated_events() == 4) self.assertTrue( round(strat.get_broker().get_cash(), 2) == round( initialCash + (127.64 - 42.09) + (105.52 - 147.67), 2))
def testCumulativeReturn(self): barFeed = yahoofeed.Feed() barFeed.addBarsFromCSV( ReturnsTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv")) strat = strategy_test.TestStrategy(barFeed, 1000) strat.addPosEntry(datetime.datetime(2001, 1, 12), strat.enterLong, ReturnsTestCase.TestInstrument, 1) # 33.06 strat.addPosExit(datetime.datetime(2001, 11, 27), strat.exitPosition) # 14.32 stratAnalyzer = returns.Returns() strat.attachAnalyzer(stratAnalyzer) strat.run() self.assertTrue( round(strat.getBroker().getCash(), 2) == round( 1000 + (14.32 - 33.06), 2)) self.assertTrue( round(33.06 * (1 + stratAnalyzer.getCumulativeReturn()), 2) == 14.32)
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' bar_feed = yahoofeed.Feed() bar_feed.add_bars_from_csv("ige", common.get_data_file_path("sharpe-ratio-test-ige.csv")) strat = strategy_test.TestStrategy(bar_feed, initialCash) strat.get_broker().set_use_adj_values(True) strat.setBrokerOrdersGTC(True) stratAnalyzer = drawdown.DrawDown() strat.attach_analyzer(stratAnalyzer) # Manually place the order to get it filled on the first bar. order = strat.get_broker().create_market_order(broker.Order.Action.BUY, "ige", quantity, True) # Adj. Close: 42.09 order.set_good_until_canceled(True) strat.get_broker().place_order(order) strat.addOrder(strategy_test.datetime_from_date(2007, 11, 13), strat.get_broker().create_market_order, broker.Order.Action.SELL, "ige", quantity, True) # Adj. Close: 127.64 strat.run() self.assertTrue(round(strat.get_broker().get_cash(), 2) == initialCash + (127.64 - 42.09) * quantity) self.assertTrue(strat.get_order_updated_events() == 2) self.assertTrue(round(stratAnalyzer.get_max_draw_down(), 5) == 0.31178) self.assertTrue(stratAnalyzer.get_longest_draw_down_duration()== 432)
def testCumulativeReturn(self): initialCash = 33.06 bar_feed = yahoofeed.Feed() bar_feed.add_bars_from_csv( ReturnsTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv")) strat = strategy_test.TestStrategy(bar_feed, initialCash) strat.addPosEntry(strategy_test.datetime_from_date(2001, 1, 12), strat.enter_long, ReturnsTestCase.TestInstrument, 1) # 33.06 strat.addPosExit(strategy_test.datetime_from_date(2001, 11, 27), strat.exit_position) # 14.32 stratAnalyzer = returns.Returns() strat.attach_analyzer(stratAnalyzer) strat.run() self.assertTrue( round(strat.get_broker().get_cash(), 2) == round( initialCash + (14.32 - 33.06), 2)) self.assertTrue( round(33.06 * (1 + stratAnalyzer.get_cumulative_returns()[-1]), 2) == 14.32)
def __createStrategy(self): barFeed = self.__loadBarFeed() return strategy_test.TestStrategy(barFeed, 1000)