Hey there. Take a look at my VP trading backtest. For some reason it never commits any trades despite the fact that it most definitely should do at least some import numpy as np
import pandas as pd
import vectorbt as vbt
import ccxt
from datetime import datetime, timedelta
from numba import jit
from numba import njit, prange
import warnings
from concurrent.futures import ThreadPoolExecutor
from functools import partial
import time
from threading import Lock
import matplotlib.pyplot as plt
import seaborn as sns
import os
warnings.filterwarnings('ignore')

class VolumeProfileStrategy:
    def __init__(self, 
                 symbol,
                 exchange_id='binance',
                 price_bucket_count=100,  # Number of price buckets for volume profile
                 profile_period='1d',     # Period for calculating volume profile
                 volume_threshold=0.7,     # Threshold for identifying significant volume nodes
                 node_distance=0.02,       # Minimum distance between volume nodes
                 entry_confirmation_bars=3, # Bars needed for entry confirmation
                 exit_confirmation_bars=2,  # Bars needed for exit confirmation
                 risk_per_trade=0.02,
                 interval='1m',
                 max_workers=5):
        
        self.symbol = symbol
        self.price_bucket_count = price_bucket_count
        self.profile_period = profile_period
        self.volume_threshold = volume_threshold
        self.node_distance = node_distance
        self.entry_confirmation_bars = entry_confirmation_bars
        self.exit_confirmation_bars = exit_confirmation_bars
        self.risk_per_trade = risk_per_trade
        self.interval = interval
        self.max_workers = max_workers
        
        # Create exchange connection pool
        self.exchange_pool = [
            getattr(ccxt, exchange_id)({
                'enableRateLimit': True,
                'options': {'defaultType': 'future'}
            }) for _ in range(max_workers)
        ]
        self.pool_lock = Lock()
        self.pool_index = 0
        
        # Pre-compile Numba functions
        self._compile_numba_functions()

    def _get_exchange(self):
        """Get next available exchange instance from the pool"""
        with self.pool_lock:
            exchange = self.exchange_pool[self.pool_index]
            self.pool_index = (self.pool_index + 1) % len(self.exchange_pool)
            return exchange

    @staticmethod
    @jit(nopython=True)
    def _calculate_volume_profile(prices, volumes, bucket_count):
        """Calculate volume profile metrics using Numba optimization"""
        if len(prices) == 0 or len(volumes) == 0:
            return np.zeros(bucket_count), np.zeros(bucket_count)
            
        min_price = np.min(prices)
        max_price = np.max(prices)
        
        # Prevent division by zero
        if min_price == max_price:
            price_range = 1.0
        else:
            price_range = max_price - min_price
            
        bucket_size = price_range / bucket_count
        
        volume_profile = np.zeros(bucket_count)
        price_buckets = np.zeros(bucket_count)
        
        for i in range(len(prices)):
            if bucket_size > 0:
                bucket_idx = int((prices[i] - min_price) / bucket_size)
                if 0 <= bucket_idx < bucket_count:
                    volume_profile[bucket_idx] += volumes[i]
                    price_buckets[bucket_idx] = min_price + (bucket_idx + 0.5) * bucket_size
            
        return volume_profile, price_buckets
    
    @staticmethod
    @jit(nopython=True)
    def _identify_volume_nodes(volume_profile, price_buckets, threshold, min_distance):
        """Identify high volume nodes using Numba optimization with more lenient conditions"""
        volume_profile = np.asarray(volume_profile)
        price_buckets = np.asarray(price_buckets)
        
        if len(volume_profile) == 0 or len(price_buckets) == 0:
            return np.zeros((0, 2), dtype=np.float64)
            
        total_volume = np.sum(volume_profile)
        if total_volume == 0:
            return np.zeros((0, 2), dtype=np.float64)
            
        normalized_volume = volume_profile / total_volume
        
        # Pre-allocate maximum possible nodes
        max_nodes = len(volume_profile)
        temp_nodes = np.zeros((max_nodes, 2), dtype=np.float64)
        node_count = 0
        
        # Find peaks more leniently
        for i in range(1, len(volume_profile) - 1):
            is_peak = normalized_volume[i] > threshold
            
            # Check if it's a local maximum or close to being one
            if i > 0:
                is_peak = is_peak and (normalized_volume[i] >= normalized_volume[i-1] * 0.95)  # Allow 5% tolerance
            if i < len(volume_profile) - 1:
                is_peak = is_peak and (normalized_volume[i] >= normalized_volume[i+1] * 0.95)  # Allow 5% tolerance
                
            if is_peak:
                temp_nodes[node_count, 0] = price_buckets[i]
                temp_nodes[node_count, 1] = normalized_volume[i]
                node_count += 1
        
        if node_count == 0:
            return np.zeros((0, 2), dtype=np.float64)
        
        # Create array with actual nodes
        nodes = temp_nodes[:node_count]
        
        # Sort nodes by price
        sorted_indices = np.argsort(nodes[:, 0])
        nodes = nodes[sorted_indices]
        
        # Filter nodes by minimum distance more leniently
        filtered_nodes = np.zeros((node_count, 2), dtype=np.float64)
        filtered_count = 0
        
        filtered_nodes[0] = nodes[0]
        filtered_count += 1
        
        for i in range(1, node_count):
            if abs(nodes[i, 0] - filtered_nodes[filtered_count-1, 0]) / filtered_nodes[filtered_count-1, 0] > min_distance:
                filtered_nodes[filtered_count] = nodes[i]
                filtered_count += 1
                
        return filtered_nodes[:filtered_count]

    def _compile_numba_functions(self):
        """Pre-compile Numba functions with dummy data"""
        # Use larger dummy dataset for more robust testing
        dummy_prices = np.array([100.0, 101.0, 102.0, 103.0, 104.0], dtype=np.float64)
        dummy_volumes = np.array([1.0, 2.0, 3.0, 2.0, 1.0], dtype=np.float64)
        
        # Test volume profile calculation
        profile, buckets = self._calculate_volume_profile(dummy_prices, dummy_volumes, 5)
        
        # Test volume nodes identification
        _ = self._identify_volume_nodes(profile, buckets, 0.2, 0.01)

    def fetch_data(self, period):
        """Fetch OHLCV data and calculate volume profile metrics with fixed storage"""
        cache_filename = f"volume_profile_cache_{self.symbol}_{self.interval}_{period}.pkl"

        if os.path.exists(cache_filename):
            print(f"Loading data from cache: {cache_filename}")
            df = pd.read_pickle(cache_filename)
            print(f"Total fetched OHLCV records: {len(df)}")
            return df

        exchange = self._get_exchange()
        end_time = int(time.time() * 1000)
        start_time = end_time - (int(period[:-1]) * 24 * 60 * 60 * 1000)

        # Fetch historical data
        all_ohlcv = []
        while start_time < end_time:
            print(f"Fetching data from {pd.to_datetime(start_time, unit='ms')}...")
            try:
                ohlcv = exchange.fetch_ohlcv(
                    symbol=self.symbol,
                    timeframe=self.interval,
                    since=start_time,
                    limit=1500
                )

                if not ohlcv:
                    break

                all_ohlcv.extend(ohlcv)
                start_time = ohlcv[-1][0] + 1

            except Exception as e:
                print(f"Error fetching data: {str(e)}")
                time.sleep(1)
                continue

        # Create DataFrame
        df = pd.DataFrame(all_ohlcv, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
        df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
        df.set_index('timestamp', inplace=True)

        # Calculate rolling volume profile
        window_size = int(pd.Timedelta(self.profile_period) / pd.Timedelta(self.interval))

        # Initialize fixed-size arrays for volume nodes
        max_nodes = self.price_bucket_count  # Assuming maximum nodes equal to bucket count
        n = len(df)
        volume_nodes_prices = np.zeros((n, max_nodes), dtype=np.float64)
        volume_nodes_volumes = np.zeros((n, max_nodes), dtype=np.float64)
        num_volume_nodes = np.zeros(n, dtype=np.int32)

        print(f"Calculating volume profiles for {n} rows...")
        for i in range(window_size, n):
            if i % 1000 == 0:  # Progress update
                print(f"Processing row {i}/{n}")

            try:
                window = df.iloc[i - window_size:i]

                if len(window) == 0 or window['close'].isna().any() or window['volume'].isna().any():
                    continue

                profile, buckets = self._calculate_volume_profile(
                    window['close'].values.astype(np.float64),
                    window['volume'].values.astype(np.float64),
                    self.price_bucket_count
                )

                nodes = self._identify_volume_nodes(
                    profile,
                    buckets,
                    self.volume_threshold,
                    self.node_distance
                )

                num_nodes = len(nodes)
                if num_nodes > max_nodes:
                    num_nodes = max_nodes  # Cap to maximum nodes

                num_volume_nodes[i] = num_nodes

                for j in range(num_nodes):
                    volume_nodes_prices[i, j] = nodes[j, 0]
                    volume_nodes_volumes[i, j] = nodes[j, 1]

            except Exception as e:
                print(f"Error calculating volume profile for window ending at {df.index[i]}: {str(e)}")
                continue

        # Assign to DataFrame
        df['volume_nodes_prices'] = list(volume_nodes_prices)
        df['volume_nodes_volumes'] = list(volume_nodes_volumes)
        df['num_volume_nodes'] = num_volume_nodes

        print(f"Saving data to cache: {cache_filename}")
        df.to_pickle(cache_filename)

        return df
    
    def plot_data(self, df):
        """Plots the OHLCV and volume profile data."""
        fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))
        
        # Plot OHLCV
        ax1.plot(df.index, df['close'], label='Close Price')
        ax1.set_title(f"{self.symbol} Price")
        ax1.set_ylabel("Price")
        ax1.legend()
        
        # Plot Volume Profile
        if 'volume_profile' in df.columns and 'price_buckets' in df.columns:
            latest_profile = df['volume_profile'].iloc[-1]
            latest_buckets = df['price_buckets'].iloc[-1]
            ax2.barh(latest_buckets, latest_profile, alpha=0.3)
            ax2.set_title("Volume Profile")
            ax2.set_xlabel("Volume")
            ax2.set_ylabel("Price")
        
        plt.tight_layout()
        plt.show()

    @staticmethod
    @njit(parallel=True)
    def generate_signals_numba(
        close_prices,
        volume_nodes_prices,
        volume_nodes_volumes,
        num_volume_nodes,
        entry_conf_bars,
        exit_conf_bars,
        node_distance
    ):
        n = len(close_prices)
        long_entries = np.zeros(n, dtype=np.bool_)
        short_entries = np.zeros(n, dtype=np.bool_)
        exits = np.zeros(n, dtype=np.bool_)

        for i in prange(n):
            if num_volume_nodes[i] == 0:
                continue  # No volume nodes to process

            highest_volume = 0.0
            highest_price = 0.0
            lowest_volume = 1.0
            lowest_price = 0.0

            # Iterate through all volume nodes at time i
            for j in range(num_volume_nodes[i]):
                volume = volume_nodes_volumes[i, j]
                price = volume_nodes_prices[i, j]

                # Identify highest volume node
                if volume > highest_volume:
                    highest_volume = volume
                    highest_price = price

                # Identify lowest volume node
                if volume < lowest_volume:
                    lowest_volume = volume
                    lowest_price = price

            # Long Entry Condition: Close price crosses above the highest volume node
            if close_prices[i] > highest_price:
                long_entries[i] = True

                # Schedule exit after exit_confirmation_bars
                exit_time = i + exit_conf_bars
                if exit_time < n:
                    exits[exit_time] = True

            # Short Entry Condition: Close price crosses below the lowest volume node
            if close_prices[i] < lowest_price:
                short_entries[i] = True

                # Schedule exit after exit_confirmation_bars
                exit_time = i + exit_conf_bars
                if exit_time < n:
                    exits[exit_time] = True

        return long_entries, short_entries, exits

    def generate_signals(self, data):
        close_prices = data['close'].values.astype(np.float64)
        volume_nodes_prices = np.vstack(data['volume_nodes_prices'].values).astype(np.float64)
        volume_nodes_volumes = np.vstack(data['volume_nodes_volumes'].values).astype(np.float64)
        num_volume_nodes = data['num_volume_nodes'].values.astype(np.int32)

        long_entries, short_entries, exits = self.generate_signals_numba(  # Use self here
            close_prices,
            volume_nodes_prices,
            volume_nodes_volumes,
            num_volume_nodes,
            self.entry_confirmation_bars,
            self.exit_confirmation_bars,
            self.node_distance
        )

        signals = pd.DataFrame({
            'long_entries': long_entries,
            'short_entries': short_entries,
            'exits': exits
        }, index=data.index)

        return signals

    def backtest(self, data, initial_capital, fee_rate):
        """Run backtest using vectorbt with additional debugging"""
        print("\nStarting backtest...")
        print(f"Data shape: {data.shape}")
        print(f"Data columns: {data.columns}")
        print(f"First few rows of close prices:\n{data['close'].head()}")

        # Define the numeric columns used in backtesting
        numeric_cols = ['close', 'volume']  # Add other numeric columns if necessary

        # Check for NaN values in numeric columns only
        if data[numeric_cols].isna().any().any():
            print("Data contains NaN values in numeric columns. Please clean the data before backtesting.")
            raise ValueError("Data contains NaN values.")

        # Check for infinite values in numeric columns only
        if np.isinf(data[numeric_cols].values).any():
            print("Data contains infinite values in numeric columns. Please clean the data before backtesting.")
            raise ValueError("Data contains infinite values.")

        signals = self.generate_signals(data)

        # Debug: Inspect signals
        print(f"Generated signals - Long: {signals['long_entries'].sum()}, Short: {signals['short_entries'].sum()}, Exits: {signals['exits'].sum()}")

        # Calculate position sizes and stop-losses
        entry_prices = pd.Series(index=data.index, dtype=float)
        entry_prices[signals['long_entries']] = data.loc[signals['long_entries'], 'close']
        entry_prices[signals['short_entries']] = data.loc[signals['short_entries'], 'close']

        stop_prices = pd.Series(index=data.index, dtype=float)
        stop_prices[signals['long_entries']] = entry_prices[signals['long_entries']] * (1 - self.risk_per_trade)
        stop_prices[signals['short_entries']] = entry_prices[signals['short_entries']] * (1 + self.risk_per_trade)

        size = initial_capital / data['close'].iloc[0]

        print("\nPre-vectorbt checks:")
        print(f"Size calculated: {size}")
        print(f"Initial close price: {data['close'].iloc[0]}")

        try:
            portfolio = vbt.Portfolio.from_signals(
                close=data['close'],
                entries=signals['long_entries'] | signals['short_entries'],
                exits=signals['exits'],
                short_entries=signals['short_entries'],
                sl_stop=stop_prices,
                init_cash=initial_capital,
                fees=fee_rate,
                size=size,
                freq=self.interval
            )

            trades = portfolio.trades.records

            print("\nTrade Summary:")
            if trades is not None and len(trades) > 0:
                print(f"Number of trades: {len(trades)}")
                print("First trade details:")
                print(trades.iloc[0])
            else:
                print("No trades generated")

            metrics = {
                'Total Return (%)': portfolio.total_return() * 100,
                'Sharpe Ratio': portfolio.sharpe_ratio(),
                'Max Drawdown (%)': portfolio.max_drawdown() * 100,
                'Number of Trades': len(trades) if trades is not None else 0,
                'Win Rate (%)': (np.sum(trades['pnl'] > 0) / len(trades)) * 100 if trades is not None and len(trades) > 0 else 0,
                'Profit Factor': np.sum(trades[trades['pnl'] > 0]['pnl']) / abs(np.sum(trades[trades['pnl'] < 0]['pnl'])) if trades is not None and len(trades) > 0 and abs(np.sum(trades[trades['pnl'] < 0]['pnl'])) > 0 else 0
            }
            print(f"Metrics: {metrics}")  # Print metrics

            return metrics, portfolio, data

        except Exception as e:
            print(f"\nError in vectorbt processing: {str(e)}")
            raise
        
    def verify_volume_nodes(self, data):
        """Verify that volume nodes are being calculated correctly"""
        print("\nVerifying volume nodes calculation...")
        
        # Check a specific time window
        sample_window = data.iloc[-100:]  # Last 100 rows
        
        # Calculate volume profile for this window
        prices = sample_window['close'].values.astype(np.float64)
        volumes = sample_window['volume'].values.astype(np.float64)
        
        profile, buckets = self._calculate_volume_profile(
            prices,
            volumes,
            self.price_bucket_count
        )
        
        nodes = self._identify_volume_nodes(
            profile,
            buckets,
            self.volume_threshold,
            self.node_distance
        )
        
        print(f"\nSample window statistics:")
        print(f"Price range: {prices.min():.2f} - {prices.max():.2f}")
        print(f"Volume range: {volumes.min():.2f} - {volumes.max():.2f}")
        print(f"Number of volume nodes found: {len(nodes)}")
        
        if len(nodes) > 0:
            print("\nVolume nodes found:")
            for node in nodes:
                print(f"Price level: {node[0]:.2f}, Relative volume: {node[1]:.4f}")
        else:
            print("No volume nodes found - this may indicate an issue with the parameters")
    
