1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
// Confirmed internal and external price structure on the selected settled chart
// timeframe. BOS and CHoCH describe close-confirmed price behavior; sweeps,
// equal levels, and fair value gaps describe candle geometry. This study does
// not observe institutions, orders, hidden liquidity, or trading intent.
requires settled_only
input sensitivity: integer = 5 min 2 max 20
input show_internal_structure: boolean = true
input show_equal_levels: boolean = true
input show_fair_value_gaps: boolean = false
// External structure is the primary decision layer and uses solid segments;
// internal structure uses dashed segments. Consumed external boundaries are
// neutral dashed, and pivot-to-confirmation lag is neutral dotted.
style external_up_segment for segment default positive line solid
style external_down_segment for segment default negative line solid
style internal_up_segment for segment default positive line dashed
style internal_down_segment for segment default negative line dashed
style structure_up_label for label default positive
style structure_down_label for label default negative
style sweep_label for label default warning
style external_high_level for level default positive line solid
style external_low_level for level default negative line solid
style broken_level for level default neutral line dashed
style confirmation_lag for segment default neutral line dotted
style equal_zone for box default neutral fill none line dotted
style equal_label for label default neutral
style fvg_box for box default neutral fill none line solid
style fvg_label for label default warning
// Closed domain vocabulary; labels and stable IDs convert explicitly to text.
type pivot_side = choice(high, low)
type structure_scale = choice(internal, external)
type move_direction = choice(up, down)
type break_kind = choice(bos, choch)
type pivot_seed {
key: bar_key
price: number
atr: number
side: pivot_side
scale: structure_scale
}
type structure_bar {
key: bar_key
open: number
high: number
low: number
close: number
atr: number
valid: boolean
prior_close: number
prior_valid: boolean
two_back_key: bar_key
two_back_high: number
two_back_low: number
two_back_valid: boolean
middle_open: number
middle_close: number
middle_atr: number
middle_valid: boolean
internal_high: option<pivot_seed>
internal_low: option<pivot_seed>
external_high: option<pivot_seed>
external_low: option<pivot_seed>
}
type pivot_state {
key: bar_key
confirmed_key: bar_key
price: number
atr: number
side: pivot_side
scale: structure_scale
swept: boolean
broken: boolean
}
type structure_event {
id: string
pivot_key: bar_key
pivot_confirmed_key: bar_key
event_key: bar_key
label_key: bar_key
price: number
atr: number
side: pivot_side
scale: structure_scale
direction: move_direction
kind: break_kind
caption: string
anchor: label_anchor
segment_style: visual_style<segment>
label_style: visual_style<label>
}
type sweep_event {
id: string
pivot_key: bar_key
pivot_confirmed_key: bar_key
event_key: bar_key
price: number
side: pivot_side
scale: structure_scale
caption: string
anchor: label_anchor
style: visual_style<label>
}
type equal_zone_item {
id: string
first_key: bar_key
second_key: bar_key
confirmed_key: bar_key
lower: number
upper: number
label_price: number
side: pivot_side
caption: string
anchor: label_anchor
style: visual_style<box>
label_style: visual_style<label>
}
type fvg_item {
id: string
origin_key: bar_key
formation_key: bar_key
through_key: bar_key
lower: number
upper: number
label_price: number
direction: move_direction
caption: string
anchor: label_anchor
style: visual_style<box>
label_style: visual_style<label>
}
type external_level_item {
id: string
pivot_key: bar_key
confirmed_key: bar_key
price: number
side: pivot_side
caption: string
anchor: label_anchor
style: visual_style<level>
lag_style: visual_style<segment>
}
type break_result {
pivot: option<pivot_state>
event: option<structure_event>
bias: integer
}
type structure_step {
high_pivot: option<pivot_state>
low_pivot: option<pivot_state>
events: list<structure_event, 12>
bias: integer
broke: boolean
}
type sweep_step {
pivot: option<pivot_state>
events: list<sweep_event, 4>
}
type equal_step {
unpaired: option<pivot_state>
zones: list<equal_zone_item, 8>
}
type fvg_step {
fvgs: list<fvg_item, 10>
}
function tracked(seed: pivot_seed, confirmed_key: bar_key): pivot_state =
pivot_state {
key = seed.key,
confirmed_key = confirmed_key,
price = seed.price,
atr = seed.atr,
side = seed.side,
scale = seed.scale,
swept = false,
broken = false
}
function structure_segment_style(scale: structure_scale, direction: move_direction): visual_style<segment> =
if scale == external then
if direction == up then style.external_up_segment
else style.external_down_segment
else if direction == up then style.internal_up_segment
else style.internal_down_segment
function structure_label_style(direction: move_direction): visual_style<label> =
if direction == up then style.structure_up_label
else style.structure_down_label
function make_structure_event(
pivot: pivot_state,
event_key: bar_key,
direction: move_direction,
kind: break_kind
): structure_event =
structure_event {
id = stable_key("smc.structure", str.choice(pivot.scale), str.choice(direction), str.choice(kind),
pivot.key, event_key),
pivot_key = pivot.key,
pivot_confirmed_key = pivot.confirmed_key,
event_key = event_key,
label_key = bar_midpoint(pivot.key, event_key),
price = pivot.price,
atr = pivot.atr,
side = pivot.side,
scale = pivot.scale,
direction = direction,
kind = kind,
caption = if kind == choch then "CHoCH" else "BOS",
anchor = if direction == up then above else below,
segment_style = structure_segment_style(pivot.scale, direction),
label_style = structure_label_style(direction)
}
function make_sweep_event(pivot: pivot_state, event_key: bar_key): sweep_event =
sweep_event {
id = stable_key("smc.sweep", str.choice(pivot.scale), str.choice(pivot.side),
pivot.key, event_key),
pivot_key = pivot.key,
pivot_confirmed_key = pivot.confirmed_key,
event_key = event_key,
price = pivot.price,
side = pivot.side,
scale = pivot.scale,
caption = if pivot.side == high then "High Sweep" else "Low Sweep",
anchor = if pivot.side == high then above else below,
style = style.sweep_label
}
function same_structure_geometry(minor: structure_event, major: structure_event): boolean =
minor.direction == major.direction
and bar_distance(minor.pivot_key, major.pivot_key) <= 1
and bar_distance(minor.event_key, major.event_key) <= 1
and abs(minor.price - major.price) <= 0.15 * max(minor.atr, major.atr)
function structure_label_conflicts(minor: structure_event, major: structure_event): boolean =
bar_distance(minor.label_key, major.label_key) <= 3
and abs(minor.price - major.price) <= 0.30 * max(minor.atr, major.atr)
function equal_survives(item: equal_zone_item, b: structure_bar): boolean =
not b.valid
or if item.side == high then b.high <= item.upper
else b.low >= item.lower
function fvg_survives(item: fvg_item, b: structure_bar): boolean =
not b.valid
or if item.direction == up then b.low > item.lower
else b.high < item.upper
function advance_fvg(item: fvg_item, b: structure_bar): fvg_item =
if not b.valid then item
else if item.direction == up then
item with {
through_key = b.key,
upper = min(item.upper, b.low),
label_price = (item.lower + min(item.upper, b.low)) / 2
}
else
item with {
through_key = b.key,
lower = max(item.lower, b.high),
label_price = (max(item.lower, b.high) + item.upper) / 2
}
// Helpers return immutable results; replay owns all state updates.
function check_break(current: option<pivot_state>, b: structure_bar, bias: integer): break_result =
if_some(current, pivot -> {
let rising = pivot.side == high
let crossed = if rising then b.close > pivot.price and b.prior_close <= pivot.price
else b.close < pivot.price and b.prior_close >= pivot.price
let direction: move_direction = if rising then up else down
let kind: break_kind =
if (rising and bias == -1) or (not rising and bias == 1) then choch else bos
if b.valid and b.prior_valid and not pivot.broken and crossed then
break_result {
pivot = some(pivot with { broken = true }),
event = some(make_structure_event(pivot, b.key, direction, kind)),
bias = if rising then 1 else -1
}
else break_result { pivot = current, event = none, bias = bias }
}, break_result { pivot = current, event = none, bias = bias })
function step_structure(
current_high: option<pivot_state>, current_low: option<pivot_state>,
b: structure_bar, bias: integer, events: list<structure_event, 12>
): structure_step = {
let { pivot: high_pivot, event: high_event, bias: high_bias } = check_break(current_high, b, bias)
let { pivot: low_pivot, event: low_event, bias: low_bias } = check_break(current_low, b, high_bias)
let after_high = if_some(high_event, event -> append_capped(events, event), events)
structure_step {
high_pivot = high_pivot,
low_pivot = low_pivot,
events = if_some(low_event, event -> append_capped(after_high, event), after_high),
bias = low_bias,
broke = is_some(high_event) or is_some(low_event)
}
}
function step_sweep(current: option<pivot_state>, b: structure_bar, events: list<sweep_event, 4>): sweep_step =
if_some(current, pivot -> {
let swept = if pivot.side == high then b.high > pivot.price and b.close <= pivot.price
else b.low < pivot.price and b.close >= pivot.price
if b.valid and not pivot.broken and not pivot.swept and swept then
sweep_step {
pivot = some(pivot with { swept = true }),
events = append_capped(events, make_sweep_event(pivot, b.key))
}
else sweep_step { pivot = current, events = events }
}, sweep_step { pivot = current, events = events })
function step_equal(
candidate: option<pivot_seed>, current: option<pivot_state>,
confirmed_key: bar_key, zones: list<equal_zone_item, 8>
): equal_step =
if_some(candidate, seed -> {
let pivot = tracked(seed, confirmed_key)
if_some(current, prior -> {
let tolerance = 0.20 * max(prior.atr, pivot.atr)
if prior.atr > 0 and pivot.atr > 0 and abs(pivot.price - prior.price) <= tolerance then
equal_step {
unpaired = none,
zones = append_capped(zones, equal_zone_item {
id = stable_key("smc.equal", str.choice(pivot.side), prior.key, pivot.key),
first_key = prior.key,
second_key = pivot.key,
confirmed_key = pivot.confirmed_key,
lower = min(prior.price, pivot.price) - 0.25 * tolerance,
upper = max(prior.price, pivot.price) + 0.25 * tolerance,
label_price = (prior.price + pivot.price) / 2,
side = pivot.side,
caption = if pivot.side == high then "EQH" else "EQL",
anchor = if pivot.side == high then above else below,
style = style.equal_zone,
label_style = style.equal_label
})
}
else equal_step { unpaired = some(pivot), zones = zones }
}, equal_step { unpaired = some(pivot), zones = zones })
}, equal_step { unpaired = current, zones = zones })
// Three-bar FVGs are geometry: candle colour is irrelevant. Both the middle
// body and completed gap must clear their fixed ATR floors.
function step_fvg(b: structure_bar, direction: move_direction, items: list<fvg_item, 10>): fvg_step = {
let lower = if direction == up then b.two_back_high else b.high
let upper = if direction == up then b.low else b.two_back_low
fvg_step {
fvgs = if b.valid and b.middle_valid and b.two_back_valid
and b.atr > 0 and b.middle_atr > 0
and abs(b.middle_close - b.middle_open) >= 0.50 * b.middle_atr
and upper > lower and upper - lower >= 0.10 * b.atr then
append_capped(items, fvg_item {
id = stable_key("smc.fvg", str.choice(direction), b.two_back_key, b.key),
origin_key = b.two_back_key,
formation_key = b.key,
through_key = b.key,
lower = lower,
upper = upper,
label_price = (lower + upper) / 2,
direction = direction,
caption = "FVG",
anchor = if direction == up then above else below,
style = style.fvg_box,
label_style = style.fvg_label
})
else items
}
}
let external_span = 3 * sensitivity
let volatility = atr(14)
let valid_price = ta.valid_ohlcv(o, h, l, c, v)
let internal_high_confirmed =
ta.pivot_high(h, sensitivity, sensitivity, latest)
let internal_low_confirmed =
ta.pivot_low(l, sensitivity, sensitivity, latest)
let external_high_confirmed =
ta.pivot_high(h, external_span, external_span, latest)
let external_low_confirmed =
ta.pivot_low(l, external_span, external_span, latest)
let enriched = history(structure_bar {
key = bar.key,
open = o,
high = h,
low = l,
close = c,
atr = volatility,
valid = valid_price,
prior_close = ref(c, 1),
prior_valid = ref(valid_price, 1),
two_back_key = ref(bar.key, 2),
two_back_high = ref(h, 2),
two_back_low = ref(l, 2),
two_back_valid = ref(valid_price, 2),
middle_open = ref(o, 1),
middle_close = ref(c, 1),
middle_atr = ref(volatility, 1),
middle_valid = ref(valid_price, 1),
internal_high = if_some(internal_high_confirmed, pivot ->
if valid_price and ref(valid_price, sensitivity) then some(pivot_seed {
key = pivot.key,
price = pivot.value,
atr = ref(volatility, sensitivity),
side = high,
scale = internal
}) else none, none),
internal_low = if_some(internal_low_confirmed, pivot ->
if valid_price and ref(valid_price, sensitivity) then some(pivot_seed {
key = pivot.key,
price = pivot.value,
atr = ref(volatility, sensitivity),
side = low,
scale = internal
}) else none, none),
external_high = if_some(external_high_confirmed, pivot ->
if valid_price and ref(valid_price, external_span) then some(pivot_seed {
key = pivot.key,
price = pivot.value,
atr = ref(volatility, external_span),
side = high,
scale = external
}) else none, none),
external_low = if_some(external_low_confirmed, pivot ->
if valid_price and ref(valid_price, external_span) then some(pivot_seed {
key = pivot.key,
price = pivot.value,
atr = ref(volatility, external_span),
side = low,
scale = external
}) else none, none)
}, max = 700, min = 3)
let structure = replay enriched {
state internal_high: option<pivot_state> = none
state internal_low: option<pivot_state> = none
state external_high: option<pivot_state> = none
state external_low: option<pivot_state> = none
state internal_bias: integer = 0
state external_bias: integer = 0
state unpaired_high: option<pivot_state> = none
state unpaired_low: option<pivot_state> = none
state external_structure_events: list<structure_event, 12> = []
state internal_structure_events: list<structure_event, 12> = []
state external_sweeps: list<sweep_event, 4> = []
state internal_sweeps: list<sweep_event, 4> = []
state equal_zones: list<equal_zone_item, 8> = []
state fvgs: list<fvg_item, 10> = []
on bar b {
// Existing geometry resolves before new facts form, so a zone cannot
// disappear on its own knowledge bar. Invalid rows retain prior state.
equal_zones = filter(equal_zones, item -> equal_survives(item, b))
fvgs = map(filter(fvgs, item -> fvg_survives(item, b)),
item -> advance_fvg(item, b))
// External breaks reset internal structure before internal classification.
// Each layer keeps the original high-then-low event and bias order.
let external_step = step_structure(external_high, external_low, b,
external_bias, external_structure_events)
{ high_pivot: external_high, low_pivot: external_low,
events: external_structure_events, bias: external_bias } = external_step
if external_step.broke {
internal_high = none
internal_low = none
internal_bias = external_bias
internal_structure_events = empty()
internal_sweeps = empty()
}
{ high_pivot: internal_high, low_pivot: internal_low,
events: internal_structure_events, bias: internal_bias } =
step_structure(internal_high, internal_low, b, internal_bias, internal_structure_events)
// Broken pivots are ineligible; a swept pivot may still break later.
{ pivot: external_high, events: external_sweeps } = step_sweep(external_high, b, external_sweeps)
{ pivot: external_low, events: external_sweeps } = step_sweep(external_low, b, external_sweeps)
{ pivot: internal_high, events: internal_sweeps } = step_sweep(internal_high, b, internal_sweeps)
{ pivot: internal_low, events: internal_sweeps } = step_sweep(internal_low, b, internal_sweeps)
// Install all pivots after event classification. Confirmation therefore
// cannot create a same-bar break or sweep.
if let seed = b.internal_high {
internal_high = some(tracked(seed, b.key))
}
if let seed = b.internal_low {
internal_low = some(tracked(seed, b.key))
}
if let seed = b.external_high {
external_high = some(tracked(seed, b.key))
}
if let seed = b.external_low {
external_low = some(tracked(seed, b.key))
}
// Consecutive internal pivots are single-use equal-level candidates.
{ unpaired: unpaired_high, zones: equal_zones } =
step_equal(b.internal_high, unpaired_high, b.key, equal_zones)
{ unpaired: unpaired_low, zones: equal_zones } =
step_equal(b.internal_low, unpaired_low, b.key, equal_zones)
{ fvgs } = step_fvg(b, up, fvgs)
{ fvgs } = step_fvg(b, down, fvgs)
}
}
let visible_internal_structure = collect<structure_event, 12> {
if show_internal_structure {
for item in structure.internal_structure_events {
if not any(structure.external_structure_events,
major -> same_structure_geometry(item, major)) {
emit item
}
}
}
}
let visible_structure = collect<structure_event, 24> {
for item in visible_internal_structure {
emit item
}
for item in structure.external_structure_events {
emit item
}
}
let visible_structure_labels = collect<structure_event, 24> {
for item in visible_internal_structure {
if not any(structure.external_structure_events,
major -> structure_label_conflicts(item, major)) {
emit item
}
}
for item in structure.external_structure_events {
emit item
}
}
let visible_sweeps = collect<sweep_event, 4> {
for item in structure.external_sweeps {
emit item
}
}
let external_levels = collect<external_level_item, 2> {
if let pivot = structure.external_high {
emit external_level_item {
id = stable_key("smc.level", str.choice(pivot.scale), str.choice(pivot.side), pivot.key),
pivot_key = pivot.key,
confirmed_key = pivot.confirmed_key,
price = pivot.price,
side = high,
caption = if pivot.broken then "Broken high" else "Swing high",
anchor = above,
style = if pivot.broken then style.broken_level
else style.external_high_level,
lag_style = style.confirmation_lag
}
}
if let pivot = structure.external_low {
emit external_level_item {
id = stable_key("smc.level", str.choice(pivot.scale), str.choice(pivot.side), pivot.key),
pivot_key = pivot.key,
confirmed_key = pivot.confirmed_key,
price = pivot.price,
side = low,
caption = if pivot.broken then "Broken low" else "Swing low",
anchor = below,
style = if pivot.broken then style.broken_level
else style.external_low_level,
lag_style = style.confirmation_lag
}
}
}
let visible_equal_zones = if show_equal_levels then structure.equal_zones
else empty()
let newest_equal_zone = collect<equal_zone_item, 1> {
if show_equal_levels {
if let item = structure.equal_zones[len(structure.equal_zones) - 1]? {
emit item
}
}
}
let visible_fvgs = collect<fvg_item, 3> {
if show_fair_value_gaps {
if let item = structure.fvgs[len(structure.fvgs) - 3]? {
emit item
}
if let item = structure.fvgs[len(structure.fvgs) - 2]? {
emit item
}
if let item = structure.fvgs[len(structure.fvgs) - 1]? {
emit item
}
}
}
let newest_fvg = collect<fvg_item, 1> {
if show_fair_value_gaps {
if let item = structure.fvgs[len(structure.fvgs) - 1]? {
emit item
}
}
}
output snapshot structure_events: list<structure_event, 24> = visible_structure
output snapshot structure_labels: list<structure_event, 24> = visible_structure_labels
output snapshot sweeps: list<sweep_event, 4> = visible_sweeps
output snapshot external_levels: list<external_level_item, 2> = external_levels
output snapshot equal_zones: list<equal_zone_item, 8> = visible_equal_zones
output snapshot newest_equal_zone: list<equal_zone_item, 1> = newest_equal_zone
output snapshot fair_value_gaps: list<fvg_item, 3> = visible_fvgs
output snapshot newest_fvg: list<fvg_item, 1> = newest_fvg
view {
pane price
segment structure_events {
id .id
from bar .pivot_key value .price
to bar .event_key value .price
style .segment_style
}
label structure_labels {
id .id
at bar .label_key value .price
text .caption
anchor .anchor
style .label_style
}
label sweeps {
id .id
at bar .event_key value .price
text .caption
anchor .anchor
style .style
}
level external_levels {
id .id
from bar .confirmed_key
to right_edge
at value .price
text .caption
anchor .anchor
style .style
}
segment external_levels as external_confirmation_lag {
id .id
from bar .pivot_key value .price
to bar .confirmed_key value .price
style .lag_style
}
box equal_zones {
id .id
from bar .confirmed_key
to right_edge
low .lower
high .upper
style .style
}
label newest_equal_zone {
id .id
at bar .confirmed_key value .label_price
text .caption
anchor .anchor
style .label_style
}
box fair_value_gaps {
id .id
from bar .origin_key
to bar .through_key
low .lower
high .upper
style .style
}
label newest_fvg {
id .id
at bar .formation_key value .label_price
text .caption
anchor .anchor
style .label_style
}
}