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 sharpe_ratio(returns, risk_free_rate, trading_periods, annualized=True): ret = 0.0 # From http://en.wikipedia.org/wiki/Sharpe_ratio: if Rf is a constant risk-free return throughout the period, # then stddev(R - Rf) = stddev(R). volatility = stats.stddev(returns, 1) if volatility != 0: excess_returns = [daily_return - (risk_free_rate/float(trading_periods)) for daily_return in returns] average_excess_returns = stats.mean(excess_returns) ret = average_excess_returns / volatility if annualized: ret = ret * math.sqrt(trading_periods) return ret
def sharpe_ratio(returns, risk_free_rate, trading_periods, annualized=True): ret = 0.0 # From http://en.wikipedia.org/wiki/Sharpe_ratio: if Rf is a constant risk-free return throughout the period, # then stddev(R - Rf) = stddev(R). volatility = stats.stddev(returns, 1) if volatility != 0: excess_returns = [ daily_return - (risk_free_rate / float(trading_periods)) for daily_return in returns ] average_excess_returns = stats.mean(excess_returns) ret = average_excess_returns / volatility if annualized: ret = ret * math.sqrt(trading_periods) return ret
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 __testMeanImpl(self, values): self.__assertEqFloat(stats.mean(values), stats.py_mean(values))
def on_bars(self, bars): pass # Load the yahoo feed from CSV files. feed = yahoofeed.Feed() feed.add_bars_from_csv("aeti", "aeti-2011-yahoofinance.csv") feed.add_bars_from_csv("egan", "egan-2011-yahoofinance.csv") feed.add_bars_from_csv("glng", "glng-2011-yahoofinance.csv") feed.add_bars_from_csv("simo", "simo-2011-yahoofinance.csv") # Evaluate the strategy with the feed's bars. myStrategy = MyStrategy(feed) # Attach returns and sharpe ratio analyzers. retAnalyzer = returns.Returns() myStrategy.attach_analyzer(retAnalyzer) sharpeRatioAnalyzer = sharpe.SharpeRatio() myStrategy.attach_analyzer(sharpeRatioAnalyzer) # Run the strategy 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))
# Load the yahoo feed from CSV files. feed = yahoofeed.Feed() feed.add_bars_from_csv("aeti", "aeti-2011-yahoofinance.csv") feed.add_bars_from_csv("egan", "egan-2011-yahoofinance.csv") feed.add_bars_from_csv("glng", "glng-2011-yahoofinance.csv") feed.add_bars_from_csv("simo", "simo-2011-yahoofinance.csv") # Evaluate the strategy with the feed's bars. myStrategy = MyStrategy(feed) # Attach returns and sharpe ratio analyzers. retAnalyzer = returns.Returns() myStrategy.attach_analyzer(retAnalyzer) sharpeRatioAnalyzer = sharpe.SharpeRatio() myStrategy.attach_analyzer(sharpeRatioAnalyzer) # Run the strategy 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))