From ce62de88f9d6191f21b558b5ed18153a93579295 Mon Sep 17 00:00:00 2001 From: moen0 Date: Thu, 9 Apr 2026 16:27:55 +0200 Subject: [PATCH] liquidity level finder --- backend/data/loader.py | 18 +++++++++---- backend/indicators/liquidity.py | 46 +++++++++++++++++++++++++++++++++ backend/run.py | 21 +++++++++++---- 3 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 backend/indicators/liquidity.py diff --git a/backend/data/loader.py b/backend/data/loader.py index f4794ed..0bb2469 100644 --- a/backend/data/loader.py +++ b/backend/data/loader.py @@ -20,8 +20,16 @@ def load_candles(filepath: str) -> list[Candle]: return candles -if __name__ == "__main__": - candles = load_candles("data.csv") - print(f"Loaded {len(candles)} candles") - print(f"First: {candles[0]}") - print(f"Last: {candles[-1]}") +def resample_candles(candles, period=5): + resampled = [] + for i in range(0, len(candles) - period + 1, period): + group = candles[i:i + period] + resampled.append(Candle( + time_open=group[0].time_open, + open=group[0].open, + high=max(c.high for c in group), + low=min(c.low for c in group), + close=group[-1].close, + volume=sum(c.volume for c in group) + )) + return resampled \ No newline at end of file diff --git a/backend/indicators/liquidity.py b/backend/indicators/liquidity.py new file mode 100644 index 0000000..7e5c884 --- /dev/null +++ b/backend/indicators/liquidity.py @@ -0,0 +1,46 @@ +def find_liquidity_levels(swings, tolerance=0.015, max_distance=100): + levels = [] + highs = [s for s in swings if s["type"] == "high"] + lows = [s for s in swings if s["type"] == "low"] + + used = set() + for i, h1 in enumerate(highs): + if i in used: + continue + cluster = [h1] + for j, h2 in enumerate(highs): + if j != i and j not in used: + if abs(h1["price"] - h2["price"]) <= tolerance and abs(h1["index"] - h2["index"]) <= max_distance: + cluster.append(h2) + used.add(j) + if len(cluster) >= 2: + avg_price = sum(s["price"] for s in cluster) / len(cluster) + levels.append({ + "price": avg_price, + "type": "equal_highs", + "count": len(cluster), + "indexes": [s["index"] for s in cluster] + }) + used.add(i) + + used = set() + for i, l1 in enumerate(lows): + if i in used: + continue + cluster = [l1] + for j, l2 in enumerate(lows): + if j != i and j not in used: + if abs(h1["price"] - h2["price"]) <= tolerance and abs(h1["index"] - h2["index"]) <= max_distance: + cluster.append(l2) + used.add(j) + if len(cluster) >= 2: + avg_price = sum(s["price"] for s in cluster) / len(cluster) + levels.append({ + "price": avg_price, + "type": "equal_lows", + "count": len(cluster), + "indexes": [s["index"] for s in cluster] + }) + used.add(i) + + return levels \ No newline at end of file diff --git a/backend/run.py b/backend/run.py index 967af13..bc4b9fb 100644 --- a/backend/run.py +++ b/backend/run.py @@ -1,12 +1,23 @@ from data.loader import load_candles from indicators.market_structure import find_swing_points, detect_structure +from indicators.liquidity import find_liquidity_levels candles = load_candles("data/data.csv") print(f"Loaded {len(candles)} candles") +from data.loader import load_candles, resample_candles -swings = find_swing_points(candles) -structure = detect_structure(swings) -print(f"Structure points: {len(structure)}") +candles_1m = load_candles("data/data.csv") +candles_3m = resample_candles(candles_1m, period=3) +candles_5m = resample_candles(candles_1m, period=5) + +print(f"1m: {len(candles_1m)} candles") +print(f"3m: {len(candles_3m)} candles") +print(f"5m: {len(candles_5m)} candles") + +swings = find_swing_points(candles_5m) +levels = find_liquidity_levels(swings) +print(f"Swing points: {len(swings)}") +print(f"Liquidity levels: {len(levels)}") +for l in levels[:5]: + print(l) -for s in structure[:10]: - print(s) \ No newline at end of file