def testTwoBarReturns_CloseClose(self): initialCash = 15.90 bar_feed = yahoofeed.Feed() bar_feed.set_bar_filter( csvfeed.DateRangeFilter( strategy_test.datetime_from_date(2001, 12, 06), strategy_test.datetime_from_date(2001, 12, 07))) bar_feed.add_bars_from_csv( ReturnsTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv")) strat = strategy_test.TestStrategy(bar_feed, 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.get_broker().create_market_order( broker.Order.Action.BUY, ReturnsTestCase.TestInstrument, 1, True) # Close: 15.90 strat.get_broker().place_order(order) strat.addOrder(strategy_test.datetime_from_date(2001, 12, 06), strat.get_broker().create_market_order, broker.Order.Action.SELL, ReturnsTestCase.TestInstrument, 1, True) # Close: 15.91 stratAnalyzer = returns.Returns() strat.attach_analyzer(stratAnalyzer) strat.run() self.assertTrue(strat.get_broker().get_cash() == initialCash + (15.91 - 15.90)) # First day returns: 0 self.assertTrue(stratAnalyzer.get_returns()[0] == 0) # Second day returns: Open vs Prev. day's close self.assertTrue(stratAnalyzer.get_returns()[1] == (15.91 - 15.90) / 15.90)
def testOneBarReturn(self): initialCash = 1000 bar_feed = yahoofeed.Feed() bar_feed.set_bar_filter( csvfeed.DateRangeFilter( strategy_test.datetime_from_date(2001, 12, 07), strategy_test.datetime_from_date(2001, 12, 07))) bar_feed.add_bars_from_csv( ReturnsTestCase.TestInstrument, common.get_data_file_path("orcl-2001-yahoofinance.csv")) strat = strategy_test.TestStrategy(bar_feed, 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.get_broker().create_market_order( broker.Order.Action.BUY, ReturnsTestCase.TestInstrument, 1, False) # Open: 15.74 strat.get_broker().place_order(order) order = strat.get_broker().create_market_order( broker.Order.Action.SELL, ReturnsTestCase.TestInstrument, 1, True) # Close: 15.91 strat.get_broker().place_order(order) stratAnalyzer = returns.Returns() strat.attach_analyzer(stratAnalyzer) strat.run() self.assertTrue(strat.get_broker().get_cash() == initialCash + (15.91 - 15.74)) finalValue = 1000 - 15.74 + 15.91 rets = (finalValue - initialCash) / float(initialCash) self.assertEqual(stratAnalyzer.get_returns()[-1], rets)
def main(): # Load the orders file. ordersFile = OrdersFile("orders.csv") print "First date", ordersFile.getFirstDate() print "Last date", ordersFile.getLastDate() print "Symbols", ordersFile.get_symbols() # Load the data from QSTK storage. QS environment variable has to be defined. feed = yahoofeed.Feed() feed.set_bar_filter( csvfeed.DateRangeFilter(ordersFile.getFirstDate(), ordersFile.getLastDate())) feed.set_daily_bar_time( datetime.time(0, 0, 0) ) # This is to match the dates loaded with the ones in the orders file. for symbol in ordersFile.get_symbols(): feed.add_bars_from_csv( symbol, os.path.join(os.getenv("QS"), "QSData", "Yahoo", symbol + ".csv")) # Run the strategy. cash = 1000000 use_adjustedClose = True myStrategy = MyStrategy(feed, cash, ordersFile, use_adjustedClose) # Attach returns and sharpe ratio analyzers. retAnalyzer = returns.Returns() myStrategy.attach_analyzer(retAnalyzer) sharpeRatioAnalyzer = sharpe.SharpeRatio() myStrategy.attach_analyzer(sharpeRatioAnalyzer) myStrategy.run() # Print the results. print "Final portfolio value: $%.2f" % myStrategy.get_result() print "Anual return: %.2f %%" % (retAnalyzer.get_cumulative_returns()[-1] * 100) print "Average daily return: %.2f %%" % ( stats.mean(retAnalyzer.get_returns()) * 100) print "Std. dev. daily return: %.4f" % (stats.stddev( retAnalyzer.get_returns())) print "Sharpe ratio: %.2f" % (sharpeRatioAnalyzer.get_sharpe_ratio(0, 252))
def testGoogle2011(self): initial_value = 1000000 bar_feed = yahoofeed.Feed() bar_feed.add_bars_from_csv( ReturnsTestCase.TestInstrument, common.get_data_file_path("goog-2011-yahoofinance.csv")) strat = strategy_test.TestStrategy(bar_feed, initial_value) order = strat.get_broker().create_market_order( broker.Order.Action.BUY, ReturnsTestCase.TestInstrument, 1654, True) # 2011-01-03 close: 604.35 strat.get_broker().place_order(order) stratAnalyzer = returns.Returns() strat.attach_analyzer(stratAnalyzer) strat.run() finalValue = strat.get_broker().get_value( strat.get_feed().get_last_bars()) self.assertEqual( round(stratAnalyzer.get_cumulative_returns()[-1], 4), round((finalValue - initial_value) / float(initial_value), 4))
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)
from pytradelib.barfeed import yahoofeed from pytradelib.stratanalyzer import returns from pytradelib.stratanalyzer import sharpe from pytradelib.stratanalyzer import drawdown from pytradelib.stratanalyzer import trades import smacross_strategy # Load the yahoo feed from the CSV file feed = yahoofeed.Feed() feed.add_bars_from_csv("orcl", "orcl-2000.csv") # Evaluate the strategy with the feed's bars. myStrategy = smacross_strategy.Strategy(feed, 20) # Attach different analyzers to a strategy before executing it. retAnalyzer = returns.Returns() myStrategy.attach_analyzer(retAnalyzer) sharpeRatioAnalyzer = sharpe.SharpeRatio() myStrategy.attach_analyzer(sharpeRatioAnalyzer) drawDownAnalyzer = drawdown.DrawDown() myStrategy.attach_analyzer(drawDownAnalyzer) tradesAnalyzer = trades.Trades() myStrategy.attach_analyzer(tradesAnalyzer) # Run the strategy. myStrategy.run() print "Final portfolio value: $%.2f" % myStrategy.get_result() print "Cumulative returns: %.2f %%" % ( retAnalyzer.get_cumulative_returns()[-1] * 100) print "Sharpe ratio: %.2f" % (sharpeRatioAnalyzer.get_sharpe_ratio(0.05, 252))