Overall Statistics |
Total Trades 16 Average Win 0.16% Average Loss -0.11% Compounding Annual Return -0.068% Drawdown 0.700% Expectancy -0.094 Net Profit -0.068% Sharpe Ratio -0.102 Probabilistic Sharpe Ratio 11.075% Loss Rate 62% Win Rate 38% Profit-Loss Ratio 1.42 Alpha 0 Beta -0.002 Annual Standard Deviation 0.005 Annual Variance 0 Information Ratio -0.676 Tracking Error 0.303 Treynor Ratio 0.323 Total Fees $16.00 |
import clr clr.AddReference("System") clr.AddReference("QuantConnect.Algorithm") clr.AddReference("QuantConnect.Indicators") clr.AddReference("QuantConnect.Common") from QuantConnect import * from QuantConnect.Parameters import * from QuantConnect.Benchmarks import * from QuantConnect.Brokerages import * from QuantConnect.Util import * from QuantConnect.Interfaces import * from QuantConnect.Algorithm import * from QuantConnect.Algorithm.Framework import * from QuantConnect.Algorithm.Framework.Selection import * from QuantConnect.Algorithm.Framework.Alphas import * from QuantConnect.Algorithm.Framework.Portfolio import * from QuantConnect.Algorithm.Framework.Execution import * from QuantConnect.Algorithm.Framework.Risk import * from QuantConnect.Indicators import * from QuantConnect.Data import * from QuantConnect.Data.Consolidators import * from QuantConnect.Data.Custom import * from QuantConnect.Data.Fundamental import * from QuantConnect.Data.Market import * from QuantConnect.Data.UniverseSelection import * from QuantConnect.Notifications import * from QuantConnect.Orders import * from QuantConnect.Orders.Fees import * from QuantConnect.Orders.Fills import * from QuantConnect.Orders.Slippage import * from QuantConnect.Scheduling import * from QuantConnect.Securities import * from QuantConnect.Securities.Equity import * from QuantConnect.Securities.Forex import * from QuantConnect.Securities.Interfaces import * from datetime import date, datetime, timedelta from QuantConnect.Python import * from QuantConnect.Storage import * QCAlgorithmFramework = QCAlgorithm QCAlgorithmFrameworkBridge = QCAlgorithm class SimpleTimeBasedAlgorithm(QCAlgorithm): def Initialize(self): self.SetStartDate(2019, 12, 1) self.SetEndDate(2020, 12, 1) self.SetCash(1000000) # Set Strategy Cash self.lastEntryDate = self.Time.date() self.lastEntryCheckDate = self.Time.date() self.totalTrades = 0 self.stopOrder = None self.bestPrice = 0 self.direction = 0 self.amzn = self.AddEquity("MSFT", Resolution.Daily) self.amzn.SetDataNormalizationMode(DataNormalizationMode.Raw) self.fast = self.EMA(self.amzn.Symbol, 15, Resolution.Daily) self.slow = self.EMA(self.amzn.Symbol, 30, Resolution.Daily) self.macd = self.MACD(self.amzn.Symbol, 15, 30, 9, MovingAverageType.Exponential, Resolution.Daily) emas = Chart("EMAs") emas.AddSeries(Series("Fast", SeriesType.Line, 0)) emas.AddSeries(Series("Slow", SeriesType.Line, 0)) emas.AddSeries(Series("Price", SeriesType.Candle, 0)) emas.AddSeries(Series("MACD", SeriesType.Line, 0, "int")) emas.AddSeries(Series("Trade", SeriesType.Scatter, 0)) self.AddChart(emas) def OnData(self, data): self.Plot("EMAs", "Fast", self.fast.Current.Value) self.Plot("EMAs", "Slow", self.slow.Current.Value) self.Plot("EMAs", "MACD", self.macd.Current.Value-self.macd.Signal.Current.Value+150) self.Plot("EMAs", "Price", self.amzn.Price) if not self.slow.IsReady: return if self.stopOrder: yes=True # enter a position if SMA and LMA diverge # exit a position based on trailing stop loss if not self.Portfolio.Invested: # We're not invested, so check if we should enter, and if so what direction self.direction = 0 #if data.Time.date() != self.lastEntryCheckDate: if (self.macd.Current.Value > self.macd.Signal.Current.Value and self.fast.Current.Value > 1.01 * self.slow.Current.Value): self.direction = 1 elif (self.macd.Current.Value < self.macd.Signal.Current.Value and self.slow.Current.Value > 1.01 * self.fast.Current.Value): self.direction = -1 if (self.direction != 0): self.MarketOrder(self.amzn.Symbol, 100*self.direction, True) self.bestPrice = self.amzn.Price self.stopOrder = self.StopMarketOrder(self.amzn.Symbol, -100*self.direction, self.GetStopPrice()) else: # if invested, update the stop order settings to trail the max of the previous and current price if (self.direction == 1): self.bestPrice = max(self.bestPrice, self.amzn.Price) elif (self.direction == -1): self.bestPrice = min(self.bestPrice, self.amzn.Price) self.stopOrder.UpdateStopPrice(self.GetStopPrice()) # return the price at which a stop order should be placed, given the def GetStopPrice(self): stopPrice = (1 - 0.1 * self.direction) * self.bestPrice self.Debug("Setting stop to " + str(stopPrice)) return stopPrice def OnOrderEvent(self, orderEvent): if orderEvent.Status == OrderStatus.UpdateSubmitted: self.Debug("Updated stop to " + str(orderEvent.StopPrice)) if orderEvent.Status == OrderStatus.Filled: order = self.Transactions.GetOrderById(orderEvent.OrderId) self.totalTrades += 1 self.Plot("EMAs", "Trade", self.amzn.Price) # if the event is an execution event, then log the price of trade self.Debug(str(self.Time.date()) + ": Completed " + str( self.totalTrades) + " trades. Traded AMZN " + " at price " + str(orderEvent.FillPrice) + ", at quantity " + str(orderEvent.Quantity) + ". Current price is " + str(self.amzn.Price))