Overall Statistics
Total Orders
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Start Equity
100000
End Equity
100000
Net Profit
0%
Sharpe Ratio
0
Sortino 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
-27.44
Tracking Error
0.192
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
Portfolio Turnover
0%
Drawdown Recovery
0
#region imports
from AlgorithmImports import *
#endregion

class GapPercentageChecker(QCAlgorithm):

    def Initialize(self):
        '''
        Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm.
        All algorithms must initialized.
        '''

        # --- USER SETTINGS ---
        # 1. Set the specific date you want to check
        self.target_date = datetime(2023, 2, 3)

        # 2. Add the ticker symbols you want to analyze
        self.tickers = ["ALR", "AWH", "BGXX"]
        # --- END USER SETTINGS ---

        # Set a date range for the history request. We only need a few days.
        # This ensures we capture the previous trading day, even with weekends.
        self.SetStartDate(self.target_date - timedelta(days=4))
        self.SetEndDate(self.target_date)

        self.SetCash(100000)  # Set a default cash amount (not used for this analysis)

        # Create Symbol objects for the history request
        self.symbols = [self.AddEquity(ticker, Resolution.Daily).Symbol for ticker in self.tickers]

        # Request 2 days of historical data ending on our target date
        # This will give us the target day and the previous trading day.
        history = self.History(self.symbols, 2, Resolution.Daily)

        if history.empty:
            self.Error("No history data was returned. Please check the date and tickers.")
            return

        self.Debug(f"--- Gap Analysis for {self.target_date.date()} ---")

        # Loop through each symbol to perform the calculation
        for symbol in self.symbols:
            # Check if the symbol exists in the history data's index
            if symbol in history.index.levels[0]:
                symbol_history = history.loc[symbol]

                # Ensure we have data for two consecutive days
                if len(symbol_history) == 2:
                    # The first row [0] is the previous day, the second row [1] is the target day
                    previous_close = symbol_history.iloc[0]['close']
                    current_open = symbol_history.iloc[1]['open']

                    # Avoid division by zero if previous close was 0
                    if previous_close == 0:
                        continue

                    # Calculate the gap percentage
                    gap_percent = ((current_open - previous_close) / previous_close) * 100

                    # Log the results in a clean format
                    self.Log(f"Ticker: {symbol.Value}")
                    self.Log(f"  -> Open Price:     ${current_open:,.2f}")
                    self.Log(f"  -> Previous Close: ${previous_close:,.2f}")
                    self.Log(f"  -> Gap:            {gap_percent:+.2f}%") # '+' sign shows direction
                else:
                    self.Log(f"Ticker: {symbol.Value} -> Could not get 2 days of data (likely a holiday or delisted).")
            else:
                 self.Log(f"Ticker: {symbol.Value} -> No data found in history request.")


    def OnData(self, data: Slice):
        '''
        OnData event is the primary entry point for your algorithm.
        Each new data point will be pumped in here.
        This algorithm does all its work in Initialize, so this method is empty.
        '''
        pass