drew2323 / v2trading

V2Trading platform - live trading engine, backtesting and research tool.
https://trading.mujdenik.eu
8 stars 3 forks source link

time and volume profile - histogram #104

Open drew2323 opened 9 months ago

drew2323 commented 9 months ago

Cílem je vytvořit tyto indikátory - arianta time a volume:

aplikuje se na časové okno, rozdělí min až max cenu v tomto okně do X binů.

Popis implemetace chatgpt konverzace

Výstup zatím:

# Example output u TIME varianty je druha hodnota počet výskytů, u VOLUME je to tradované volume
# {
#   (50, 52): 150,
#   (52, 54): 200,
#   (54, 56): 300,
#   (56, 58): 180,
#   (58, 60): 170
# }

Podobný histogram v barstats.py v custom indicators.

dořešit

Dokumentace k lightweightchart plugins

drew2323 commented 9 months ago
import numpy as np

def create_bins(source_array, bin_count):
    # Calculate the minimum and maximum prices from the source array
    min_price = np.min(source_array)
    max_price = np.max(source_array)
    # Create and return bin edges
    return np.linspace(min_price, max_price, bin_count + 1)

def update_histogram(data_point, bins, histogram):
    # Find the index of the bin to which the data point belongs
    bin_index = np.digitize([data_point], bins) - 1
    # Increment the corresponding bin in the histogram
    histogram[bin_index[0]] += 1

def histogram_to_dict(bins, histogram):
    # Create a dictionary pairing each bin range with its count
    bin_ranges = [(bins[i], bins[i+1]) for i in range(len(bins)-1)]
    return dict(zip(bin_ranges, histogram))

# Example usage

# Define the number of bins
bin_count = 10  

# Assuming source_array is your array of historical price data
source_array = [/* Your historical price data here */]

# Create initial bins based on the source array
bins = create_bins(source_array, bin_count)

# Initialize the histogram with zeros
histogram = np.zeros(len(bins) - 1)

# Assuming new_price_data is a stream of new price data points
new_price_data = [/* Your new price data here */]

# Update the histogram for each new data point
for price in new_price_data:
    update_histogram(price, bins, histogram)

# Convert histogram to a more interpretable dictionary format
histogram_dict = histogram_to_dict(bins, histogram)

# Now, 'histogram_dict' contains the count of times prices were in each bin range
print("Histogram:", histogram_dict)