Overall Statistics
Total Trades
28
Average Win
5.18%
Average Loss
-4.45%
Compounding Annual Return
0.592%
Drawdown
17.500%
Expectancy
0.083
Net Profit
2.750%
Sharpe Ratio
0.091
Probabilistic Sharpe Ratio
0.959%
Loss Rate
50%
Win Rate
50%
Profit-Loss Ratio
1.17
Alpha
-0.025
Beta
0.251
Annual Standard Deviation
0.081
Annual Variance
0.007
Information Ratio
-0.849
Tracking Error
0.141
Treynor Ratio
0.03
Total Fees
$28.00
Estimated Strategy Capacity
$1100000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
from AlgorithmImports import *

class MACDTrendAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2017, 7, 1)
        self.SetEndDate(2022, 2, 1)
        self.SetCash(10000)
        self.AddEquity("SPY", Resolution.Daily)

        # define our daily macd(12,26) with a 9 day signal
        self.__macd = self.MACD("SPY", 12, 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)


    def OnData(self, data):
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.'''
        # wait for our macd to fully initialize
        if not 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

        # 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