Created with Highcharts 12.1.2Equity20062008201020122014201620182020202220242026050M100M-50-25000.050.1012025M50M01G48505254
Overall Statistics
Total Orders
28
Average Win
146.64%
Average Loss
0%
Compounding Annual Return
24.207%
Drawdown
53.700%
Expectancy
0
Start Equity
1000000
End Equity
77998895.50
Net Profit
7699.890%
Sharpe Ratio
0.679
Sortino Ratio
0.526
Probabilistic Sharpe Ratio
5.206%
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
0.122
Beta
0.977
Annual Standard Deviation
0.267
Annual Variance
0.071
Information Ratio
0.549
Tracking Error
0.219
Treynor Ratio
0.186
Total Fees
$56448.78
Estimated Strategy Capacity
$22000000.00
Lowest Capacity Asset
BGU U7EC123NWZTX
Portfolio Turnover
0.30%
from AlgorithmImports import *

class MacdReversionStrategy(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2005, 1, 1)
        self.SetEndDate(2025, 1, 31)
        self.SetCash(1000000)  # Set initial capital to 1 million dollars
        
        self.spxl = self.AddEquity("SPXL", Resolution.Daily).Symbol

        self.fast_period = 12
        self.slow_period = 26
        self.signal_period = 9

        self.macd = self.MACD(self.spxl, self.fast_period, self.slow_period, self.signal_period, MovingAverageType.Wilders, Resolution.Daily, Field.Close)
        self.sma150 = self.SMA(self.spxl, 150, Resolution.Daily)  # 150-day moving average
        self.sma10 = self.SMA(self.spxl, 10, Resolution.Daily)  # 10-day moving average

        self.previous_histogram = None
        self.entry_price = None
        self.stop_loss = 50
        self.SetWarmUp(150)

    def OnData(self, data):
        if self.IsWarmingUp:
            return
        if not self.macd.IsReady or not self.sma150.IsReady or not self.sma10.IsReady:
            return

        price = self.Securities[self.spxl].Close
        macd_histogram = self.macd.Current.Value - self.macd.Signal.Current.Value
        close_above_sma150 = price > self.sma150.Current.Value
        close_below_sma10 = price < self.sma10.Current.Value

        if close_above_sma150 and close_below_sma10 and self.previous_histogram is not None:
            if macd_histogram > self.previous_histogram and macd_histogram < 0:
                self.SetHoldings(self.spxl, 1)  # Use full portfolio on every trade
                self.entry_price = price
        
        self.previous_histogram = macd_histogram

        # Exit condition: closing candle $10 above entry price or $50 below entry price (stop loss)
        if self.Portfolio[self.spxl].Invested and self.entry_price is not None:
            if price >= self.entry_price + 10 or price <= self.entry_price - self.stop_loss:
                self.Liquidate(self.spxl)