def progress_bar(current, total):
    """Prints a simple progress bar."""
    percent_complete = int(100 * current / total)
    if percent_complete % 1 == 0: # Only update for whole percentage changes
      bar_length = 20
      filled_length = int(bar_length * current / total)
      bar = '=' * filled_length + '-' * (bar_length - filled_length)
      print(f'\r[{bar}] {percent_complete}%', end='', flush=True)

def optimize_parameters(strategy, param_grid, initial_capital, fee_rate, period):
    results = []
    print("Fetching data...")
    data = strategy.fetch_data(period)  # Fetch data here
    print("Data fetched successfully.")

    from itertools import product
    param_combinations = [dict(zip(param_grid.keys(), values)) 
                         for values in product(*param_grid.values())]
    
    total_combinations = len(param_combinations)
    print(f"Testing {total_combinations} parameter combinations...")
    
    for i, params in enumerate(param_combinations, 1):
        progress_bar(i, total_combinations)
        print(f"\nTesting combination {i}/{total_combinations}: {params}")
        
        # Update strategy parameters
        for param, value in params.items():
            setattr(strategy, param, value)
        
        # Run backtest
        try:
            metrics, _, _ = strategy.backtest(data, initial_capital, fee_rate)  # Pass data to backtest
            print(f"Metrics for combination {i}: {metrics}")
            results.append({**params, **metrics})
        except Exception as e:
            print(f"Error testing combination {params}: {str(e)}")
            results.append({**params, 'Error': str(e)})

    results_df = pd.DataFrame(results)
    print("\nAvailable columns in results:")
    print(results_df.columns.tolist())  # Print list of columns
    
    if results_df.empty:
        print("No successful backtests. `results` DataFrame is empty.")
    else:
        print("Backtests completed successfully.")
    
    return results_df

