Repository navigation
Expand file tree
/
Copy pathChampionOrderflowStrat.cpp
More file actions
1675 lines (1444 loc) · 69.1 KB
/
Copy pathChampionOrderflowStrat.cpp
File metadata and controls
1675 lines (1444 loc) · 69.1 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
/*=============================================================================
RLC Framework - Regime / Location / Confirmation
---------------------------------------------------------------------------
A Sierra Chart ACSIL study that encodes a four-stage discretionary
order-flow process into a single indicator:
1. ENVIRONMENT Market structure (HH/HL vs LH/LL) + gamma (GEX) regime
2. LOCATION Session volume profile (POC / VAH / VAL) + deep
Fibonacci retracement "golden pocket" (.705/.786/.886)
that must sit OUTSIDE the value area
3. CONFIRMATION Bid/ask footprint order flow: absorption (aggressive
participation that fails) then a dominance shift
(delta flip + stacked 400% diagonal imbalances)
4. MANAGEMENT Entry marker, invalidation stop, 1.5R / 2R targets,
POC / swing reference, and a trailing stop
This study DOES NOT place orders. It marks, measures and alerts.
Requirements:
- Intraday chart with Volume at Price (bid/ask) data
- Data feed that supplies bid/ask volume (most futures feeds do)
Build: Analysis >> Build Custom Studies DLL (or Remote Build)
---------------------------------------------------------------------------
If your Sierra Chart build does not have
AddAndManageSingleTextDrawingForStudy(), set RLC_ENABLE_DASHBOARD to 0.
Everything else in the study is unaffected; the same information is always
available in the Values Window.
=============================================================================*/
#include "sierrachart.h"
#include <vector>
#include <map>
#include <algorithm>
SCDLLName("Champion_Orderflow_Strat")
#define RLC_ENABLE_DASHBOARD 1
/*============================================================================*/
/* Types and helpers */
/*============================================================================*/
namespace RLC
{
const int STAGE_IDLE = 0; // nothing to do
const int STAGE_ARMED = 1; // environment + location valid, price in zone
const int STAGE_ABSORB = 2; // failed aggressive push recorded
const int STAGE_TRADE = 3; // triggered, managing
const int STRUCT_DOWN = -1;
const int STRUCT_FLAT = 0;
const int STRUCT_UP = 1;
const int GAMMA_NEG = -1;
const int GAMMA_UNK = 0;
const int GAMMA_POS = 1;
// one price row of a footprint bar
struct s_Lvl
{
int Tick;
double Bid;
double Ask;
};
static bool LvlLess(const s_Lvl& A, const s_Lvl& B) { return A.Tick < B.Tick; }
// order flow summary of a single bar
struct s_OF
{
double Delta;
double Volume;
int BuyImb;
int SellImb;
int StackBuy;
int StackSell;
void Clear()
{
Delta = 0.0; Volume = 0.0;
BuyImb = 0; SellImb = 0; StackBuy = 0; StackSell = 0;
}
};
// everything the study has to remember between calls
struct s_State
{
// ---- session volume profile (closed bars only) -------------------
std::map<int, double> SessionVol; // price in ticks -> volume
int TradingDayDate;
int LastProfileBar;
float POC, VAH, VAL;
int ProfileValid;
float PriorPOC, PriorVAH, PriorVAL;
int PriorValid;
// ---- evaluation bookkeeping --------------------------------------
int LastEvalBar;
// ---- environment (cached, computed on each closed bar) -----------
int Structure;
int Gamma;
float LastSwingHigh;
float LastSwingLow;
// ---- active retracement legs / zones ------------------------------
int LongZoneValid;
float LongLegLow, LongLegHigh;
float LongFib1, LongFib2, LongFib3; // .705 / .786 / .886 prices
float LongZoneTop, LongZoneBot;
int ShortZoneValid;
float ShortLegLow, ShortLegHigh;
float ShortFib1, ShortFib2, ShortFib3;
float ShortZoneTop, ShortZoneBot;
// ---- setup state machines -----------------------------------------
int LongStage;
int LongAbsorbBar;
float LongFailLow;
float LongAbsorbHigh;
int ShortStage;
int ShortAbsorbBar;
float ShortFailHigh;
float ShortAbsorbLow;
// ---- active (displayed) trade --------------------------------------
int TradeDir; // +1 long, -1 short, 0 flat
int TradeEntryBar;
float Entry, Stop, InitialStop, Tgt1, Tgt2, Trail;
int TrailActive;
int Tgt1Hit;
float BestPrice;
int SetupsThisSession;
// scratch buffer for footprint rows (per study instance, not static)
std::vector<s_Lvl> Scratch;
void ResetTrade()
{
TradeDir = 0; TradeEntryBar = -1;
Entry = Stop = InitialStop = Tgt1 = Tgt2 = Trail = 0.0f;
TrailActive = 0; Tgt1Hit = 0; BestPrice = 0.0f;
}
void ResetSetups()
{
LongStage = STAGE_IDLE; LongAbsorbBar = -1;
LongFailLow = 0.0f; LongAbsorbHigh = 0.0f;
ShortStage = STAGE_IDLE; ShortAbsorbBar = -1;
ShortFailHigh = 0.0f; ShortAbsorbLow = 0.0f;
}
void Reset()
{
SessionVol.clear();
TradingDayDate = -1;
LastProfileBar = -1;
POC = VAH = VAL = 0.0f; ProfileValid = 0;
PriorPOC = PriorVAH = PriorVAL = 0.0f; PriorValid = 0;
LastEvalBar = -1;
Structure = STRUCT_FLAT;
Gamma = GAMMA_UNK;
LastSwingHigh = LastSwingLow = 0.0f;
LongZoneValid = 0; ShortZoneValid = 0;
LongLegLow = LongLegHigh = 0.0f;
LongFib1 = LongFib2 = LongFib3 = 0.0f;
LongZoneTop = LongZoneBot = 0.0f;
ShortLegLow = ShortLegHigh = 0.0f;
ShortFib1 = ShortFib2 = ShortFib3 = 0.0f;
ShortZoneTop = ShortZoneBot = 0.0f;
ResetSetups();
ResetTrade();
SetupsThisSession = 0;
Scratch.clear();
}
s_State() { Reset(); }
};
/*---------------------------------------------------------------------------*/
/* Footprint order flow for one bar */
/*---------------------------------------------------------------------------*/
static void ComputeOrderFlow(SCStudyInterfaceRef sc,
int BarIndex,
double ImbalanceRatio,
double MinImbVolume,
s_OF& OF,
std::vector<s_Lvl>& Scratch)
{
OF.Clear();
if (sc.VolumeAtPriceForBars == NULL)
return;
if (BarIndex < 0 || BarIndex >= sc.ArraySize)
return;
const int NumLevels = sc.VolumeAtPriceForBars->GetSizeAtBarIndex(BarIndex);
if (NumLevels <= 0)
return;
Scratch.clear();
Scratch.reserve(NumLevels);
for (int e = 0; e < NumLevels; ++e)
{
const s_VolumeAtPriceV2* p_VAP = NULL;
if (sc.VolumeAtPriceForBars->GetVAPElementAtIndex(BarIndex, e, &p_VAP) == 0)
continue;
if (p_VAP == NULL)
continue;
s_Lvl L;
L.Tick = p_VAP->PriceInTicks;
L.Bid = (double)p_VAP->BidVolume; // traded at the bid = selling
L.Ask = (double)p_VAP->AskVolume; // traded at the ask = buying
Scratch.push_back(L);
OF.Delta += L.Ask - L.Bid;
OF.Volume += (double)p_VAP->Volume;
}
if (Scratch.empty())
return;
std::sort(Scratch.begin(), Scratch.end(), LvlLess);
// Diagonal imbalance, the standard footprint definition:
// BUY imbalance at price P : Ask(P) vs Bid(P - 1 tick)
// SELL imbalance at price P : Bid(P) vs Ask(P + 1 tick)
int RunBuy = 0, RunSell = 0;
const int N = (int)Scratch.size();
for (int i = 0; i < N; ++i)
{
const double Ask = Scratch[i].Ask;
const double Bid = Scratch[i].Bid;
double BidBelow = 0.0;
if (i > 0 && Scratch[i - 1].Tick == Scratch[i].Tick - 1)
BidBelow = Scratch[i - 1].Bid;
double AskAbove = 0.0;
if (i + 1 < N && Scratch[i + 1].Tick == Scratch[i].Tick + 1)
AskAbove = Scratch[i + 1].Ask;
const bool BuyImb =
(Ask >= MinImbVolume) &&
(BidBelow <= 0.0 ? true : (Ask >= ImbalanceRatio * BidBelow));
const bool SellImb =
(Bid >= MinImbVolume) &&
(AskAbove <= 0.0 ? true : (Bid >= ImbalanceRatio * AskAbove));
if (BuyImb)
{
OF.BuyImb++;
RunBuy++;
if (RunBuy > OF.StackBuy) OF.StackBuy = RunBuy;
}
else
RunBuy = 0;
if (SellImb)
{
OF.SellImb++;
RunSell++;
if (RunSell > OF.StackSell) OF.StackSell = RunSell;
}
else
RunSell = 0;
}
}
/*---------------------------------------------------------------------------*/
/* Add one bar's traded volume into the session profile map */
/*---------------------------------------------------------------------------*/
static void AddBarToProfile(SCStudyInterfaceRef sc, int BarIndex,
std::map<int, double>& Profile)
{
if (sc.VolumeAtPriceForBars == NULL)
return;
const int NumLevels = sc.VolumeAtPriceForBars->GetSizeAtBarIndex(BarIndex);
for (int e = 0; e < NumLevels; ++e)
{
const s_VolumeAtPriceV2* p_VAP = NULL;
if (sc.VolumeAtPriceForBars->GetVAPElementAtIndex(BarIndex, e, &p_VAP) == 0)
continue;
if (p_VAP == NULL)
continue;
Profile[p_VAP->PriceInTicks] += (double)p_VAP->Volume;
}
}
/*---------------------------------------------------------------------------*/
/* POC + value area from a price->volume map */
/*---------------------------------------------------------------------------*/
static bool ComputeValueArea(const std::map<int, double>& Profile,
double ValueAreaPercent,
float TickSize,
float& OutPOC, float& OutVAH, float& OutVAL)
{
if (Profile.empty())
return false;
std::vector<int> Ticks;
std::vector<double> Vols;
Ticks.reserve(Profile.size());
Vols.reserve(Profile.size());
double Total = 0.0;
for (std::map<int, double>::const_iterator it = Profile.begin(); it != Profile.end(); ++it)
{
Ticks.push_back(it->first);
Vols.push_back(it->second);
Total += it->second;
}
if (Total <= 0.0)
return false;
const int N = (int)Ticks.size();
int PocIdx = 0;
double PocVol = Vols[0];
for (int i = 1; i < N; ++i)
{
if (Vols[i] > PocVol)
{
PocVol = Vols[i];
PocIdx = i;
}
}
const double Target = Total * (ValueAreaPercent / 100.0);
double Acc = Vols[PocIdx];
int Lo = PocIdx, Hi = PocIdx;
while (Acc < Target && (Lo > 0 || Hi < N - 1))
{
const double VolAbove = (Hi < N - 1) ? Vols[Hi + 1] : -1.0;
const double VolBelow = (Lo > 0) ? Vols[Lo - 1] : -1.0;
if (VolAbove < 0.0 && VolBelow < 0.0)
break;
if (VolAbove >= VolBelow)
{
Hi++;
Acc += Vols[Hi];
}
else
{
Lo--;
Acc += Vols[Lo];
}
}
OutPOC = (float)(Ticks[PocIdx] * TickSize);
OutVAL = (float)(Ticks[Lo] * TickSize);
OutVAH = (float)(Ticks[Hi] * TickSize);
return true;
}
/*---------------------------------------------------------------------------*/
/* Swing pivot search (used for structure and for the retracement leg) */
/*---------------------------------------------------------------------------*/
static bool IsPivotHigh(SCFloatArrayRef High, int K, int Strength, int MaxIndex)
{
if (K - Strength < 0 || K + Strength > MaxIndex)
return false;
const float V = High[K];
for (int j = 1; j <= Strength; ++j)
{
if (High[K - j] > V) return false; // strictly higher on the left invalidates
if (High[K + j] >= V) return false; // ties on the right invalidate
}
return true;
}
static bool IsPivotLow(SCFloatArrayRef Low, int K, int Strength, int MaxIndex)
{
if (K - Strength < 0 || K + Strength > MaxIndex)
return false;
const float V = Low[K];
for (int j = 1; j <= Strength; ++j)
{
if (Low[K - j] < V) return false;
if (Low[K + j] <= V) return false;
}
return true;
}
// Walks backwards from StartFrom looking for the most recent confirmed pivot.
static bool FindLastPivot(SCFloatArrayRef High, SCFloatArrayRef Low,
bool WantHigh,
int StartFrom, int MaxIndex,
int Strength, int MaxLookback,
int& OutIndex, float& OutPrice)
{
int Stop = StartFrom - MaxLookback;
if (Stop < Strength) Stop = Strength;
for (int k = StartFrom; k >= Stop; --k)
{
if (WantHigh)
{
if (IsPivotHigh(High, k, Strength, MaxIndex))
{
OutIndex = k; OutPrice = High[k];
return true;
}
}
else
{
if (IsPivotLow(Low, k, Strength, MaxIndex))
{
OutIndex = k; OutPrice = Low[k];
return true;
}
}
}
return false;
}
// Value-up / value-down / sideways from the last two swing highs and lows.
static int DetectStructure(SCFloatArrayRef High, SCFloatArrayRef Low,
int EndIndex, int Strength, int MaxLookback,
float& OutSwingHigh, float& OutSwingLow)
{
OutSwingHigh = 0.0f;
OutSwingLow = 0.0f;
float PH[2] = { 0.0f, 0.0f };
float PL[2] = { 0.0f, 0.0f };
int FoundH = 0, FoundL = 0;
int Start = EndIndex - Strength;
int Stop = Start - MaxLookback;
if (Stop < Strength) Stop = Strength;
for (int k = Start; k >= Stop && (FoundH < 2 || FoundL < 2); --k)
{
if (FoundH < 2 && IsPivotHigh(High, k, Strength, EndIndex))
PH[FoundH++] = High[k];
if (FoundL < 2 && IsPivotLow(Low, k, Strength, EndIndex))
PL[FoundL++] = Low[k];
}
if (FoundH > 0) OutSwingHigh = PH[0];
if (FoundL > 0) OutSwingLow = PL[0];
if (FoundH < 2 || FoundL < 2)
return STRUCT_FLAT;
if (PH[0] > PH[1] && PL[0] > PL[1]) return STRUCT_UP; // HH + HL
if (PH[0] < PH[1] && PL[0] < PL[1]) return STRUCT_DOWN; // LH + LL
return STRUCT_FLAT;
}
static const char* StructureText(int S)
{
if (S == STRUCT_UP) return "VALUE-UP";
if (S == STRUCT_DOWN) return "VALUE-DOWN";
return "SIDEWAYS";
}
static const char* GammaText(int G)
{
if (G == GAMMA_POS) return "POSITIVE (dampening)";
if (G == GAMMA_NEG) return "NEGATIVE (amplifying)";
return "UNKNOWN";
}
static const char* StageText(int S)
{
if (S == STAGE_ARMED) return "ARMED (in location)";
if (S == STAGE_ABSORB) return "ABSORPTION SEEN";
if (S == STAGE_TRADE) return "IN TRADE";
return "IDLE";
}
} // namespace RLC
/*=============================================================================*/
/* The study */
/*=============================================================================*/
SCSFExport scsf_RLCFramework(SCStudyInterfaceRef sc)
{
/*------------------------------------------------------------------ plots */
SCSubgraphRef Sub_VAH = sc.Subgraph[0];
SCSubgraphRef Sub_VAL = sc.Subgraph[1];
SCSubgraphRef Sub_POC = sc.Subgraph[2];
SCSubgraphRef Sub_PriorVAH = sc.Subgraph[3];
SCSubgraphRef Sub_PriorVAL = sc.Subgraph[4];
SCSubgraphRef Sub_PriorPOC = sc.Subgraph[5];
SCSubgraphRef Sub_Fib705 = sc.Subgraph[6];
SCSubgraphRef Sub_Fib786 = sc.Subgraph[7];
SCSubgraphRef Sub_Fib886 = sc.Subgraph[8];
SCSubgraphRef Sub_Absorption = sc.Subgraph[9];
SCSubgraphRef Sub_LongSig = sc.Subgraph[10];
SCSubgraphRef Sub_ShortSig = sc.Subgraph[11];
SCSubgraphRef Sub_EntryLine = sc.Subgraph[12];
SCSubgraphRef Sub_StopLine = sc.Subgraph[13];
SCSubgraphRef Sub_Target1 = sc.Subgraph[14];
SCSubgraphRef Sub_Target2 = sc.Subgraph[15];
SCSubgraphRef Sub_TrailLine = sc.Subgraph[16];
SCSubgraphRef Sub_Delta = sc.Subgraph[17];
SCSubgraphRef Sub_BuyImb = sc.Subgraph[18];
SCSubgraphRef Sub_SellImb = sc.Subgraph[19];
SCSubgraphRef Sub_StackBuy = sc.Subgraph[20];
SCSubgraphRef Sub_StackSell = sc.Subgraph[21];
SCSubgraphRef Sub_Structure = sc.Subgraph[22];
SCSubgraphRef Sub_GammaState = sc.Subgraph[23];
SCSubgraphRef Sub_StageOut = sc.Subgraph[24];
SCSubgraphRef Sub_Dashboard = sc.Subgraph[25];
/*----------------------------------------------------------------- inputs */
SCInputRef In_EnableLong = sc.Input[0];
SCInputRef In_EnableShort = sc.Input[1];
SCInputRef In_StructChart = sc.Input[2];
SCInputRef In_StructStrength = sc.Input[3];
SCInputRef In_StructLookback = sc.Input[4];
SCInputRef In_StructFilter = sc.Input[5];
SCInputRef In_GammaMode = sc.Input[6];
SCInputRef In_GammaFlip = sc.Input[7];
SCInputRef In_GammaRequired = sc.Input[8];
SCInputRef In_VAPercent = sc.Input[9];
SCInputRef In_LocSource = sc.Input[10];
SCInputRef In_ZoneOutsideVA = sc.Input[11];
SCInputRef In_VATolTicks = sc.Input[12];
SCInputRef In_LegStrength = sc.Input[13];
SCInputRef In_Fib1 = sc.Input[14];
SCInputRef In_Fib2 = sc.Input[15];
SCInputRef In_Fib3 = sc.Input[16];
SCInputRef In_MinLegTicks = sc.Input[17];
SCInputRef In_LegLookback = sc.Input[18];
SCInputRef In_ImbRatioPct = sc.Input[19];
SCInputRef In_ImbMinVol = sc.Input[20];
SCInputRef In_MinStacked = sc.Input[21];
SCInputRef In_AbsMinDelta = sc.Input[22];
SCInputRef In_AbsAutoMult = sc.Input[23];
SCInputRef In_AbsClosePos = sc.Input[24];
SCInputRef In_AbsNeedImb = sc.Input[25];
SCInputRef In_TrigMaxBars = sc.Input[26];
SCInputRef In_TrigNeedBreak = sc.Input[27];
SCInputRef In_StopBuffer = sc.Input[28];
SCInputRef In_R1 = sc.Input[29];
SCInputRef In_R2 = sc.Input[30];
SCInputRef In_TrailActivateR = sc.Input[31];
SCInputRef In_TrailLookback = sc.Input[32];
SCInputRef In_TrailBuffer = sc.Input[33];
SCInputRef In_UseSession = sc.Input[34];
SCInputRef In_SessionStart = sc.Input[35];
SCInputRef In_SessionEnd = sc.Input[36];
SCInputRef In_MaxSetups = sc.Input[37];
SCInputRef In_Alerts = sc.Input[38];
SCInputRef In_DrawZone = sc.Input[39];
SCInputRef In_ShowPrior = sc.Input[40];
SCInputRef In_ShowDash = sc.Input[41];
SCInputRef In_DashX = sc.Input[42];
SCInputRef In_DashY = sc.Input[43];
/*======================================================================*/
/* Defaults */
/*======================================================================*/
if (sc.SetDefaults)
{
sc.GraphName = "Champion OrderFlow Strat (Regime / Location / Confirmation)";
sc.StudyDescription = "Environment, Location, Confirmation and trade management "
"framework using session volume profile, deep Fibonacci "
"retracement and bid/ask footprint order flow.";
sc.GraphRegion = 0;
sc.AutoLoop = 1;
sc.MaintainVolumeAtPriceData = 1;
sc.ValueFormat = VALUEFORMAT_INHERITED;
sc.DrawZeros = 0;
sc.AlertOnlyOncePerBar = 1;
// ---- profile lines
Sub_VAH.Name = "Value Area High";
Sub_VAH.DrawStyle = DRAWSTYLE_DASH;
Sub_VAH.PrimaryColor = RGB(120, 170, 255);
Sub_VAH.LineWidth = 1;
Sub_VAH.DrawZeros = 0;
Sub_VAL.Name = "Value Area Low";
Sub_VAL.DrawStyle = DRAWSTYLE_DASH;
Sub_VAL.PrimaryColor = RGB(120, 170, 255);
Sub_VAL.LineWidth = 1;
Sub_VAL.DrawZeros = 0;
Sub_POC.Name = "Point of Control";
Sub_POC.DrawStyle = DRAWSTYLE_LINE;
Sub_POC.PrimaryColor = RGB(255, 200, 0);
Sub_POC.LineWidth = 2;
Sub_POC.DrawZeros = 0;
Sub_PriorVAH.Name = "Prior Session VAH";
Sub_PriorVAH.DrawStyle = DRAWSTYLE_DASH;
Sub_PriorVAH.PrimaryColor = RGB(90, 110, 150);
Sub_PriorVAH.LineWidth = 1;
Sub_PriorVAH.DrawZeros = 0;
Sub_PriorVAL.Name = "Prior Session VAL";
Sub_PriorVAL.DrawStyle = DRAWSTYLE_DASH;
Sub_PriorVAL.PrimaryColor = RGB(90, 110, 150);
Sub_PriorVAL.LineWidth = 1;
Sub_PriorVAL.DrawZeros = 0;
Sub_PriorPOC.Name = "Prior Session POC";
Sub_PriorPOC.DrawStyle = DRAWSTYLE_DASH;
Sub_PriorPOC.PrimaryColor = RGB(160, 130, 40);
Sub_PriorPOC.LineWidth = 1;
Sub_PriorPOC.DrawZeros = 0;
// ---- fib zone
Sub_Fib705.Name = "Fib 0.705 (zone edge)";
Sub_Fib705.DrawStyle = DRAWSTYLE_LINE;
Sub_Fib705.PrimaryColor = RGB(0, 200, 120);
Sub_Fib705.LineWidth = 1;
Sub_Fib705.DrawZeros = 0;
Sub_Fib786.Name = "Fib 0.786";
Sub_Fib786.DrawStyle = DRAWSTYLE_LINE;
Sub_Fib786.PrimaryColor = RGB(0, 160, 200);
Sub_Fib786.LineWidth = 1;
Sub_Fib786.DrawZeros = 0;
Sub_Fib886.Name = "Fib 0.886 (zone edge)";
Sub_Fib886.DrawStyle = DRAWSTYLE_LINE;
Sub_Fib886.PrimaryColor = RGB(0, 200, 120);
Sub_Fib886.LineWidth = 1;
Sub_Fib886.DrawZeros = 0;
// ---- markers
Sub_Absorption.Name = "Absorption Bar";
Sub_Absorption.DrawStyle = DRAWSTYLE_POINT;
Sub_Absorption.PrimaryColor = RGB(255, 140, 0);
Sub_Absorption.LineWidth = 5;
Sub_Absorption.DrawZeros = 0;
Sub_LongSig.Name = "Long Trigger";
Sub_LongSig.DrawStyle = DRAWSTYLE_ARROW_UP;
Sub_LongSig.PrimaryColor = RGB(0, 255, 90);
Sub_LongSig.LineWidth = 3;
Sub_LongSig.DrawZeros = 0;
Sub_ShortSig.Name = "Short Trigger";
Sub_ShortSig.DrawStyle = DRAWSTYLE_ARROW_DOWN;
Sub_ShortSig.PrimaryColor = RGB(255, 60, 60);
Sub_ShortSig.LineWidth = 3;
Sub_ShortSig.DrawZeros = 0;
// ---- trade management lines
Sub_EntryLine.Name = "Entry";
Sub_EntryLine.DrawStyle = DRAWSTYLE_LINE;
Sub_EntryLine.PrimaryColor = RGB(255, 255, 255);
Sub_EntryLine.LineWidth = 1;
Sub_EntryLine.DrawZeros = 0;
Sub_StopLine.Name = "Stop (invalidation)";
Sub_StopLine.DrawStyle = DRAWSTYLE_LINE;
Sub_StopLine.PrimaryColor = RGB(255, 60, 60);
Sub_StopLine.LineWidth = 2;
Sub_StopLine.DrawZeros = 0;
Sub_Target1.Name = "Target 1 (R multiple)";
Sub_Target1.DrawStyle = DRAWSTYLE_LINE;
Sub_Target1.PrimaryColor = RGB(0, 210, 255);
Sub_Target1.LineWidth = 1;
Sub_Target1.DrawZeros = 0;
Sub_Target2.Name = "Target 2 (R multiple)";
Sub_Target2.DrawStyle = DRAWSTYLE_LINE;
Sub_Target2.PrimaryColor = RGB(0, 255, 200);
Sub_Target2.LineWidth = 1;
Sub_Target2.DrawZeros = 0;
Sub_TrailLine.Name = "Trailing Stop";
Sub_TrailLine.DrawStyle = DRAWSTYLE_DASH;
Sub_TrailLine.PrimaryColor = RGB(255, 170, 60);
Sub_TrailLine.LineWidth = 2;
Sub_TrailLine.DrawZeros = 0;
// ---- data only (Values Window)
Sub_Delta.Name = "Bar Delta"; Sub_Delta.DrawStyle = DRAWSTYLE_IGNORE;
Sub_BuyImb.Name = "Buy Imbalances"; Sub_BuyImb.DrawStyle = DRAWSTYLE_IGNORE;
Sub_SellImb.Name = "Sell Imbalances"; Sub_SellImb.DrawStyle = DRAWSTYLE_IGNORE;
Sub_StackBuy.Name = "Stacked Buy Imb"; Sub_StackBuy.DrawStyle = DRAWSTYLE_IGNORE;
Sub_StackSell.Name = "Stacked Sell Imb"; Sub_StackSell.DrawStyle = DRAWSTYLE_IGNORE;
Sub_Structure.Name = "Structure (1/0/-1)"; Sub_Structure.DrawStyle = DRAWSTYLE_IGNORE;
Sub_GammaState.Name = "Gamma Regime (1/0/-1)"; Sub_GammaState.DrawStyle = DRAWSTYLE_IGNORE;
Sub_StageOut.Name = "Setup Stage (0-3)"; Sub_StageOut.DrawStyle = DRAWSTYLE_IGNORE;
// The text drawing function requires DRAWSTYLE_CUSTOM_TEXT on this
// subgraph. PrimaryColor = text color, SecondaryColor = background,
// LineWidth = font size.
Sub_Dashboard.Name = "Dashboard Text";
Sub_Dashboard.DrawStyle = DRAWSTYLE_CUSTOM_TEXT;
Sub_Dashboard.PrimaryColor = RGB(230, 230, 230);
Sub_Dashboard.SecondaryColor = RGB(20, 20, 20);
Sub_Dashboard.LineWidth = 9; // font size for the text drawing
Sub_Dashboard.DrawZeros = 0;
/*------------------------------------------------------------ inputs */
In_EnableLong.Name = "1. Enable Long Setups";
In_EnableLong.SetYesNo(1);
In_EnableShort.Name = "1. Enable Short Setups";
In_EnableShort.SetYesNo(1);
In_StructChart.Name = "2. Environment: Structure Source Chart Number (0 = this chart)";
In_StructChart.SetInt(0);
In_StructChart.SetIntLimits(0, 10000);
In_StructStrength.Name = "2. Environment: Structure Pivot Strength (bars each side)";
In_StructStrength.SetInt(5);
In_StructStrength.SetIntLimits(1, 100);
In_StructLookback.Name = "2. Environment: Structure Lookback (bars)";
In_StructLookback.SetInt(300);
In_StructLookback.SetIntLimits(20, 5000);
In_StructFilter.Name = "2. Environment: Structure Filter";
In_StructFilter.SetCustomInputStrings("Off;Require Aligned Structure;Aligned or Sideways");
In_StructFilter.SetCustomInputIndex(1);
In_GammaMode.Name = "2. Environment: Gamma (GEX) Mode";
In_GammaMode.SetCustomInputStrings("Off;Force Positive;Force Negative;Auto from Gamma Flip Level");
In_GammaMode.SetCustomInputIndex(0);
In_GammaFlip.Name = "2. Environment: Gamma Flip Level (price)";
In_GammaFlip.SetFloat(0.0f);
In_GammaRequired.Name = "2. Environment: Required Gamma Regime";
In_GammaRequired.SetCustomInputStrings("Any;Positive Only;Negative Only");
In_GammaRequired.SetCustomInputIndex(0);
In_VAPercent.Name = "3. Location: Value Area Percent";
In_VAPercent.SetFloat(70.0f);
In_VAPercent.SetFloatLimits(30.0f, 95.0f);
In_LocSource.Name = "3. Location: Profile Used for Discount/Premium";
In_LocSource.SetCustomInputStrings("Developing Session;Prior Session;Either");
In_LocSource.SetCustomInputIndex(0);
In_ZoneOutsideVA.Name = "3. Location: Require Fib Zone Outside Value Area";
In_ZoneOutsideVA.SetYesNo(1);
In_VATolTicks.Name = "3. Location: Value Area Tolerance (ticks)";
In_VATolTicks.SetInt(2);
In_VATolTicks.SetIntLimits(0, 200);
In_LegStrength.Name = "3. Location: Retracement Leg Pivot Strength";
In_LegStrength.SetInt(3);
In_LegStrength.SetIntLimits(1, 50);
In_Fib1.Name = "3. Location: Fib Level 1 (zone edge)";
In_Fib1.SetFloat(0.705f);
In_Fib1.SetFloatLimits(0.1f, 0.99f);
In_Fib2.Name = "3. Location: Fib Level 2 (mid)";
In_Fib2.SetFloat(0.786f);
In_Fib2.SetFloatLimits(0.1f, 0.99f);
In_Fib3.Name = "3. Location: Fib Level 3 (zone edge)";
In_Fib3.SetFloat(0.886f);
In_Fib3.SetFloatLimits(0.1f, 0.99f);
In_MinLegTicks.Name = "3. Location: Minimum Leg Size (ticks)";
In_MinLegTicks.SetInt(8);
In_MinLegTicks.SetIntLimits(1, 100000);
In_LegLookback.Name = "3. Location: Leg Search Lookback (bars)";
In_LegLookback.SetInt(150);
In_LegLookback.SetIntLimits(10, 5000);
In_ImbRatioPct.Name = "4. Confirmation: Diagonal Imbalance Ratio (percent)";
In_ImbRatioPct.SetInt(400);
In_ImbRatioPct.SetIntLimits(110, 5000);
In_ImbMinVol.Name = "4. Confirmation: Imbalance Minimum Volume per Level";
In_ImbMinVol.SetInt(10);
In_ImbMinVol.SetIntLimits(0, 100000);
In_MinStacked.Name = "4. Confirmation: Minimum Stacked Imbalances to Trigger";
In_MinStacked.SetInt(2);
In_MinStacked.SetIntLimits(1, 50);
In_AbsMinDelta.Name = "4. Confirmation: Absorption Minimum |Delta| (0 = auto)";
In_AbsMinDelta.SetInt(0);
In_AbsMinDelta.SetIntLimits(0, 10000000);
In_AbsAutoMult.Name = "4. Confirmation: Auto Delta Threshold (x 20-bar average)";
In_AbsAutoMult.SetFloat(1.0f);
In_AbsAutoMult.SetFloatLimits(0.1f, 20.0f);
In_AbsClosePos.Name = "4. Confirmation: Absorption Close Position in Bar Range (0-1)";
In_AbsClosePos.SetFloat(0.5f);
In_AbsClosePos.SetFloatLimits(0.0f, 1.0f);
In_AbsNeedImb.Name = "4. Confirmation: Absorption Requires Opposing Imbalance";
In_AbsNeedImb.SetYesNo(1);
In_TrigMaxBars.Name = "4. Confirmation: Max Bars from Absorption to Trigger";
In_TrigMaxBars.SetInt(6);
In_TrigMaxBars.SetIntLimits(1, 200);
In_TrigNeedBreak.Name = "4. Confirmation: Trigger Must Close Beyond Absorption Bar";
In_TrigNeedBreak.SetYesNo(0);
In_StopBuffer.Name = "5. Management: Stop Buffer (ticks)";
In_StopBuffer.SetInt(2);
In_StopBuffer.SetIntLimits(0, 1000);
In_R1.Name = "5. Management: Target 1 (R multiple)";
In_R1.SetFloat(1.5f);
In_R1.SetFloatLimits(0.1f, 100.0f);
In_R2.Name = "5. Management: Target 2 (R multiple)";
In_R2.SetFloat(2.0f);
In_R2.SetFloatLimits(0.1f, 100.0f);
In_TrailActivateR.Name = "5. Management: Activate Trailing Stop at (R multiple)";
In_TrailActivateR.SetFloat(1.0f);
In_TrailActivateR.SetFloatLimits(0.1f, 100.0f);
In_TrailLookback.Name = "5. Management: Trailing Stop Swing Lookback (bars)";
In_TrailLookback.SetInt(3);
In_TrailLookback.SetIntLimits(1, 200);
In_TrailBuffer.Name = "5. Management: Trailing Stop Buffer (ticks)";
In_TrailBuffer.SetInt(2);
In_TrailBuffer.SetIntLimits(0, 1000);
In_UseSession.Name = "6. Filters: Use Session Time Filter";
In_UseSession.SetYesNo(1);
In_SessionStart.Name = "6. Filters: Session Start Time";
In_SessionStart.SetTime(HMS_TIME(9, 30, 0));
In_SessionEnd.Name = "6. Filters: Session End Time";
In_SessionEnd.SetTime(HMS_TIME(15, 45, 0));
In_MaxSetups.Name = "6. Filters: Max Setups Per Session (0 = unlimited)";
In_MaxSetups.SetInt(0);
In_MaxSetups.SetIntLimits(0, 100);
In_Alerts.Name = "7. Display: Enable Alerts";
In_Alerts.SetYesNo(1);
In_DrawZone.Name = "7. Display: Draw Golden Pocket Zone";
In_DrawZone.SetYesNo(1);
In_ShowPrior.Name = "7. Display: Show Prior Session Levels";
In_ShowPrior.SetYesNo(1);
In_ShowDash.Name = "7. Display: Show Status Dashboard";
In_ShowDash.SetYesNo(1);
In_DashX.Name = "7. Display: Dashboard Horizontal Position (0-150)";
In_DashX.SetInt(20);
In_DashX.SetIntLimits(0, 150);
In_DashY.Name = "7. Display: Dashboard Vertical Position (0-100)";
In_DashY.SetInt(92);
In_DashY.SetIntLimits(0, 100);
return;
}
/*======================================================================*/
/* Persistent state */
/*======================================================================*/
RLC::s_State* p = (RLC::s_State*)sc.GetPersistentPointer(1);
if (sc.LastCallToFunction)
{
if (p != NULL)
{
delete p;
sc.SetPersistentPointer(1, NULL);
}
return;
}
if (p == NULL)
{
p = new RLC::s_State();
sc.SetPersistentPointer(1, p);
}
if (sc.Index == 0)
p->Reset(); // full recalculation restart
if (sc.VolumeAtPriceForBars == NULL)
{
if (sc.Index == 0)
sc.AddMessageToLog("RLC Framework: no Volume at Price data on this chart. "
"Use an intraday chart with bid/ask volume.", 1);
return;
}
const float TickSize = sc.TickSize;
const double ImbRatio = In_ImbRatioPct.GetInt() / 100.0;
const double ImbMinVol = (double)In_ImbMinVol.GetInt();
const int LineBase = 5000000 + (sc.StudyGraphInstanceID * 10);
/*----------------------------------------------------------------------*/
/* Live order flow for the current (forming) bar */
/*----------------------------------------------------------------------*/
RLC::s_OF OFNow;
RLC::ComputeOrderFlow(sc, sc.Index, ImbRatio, ImbMinVol, OFNow, p->Scratch);
Sub_Delta[sc.Index] = (float)OFNow.Delta;
Sub_BuyImb[sc.Index] = (float)OFNow.BuyImb;
Sub_SellImb[sc.Index] = (float)OFNow.SellImb;
Sub_StackBuy[sc.Index] = (float)OFNow.StackBuy;
Sub_StackSell[sc.Index] = (float)OFNow.StackSell;
/*======================================================================*/
/* Once-per-closed-bar evaluation */
/* Everything that creates a signal is computed on a CLOSED bar so the */
/* study never repaints. */
/*======================================================================*/
if (sc.Index >= 1 && sc.Index > p->LastEvalBar)
{
const int E = sc.Index - 1; // the bar being evaluated
p->LastEvalBar = sc.Index;
/*---------------------------------------------- 1. session profile */
for (int b = p->LastProfileBar + 1; b <= E; ++b)
{
if (b < 0)
continue;
const int DayDate = sc.GetTradingDayStartDateTimeOfBar(sc.BaseDateTimeIn[b]).GetDate();
if (DayDate != p->TradingDayDate)
{
if (p->ProfileValid)
{
p->PriorPOC = p->POC;
p->PriorVAH = p->VAH;
p->PriorVAL = p->VAL;
p->PriorValid = 1;
}
p->SessionVol.clear();
p->ProfileValid = 0;
p->TradingDayDate = DayDate;
p->SetupsThisSession = 0;
}
RLC::AddBarToProfile(sc, b, p->SessionVol);
p->LastProfileBar = b;
}
if (RLC::ComputeValueArea(p->SessionVol, In_VAPercent.GetFloat(), TickSize,
p->POC, p->VAH, p->VAL))
p->ProfileValid = 1;
/*--------------------------------------------------- 2. structure */
if (E >= 2 * In_StructStrength.GetInt() + 2)
{
const int StructChart = In_StructChart.GetInt();
if (StructChart <= 0 || StructChart == sc.ChartNumber)
{
p->Structure = RLC::DetectStructure(sc.High, sc.Low, E,
In_StructStrength.GetInt(),
In_StructLookback.GetInt(),
p->LastSwingHigh, p->LastSwingLow);
}
else
{
SCGraphData HTFData;
sc.GetChartBaseData(StructChart, HTFData);
SCFloatArrayRef HTFHigh = HTFData[SC_HIGH];
SCFloatArrayRef HTFLow = HTFData[SC_LOW];
const int HTFSize = HTFHigh.GetArraySize();
if (HTFSize > 0)
{
// This function maps a bar index on THIS chart to the
// containing bar index on the referenced chart.
int HTFIndex = sc.GetContainingIndexForDateTimeIndex(StructChart, E);
if (HTFIndex >= HTFSize) HTFIndex = HTFSize - 1;
if (HTFIndex >= 2 * In_StructStrength.GetInt() + 2)
{