Overall Statistics |
Total Trades 172 Average Win 3.60% Average Loss -2.66% Compounding Annual Return 2.190% Drawdown 38.600% Expectancy 0.107 Net Profit 21.770% Sharpe Ratio 0.196 Loss Rate 53% Win Rate 47% Profit-Loss Ratio 1.36 Alpha -0.011 Beta 0.524 Annual Standard Deviation 0.143 Annual Variance 0.02 Information Ratio -0.343 Tracking Error 0.137 Treynor Ratio 0.053 Total Fees $886.57 |
from System import * from QuantConnect import * from QuantConnect.Algorithm import * from QuantConnect.Indicators import * from QuantConnect.Data.Market import TradeBar from datetime import datetime class RollingWindowAlgorithm(QCAlgorithm): def Initialize(self): '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.''' self.SetStartDate(2004,10,1) #Set Start Date self.SetEndDate(2013,11,1) #Set End Date self.SetCash(100000) #Set Strategy Cash # Find more symbols here: http://quantconnect.com/data self.AddEquity("SPY", Resolution.Daily) # define our daily macd(12,26) with a 9 day signal self.__macd = self.MACD("SPY", 9, 26, 9, MovingAverageType.Exponential, Resolution.Daily) self.__previous = datetime.min self.PlotIndicator("MACD", True, self.__macd, self.__macd.Signal) self.PlotIndicator("SPY", self.__macd.Fast, self.__macd.Slow) # Creates a Rolling Window indicator to keep the 2 TradeBar self.window = RollingWindow[TradeBar](2) # For other security types, use QuoteBar # Creates an indicator and adds to a rolling window when it is updated #self.MACD("SPY", 9, 26, 9, MovingAverageType.Exponential, Resolution.Daily).Updated += self.MacdUpdated self.SMA("SPY", 5).Updated += self.MacdUpdated self.MacdWin = RollingWindow[IndicatorDataPoint,](5) def MacdUpdated(self, sender, updated): '''Adds updated values to rolling window''' self.MacdWin.Add(updated) def OnData(self, data): '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.''' # Add SPY TradeBar in rollling window self.window.Add(data["SPY"]) # Wait for windows to be ready. if not (self.window.IsReady and self.MacdWin.IsReady and self.__macd.IsReady): return # only once per day if self.__previous.date() == self.Time.date(): return # define a small tolerance on our checks to avoid bouncing tolerance = 0.0025; holdings = self.Portfolio["SPY"].Quantity signalDeltaPercent = (self.__macd.Current.Value - self.__macd.Signal.Current.Value)/self.__macd.Fast.Current.Value currBar = self.window[0] # Current bar had index zero. pastBar = self.window[1] # Past bar has index one. self.Log("Price: {0} -> {1} ... {2} -> {3}".format(pastBar.Time, pastBar.Close, currBar.Time, currBar.Close)) currMacd = self.MacdWin[0] # Current SMA had index zero. pastMacd = self.MacdWin[self.MacdWin.Count-1] # Oldest SMA has index of window count minus 1. self.Log("SMA: {0} -> {1} ... {2} -> {3}".format(pastMacd.Time, pastMacd.Value, currMacd.Time, currMacd.Value)) if not self.Portfolio.Invested and currMacd.Value > pastMacd.Value: self.SetHoldings("SPY", 1.0) # if our macd is greater than our signal, then let's go long if holdings <= 0 and signalDeltaPercent > tolerance: # 0.01% # longterm says buy as well self.SetHoldings("SPY", 1.0) # of our macd is less than our signal, then let's go short elif holdings >= 0 and signalDeltaPercent < -tolerance: self.Liquidate("SPY") self.__previous = self.Time