| Overall Statistics |
|
Total Trades 4 Average Win 2.18% Average Loss -0.46% Compounding Annual Return 8.841% Drawdown 3.500% Expectancy 1.875 Net Profit 1.709% Sharpe Ratio 0.859 Probabilistic Sharpe Ratio 46.797% Loss Rate 50% Win Rate 50% Profit-Loss Ratio 4.75 Alpha 0.085 Beta -0.017 Annual Standard Deviation 0.093 Annual Variance 0.009 Information Ratio -1.511 Tracking Error 0.152 Treynor Ratio -4.818 Total Fees $5.42 Estimated Strategy Capacity $610000000.00 Lowest Capacity Asset SPY R735QTJ8XC9X |
from QuantConnect.Statistics import *
class SmoothVioletAlligator(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 11, 21) # Set Start Date
self.SetEndDate(2021, 2, 1)
self.SetCash(100000) # Set Strategy Cash
self.AddEquity("SPY", Resolution.Daily)
self.spyEMA = self.EMA("SPY", 21, Resolution.Daily, Field.Close)
self.EnableAutomaticIndicatorWarmUp = True
self.lastAction = None
def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Arguments:
data: Slice object keyed by symbol containing the stock data
'''
if not self.spyEMA.IsReady:
return
s = 'SPY'
if not (data.ContainsKey(s) and data.Bars.ContainsKey(s)):
return
if self.lastAction is not None and self.lastAction.date() == self.Time.date(): return
self.lastAction = self.Time
if not self.Portfolio.Invested:
if data[s].Close > self.spyEMA.Current.Value:
self.SetHoldings(s, 1)
elif self.Portfolio.Invested and data[s].Close < self.spyEMA.Current.Value:
self.Liquidate(s)
def OnEndOfAlgorithm(self):
for trade in self.TradeBuilder.ClosedTrades:
self.Log(f"-Trade- \n{self.trade_to_string(trade)}")
statistics = TradeStatistics(self.TradeBuilder.ClosedTrades)
self.Log(f"-Statistics- \n{self.statistics_to_string(statistics)}")
def statistics_to_string(self, statistics):
return 'AverageEndTradeDrawdown={0};AverageLosingTradeDuration={1};AverageLoss={2};AverageMAE={3};AverageMFE={4};AverageProfit={5};AverageProfitLoss={6};AverageTradeDuration={7};AverageWinningTradeDuration={8};EndDateTime={9};LargestLoss={10};LargestMAE={11};LargestMFE={12};LargestProfit={13};LossRate={14};MaxConsecutiveLosingTrades={15};MaxConsecutiveWinningTrades={16};MaximumClosedTradeDrawdown={17};MaximumDrawdownDuration={18};MaximumEndTradeDrawdown={19};MaximumIntraTradeDrawdown={20};NumberOfLosingTrades={21};NumberOfWinningTrades={22};ProfitFactor={23};ProfitLossDownsideDeviation={24};ProfitLossRatio={25};ProfitLossStandardDeviation={26};ProfitToMaxDrawdownRatio={27};SharpeRatio={28};SortinoRatio={29};StartDateTime={30};TotalFees={31};TotalLoss={32};TotalNumberOfTrades={33};TotalProfit={34};TotalProfitLoss={35};WinLossRatio={36};WinRate={37}'.format(
statistics.AverageEndTradeDrawdown,
statistics.AverageLosingTradeDuration,
statistics.AverageLoss,
statistics.AverageMAE,
statistics.AverageMFE,
statistics.AverageProfit,
statistics.AverageProfitLoss,
statistics.AverageTradeDuration,
statistics.AverageWinningTradeDuration,
statistics.EndDateTime,
statistics.LargestLoss,
statistics.LargestMAE,
statistics.LargestMFE,
statistics.LargestProfit,
statistics.LossRate,
statistics.MaxConsecutiveLosingTrades,
statistics.MaxConsecutiveWinningTrades,
statistics.MaximumClosedTradeDrawdown,
statistics.MaximumDrawdownDuration,
statistics.MaximumEndTradeDrawdown,
statistics.MaximumIntraTradeDrawdown,
statistics.NumberOfLosingTrades,
statistics.NumberOfWinningTrades,
statistics.ProfitFactor,
statistics.ProfitLossDownsideDeviation,
statistics.ProfitLossRatio,
statistics.ProfitLossStandardDeviation,
statistics.ProfitToMaxDrawdownRatio,
statistics.SharpeRatio,
statistics.SortinoRatio,
statistics.StartDateTime,
statistics.TotalFees,
statistics.TotalLoss,
statistics.TotalNumberOfTrades,
statistics.TotalProfit,
statistics.TotalProfitLoss,
statistics.WinLossRatio,
statistics.WinRate)
def trade_to_string(self, trade):
return 'Symbol: {0} EntryTime: {1} EntryPrice {2} Direction: {3} Quantity: {4} ExitTime: {5} ExitPrice: {6} ProfitLoss: {7} TotalFees: {8} MAE: {9} MFE: {10} EndTradeDrawdown: {11}'.format(
trade.Symbol,
trade.EntryTime,
trade.EntryPrice,
trade.Direction,
trade.Quantity,
trade.ExitTime,
trade.ExitPrice,
trade.ProfitLoss,
trade.TotalFees,
trade.MAE,
trade.MFE,
trade.EndTradeDrawdown)