Overall Statistics |
Total Trades 219 Average Win 2.98% Average Loss -0.98% Compounding Annual Return 13.442% Drawdown 18.300% Expectancy 0.872 Net Profit 141.947% Sharpe Ratio 0.852 Loss Rate 54% Win Rate 46% Profit-Loss Ratio 3.04 Alpha 0.113 Beta -0.007 Annual Standard Deviation 0.132 Annual Variance 0.017 Information Ratio 0.136 Tracking Error 0.244 Treynor Ratio -16.168 Total Fees $647.67 |
import clr clr.AddReference("System") clr.AddReference("QuantConnect.Algorithm") clr.AddReference("QuantConnect.Indicators") clr.AddReference("QuantConnect.Common") from System import * from QuantConnect import * from QuantConnect.Algorithm import * from QuantConnect.Indicators import * ### <summary> ### In this example we look at the canonical 15/30 day moving average cross. This algorithm ### will go long when the 15 crosses above the 30 and will liquidate when the 15 crosses ### back below the 30. ### </summary> ### <meta name="tag" content="indicators" /> ### <meta name="tag" content="indicator classes" /> ### <meta name="tag" content="moving average cross" /> ### <meta name="tag" content="strategy example" /> class MovingAverageCrossAlgorithm(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(2008, 1, 1) #Set Start Date self.SetEndDate(2015, 1, 1) #Set End Date self.SetCash(100000) #Set Strategy Cash # Find more symbols here: http://quantconnect.com/data self.AddEquity("SPY") self.AddEquity("GOOGL") self.AddEquity("AMZN") self.AddEquity("MSFT") self.AddEquity("AAPL") # create a 15 day exponential moving average self.fast = self.EMA("SPY", 15, Resolution.Daily) self.fasta = self.EMA("GOOGL", 15, Resolution.Daily) self.fastb = self.EMA("AMZN", 15, Resolution.Daily) self.fastc = self.EMA("MSFT", 15, Resolution.Daily) self.fastd = self.EMA("AAPL", 15, Resolution.Daily) # create a 30 day exponential moving average self.slow = self.EMA("SPY", 30, Resolution.Daily) self.slowa = self.EMA("GOOGL", 30, Resolution.Daily) self.slowb = self.EMA("AMZN", 30, Resolution.Daily) self.slowc = self.EMA("MSFT", 30, Resolution.Daily) self.slowd = self.EMA("AAPL", 30, Resolution.Daily) self.previous = None def OnData(self, data): '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.''' # a couple things to notice in this method: # 1. We never need to 'update' our indicators with the data, the engine takes care of this for us # 2. We can use indicators directly in math expressions # 3. We can easily plot many indicators at the same time # wait for our slow and fast ema to fully initialize if not self.slow.IsReady: return if not self.fast.IsReady: return if not self.slowa.IsReady: return if not self.fasta.IsReady: return if not self.slowb.IsReady: return if not self.fastb.IsReady: return if not self.slowc.IsReady: return if not self.fastc.IsReady: return if not self.slowd.IsReady: return if not self.fastd.IsReady: return # only once per day if self.previous is not None and self.previous.date() == self.Time.date(): return # define a small tolerance on our checks to avoid bouncing tolerance = 0.00015 holdings = self.Portfolio["SPY"].Quantity holdingsa = self.Portfolio["GOOGL"].Quantity holdingsb = self.Portfolio["AMZN"].Quantity holdingsc = self.Portfolio["MSFT"].Quantity holdingsd = self.Portfolio["AAPL"].Quantity # we only want to go long if we're currently short or flat if holdings <= 0: # if the fast is greater than the slow, we'll go long if self.fast.Current.Value > self.slow.Current.Value *(1 + tolerance): self.Log("BUY SPY >> {0}".format(self.Securities["SPY"].Price)) self.SetHoldings("SPY", .2) if holdingsa <= 0: if self.fasta.Current.Value > self.slowa.Current.Value *(1 + tolerance): self.Log("BUY GOOGL >> {0}".format(self.Securities["GOOGL"].Price)) self.SetHoldings("GOOGL", .2) if holdingsb <= 0: if self.fastb.Current.Value > self.slowb.Current.Value *(1 + tolerance): self.Log("BUY AMZN >> {0}".format(self.Securities["AMZN"].Price)) self.SetHoldings("AMZN", .2) if holdingsc <= 0: if self.fastc.Current.Value > self.slowc.Current.Value *(1 + tolerance): self.Log("BUY MSFT >> {0}".format(self.Securities["MSFT"].Price)) self.SetHoldings("MSFT", .2) if holdingsd <= 0: if self.fastd.Current.Value > self.slowd.Current.Value *(1 + tolerance): self.Log("BUY AAPL >> {0}".format(self.Securities["AAPL"].Price)) self.SetHoldings("AAPL", .2) # we only want to liquidate if we're currently long # if the fast is less than the slow we'll liquidate our long if holdings > 0 and self.fast.Current.Value < self.slow.Current.Value: self.Log("SELL SPY >> {0}".format(self.Securities["SPY"].Price)) self.Liquidate("SPY") if holdingsa > 0 and self.fasta.Current.Value < self.slowa.Current.Value: self.Log("SELL GOOGL >> {0}".format(self.Securities["GOOGL"].Price)) self.Liquidate("GOOGL") if holdingsb > 0 and self.fastb.Current.Value < self.slowb.Current.Value: self.Log("SELL AMZN >> {0}".format(self.Securities["AMZN"].Price)) self.Liquidate("AMZN") if holdingsc > 0 and self.fastc.Current.Value < self.slowc.Current.Value: self.Log("SELL MSFT >> {0}".format(self.Securities["MSFT"].Price)) self.Liquidate("MSFT") if holdingsd > 0 and self.fastd.Current.Value < self.slowd.Current.Value: self.Log("SELL AAPL >> {0}".format(self.Securities["AAPL"].Price)) self.Liquidate("AAPL") self.previous = self.Time