added swing point detection & market structure labeling
This commit is contained in:
@@ -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
@@ -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)
|
||||
Reference in New Issue
Block a user