Overall Statistics
Total Trades
0
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.435
Tracking Error
0.201
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
#region imports
from AlgorithmImports import *
#endregion
# MACD Signal Delta Percent simplified


class MACDTrendAlgorithm(QCAlgorithm):

    tradeLock = True

    def Initialize(self):
        self.SetStartDate(2022, 1, 1)
        self.SetEndDate(2022, 8, 12)
        self.SetCash(100000)
        self.stopBuffer = 0.10
        self.X = self.AddEquity("QQQ", Resolution.Hour).Symbol
        
        #indicator set up
        self.macd = self.MACD(self.X, 12, 26, 9, MovingAverageType.Exponential, Resolution.Hour)
        self.ema =  self.EMA(self.X, 100, Resolution.Hour)

        #indicator previous values setup
        self.macdPrev = None
        self.signalPrev = None
        self.priceHighPrev = None
        self.priceLowPrev = None

        #scheduled events
        self.Schedule.On(self.DateRules.EveryDay(self.X), self.TimeRules.AfterMarketOpen(self.X, 30), self.TradeLock)
        self.Schedule.On(self.DateRules.EveryDay(self.X), self.TimeRules.BeforeMarketClose(self.X, 5), self.EODActivity)

        self.SetWarmup(100)
        

    def OnData(self, data):

        if self.IsWarmingUp or not self.ema.IsReady:
            return

        if self.macdPrev is None:
            self.macdCurr = self.macd.Current.Value
            self.signalPrev = self.macd.Signal.Current.Value
            self.emaPrev = self.ema.Current.Value
            self.priceHighPrev = self.Securities[self.X].High
            self.priceLowPrev = self.Securities[self.X].Low 
            return
       
        macdCurr = self.macd.Current.Value
        signalCurr = self.macd.Signal.Current.Value
        emaCurr = self.ema.Current.Value
        priceHighCurr = self.Securities[self.X].High
        priceLowCurr = self.Securities[self.X].Low

        #CRITERIA FOR ENTERING A LONG POSITION
        #1. Price must be above the 100-day EMA
        #2. MACD must be crossing up and over the signal line
        #3. Stop-Loss should be set to bar's low minus 0.XX cents

        #CRITERIA FOR ENTERING A SHORT POSITION
        #1. Price must be below the 100-day EMA
        #2. MACD must be crossing down and over the signal line
        #3. Stop-Loss should be set to bar's high minus 0.XX cents        
        if not self.Portfolio.Invested and self.tradeLock is False:
            #LONG TRADE ENTRY
            if self.priceLowPrev > self.emaPrev and self.priceLowCurr > self.emaCurr:
                if self.macdPrev < self.signalPrev and self.macdCurr > self.signalCurr: 
                    self.SetHoldings(self.X, 0.8)
                    holdings = self.Portfolio[self.X].Quantity
                    self.StopMarketOrder(self.X, -self.holdings, self.priceLowCurr - self.stopBuffer)
            #SHORT TRADE ENTRY        
            elif self.priceHighPrev < self.emaPrev and self.priceHighCurr < self.emaCurr:
                if self.macdPrev > self.signalPrev and self.macdCurr < self.signalCurr: 
                    self.SetHoldings(self.X, -0.8)
                    holdings = self.Portfolio[self.X].Quantity
                    self.StopMarketOrder(self.X, self.holdings, self.priceHighCurr + self.stopBuffer)       
        elif self.Portfolio.IsLong:
            if self.macdPrev > self.signalPrev and self.macdCurr < self.signalCurr: 
                self.Transactions.CancelOpenOrders(self.X)
                self.Liquidate() 
        elif self.Portfolio.IsShort:
            if self.macdPrev < self.signalPrev and self.macdCurr > self.signalCurr: 
                self.Transactions.CancelOpenOrders(self.X)
                self.Liquidate() 
        
        self.macdPrev = self.macd.Current.Value
        self.signalPrev = self.macd.Signal.Current.Value
        self.emaPrev = self.ema.Current.Value
        self.priceLowPrev = self.Securities[self.X].High
        self.priceHighPrev = self.Securities[self.X].Low
                       
    def TradeLock(self):
        self.tradeLock = False

    def EODActivity(self):
        if self.Portfolio.Invested:
            self.Liquidate()
        self.tradeLock = True