Repository navigation
Expand file tree
/
Copy pathOrion.cpp
More file actions
1706 lines (1557 loc) · 51.5 KB
/
Copy pathOrion.cpp
File metadata and controls
1706 lines (1557 loc) · 51.5 KB
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "sierrachart.h"
#include <cstdlib>
#include <cmath>
namespace orion {
constexpr int kMaxLevels = 1024;
struct VapLevel {
int price_ticks = 0;
double bid = 0;
double ask = 0;
double volume() const { return bid + ask; }
double delta() const { return ask - bid; }
};
struct GroupedLevel {
int bucket = 0;
double bid = 0;
double ask = 0;
double volume() const { return bid + ask; }
double delta() const { return ask - bid; }
};
struct StackResult {
int stacked = 0;
int scale = 1;
int poc_bucket = 0;
int poc_ticks = 0;
int zone_low_bucket = 0;
int zone_high_bucket = 0;
int zone_low_ticks = 0;
int zone_high_ticks = 0;
double extreme_volume = 0;
double extreme_delta = 0;
double zone_volume = 0;
bool anchored = false;
};
inline int floor_div(int a, int b) {
if (b < 1)
b = 1;
int q = a / b;
int r = a % b;
if (r != 0 && a < 0)
--q;
return q;
}
inline int bucket_of(int price_ticks, int ticks_per_level) {
return floor_div(price_ticks, ticks_per_level < 1 ? 1 : ticks_per_level);
}
inline bool ask_dominant(double bid, double ask, double ratio) {
if (ratio <= 0)
return true;
if (ask <= 0)
return false;
if (bid <= 0)
return ask > 0;
return ask >= bid * ratio;
}
inline bool bid_dominant(double bid, double ask, double ratio) {
if (ratio <= 0)
return true;
if (bid <= 0)
return false;
if (ask <= 0)
return bid > 0;
return bid >= ask * ratio;
}
// src must be sorted by price_ticks ascending. Merges into ticks-per-level buckets.
inline int group_vap(
const VapLevel* src,
int nsrc,
int ticks_per_level,
GroupedLevel* dst,
int maxdst,
bool* truncated = nullptr
) {
if (truncated != nullptr)
*truncated = false;
if (src == nullptr || dst == nullptr || nsrc <= 0 || maxdst <= 0)
return 0;
if (ticks_per_level < 1)
ticks_per_level = 1;
int ndst = 0;
GroupedLevel cur;
bool have = false;
for (int i = 0; i < nsrc; ++i) {
const int bucket = bucket_of(src[i].price_ticks, ticks_per_level);
if (!have) {
cur.bucket = bucket;
cur.bid = src[i].bid;
cur.ask = src[i].ask;
have = true;
continue;
}
if (bucket == cur.bucket) {
cur.bid += src[i].bid;
cur.ask += src[i].ask;
continue;
}
if (ndst >= maxdst) {
if (truncated != nullptr)
*truncated = true;
return ndst;
}
dst[ndst++] = cur;
cur.bucket = bucket;
cur.bid = src[i].bid;
cur.ask = src[i].ask;
}
if (have) {
if (ndst < maxdst) {
dst[ndst++] = cur;
} else if (truncated != nullptr) {
*truncated = true;
}
}
return ndst;
}
inline double bar_delta_from_vap(const VapLevel* src, int n) {
double delta = 0;
if (src == nullptr)
return 0;
for (int i = 0; i < n; ++i)
delta += src[i].delta();
return delta;
}
inline bool should_reset_persistents(bool full_recalc, int index, int update_start_index) {
return full_recalc && index == update_start_index;
}
inline bool trigger_bar_ok(int index, int armed_bar, int armed_dir) {
return armed_dir != 0 && armed_bar >= 0 && index > armed_bar;
}
inline bool arm_in_lifetime(int index, int armed_bar, int lifetime_bars) {
if (lifetime_bars < 1)
lifetime_bars = 1;
return index - armed_bar <= lifetime_bars;
}
inline bool can_trigger(int index, int armed_bar, int armed_dir, int lifetime_bars) {
return trigger_bar_ok(index, armed_bar, armed_dir)
&& arm_in_lifetime(index, armed_bar, lifetime_bars);
}
// Walk from the extreme. Long = lowest buckets, ask-dominant. Short = highest, bid-dominant.
// Consecutive buckets only; a gap ends the stack.
inline StackResult count_stacked(
const GroupedLevel* levels,
int n,
bool is_short,
double min_volume,
double imbalance_ratio,
int anchor_tolerance_buckets
) {
StackResult out;
if (levels == nullptr || n <= 0)
return out;
if (anchor_tolerance_buckets < 0)
anchor_tolerance_buckets = 0;
const int extreme_i = is_short ? (n - 1) : 0;
const int extreme_bucket = levels[extreme_i].bucket;
out.extreme_volume = levels[extreme_i].volume();
out.extreme_delta = levels[extreme_i].delta();
out.anchored = true;
int prev_bucket = extreme_bucket;
const int step = is_short ? -1 : 1;
for (int i = extreme_i; i >= 0 && i < n; i += step) {
const int dist = is_short
? (extreme_bucket - levels[i].bucket)
: (levels[i].bucket - extreme_bucket);
if (dist > anchor_tolerance_buckets)
break;
if (i != extreme_i && levels[i].bucket != prev_bucket + step)
break;
if (levels[i].volume() < min_volume)
break;
const bool dominant = is_short
? bid_dominant(levels[i].bid, levels[i].ask, imbalance_ratio)
: ask_dominant(levels[i].bid, levels[i].ask, imbalance_ratio);
if (!dominant)
break;
++out.stacked;
out.zone_volume += levels[i].volume();
if (out.stacked == 1) {
out.zone_low_bucket = levels[i].bucket;
out.zone_high_bucket = levels[i].bucket;
} else if (levels[i].bucket < out.zone_low_bucket) {
out.zone_low_bucket = levels[i].bucket;
} else if (levels[i].bucket > out.zone_high_bucket) {
out.zone_high_bucket = levels[i].bucket;
}
prev_bucket = levels[i].bucket;
}
return out;
}
inline int grouped_poc_bucket(const GroupedLevel* levels, int n, bool prefer_high) {
if (levels == nullptr || n <= 0)
return 0;
int best_bucket = levels[0].bucket;
double best_vol = levels[0].volume();
for (int i = 1; i < n; ++i) {
const double vol = levels[i].volume();
if (vol > best_vol + 1e-12) {
best_vol = vol;
best_bucket = levels[i].bucket;
continue;
}
if (std::fabs(vol - best_vol) <= 1e-12) {
if (prefer_high && levels[i].bucket > best_bucket)
best_bucket = levels[i].bucket;
if (!prefer_high && levels[i].bucket < best_bucket)
best_bucket = levels[i].bucket;
}
}
return best_bucket;
}
inline int grouped_poc_ticks(int bucket, int scale) {
if (scale < 1)
scale = 1;
return bucket * scale + scale / 2;
}
inline void fill_grouped_poc(StackResult* st, const GroupedLevel* levels, int n, bool is_short) {
if (st == nullptr)
return;
st->poc_bucket = grouped_poc_bucket(levels, n, is_short);
st->poc_ticks = grouped_poc_ticks(st->poc_bucket, st->scale);
if (st->stacked <= 0)
return;
const int scale = st->scale < 1 ? 1 : st->scale;
st->zone_low_ticks = st->zone_low_bucket * scale;
st->zone_high_ticks = (st->zone_high_bucket + 1) * scale - 1;
if (st->zone_high_ticks < st->zone_low_ticks)
st->zone_high_ticks = st->zone_low_ticks;
}
// Try grouping 1–4 plus the user scale when it is outside 1–4.
// Keep the largest stack; a tie uses the tighter scale. Anchor is in
// grouped-bucket units. POC is the max-volume grouped bucket at the
// winning scale, converted back to ticks at bucket center.
inline StackResult count_stacked_multiscale(
const VapLevel* src,
int nsrc,
int user_ticks,
bool is_short,
double min_volume,
double imbalance_ratio,
int anchor_buckets,
int min_stacked,
GroupedLevel* scratch,
int scratch_max
) {
(void)min_stacked;
StackResult best;
int scales[5];
int nscales = 0;
for (int s = 1; s <= 4; ++s)
scales[nscales++] = s;
if (user_ticks > 4) {
scales[nscales++] = user_ticks;
} else if (user_ticks < 1) {
scales[0] = 1;
}
for (int i = 0; i < nscales; ++i) {
const int ng = group_vap(src, nsrc, scales[i], scratch, scratch_max);
StackResult st = count_stacked(
scratch, ng, is_short, min_volume, imbalance_ratio, anchor_buckets);
st.scale = scales[i];
fill_grouped_poc(&st, scratch, ng, is_short);
if (st.stacked > best.stacked)
best = st;
else if (st.stacked == best.stacked && st.stacked > 0 && scales[i] < best.scale)
best = st;
}
return best;
}
inline bool lookback_extreme(
const double* series,
int index,
int lookback,
bool want_high
) {
if (series == nullptr || index < 0)
return false;
if (lookback <= 0)
return true;
if (index < lookback)
return false;
const double cur = series[index];
if (want_high) {
if (cur <= series[index - 1])
return false;
for (int i = 2; i <= lookback; ++i) {
if (cur < series[index - i])
return false;
}
return true;
}
if (cur >= series[index - 1])
return false;
for (int i = 2; i <= lookback; ++i) {
if (cur > series[index - i])
return false;
}
return true;
}
// Directional bar-delta filter. Threshold 0 requires a strict sign
// (long > 0, short < 0) so a flat bar cannot pass both sides.
inline bool bar_delta_supports(double ask_minus_bid, double threshold, bool is_short) {
if (is_short) {
if (ask_minus_bid >= 0)
return false;
return ask_minus_bid <= -threshold;
}
if (ask_minus_bid <= 0)
return false;
return ask_minus_bid >= threshold;
}
// mode 0 = directional (long +delta, short -delta). mode 1 = |delta| <= threshold.
inline bool bar_delta_filter(
double ask_minus_bid,
double threshold,
bool is_short,
int mode
) {
if (mode == 1)
return threshold <= 0 || std::fabs(ask_minus_bid) <= threshold;
return bar_delta_supports(ask_minus_bid, threshold, is_short);
}
inline int pick_setup_direction(
bool long_ok,
int long_stack,
bool short_ok,
int short_stack,
double bar_delta
) {
if (long_ok && short_ok) {
if (long_stack != short_stack)
return long_stack > short_stack ? 1 : -1;
return bar_delta < 0 ? -1 : 1;
}
if (short_ok)
return -1;
if (long_ok)
return 1;
return 0;
}
inline int trigger_offset_ticks(int arrow_offset) {
if (arrow_offset < 0)
arrow_offset = 0;
return arrow_offset + 3;
}
inline int poc_price_ticks(const VapLevel* src, int n, bool prefer_high) {
if (src == nullptr || n <= 0)
return 0;
int best = src[0].price_ticks;
double best_vol = src[0].volume();
for (int i = 1; i < n; ++i) {
const double vol = src[i].volume();
if (vol > best_vol + 1e-12) {
best_vol = vol;
best = src[i].price_ticks;
continue;
}
if (std::fabs(vol - best_vol) <= 1e-12) {
if (prefer_high && src[i].price_ticks > best)
best = src[i].price_ticks;
if (!prefer_high && src[i].price_ticks < best)
best = src[i].price_ticks;
}
}
return best;
}
inline bool poc_isolated_in_wick(
int poc_ticks,
double open,
double close,
double tick_size,
bool is_short
) {
if (tick_size <= 0)
return false;
const double poc = static_cast<double>(poc_ticks) * tick_size;
const double body_high = open > close ? open : close;
const double body_low = open < close ? open : close;
if (is_short)
return poc > body_high;
return poc < body_low;
}
// pct==0 means the filter is off (always passes).
inline bool poc_in_extreme_range_pct(
int poc_ticks,
double high,
double low,
double tick_size,
double pct,
bool is_short
) {
if (pct <= 0)
return true;
if (tick_size <= 0)
return false;
const double range = high - low;
if (range <= 0)
return false;
const double poc = static_cast<double>(poc_ticks) * tick_size;
const double from_extreme = is_short ? (high - poc) : (poc - low);
return from_extreme <= range * (pct / 100.0);
}
inline bool opposite_color_close(double open, double close, bool is_short) {
if (is_short)
return close < open;
return close > open;
}
inline bool volume_gate(double volume, double volume_ma, double min_pct, bool enabled) {
if (!enabled)
return true;
if (volume_ma <= 0)
return true;
return volume + 1e-9 >= volume_ma * (min_pct / 100.0);
}
inline double scale_threshold(
double absolute,
double volume_ma,
double baseline_volume,
bool enabled
) {
if (!enabled || baseline_volume <= 0)
return absolute;
double scale = volume_ma / baseline_volume;
if (scale < 0.15)
scale = 0.15;
if (scale > 3.0)
scale = 3.0;
return absolute * scale;
}
// mode 0 = absolute contracts, mode 1 = percent of |climax|.
// Short climax is a +max-delta spike; long climax is a -min-delta spike.
inline bool rebound_hit(
double climax_delta,
double current_delta,
bool is_short,
int rebound_mode,
double rebound_abs,
double rebound_pct
) {
double need = rebound_abs;
if (rebound_mode == 1) {
need = std::fabs(climax_delta) * (rebound_pct / 100.0);
}
if (need < 0)
need = 0;
if (is_short)
return current_delta <= climax_delta - need;
return current_delta >= climax_delta + need;
}
inline bool climax_reached(
double extreme_delta,
double climax_min,
bool is_short
) {
if (climax_min <= 0)
return true;
if (is_short)
return extreme_delta >= climax_min;
return extreme_delta <= -climax_min;
}
inline bool in_session(int time_seconds, int start_seconds, int end_seconds, bool enabled) {
if (!enabled)
return true;
if (start_seconds == end_seconds)
return true;
if (start_seconds < end_seconds)
return time_seconds >= start_seconds && time_seconds <= end_seconds;
return time_seconds >= start_seconds || time_seconds <= end_seconds;
}
inline bool extreme_broken(
double high,
double low,
double armed_extreme,
double tick_size,
int break_ticks,
bool is_short
) {
if (break_ticks <= 0)
return false;
const double pad = static_cast<double>(break_ticks) * tick_size;
if (is_short)
return high > armed_extreme + pad;
return low < armed_extreme - pad;
}
inline void consider_lowest_price(VapLevel* arr, int* n, int cap, const VapLevel& x) {
if (arr == nullptr || n == nullptr || cap <= 0)
return;
if (*n < cap) {
arr[(*n)++] = x;
return;
}
int worst = 0;
for (int i = 1; i < cap; ++i) {
if (arr[i].price_ticks > arr[worst].price_ticks)
worst = i;
}
if (x.price_ticks < arr[worst].price_ticks)
arr[worst] = x;
}
inline void consider_highest_price(VapLevel* arr, int* n, int cap, const VapLevel& x) {
if (arr == nullptr || n == nullptr || cap <= 0)
return;
if (*n < cap) {
arr[(*n)++] = x;
return;
}
int worst = 0;
for (int i = 1; i < cap; ++i) {
if (arr[i].price_ticks < arr[worst].price_ticks)
worst = i;
}
if (x.price_ticks > arr[worst].price_ticks)
arr[worst] = x;
}
inline int merge_extreme_vap(
const VapLevel* lows,
int nlow,
const VapLevel* highs,
int nhigh,
VapLevel* dst,
int maxdst
) {
if (dst == nullptr || maxdst <= 0)
return 0;
int written = 0;
auto push_unique = [&](const VapLevel& x) {
if (written >= maxdst)
return;
for (int i = 0; i < written; ++i) {
if (dst[i].price_ticks == x.price_ticks)
return;
}
dst[written++] = x;
};
if (lows != nullptr) {
for (int i = 0; i < nlow; ++i)
push_unique(lows[i]);
}
if (highs != nullptr) {
for (int i = 0; i < nhigh; ++i)
push_unique(highs[i]);
}
for (int i = 1; i < written; ++i) {
VapLevel cur = dst[i];
int j = i;
while (j > 0 && dst[j - 1].price_ticks > cur.price_ticks) {
dst[j] = dst[j - 1];
--j;
}
dst[j] = cur;
}
return written;
}
} // namespace orion
/*==========================================================================
ORION - Absorption Climax
Setup (arrow): stacked bid/ask absorption at a lookback swing.
Trigger (point): Numbers Bars max/min delta climax, then rebound on
the bar's ask-bid delta. Study ID 0 falls back to Ask-Bid, with
intra-bar max/min on the forming bar.
Alerts: setup short, setup long, and trigger, each with its own on/off
toggle and alert number (defaults 1, 2, 3). The setup master toggle
stays off by default; turn it on to arm the per-direction alerts.
INSTALL
-------
1. Copy Orion.cpp into ACS_Source (this file is self-contained):
C:\SierraChart\ACS_Source\Orion.cpp
2. Analysis >> Build Custom Studies DLL
File >> Select Files -> Orion.cpp
Build >> Remote Build
3. Analysis >> Studies >> Add Custom Study
expand Orion -> Orion - Absorption Climax
4. Point Max Delta / Min Delta at Numbers Bars Calculated Values
subgraphs for maximum and minimum ask-bid difference.
5. Recalculate. After input-index changes, remove the study and add it again.
==========================================================================*/
SCDLLName("Orion")
namespace {
constexpr int kStatusDrawing = 202609041;
constexpr int kDataDrawing1 = 202609042;
constexpr int kDataDrawing2 = 202609043;
int CmpVapPrice(const void* a, const void* b) {
const orion::VapLevel* lhs = static_cast<const orion::VapLevel*>(a);
const orion::VapLevel* rhs = static_cast<const orion::VapLevel*>(b);
if (lhs->price_ticks < rhs->price_ticks)
return -1;
if (lhs->price_ticks > rhs->price_ticks)
return 1;
return 0;
}
bool FillVapLevel(const s_VolumeAtPriceV2* vap, orion::VapLevel* out) {
if (vap == nullptr || out == nullptr)
return false;
out->price_ticks = vap->PriceInTicks;
out->bid = vap->GetBidVolume();
out->ask = vap->GetAskVolume();
return true;
}
int CollectVap(
SCStudyInterfaceRef sc,
int bar_index,
orion::VapLevel* out,
int max_out,
bool* truncated,
double* out_delta = nullptr,
double* out_volume = nullptr
) {
if (truncated != nullptr)
*truncated = false;
if (out_delta != nullptr)
*out_delta = 0;
if (out_volume != nullptr)
*out_volume = 0;
if (sc.VolumeAtPriceForBars == nullptr || out == nullptr || max_out <= 0)
return 0;
if (bar_index < 0)
return 0;
if (static_cast<int>(sc.VolumeAtPriceForBars->GetNumberOfBars()) <= bar_index)
return 0;
const int n = sc.VolumeAtPriceForBars->GetSizeAtBarIndex(bar_index);
double acc_delta = 0;
double acc_volume = 0;
auto account = [&](const orion::VapLevel& level) {
acc_delta += level.delta();
acc_volume += level.volume();
};
if (n <= max_out) {
int written = 0;
for (int i = 0; i < n; ++i) {
const s_VolumeAtPriceV2* vap = nullptr;
if (!sc.VolumeAtPriceForBars->GetVAPElementAtIndex(bar_index, i, &vap))
continue;
if (!FillVapLevel(vap, &out[written]))
continue;
account(out[written]);
++written;
}
if (out_delta != nullptr)
*out_delta = acc_delta;
if (out_volume != nullptr)
*out_volume = acc_volume;
if (written > 1)
qsort(out, static_cast<size_t>(written), sizeof(orion::VapLevel), CmpVapPrice);
return written;
}
if (truncated != nullptr)
*truncated = true;
int keep = max_out / 2;
if (keep < 1)
keep = 1;
orion::VapLevel lows[orion::kMaxLevels];
orion::VapLevel highs[orion::kMaxLevels];
int nlow = 0;
int nhigh = 0;
for (int i = 0; i < n; ++i) {
const s_VolumeAtPriceV2* vap = nullptr;
if (!sc.VolumeAtPriceForBars->GetVAPElementAtIndex(bar_index, i, &vap))
continue;
orion::VapLevel level;
if (!FillVapLevel(vap, &level))
continue;
account(level);
orion::consider_lowest_price(lows, &nlow, keep, level);
orion::consider_highest_price(highs, &nhigh, keep, level);
}
if (out_delta != nullptr)
*out_delta = acc_delta;
if (out_volume != nullptr)
*out_volume = acc_volume;
return orion::merge_extreme_vap(lows, nlow, highs, nhigh, out, max_out);
}
double CollectVapDelta(SCStudyInterfaceRef sc, int bar_index, bool* had_vap) {
if (had_vap != nullptr)
*had_vap = false;
if (sc.VolumeAtPriceForBars == nullptr || bar_index < 0)
return 0;
if (static_cast<int>(sc.VolumeAtPriceForBars->GetNumberOfBars()) <= bar_index)
return 0;
const int n = sc.VolumeAtPriceForBars->GetSizeAtBarIndex(bar_index);
if (n <= 0)
return 0;
if (had_vap != nullptr)
*had_vap = true;
double delta = 0;
for (int i = 0; i < n; ++i) {
const s_VolumeAtPriceV2* vap = nullptr;
if (!sc.VolumeAtPriceForBars->GetVAPElementAtIndex(bar_index, i, &vap) || vap == nullptr)
continue;
delta += vap->GetAskVolume() - vap->GetBidVolume();
}
return delta;
}
double VolumeMA(SCStudyInterfaceRef sc, int index, int length) {
if (length < 1)
length = 1;
double sum = 0;
int n = 0;
const int start = index - length + 1;
for (int i = (start < 0 ? 0 : start); i <= index; ++i) {
sum += sc.Volume[i];
++n;
}
if (n <= 0)
return 0;
return sum / static_cast<double>(n);
}
bool IsLookbackSwing(SCStudyInterfaceRef sc, int index, int lookback, bool is_high) {
if (index < 0)
return false;
if (lookback <= 0)
lookback = 1;
if (index < lookback)
return false;
if (is_high) {
if (sc.High[index] <= sc.High[index - 1])
return false;
for (int i = 2; i <= lookback; ++i) {
if (sc.High[index] < sc.High[index - i])
return false;
}
return true;
}
if (sc.Low[index] >= sc.Low[index - 1])
return false;
for (int i = 2; i <= lookback; ++i) {
if (sc.Low[index] > sc.Low[index - i])
return false;
}
return true;
}
bool UsableNumber(float value) {
if (value == 0)
return true;
if (value >= static_cast<float>(MAX_CHART_DATA_VALUE) * 0.5f)
return false;
if (value <= static_cast<float>(-MAX_CHART_DATA_VALUE) * 0.5f)
return false;
return true;
}
float DeltaAt(
SCStudyInterfaceRef sc,
const SCFloatArray& array,
int index,
bool have_array
) {
if (have_array) {
if (array.GetArraySize() > index) {
const float value = array[index];
if (UsableNumber(value))
return value;
}
return 0;
}
return static_cast<float>(sc.AskVolume[index] - sc.BidVolume[index]);
}
} // namespace
SCSFExport scsf_OrionAbsorptionClimax(SCStudyInterfaceRef sc) {
SCSubgraphRef SetupLong = sc.Subgraph[0];
SCSubgraphRef SetupShort = sc.Subgraph[1];
SCSubgraphRef TriggerLong = sc.Subgraph[2];
SCSubgraphRef TriggerShort = sc.Subgraph[3];
SCSubgraphRef ZoneHigh = sc.Subgraph[4];
SCSubgraphRef ZoneLow = sc.Subgraph[5];
SCSubgraphRef Status = sc.Subgraph[6];
SCInputRef InTicksPerLevel = sc.Input[0];
SCInputRef InMinVolPerLevel = sc.Input[1];
SCInputRef InImbalanceRatio = sc.Input[2];
SCInputRef InMinStacked = sc.Input[3];
SCInputRef InAnchorTicks = sc.Input[4];
SCInputRef InExtremeVolPct = sc.Input[5];
SCInputRef InDeltaThreshold = sc.Input[6];
SCInputRef InDeltaMode = sc.Input[7];
SCInputRef InRequirePocWick = sc.Input[8];
SCInputRef InPocRangePct = sc.Input[9];
SCInputRef InRequireOppClose = sc.Input[10];
SCInputRef InSwingBars = sc.Input[11];
SCInputRef InSessionFilter = sc.Input[12];
SCInputRef InSessionStart = sc.Input[13];
SCInputRef InSessionEnd = sc.Input[14];
SCInputRef InSetupOnClose = sc.Input[15];
SCInputRef InArrowOffset = sc.Input[16];
SCInputRef InArrowSize = sc.Input[17];
SCInputRef InSetupAlert = sc.Input[18];
SCInputRef InEnableTrigger = sc.Input[19];
SCInputRef InMaxDelta = sc.Input[20];
SCInputRef InMinDelta = sc.Input[21];
SCInputRef InLifetimeBars = sc.Input[22];
SCInputRef InInvalidateTicks = sc.Input[23];
SCInputRef InReboundMode = sc.Input[24];
SCInputRef InReboundAbs = sc.Input[25];
SCInputRef InReboundPct = sc.Input[26];
SCInputRef InClimaxMin = sc.Input[27];
SCInputRef InTriggerSize = sc.Input[28];
SCInputRef InTriggerAlert = sc.Input[29];
SCInputRef InRelVolumeGate = sc.Input[30];
SCInputRef InVolumeMALen = sc.Input[31];
SCInputRef InMinVolVsMAPct = sc.Input[32];
SCInputRef InScaleWithMA = sc.Input[33];
SCInputRef InBaselineVolume = sc.Input[34];
SCInputRef InLogSignals = sc.Input[35];
SCInputRef InVersion = sc.Input[36];
SCInputRef InShowStatus = sc.Input[37];
SCInputRef InShowZone = sc.Input[38];
SCInputRef InSetupLongAlert = sc.Input[39];
SCInputRef InSetupShortAlert = sc.Input[40];
SCInputRef InSetupLongAlertNum = sc.Input[41];
SCInputRef InSetupShortAlertNum = sc.Input[42];
SCInputRef InTriggerAlertNum = sc.Input[43];
SCInputRef InShowData = sc.Input[44];
if (sc.SetDefaults) {
sc.GraphName = "Orion - Absorption Climax";
sc.StudyDescription =
"Stacked absorption setup at a swing, then a max/min delta climax "
"rebound trigger. Relative volume gate and MA-scaled thresholds "
"are for thin sessions.";
sc.AutoLoop = 1;
sc.GraphRegion = 0;
sc.CalculationPrecedence = LOW_PREC_LEVEL;
sc.UpdateAlways = 1;
sc.DrawZeros = 0;
sc.FreeDLL = 0;
sc.MaintainVolumeAtPriceData = 1;
sc.ScaleRangeType = SCALE_SAMEASREGION;
SetupLong.Name = "Setup Long";
SetupLong.DrawStyle = DRAWSTYLE_ARROW_UP;
SetupLong.PrimaryColor = RGB(46, 230, 163);
SetupLong.LineWidth = 2;
SetupLong.DrawZeros = false;
SetupShort.Name = "Setup Short";
SetupShort.DrawStyle = DRAWSTYLE_ARROW_DOWN;
SetupShort.PrimaryColor = RGB(255, 92, 122);
SetupShort.LineWidth = 2;
SetupShort.DrawZeros = false;
TriggerLong.Name = "Trigger Long";
TriggerLong.DrawStyle = DRAWSTYLE_POINT;
TriggerLong.PrimaryColor = RGB(0, 255, 255);
TriggerLong.LineWidth = 5;
TriggerLong.DrawZeros = false;
TriggerShort.Name = "Trigger Short";
TriggerShort.DrawStyle = DRAWSTYLE_POINT;
TriggerShort.PrimaryColor = RGB(255, 165, 0);
TriggerShort.LineWidth = 5;
TriggerShort.DrawZeros = false;
ZoneHigh.Name = "Zone High";
ZoneHigh.DrawStyle = DRAWSTYLE_TRANSPARENT_FILL_RECTANGLE_TOP;
ZoneHigh.PrimaryColor = RGB(70, 110, 150);
ZoneHigh.DrawZeros = false;
ZoneLow.Name = "Zone Low";
ZoneLow.DrawStyle = DRAWSTYLE_TRANSPARENT_FILL_RECTANGLE_BOTTOM;
ZoneLow.PrimaryColor = RGB(70, 110, 150);
ZoneLow.DrawZeros = false;
Status.Name = "Status";
Status.DrawStyle = DRAWSTYLE_IGNORE;
Status.PrimaryColor = RGB(220, 220, 220);
Status.LineWidth = 12;
Status.DrawZeros = false;
unsigned short order = 1;
InTicksPerLevel.Name = "[Grouping] Ticks per level (also tries 1-4)";
InTicksPerLevel.SetInt(2);
InTicksPerLevel.SetIntLimits(1, 100);
InTicksPerLevel.DisplayOrder = order++;
InMinVolPerLevel.Name = "[Absorption] Min volume per level";
InMinVolPerLevel.SetInt(25);
InMinVolPerLevel.SetIntLimits(0, 1000000);
InMinVolPerLevel.DisplayOrder = order++;
InImbalanceRatio.Name = "[Absorption] Imbalance ratio % (300 = 3:1)";
InImbalanceRatio.SetInt(300);
InImbalanceRatio.SetIntLimits(100, 2000);
InImbalanceRatio.DisplayOrder = order++;
InMinStacked.Name = "[Absorption] Min stacked levels";
InMinStacked.SetInt(2);
InMinStacked.SetIntLimits(1, 20);
InMinStacked.DisplayOrder = order++;
InAnchorTicks.Name = "[Absorption] Anchor tolerance (grouped buckets)";
InAnchorTicks.SetInt(3);
InAnchorTicks.SetIntLimits(0, 20);
InAnchorTicks.DisplayOrder = order++;
InExtremeVolPct.Name = "[Absorption] Min % of bar volume at extreme (0=off)";
InExtremeVolPct.SetInt(0);
InExtremeVolPct.SetIntLimits(0, 100);
InExtremeVolPct.DisplayOrder = order++;
InDeltaThreshold.Name = "[Exhaustion] Bar delta threshold (0 = sign only)";
InDeltaThreshold.SetInt(0);
InDeltaThreshold.SetIntLimits(0, 100000);
InDeltaThreshold.DisplayOrder = order++;
InDeltaMode.Name = "[Exhaustion] Bar delta filter";
InDeltaMode.SetCustomInputStrings("Directional (sign/min);Magnitude cap |delta|<=");
InDeltaMode.SetCustomInputIndex(0);
InDeltaMode.DisplayOrder = order++;
InRequirePocWick.Name = "[Exhaustion] Require POC isolated in wick";
InRequirePocWick.SetYesNo(false);
InRequirePocWick.DisplayOrder = order++;
InPocRangePct.Name = "[Exhaustion] POC in extreme X% of range (0=off)";
InPocRangePct.SetInt(50);
InPocRangePct.SetIntLimits(0, 100);
InPocRangePct.DisplayOrder = order++;
InRequireOppClose.Name = "[Exhaustion] Require opposite-color close";
InRequireOppClose.SetYesNo(false);
InRequireOppClose.DisplayOrder = order++;
InSwingBars.Name = "[Context] Lookback bars to confirm high/low";
InSwingBars.SetInt(8);
InSwingBars.SetIntLimits(1, 200);
InSwingBars.DisplayOrder = order++;
InSessionFilter.Name = "[Context] Enable session filter";
InSessionFilter.SetYesNo(false);
InSessionFilter.DisplayOrder = order++;