Overall Statistics
Total Orders
102
Average Win
3.32%
Average Loss
-1.36%
Compounding Annual Return
174.508%
Drawdown
14.400%
Expectancy
0.540
Start Equity
1000000
End Equity
1288514.89
Net Profit
28.851%
Sharpe Ratio
3.092
Sortino Ratio
4.817
Probabilistic Sharpe Ratio
79.604%
Loss Rate
55%
Win Rate
45%
Profit-Loss Ratio
2.44
Alpha
0.745
Beta
1.31
Annual Standard Deviation
0.351
Annual Variance
0.123
Information Ratio
2.491
Tracking Error
0.331
Treynor Ratio
0.828
Total Fees
$1895.04
Estimated Strategy Capacity
$11000000.00
Lowest Capacity Asset
NFLX SEWJWLJNHZDX
Portfolio Turnover
74.60%
from AlgorithmImports import *
from datetime import datetime

class Magnificent7IntradayStrategy(QCAlgorithm):
    
    def initialize(self):
        self.set_start_date(2024, 1, 1)
        self.set_end_date(2024, 4, 1)
        self.set_cash(1000000)

        # Define the MEGA-CAP stocks
        self.symbols = [self.add_equity(ticker, Resolution.MINUTE).symbol for ticker in ["TSLA", "AMZN", "NFLX", "AAPL", "NVDA", "GOOGL", "MSFT", "META"]]
        
        # Define indicators
        self.rsi_indicators = {symbol: self.rsi(symbol, 14, MovingAverageType.WILDERS, Resolution.MINUTE) for symbol in self.symbols}
        self.sma_indicators = {symbol: self.sma(symbol, 200, Resolution.MINUTE) for symbol in self.symbols}
        
        # Schedule trading hours
        self.schedule.on(self.date_rules.every_day(), self.time_rules.every(timedelta(minutes=15)), self.trade)
        
        # Risk management
        self.set_risk_management(MaximumDrawdownPercentPerSecurity(0.015))
        
        # Track last trade date for each symbol
        self.last_trade_date = {symbol: None for symbol in self.symbols}
        self.settings.minimum_order_margin_portfolio_percentage = 0

    
    def trade(self):
        current_time = self.Time
        if current_time.weekday() >= 5 or not (current_time.hour == 9 and current_time.minute >= 45):
            return
        
        for symbol in self.symbols:
            if not self.rsi_indicators[symbol].is_ready or not self.sma_indicators[symbol].is_ready:
                continue
            
            price = self.securities[symbol].price
            rsi_value = self.rsi_indicators[symbol].current.value
            sma_value = self.sma_indicators[symbol].current.value
            
            # Check if a trade has already been made today
            if self.last_trade_date[symbol] == current_time.date():
                continue
            
            # Entry condition
            if rsi_value < 30 and price < sma_value:
                self.set_holdings(symbol, 1)
                self.last_trade_date[symbol] = current_time.date()
            
            # Exit conditions
            if self.portfolio[symbol].invested:
                previous_day_high = self.history(symbol, 1, Resolution.Daily)["high"].iloc[0]
                if price >= previous_day_high or rsi_value > 80:
                    self.liquidate(symbol)
                    self.last_trade_date[symbol] = current_time.date()  # Update trade date on exit