Overall Statistics |
Total Trades 8 Average Win 0% Average Loss 0% Compounding Annual Return 0% Drawdown 0% Expectancy 0 Net Profit 0% Sharpe Ratio 0 Probabilistic Sharpe Ratio 0% Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 0 Beta 0 Annual Standard Deviation 0 Annual Variance 0 Information Ratio 0 Tracking Error 0 Treynor Ratio 0 Total Fees $8.00 Estimated Strategy Capacity $13000000.00 Lowest Capacity Asset AAPL R735QTJ8XC9X |
# 2SD with 3HULL and 7 HULL # This algo waits until the price crosses the 2SD # Then, the algo waits for a full turn on the 3HULL # on a 3 minute chart. Then, algo waits until price # is at or better the 7HULL. Stop loss at $0.10 from AlgorithmImports import * #endregion from QuantConnect.Data.Market import TradeBar from datetime import timedelta from System import * from QuantConnect import * from QuantConnect.Algorithm import * from QuantConnect.Indicators import * import decimal as d import statistics from System.Drawing import Color class MyAlgorithm(QCAlgorithm): def Initialize(self): self.SetStartDate(2022, 4, 19) # Set Start Date self.SetEndDate(2022, 4, 19) self.SetCash(250000) # Set Strategy Cash #self.SetWarmup(100) self.symbolData = dict() self.myBBUpperTouches = [] self.myBBLowerTouches = [] self.myBBUpperCount = 0 self.myBBLowerCount = 0 self.myATR = [] self.myATR.append(0.45) self.myATR.append(0.50) self.myATR.append(0.60) self.myLastLowerBB = 0 self.myLastHigherBB = 0 self.myTradeLong = 0 self.myTradeShort = 0 # Specify "Long" or "Short" self.whichTradeIsNext = "None" # Specify "Long" or "Short" self.whichWay3HULL = "None" self.isFirstTime = 1 self.myLastLow = 0 self.myLastHigh = 0 self.myHighestHigh = 0 self.myLowestLow = 0 self.myHighs = [] self.myCloses = [] self.myLows = [] # This is the amount below or above the high / low of the last bar to stop us out self.belowHighLow = 0.12 myHullShortLength = 9 myHullLongLength = 21 self.myPrices = [] self.myVolumes = [] self.myPricesSum = [] self.myVolumesSum = [] self.myPriceTimesVolumeSum = [] self.myVWAP = [] self.myHullShort = [] self.myHullLong = [] self.isHitAbove2SD = 0 self.isHitBelow2SD = 0 self.isHullGreenToRed = 0 self.isHullRedToGreen = 0 self.isLimitOrder = 0 self.myOrderPrice = 0 self.isInShortTrade = 0 self.isInLongTrade = 0 self.hull3Price = 0 self.myHMAShort = HullMovingAverage(myHullShortLength) self.myHMALong = HullMovingAverage(myHullLongLength) self.my2SDHigh = 0 self.my2SDLow = 0 self.myCurrent7HULLPrice = 0 self.isInShortTrade = 0 self.isInLongTrade = 0 for ticker in ["AAPL"]: symbol = self.AddEquity(ticker, Resolution.Second, Market.USA, True, 0, True).Symbol self.RegisterIndicator(symbol, self.myHMAShort, Resolution.Minute) self.RegisterIndicator(symbol, self.myHMALong, Resolution.Minute) self.bb25 = self.BB(symbol, 1200, 2.5) self.bb20 = self.BB(symbol, 1200, 2.0) self.bb10 = self.BB(symbol, 1200, 1.0) self.aapl = symbol consolidator_minute = TradeBarConsolidator(180) consolidator_minute.DataConsolidated += self.OnThreeMinuteData self.SubscriptionManager.AddConsolidator(symbol, consolidator_minute) self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.At(15, 59, 52), Action(self.End_of_day_liquidate)) self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.At(16, 00, 00), Action(self.Resetting)) def OnThreeMinuteData(self, sender, bar): myVolumesSumTemp = 0 myPricesSumTemp = 0 myPriceTimesVolumeSumTemp = 0 myVWAPTemp = 0 myHighSD = 0 myLowSD = 0 myCurrentPrice = 0 symbol = self.aapl myCurrentPrice = self.Securities[symbol].Open self.myVolumes.append(self.Securities[symbol].Volume) for x in self.myVolumes: myVolumesSumTemp += x self.myVolumesSum.append(myVolumesSumTemp) self.myPrices.append(myCurrentPrice) for x in self.myPrices: myPricesSumTemp += x self.myPricesSum.append(myPricesSumTemp) myPriceTimesVolumeTemp = myCurrentPrice * self.Securities[symbol].Volume self.myPriceTimesVolumeSum.append(myPriceTimesVolumeTemp) for x in self.myPriceTimesVolumeSum: myPriceTimesVolumeSumTemp += x myVWAPTemp = (myPriceTimesVolumeSumTemp)/myVolumesSumTemp self.myVWAP.append(myVWAPTemp) if(len(self.myVWAP) > 1): myHighSD = myVWAPTemp + ( 2.0 * statistics.stdev(self.myPrices) ) myLowSD = myVWAPTemp - ( 2.0 * statistics.stdev(self.myPrices) ) else: myHighSD = myVWAPTemp myLowSD = myVWAPTemp self.myHullShort.append(self.myHMAShort.Current.Value) myHullShortLen = len(self.myHullShort) self.myHullLong.append(self.myHMALong.Current.Value) myHullLongLen = len(self.myHullLong) #if( myHullLongLen >= 21 ): # self.Plot(symbol, "High 2SD", myHighSD) # self.Plot(symbol, "MYVWAP", myVWAPTemp) # self.Plot(symbol, "Price", myCurrentPrice) # self.Plot(symbol, "Low 2SD", myLowSD) self.my2SDHigh = myHighSD self.my2SDLow = myLowSD if(bar.High >= myHighSD): self.isHitAbove2SD = 1 else: self.isHitAbove2SD = 0 if(bar.Low < myLowSD): self.isHitBelow2SD = 1 else: self.isHitBelow2SD = 0 if(len(self.myHullShort) >= 2 and self.myHullShort[-1] < self.myHullShort[-2]): self.whichWay3HULL = "Short" if(len(self.myHullShort) >= 2 and self.myHullShort[-1] > self.myHullShort[-2]): self.whichWay3HULL = "Long" self.myCurrent7HULLPrice = self.myHMALong.Current.Value # Do not trade until 10AM if(self.Time.hour > 9 and self.Time.hour < 16): if( myHullLongLen >= 21 ): self.Plot(symbol, "High 2SD", myHighSD) #self.Plot(symbol, "MYVWAP", myVWAPTemp) self.Plot(symbol, "Price", myCurrentPrice) self.Plot(symbol, "Low 2SD", myLowSD) self.myATR.append(bar.High - bar.Low) self.myATR.pop(0) self.myATRAverage = sum(self.myATR) / len(self.myATR) self.myLastLow = bar.Low self.myLastHigh = bar.High self.myHighs.append(bar.High) self.myCloses.append(bar.Close) self.myLows.append(bar.Low) self.myHighestHigh = max(self.myCloses) self.myLowestLow = min(self.myCloses) if(bar.High > self.bb20.UpperBand.Current.Value): self.myLastHigherBB = 1 else: self.myLastHigherBB = 0 if(bar.Low < self.bb20.LowerBand.Current.Value): self.myLastLowerBB = 1 else: self.myLastLowerBB = 0 if(self.whichTradeIsNext == "Short" and self.myLastHigherBB <= 20 and bar.Low <= self.bb20.LowerBand.Current.Value): self.myTradeShort = 1 if(self.whichTradeIsNext == "Long" and self.myLastLowerBB <= 20 and bar.High >= self.bb20.UpperBand.Current.Value): self.myTradeLong = 1 #if(bar.High >= self.bb20.UpperBand.Current.Value): # self.isFirstTime = 0 # self.whichTradeIsNext = "Short" #if(bar.Low <= self.bb20.LowerBand.Current.Value): # self.isFirstTime = 0 # self.whichTradeIsNext = "Long" def Resetting(self): self.myBBUpperCount = 0 self.myBBLowerCount = 0 self.isInShortTrade = 0 self.isInLongTrade = 0 self.myLastHigherBB = 0 self.myLastLowerBB = 0 self.isLookingToShort = 0 self.isLookingToLong = 0 self.myTradeShort = 0 self.myTradeLong = 0 self.isFirstTime = 1 self.myPrices.clear() self.myVolumes.clear() self.myPricesSum.clear() self.myVolumesSum.clear() self.myPriceTimesVolumeSum.clear() self.myVWAP.clear() self.myHullShort.clear() self.myHullLong.clear() self.isHitAbove2SD = 0 self.isHitBelow2SD = 0 self.myOrderPrice = 0 self.isHullGreenToRed = 0 self.isHullRedToGreen = 0 self.isLimitOrder = 0 self.myOrderPrice = 0 self.isInShortTrade = 0 self.isInLongTrade = 0 self.hull3Price = 0 self.my2SDHigh = 0 self.my2SDLow = 0 self.myCurrent7HULLPrice = 0 def OnData(self, data): # Do not trade until 10AM, do not trade the last minute of the day timeCombined = str(self.Time.hour) + str(self.Time.minute) if(self.Time.hour > 9 and self.Time.hour < 16 and timeCombined != "1559"): myCurrentPrice = self.Securities[self.aapl].Open #if(myCurrentPrice - self.bb20.MiddleBand.Current.Value < 0.25): # self.isHitAbove2SD = 0 #if(self.bb20.MiddleBand.Current.Value - myCurrentPrice < 0.25): # self.isHitBelow2SD = 0 if(self.isInShortTrade == 0 and self.isHitAbove2SD == 1 and self.whichWay3HULL == "Short" and myCurrentPrice >= self.myCurrent7HULLPrice): ticket = self.MarketOrder(self.aapl, -100) self.isInShortTrade = 1 self.myTradeShort = 0 #self.Debug(timeString + " Short Trade: {0}".format(myCurrentPrice)) if(self.isInLongTrade == 0 and self.isHitBelow2SD == 1 and self.whichWay3HULL == "Long" and myCurrentPrice <= self.myCurrent7HULLPrice): ticket = self.MarketOrder(self.aapl, 100) self.isInLongTrade = 1 self.myTradeLong = 0 #self.Debug(timeString + " Long Trade: {0}".format(myCurrentPrice)) # Once we have made $0.30, then tighten the stop loss #if(self.Portfolio[self.aapl].UnrealizedProfit >= 30 and self.belowHighLow != -0.10): # self.belowHighLow = 0.02 # Once we have made $0.50, then tighten the stop loss even further #if(self.Portfolio[self.aapl].UnrealizedProfit >= 50): # self.belowHighLow = -0.10 if(self.isInLongTrade == 1 and myCurrentPrice >= self.bb20.UpperBand.Current.Value - 0.03): self.Liquidate(self.aapl) self.isInLongTrade = 0 self.whichTradeIsNext = "Short" self.myTradeShort = 0 self.myTradeLong = 0 self.belowHighLow = 0.12 self.isHitBelow2SD = 0 if(self.isInShortTrade == 1 and myCurrentPrice <= self.bb20.LowerBand.Current.Value + 0.03): self.Liquidate(self.aapl) self.isInShortTrade = 0 self.whichTradeIsNext = "Long" self.myTradeShort = 0 self.myTradeLong = 0 self.belowHighLow = 0.12 self.isHitAbove2SD = 0 if((self.isInShortTrade == 1 or self.isInLongTrade == 1) and (self.Portfolio[self.aapl].UnrealizedProfit <= -10)): self.Liquidate(self.aapl) if(self.isInShortTrade == 1): self.whichTradeIsNext = "Long" else: self.whichTradeIsNext = "Short" self.isInShortTrade = 0 self.isInLongTrade = 0 self.isHitAbove2SD = 0 self.isHitBelow2SD = 0 self.myTradeShort = 0 self.myTradeLong = 0 self.belowHighLow = 0.12 def End_of_day_liquidate(self): self.Liquidate() #self.Debug("Liquidate End of Day: {0}".format(self.Securities["AAPL"].Open))