def run_smacross_strategy(): global feed print "Running smacross_strategy.Strategy" strat = smacross_strategy.Strategy(feed, instrument, 20) strat.run() print strat.getResult()
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))
from pyalgotrade import plotter from pyalgotrade.barfeed import yahoofeed from pyalgotrade.stratanalyzer import returns import smacross_strategy # Load the yahoo feed from the CSV file feed = yahoofeed.Feed() feed.addBarsFromCSV("orcl", "orcl-2000.csv") # Evaluate the strategy with the feed's bars. myStrategy = smacross_strategy.Strategy(feed, "orcl", 20) # Attach a returns analyzers to the strategy. returnsAnalyzer = returns.Returns() myStrategy.attachAnalyzer(returnsAnalyzer) # Attach the plotter to the strategy. plt = plotter.StrategyPlotter(myStrategy) # Include the SMA in the instrument's subplot to get it displayed along with the closing prices. plt.getInstrumentSubplot("orcl").addDataSeries("SMA", myStrategy.getSMA()) # Plot the strategy returns at each bar. plt.getOrCreateSubplot("returns").addDataSeries("Net return", returnsAnalyzer.getReturns()) plt.getOrCreateSubplot("returns").addDataSeries( "Cum. return", returnsAnalyzer.getCumulativeReturns()) # Run the strategy. myStrategy.run() print "Final portfolio value: $%.2f" % myStrategy.getResult() # Plot the strategy.