added swing point detection & market structure labeling

This commit is contained in:
moen0
2026-04-09 16:15:54 +02:00
parent 23568d4644
commit 245eca4155
2 changed files with 58 additions and 5 deletions
+52
View File
@@ -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
+6 -5
View File
@@ -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)
for s in structure[:10]:
print(s)