if __name__ == "__main__":
    # Test with a single parameter set
    strategy = VolumeProfileStrategy(
        symbol='SOL/USDT',
        exchange_id='binance'
    )
    
    param_grid = {
        'price_bucket_count': [50],
        'volume_threshold': np.linspace(0.01, 1, 3).tolist(),  # Much lower thresholds
        'node_distance': np.linspace(0.0001, 0.015, 3).tolist(),  # More granular distances
        'entry_confirmation_bars': [2],
        'exit_confirmation_bars': [1],
        'risk_per_trade': [0.02]
    }
        
    results = optimize_parameters(
        strategy=strategy,
        param_grid=param_grid,
        initial_capital=10000,
        fee_rate=0.001,
        period='90d'
    )
    
    print("\nTop 5 parameter combinations by Sharpe ratio:")
    print(results.sort_values('Sharpe Ratio', ascending=False).head())
        # Create the heatmap with corrected scaling
    heatmap_data = results.pivot_table(values='Sharpe Ratio', index='volume_threshold', columns='node_distance')
    plt.figure(figsize=(10, 8))
    sns.heatmap(heatmap_data, annot=False, cmap='viridis', fmt=".2f", vmin=heatmap_data.min().min(), vmax=heatmap_data.max().max()) # Set vmin and vmax
    plt.title('Sharpe Ratio Heatmap')
    plt.ylabel('volume_threshold')
    plt.xlabel('node_distance')
    plt.show()

It just prints out Available columns in results:
['price_bucket_count', 'volume_threshold', 'node_distance', 'entry_confirmation_bars', 'exit_confirmation_bars', 'risk_per_trade', 'Total Return (%)', 'Sharpe Ratio', 'Max Drawdown (%)', 'Number of Trades', 'Win Rate (%)', 'Profit Factor']
Backtests completed successfully.

Top 5 parameter combinations by Sharpe ratio:
   price_bucket_count  volume_threshold  node_distance  entry_confirmation_bars  exit_confirmation_bars  ...  Sharpe Ratio  Max Drawdown (%)  Number of Trades  Win Rate (%)  Profit Factor
0                  50             0.010        0.00010                        2                       1  ...           inf               0.0                 0             0              0
1                  50             0.010        0.00755                        2                       1  ...           inf               0.0                 0             0              0
2                  50             0.010        0.01500                        2                       1  ...           inf               0.0                 0             0              0
3                  50             0.505        0.00010                        2                       1  ...           inf               0.0                 0             0              0
4                  50             0.505        0.00755                        2                       1  ...           inf               0.0                 0             0              0