Skip to content
The Chat Archive
o1-mini-2024-09-12· OpenAI WildChat-4.8M (AI2)

Write me code for Leveraging BQMs for Identifying Significant Price Levels and Their…

  1. User

    Write me code for Leveraging BQMs for Identifying Significant Price Levels and Their Strength in Stock Market Data. I aim to identify significant price levels in the SPY market and determine their strength based on historical data for use in a live market setting during trading hours.\n\nA significant level is a price point where the market tends to pause, reverse, or break through. The strength of a level is its ability to hold or resist price movements.\n\nKey components needed:\n1. Formulating the BQM\n- Define binary variables for potential significant price levels (e.g., x1, x2, x3 for every 5 points)\n- Objective function to maximize identified significant levels\n- Binary constraints (0 or 1)\n\n2. Implementation needs:\n- Training code (quanttrainingcode.py) using historical data\n- Prediction code (quantpredictcode.py) for live market use\n- Continuous BQM updates with latest market data\n- Strength calculation based on historical price interactions\n- Alerts for entry, hold, and exit signals\n- Logging predictions to CSV/TXT file with timestamps\n\nPlease provide both training and prediction codes with these specifications

  2. Assistant

    Certainly! Below are two Python scripts designed to help you identify significant price levels in the SPY market using Binary Quadratic Models (BQM). The first script, quanttrainingcode.py, is responsible for training the model using historical data. The second script, quantpredictcode.py, handles live predictions during trading hours.

    Please Note: Implementing automated trading strategies involves significant risk. Ensure you thoroughly backtest and validate these scripts before deploying them in a live environment.


    1. quanttrainingcode.py

    This script processes historical SPY data to identify potential significant price levels and calculates their strength based on historical interactions. It then formulates a BQM to determine the most significant levels.

    Key Components:

    • Data Loading and Preprocessing: Loads historical data and defines potential price levels at every $5 interval.
    • Strength Calculation: Determines the strength of each price level based on how often the price interacted with it (paused, reversed, or broke through).
    • BQM Formulation: Constructs a BQM where each binary variable represents whether a price level is significant. The objective is to maximize the total strength of the selected price levels.
    • Model Saving: Saves the computed strengths for future use in the prediction script.

    Dependencies:

    • pandas
    • numpy
    • dimod
    • pickle (for saving the model)

    Code:

    # quanttrainingcode.py
    
    import pandas as pd
    import numpy as np
    import dimod
    import pickle
    import os
    
    # Configuration
    HISTORICAL_DATA_PATH = 'historical_spy_data.csv'  # Path to your historical SPY data
    MODEL_OUTPUT_PATH = 'bqm_model.pkl'
    PRICE_LEVEL_INTERVAL = 5  # Define price levels every $5
    
    def load_historical_data(path):
        """
        Load historical SPY data.
        Expected columns: ['Date', 'Open', 'High', 'Low', 'Close', 'Volume']
        """
        df = pd.read_csv(path, parse_dates=['Date'])
        df.sort_values('Date', inplace=True)
        df.reset_index(drop=True, inplace=True)
        return df
    
    def define_price_levels(df, interval):
        """
        Define potential price levels based on the price interval.
        """
        min_price = np.floor(df['Low'].min() / interval) * interval
        max_price = np.ceil(df['High'].max() / interval) * interval
        price_levels = np.arange(min_price, max_price + interval, interval)
        return price_levels
    
    def calculate_strength(df, price_levels):
        """
        Calculate the strength of each price level based on historical interactions.
        Strength can be defined as the number of times the price has interacted with the level.
        """
        strength = {}
        for level in price_levels:
            # Count how many times the price touched or crossed the level
            hits = ((df['Low'] <= level) & (df['High'] >= level)).sum()
            strength[level] = hits
        return strength
    
    def formulate_bqm(strength_dict):
        """
        Formulate a Binary Quadratic Model.
        Objective: Maximize the total strength of selected price levels.
        Possible Constraints:
            - Minimum distance between selected levels to avoid overcrowding.
        """
        bqm = dimod.BinaryQuadraticModel({}, {}, 0.0, dimod.BINARY)
    
        # Add linear terms (strengths)
        for level, strength in strength_dict.items():
            bqm.add_variable(level, strength)
    
        # Optional: Add quadratic penalties to enforce minimum distance (e.g., 10 points apart)
        MIN_DISTANCE = 10
        levels = sorted(strength_dict.keys())
        for i in range(len(levels)):
            for j in range(i + 1, len(levels)):
                if abs(levels[j] - levels[i]) < MIN_DISTANCE:
                    # Penalize selecting both levels
                    penalty = -min(strength_dict[levels[i]], strength_dict[levels[j]])
                    bqm.add_interaction(levels[i], levels[j], penalty)
    
        return bqm
    
    def main():
        # Load data
        df = load_historical_data(HISTORICAL_DATA_PATH)
        print("Historical data loaded.")
    
        # Define price levels
        price_levels = define_price_levels(df, PRICE_LEVEL_INTERVAL)
        print(f"Defined {len(price_levels)} price levels.")
    
        # Calculate strength
        strength = calculate_strength(df, price_levels)
        print("Calculated strength for each price level.")
    
        # Formulate BQM
        bqm = formulate_bqm(strength)
        print("Formulated BQM.")
    
        # Save model strengths for future use
        with open(MODEL_OUTPUT_PATH, 'wb') as f:
            pickle.dump(strength, f)
        print(f"Saved strength dictionary to {MODEL_OUTPUT_PATH}.")
    
        # Optionally, solve the BQM using a sampler (e.g., ExactSolver)
        # and save the solution
        sampler = dimod.ExactSolver()
        solution = sampler.sample(bqm)
        best_solution = next(iter(solution))
        selected_levels = [level for level, value in best_solution.items() if value == 1]
        print("Selected Significant Price Levels:")
        print(selected_levels)
    
        # Save selected levels
        with open('selected_levels.pkl', 'wb') as f:
            pickle.dump(selected_levels, f)
        print("Saved selected price levels to selected_levels.pkl.")
    
    if __name__ == "__main__":
        main()
    

    Instructions:

    1. Prepare Historical Data: Ensure you have a CSV file named historical_spy_data.csv with columns ['Date', 'Open', 'High', 'Low', 'Close', 'Volume'].
    2. Install Dependencies:
      pip install pandas numpy dimod
      
    3. Run the Training Script:
      python quanttrainingcode.py
      
    4. Outputs:
      • bqm_model.pkl: Serialized strength dictionary.
      • selected_levels.pkl: Pickled list of selected significant price levels.

    2. quantpredictcode.py

    This script uses the trained model to identify significant price levels in real-time during trading hours. It updates the BQM with the latest market data, calculates the strength of levels, solves the BQM, generates trading signals, and logs predictions.

    Key Components:

    • Live Data Fetching: Retrieves the latest SPY data using an API (e.g., Yahoo Finance).
    • Strength Update: Updates the strength of each price level based on recent interactions.
    • BQM Formulation and Solving: Constructs and solves the BQM to identify significant price levels in real-time.
    • Signal Generation: Generates entry, hold, and exit signals based on price interactions with significant levels.
    • Logging and Alerts: Logs predictions with timestamps and generates alerts for trading actions.

    Dependencies:

    • pandas
    • numpy
    • dimod
    • pickle
    • yfinance (for live data fetching)
    • schedule and time (for scheduling tasks)

    Code:

    # quantpredictcode.py
    
    import pandas as pd
    import numpy as np
    import dimod
    import pickle
    import yfinance as yf
    import time
    import schedule
    from datetime import datetime
    
    # Configuration
    MODEL_PATH = 'bqm_model.pkl'
    PRICE_LEVEL_INTERVAL = 5
    LOG_FILE = 'predictions_log.csv'
    ALERT_FILE = 'trading_alerts.txt'
    FETCH_INTERVAL = 1  # in minutes
    
    def load_strength_model(path):
        with open(path, 'rb') as f:
            strength = pickle.load(f)
        return strength
    
    def fetch_live_data(ticker='SPY', period='1d', interval='1m'):
        """
        Fetch live data using yfinance.
        """
        data = yf.download(tickers=ticker, period=period, interval=interval)
        if data.empty:
            return None
        data.reset_index(inplace=True)
        return data
    
    def define_price_levels(strength_dict, interval):
        """
        Define price levels based on the strength dictionary keys.
        """
        price_levels = sorted(strength_dict.keys())
        return price_levels
    
    def calculate_live_strength(live_data, price_levels):
        """
        Calculate the current strength based on recent live data.
        For simplicity, use the number of touches in the last N minutes.
        """
        strength = {}
        for level in price_levels:
            hits = ((live_data['Low'] <= level) & (live_data['High'] >= level)).sum()
            strength[level] = hits
        return strength
    
    def formulate_bqm_live(strength_dict):
        """
        Formulate BQM for live data.
        """
        bqm = dimod.BinaryQuadraticModel({}, {}, 0.0, dimod.BINARY)
    
        # Add linear terms (strengths)
        for level, strength in strength_dict.items():
            bqm.add_variable(level, strength)
    
        # Optional: Add quadratic penalties to enforce minimum distance
        MIN_DISTANCE = 10
        levels = sorted(strength_dict.keys())
        for i in range(len(levels)):
            for j in range(i + 1, len(levels)):
                if abs(levels[j] - levels[i]) < MIN_DISTANCE:
                    penalty = -min(strength_dict[levels[i]], strength_dict[levels[j]])
                    bqm.add_interaction(levels[i], levels[j], penalty)
    
        return bqm
    
    def generate_signals(selected_levels, current_price):
        """
        Generate trading signals based on current price and selected levels.
        """
        signals = []
    
        if not selected_levels:
            return signals
    
        closest_below = max([level for level in selected_levels if level <= current_price], default=None)
        closest_above = min([level for level in selected_levels if level >= current_price], default=None)
    
        if closest_below is not None:
            if current_price > closest_below:
                signals.append(f"Price above support level {closest_below}: Consider HOLD or BUY")
            elif current_price < closest_below:
                signals.append(f"Price below support level {closest_below}: Consider SELL or EXIT")
    
        if closest_above is not None:
            if current_price < closest_above:
                signals.append(f"Price below resistance level {closest_above}: Consider HOLD or BUY")
            elif current_price > closest_above:
                signals.append(f"Price above resistance level {closest_above}: Consider SELL or EXIT")
    
        return signals
    
    def log_prediction(timestamp, selected_levels, current_price):
        """
        Log the prediction to a CSV file.
        """
        log_entry = {
            'Timestamp': timestamp,
            'Selected_Levels': selected_levels,
            'Current_Price': current_price
        }
        df = pd.DataFrame([log_entry])
        if not os.path.isfile(LOG_FILE):
            df.to_csv(LOG_FILE, index=False)
        else:
            df.to_csv(LOG_FILE, mode='a', header=False, index=False)
    
    def send_alert(signals):
        """
        Send trading alerts by writing to a text file.
        """
        with open(ALERT_FILE, 'a') as f:
            for signal in signals:
                timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                f.write(f"{timestamp}: {signal}\n")
    
    def prediction_task():
        # Load the strength model
        strength_model = load_strength_model(MODEL_PATH)
        price_levels = define_price_levels(strength_model, PRICE_LEVEL_INTERVAL)
    
        # Fetch live data
        live_data = fetch_live_data()
        if live_data is None:
            print("No live data fetched.")
            return
    
        # Calculate live strength
        live_strength = calculate_live_strength(live_data, price_levels)
    
        # Formulate BQM
        bqm = formulate_bqm_live(live_strength)
    
        # Solve the BQM
        sampler = dimod.ExactSolver()
        solution = sampler.sample(bqm)
        best_solution = next(iter(solution))
        selected_levels = [level for level, value in best_solution.items() if value == 1]
    
        # Get the latest price
        current_price = live_data['Close'].iloc[-1]
    
        # Generate signals
        signals = generate_signals(selected_levels, current_price)
    
        # Log prediction
        timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        log_prediction(timestamp, selected_levels, current_price)
    
        # Send alerts
        if signals:
            send_alert(signals)
            print(f"{timestamp}: Alerts generated.")
        else:
            print(f"{timestamp}: No significant signals.")
    
    def main():
        # Schedule the prediction_task to run every FETCH_INTERVAL minutes
        schedule.every(FETCH_INTERVAL).minutes.do(prediction_task)
    
        print("Starting prediction loop. Press Ctrl+C to exit.")
        while True:
            schedule.run_pending()
            time.sleep(1)
    
    if __name__ == "__main__":
        import os
        main()
    

    Instructions:

    1. Ensure Model Files Exist: Make sure bqm_model.pkl (from the training script) is present in the same directory.
    2. Install Dependencies:
      pip install pandas numpy dimod yfinance schedule
      
    3. Run the Prediction Script:
      python quantpredictcode.py
      
    4. Outputs:
      • predictions_log.csv: Logs of each prediction with timestamps, selected levels, and current prices.
      • trading_alerts.txt: Alerts for trading actions based on signals.

    Customization:

    • Fetch Interval: Adjust FETCH_INTERVAL to change how often the script fetches new data (default is every 1 minute).
    • Price Level Interval: Adjust PRICE_LEVEL_INTERVAL if you wish to define price levels at different intervals.
    • Alert Mechanism: Currently, alerts are written to a text file. You can modify the send_alert function to integrate with email, SMS, or other notification systems.

    Additional Recommendations

    • Backtesting: Before deploying, perform extensive backtesting on historical data to evaluate the effectiveness of the identified price levels and the generated signals.
    • Error Handling: Enhance the scripts with comprehensive error handling to manage potential issues like data fetch failures or solver errors.
    • Optimization: For real-time performance, consider using more efficient solvers or quantum annealers if available.
    • Security: Secure any API keys or sensitive information if you extend the scripts to use services that require authentication.

    By following the above guidelines and utilizing the provided scripts, you can develop a system to identify and monitor significant price levels in the SPY market, aiding in making informed trading decisions during live market conditions.