Strategy analyzers provide an extensible way to attach different calculations to strategy executions.
Base class for strategy analyzers.
Note
This is a base class and should not be used directly.
A pyalgotrade.stratanalyzer.StrategyAnalyzer that calculates returns and cumulative returns for the whole portfolio.
Returns a pyalgotrade.dataseries.DataSeries with the cumulative returns for each bar.
Returns a pyalgotrade.dataseries.DataSeries with the returns for each bar.
A pyalgotrade.stratanalyzer.StrategyAnalyzer that calculates Sharpe ratio for the whole portfolio.
Returns the Sharpe ratio for the strategy execution. If the volatility is 0, 0 is returned.
Parameters: |
|
---|
Note
A pyalgotrade.stratanalyzer.StrategyAnalyzer that calculates max. drawdown and longest drawdown duration for the portfolio.
Returns the duration of the longest drawdown.
Note
Note that this is the duration of the longest drawdown, not necessarily the deepest one.
Returns the max. (deepest) drawdown.
A pyalgotrade.stratanalyzer.StrategyAnalyzer that records the profit/loss and returns of every completed trade.
Note
This analyzer operates on individual completed trades. For example, lets say you start with a $1000 cash, and then you buy 1 share of XYZ for $10 and later sell it for $20:
- The trade’s profit was $10.
- The trade’s return is 100%, even though your whole portfolio went from $1000 to $1020, a 2% return.
Returns the total number of trades.
Returns the number of profitable trades.
Returns the number of unprofitable trades.
Returns the number of trades whose net profit was 0.
Returns a numpy.array with the profits/losses for each trade.
Returns a numpy.array with the profits for each profitable trade.
Returns a numpy.array with the losses for each unprofitable trade.
Returns a numpy.array with the returns for each trade.
Returns a numpy.array with the positive returns for each trade.
Returns a numpy.array with the negative returns for each trade.
Returns a numpy.array with the commissions for each trade.
Returns a numpy.array with the commissions for each profitable trade.
Returns a numpy.array with the commissions for each unprofitable trade.
Returns a numpy.array with the commissions for each trade whose net profit was 0.
This example depends on smacross_strategy.py from the tutorial section.
from pyalgotrade.barfeed import yahoofeed
from pyalgotrade.stratanalyzer import returns
from pyalgotrade.stratanalyzer import sharpe
from pyalgotrade.stratanalyzer import drawdown
from pyalgotrade.stratanalyzer import trades
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, 20)
# Attach different analyzers to a strategy before executing it.
retAnalyzer = returns.Returns()
myStrategy.attachAnalyzer(retAnalyzer)
sharpeRatioAnalyzer = sharpe.SharpeRatio()
myStrategy.attachAnalyzer(sharpeRatioAnalyzer)
drawDownAnalyzer = drawdown.DrawDown()
myStrategy.attachAnalyzer(drawDownAnalyzer)
tradesAnalyzer = trades.Trades()
myStrategy.attachAnalyzer(tradesAnalyzer)
# Run the strategy.
myStrategy.run()
print "Final portfolio value: $%.2f" % myStrategy.getResult()
print "Cumulative returns: %.2f %%" % (retAnalyzer.getCumulativeReturns()[-1] * 100)
print "Sharpe ratio: %.2f" % (sharpeRatioAnalyzer.getSharpeRatio(0.05, 252))
print "Max. drawdown: %.2f %%" % (drawDownAnalyzer.getMaxDrawDown() * 100)
print "Longest drawdown duration: %d days" % (drawDownAnalyzer.getLongestDrawDownDuration())
print
print "Total trades: %d" % (tradesAnalyzer.getCount())
if tradesAnalyzer.getCount() > 0:
profits = tradesAnalyzer.getAll()
print "Avg. profit: $%2.f" % (profits.mean())
print "Profits std. dev.: $%2.f" % (profits.std())
print "Max. profit: $%2.f" % (profits.max())
print "Min. profit: $%2.f" % (profits.min())
returns = tradesAnalyzer.getAllReturns()
print "Avg. return: %2.f %%" % (returns.mean() * 100)
print "Returns std. dev.: %2.f %%" % (returns.std() * 100)
print "Max. return: %2.f %%" % (returns.max() * 100)
print "Min. return: %2.f %%" % (returns.min() * 100)
print
print "Profitable trades: %d" % (tradesAnalyzer.getProfitableCount())
if tradesAnalyzer.getProfitableCount() > 0:
profits = tradesAnalyzer.getProfits()
print "Avg. profit: $%2.f" % (profits.mean())
print "Profits std. dev.: $%2.f" % (profits.std())
print "Max. profit: $%2.f" % (profits.max())
print "Min. profit: $%2.f" % (profits.min())
returns = tradesAnalyzer.getPositiveReturns()
print "Avg. return: %2.f %%" % (returns.mean() * 100)
print "Returns std. dev.: %2.f %%" % (returns.std() * 100)
print "Max. return: %2.f %%" % (returns.max() * 100)
print "Min. return: %2.f %%" % (returns.min() * 100)
print
print "Unprofitable trades: %d" % (tradesAnalyzer.getUnprofitableCount())
if tradesAnalyzer.getUnprofitableCount() > 0:
losses = tradesAnalyzer.getLosses()
print "Avg. loss: $%2.f" % (losses.mean())
print "Losses std. dev.: $%2.f" % (losses.std())
print "Max. loss: $%2.f" % (losses.min())
print "Min. loss: $%2.f" % (losses.max())
returns = tradesAnalyzer.getNegativeReturns()
print "Avg. return: %2.f %%" % (returns.mean() * 100)
print "Returns std. dev.: %2.f %%" % (returns.std() * 100)
print "Max. return: %2.f %%" % (returns.max() * 100)
print "Min. return: %2.f %%" % (returns.min() * 100)
The output should look like this:
Final portfolio value: $1124.90
Cumulative returns: 12.49 %
Sharpe ratio: 0.39
Max. drawdown: 17.85 %
Longest drawdown duration: 193 days
Total trades: 11
Avg. profit: $ 9
Profits std. dev.: $66
Max. profit: $186
Min. profit: $-58
Avg. return: 2 %
Returns std. dev.: 10 %
Max. return: 30 %
Min. return: -7 %
Profitable trades: 3
Avg. profit: $101
Profits std. dev.: $61
Max. profit: $186
Min. profit: $47
Avg. return: 15 %
Returns std. dev.: 11 %
Max. return: 30 %
Min. return: 6 %
Unprofitable trades: 8
Avg. loss: $-26
Losses std. dev.: $17
Max. loss: $-58
Min. loss: $-3
Avg. return: -3 %
Returns std. dev.: 2 %
Max. return: -0 %
Min. return: -7 %