diff --git a/backend/indicators/market_structure.py b/backend/indicators/market_structure.py new file mode 100644 index 0000000..93df601 --- /dev/null +++ b/backend/indicators/market_structure.py @@ -0,0 +1,52 @@ +def find_swing_points(candles, lookback=5): + swings = [] + + for i in range(lookback, len(candles) - lookback): + high = candles[i].high + low = candles[i].low + #check if higher + is_swing_high = all( + high > candles[i + j].high + for j in range(-lookback, lookback + 1) + if j != 0 + ) + + # check if lower + is_swing_low = all( + low < candles[i + j].low + for j in range(-lookback, lookback + 1) + if j != 0 + ) + + if is_swing_high: + swings.append({"index": i, "price": high, "type": "high"}) + if is_swing_low: + swings.append({"index": i, "price": low, "type": "low"}) + + return swings + +def detect_structure(swings): + last_high = None + last_low = None + structure = [] + + for swing in swings: + if swing["type"] == "high": + if last_high is not None: + if swing["price"] > last_high["price"]: + label = "HH" + else: + label = "LH" + structure.append({**swing, "label": label}) + last_high = swing + + elif swing["type"] == "low": + if last_low is not None: + if swing["price"] > last_low["price"]: + label = "HL" + else: + label = "LL" + structure.append({**swing, "label": label}) + last_low = swing + + return structure \ No newline at end of file diff --git a/backend/run.py b/backend/run.py index 5ae78c4..967af13 100644 --- a/backend/run.py +++ b/backend/run.py @@ -1,11 +1,12 @@ from data.loader import load_candles -from engine.backtester import run_backtest +from indicators.market_structure import find_swing_points, detect_structure candles = load_candles("data/data.csv") print(f"Loaded {len(candles)} candles") -trades = run_backtest(candles, starting_balance=10000.0) -print(f"Completed {len(trades)} trades") +swings = find_swing_points(candles) +structure = detect_structure(swings) +print(f"Structure points: {len(structure)}") -for trade in trades[:5]: - print(trade) \ No newline at end of file +for s in structure[:10]: + print(s) \ No newline at end of file