-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOrderflowConfluence.cpp
More file actions
386 lines (338 loc) · 16.2 KB
/
Copy pathOrderflowConfluence.cpp
File metadata and controls
386 lines (338 loc) · 16.2 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
// =============================================================================
// OrderflowConfluence.cpp
// Sierra Chart ACSIL Custom Study
//
// PURPOSE:
// Combines up to 25 bull and 25 bear orderflow triggers via a weighted-point
// system. Fires two types of signals:
//
// 1. IMMEDIATE SIGNAL (arrows):
// Current bar's point score >= threshold → arrow on that bar only.
//
// 2. LOOKBACK CONFLUENCE (background shading):
// Rolling sum of scores over the last N bars >= threshold → background
// shading that persists until the rolling sum falls below the threshold.
//
// SCORING MODES (user-selectable):
// Shared — Bull triggers add points (+), bear triggers subtract (−).
// One combined score. Long fires at >= +threshold, short at
// <= −threshold.
// Independent — Bull and bear pools are completely separate. Both can fire
// on the same bar.
//
// TRIGGER ACTIVE CONDITION:
// A trigger fires (contributes its weight) when its referenced study
// subgraph has a non-zero value on the current bar.
//
// INPUT LAYOUT (106 total — within Sierra Chart's 128 limit):
// [0 .. 49] Bull triggers: Input[2n] = Study Subgraph reference
// Input[2n+1] = Weight (0=off, 1, 2, or 3 pts)
// [50 .. 99] Bear triggers: Input[50+2n] = Study Subgraph reference
// Input[50+2n+1] = Weight (0=off, 1, 2, or 3 pts)
// [100] Scoring Mode (Shared / Independent)
// [101] Long Entry Threshold (immediate arrow)
// [102] Short Entry Threshold (immediate arrow)
// [103] Lookback Window (bars)
// [104] Long Confluence Threshold (lookback sum)
// [105] Short Confluence Threshold (lookback sum, entered as positive)
//
// SUBGRAPHS:
// SG0 Long Signal — ↑ Arrow, green, fires on immediate long threshold
// SG1 Short Signal — ↓ Arrow, red, fires on immediate short threshold
// SG2 Long Confluence — Background, green, on while lookback sum ≥ threshold
// SG3 Short Confluence — Background, red, on while lookback sum ≥ threshold
//
// AUXILIARY ARRAYS (stored inside sg_LongSignal.Arrays[]):
// [0] longScore per bar (bull points earned this bar)
// [1] shortScore per bar (bear points earned this bar, stored positive)
//
// SETUP:
// 1. Copy to Sierra Chart ACS_Source folder.
// 2. Build via Analysis >> Build Custom Studies DLL.
// 3. Add the study to your chart.
// 4. In study settings, assign each Bull/Bear trigger to the study+subgraph
// you want to monitor, and set its weight (0 = disabled).
// 5. Adjust thresholds to match your setup.
// =============================================================================
#include "sierrachart.h"
SCDLLName("OrderflowConfluence")
// =============================================================================
SCSFExport scsf_OrderflowConfluence(SCStudyInterfaceRef sc)
{
// -------------------------------------------------------------------------
// SUBGRAPHS
// -------------------------------------------------------------------------
SCSubgraphRef sg_LongSignal = sc.Subgraph[0];
SCSubgraphRef sg_ShortSignal = sc.Subgraph[1];
SCSubgraphRef sg_LongConfluence = sc.Subgraph[2];
SCSubgraphRef sg_ShortConfluence = sc.Subgraph[3];
// sg_LongSignal.Arrays[0] — bull (long) score stored per bar
// sg_LongSignal.Arrays[1] — bear (short) score stored per bar (positive)
// -------------------------------------------------------------------------
// INPUTS — signal settings (indices 100-105)
// -------------------------------------------------------------------------
SCInputRef in_ScoringMode = sc.Input[100];
SCInputRef in_LongEntryThreshold = sc.Input[101];
SCInputRef in_ShortEntryThreshold = sc.Input[102];
SCInputRef in_LookbackBars = sc.Input[103];
SCInputRef in_LongLookbackThresh = sc.Input[104];
SCInputRef in_ShortLookbackThresh = sc.Input[105];
// =========================================================================
// SET DEFAULTS
// =========================================================================
if (sc.SetDefaults)
{
sc.GraphName = "Orderflow Confluence";
sc.StudyDescription =
"Combines up to 25 bull + 25 bear orderflow triggers via weighted "
"point scores. Fires arrows on immediate threshold and background "
"shading when a rolling lookback sum meets the confluence threshold. "
"Inputs [0-49]: Bull triggers (study subgraph + weight). "
"Inputs [50-99]: Bear triggers. Inputs [100-105]: thresholds.";
sc.AutoLoop = 1;
sc.GraphRegion = 0; // overlay on main price chart
sc.FreeDLL = 0;
sc.DrawZeros = 0;
sc.UpdateAlways = 1; // keep current bar live
// -- Subgraph: Long Signal (arrow up) --------------------------------
sg_LongSignal.Name = "Long Signal";
sg_LongSignal.DrawStyle = DRAWSTYLE_ARROWUP;
sg_LongSignal.LineWidth = 3;
sg_LongSignal.PrimaryColor = RGB(0, 220, 80);
sg_LongSignal.DrawZeros = 0;
// -- Subgraph: Short Signal (arrow down) -----------------------------
sg_ShortSignal.Name = "Short Signal";
sg_ShortSignal.DrawStyle = DRAWSTYLE_ARROWDOWN;
sg_ShortSignal.LineWidth = 3;
sg_ShortSignal.PrimaryColor = RGB(220, 50, 50);
sg_ShortSignal.DrawZeros = 0;
// -- Subgraph: Long Confluence (background) --------------------------
sg_LongConfluence.Name = "Long Confluence (Lookback)";
sg_LongConfluence.DrawStyle = DRAWSTYLE_BACKGROUND;
sg_LongConfluence.PrimaryColor = RGB(0, 180, 80);
sg_LongConfluence.DrawZeros = 0;
// -- Subgraph: Short Confluence (background) -------------------------
sg_ShortConfluence.Name = "Short Confluence (Lookback)";
sg_ShortConfluence.DrawStyle = DRAWSTYLE_BACKGROUND;
sg_ShortConfluence.PrimaryColor = RGB(220, 50, 50);
sg_ShortConfluence.DrawZeros = 0;
// -- Bull trigger names (first 10 pre-labeled, rest generic) ---------
static const char* BullNames[25] = {
"Bull Trigger 1 — Absorption",
"Bull Trigger 2 — Stacked Imbalance",
"Bull Trigger 3 — Delta Divergence",
"Bull Trigger 4 — Bid Exhaustion",
"Bull Trigger 5 — Sweep Long",
"Bull Trigger 6 — Volume Climax",
"Bull Trigger 7 — POC Reclaim",
"Bull Trigger 8 — VWAP Reclaim",
"Bull Trigger 9 — Trapped Shorts",
"Bull Trigger 10 — Iceberg Bid",
"Bull Trigger 11",
"Bull Trigger 12",
"Bull Trigger 13",
"Bull Trigger 14",
"Bull Trigger 15",
"Bull Trigger 16",
"Bull Trigger 17",
"Bull Trigger 18",
"Bull Trigger 19",
"Bull Trigger 20",
"Bull Trigger 21",
"Bull Trigger 22",
"Bull Trigger 23",
"Bull Trigger 24",
"Bull Trigger 25"
};
for (int i = 0; i < 25; ++i)
{
const int refIdx = i * 2; // study subgraph reference
const int wtIdx = i * 2 + 1; // weight
sc.Input[refIdx].Name = BullNames[i];
sc.Input[refIdx].SetStudySubgraphValues(0, 0);
SCString wtName;
wtName.Format("%s — Weight", BullNames[i]);
sc.Input[wtIdx].Name = wtName;
sc.Input[wtIdx].SetCustomInputStrings("0 — Disabled;1 Point;2 Points;3 Points");
sc.Input[wtIdx].SetCustomInputIndex(0);
}
// -- Bear trigger names ----------------------------------------------
static const char* BearNames[25] = {
"Bear Trigger 1 — Absorption",
"Bear Trigger 2 — Stacked Imbalance",
"Bear Trigger 3 — Delta Divergence",
"Bear Trigger 4 — Ask Exhaustion",
"Bear Trigger 5 — Sweep Short",
"Bear Trigger 6 — Volume Climax",
"Bear Trigger 7 — POC Rejection",
"Bear Trigger 8 — VWAP Rejection",
"Bear Trigger 9 — Trapped Longs",
"Bear Trigger 10 — Iceberg Ask",
"Bear Trigger 11",
"Bear Trigger 12",
"Bear Trigger 13",
"Bear Trigger 14",
"Bear Trigger 15",
"Bear Trigger 16",
"Bear Trigger 17",
"Bear Trigger 18",
"Bear Trigger 19",
"Bear Trigger 20",
"Bear Trigger 21",
"Bear Trigger 22",
"Bear Trigger 23",
"Bear Trigger 24",
"Bear Trigger 25"
};
for (int i = 0; i < 25; ++i)
{
const int refIdx = 50 + i * 2;
const int wtIdx = 50 + i * 2 + 1;
sc.Input[refIdx].Name = BearNames[i];
sc.Input[refIdx].SetStudySubgraphValues(0, 0);
SCString wtName;
wtName.Format("%s — Weight", BearNames[i]);
sc.Input[wtIdx].Name = wtName;
sc.Input[wtIdx].SetCustomInputStrings("0 — Disabled;1 Point;2 Points;3 Points");
sc.Input[wtIdx].SetCustomInputIndex(0);
}
// -- Signal / threshold settings -------------------------------------
in_ScoringMode.Name = "Scoring Mode";
in_ScoringMode.SetCustomInputStrings(
"Shared — Bull adds / Bear subtracts (one combined score);"
"Independent — Separate long and short score pools");
in_ScoringMode.SetCustomInputIndex(0);
in_LongEntryThreshold.Name = "Long Entry Threshold — Immediate Arrow (pts)";
in_LongEntryThreshold.SetInt(5);
in_LongEntryThreshold.SetIntLimits(1, 75);
in_ShortEntryThreshold.Name = "Short Entry Threshold — Immediate Arrow (pts, enter positive)";
in_ShortEntryThreshold.SetInt(5);
in_ShortEntryThreshold.SetIntLimits(1, 75);
in_LookbackBars.Name = "Lookback Window (bars)";
in_LookbackBars.SetInt(5);
in_LookbackBars.SetIntLimits(1, 200);
in_LongLookbackThresh.Name = "Long Confluence Threshold — Lookback Sum (pts)";
in_LongLookbackThresh.SetInt(6);
in_LongLookbackThresh.SetIntLimits(1, 225);
in_ShortLookbackThresh.Name = "Short Confluence Threshold — Lookback Sum (pts, enter positive)";
in_ShortLookbackThresh.SetInt(6);
in_ShortLookbackThresh.SetIntLimits(1, 225);
return;
}
// =========================================================================
// PER-BAR CALCULATION
// =========================================================================
const int idx = sc.Index;
// -------------------------------------------------------------------------
// 1. Compute bull score for this bar
// A trigger is active when its referenced SG has a non-zero value.
// Weight comes from the custom input index (0=off, 1, 2, or 3 pts).
// -------------------------------------------------------------------------
float longScore = 0.0f;
for (int i = 0; i < 25; ++i)
{
const int refIdx = i * 2;
const int wtIdx = i * 2 + 1;
const int weight = sc.Input[wtIdx].GetIndex(); // 0,1,2,3
if (weight == 0)
continue;
SCFloatArray refData;
sc.GetStudyArrayFromChartUsingID(
sc.ChartNumber,
sc.Input[refIdx].GetStudyID(),
sc.Input[refIdx].GetSubgraphIndex(),
refData
);
if (refData.GetArraySize() == 0)
continue;
if (refData[idx] != 0.0f)
longScore += static_cast<float>(weight);
}
// -------------------------------------------------------------------------
// 2. Compute bear score for this bar (stored and used as a positive value;
// sign is applied later based on scoring mode)
// -------------------------------------------------------------------------
float shortScore = 0.0f;
for (int i = 0; i < 25; ++i)
{
const int refIdx = 50 + i * 2;
const int wtIdx = 50 + i * 2 + 1;
const int weight = sc.Input[wtIdx].GetIndex();
if (weight == 0)
continue;
SCFloatArray refData;
sc.GetStudyArrayFromChartUsingID(
sc.ChartNumber,
sc.Input[refIdx].GetStudyID(),
sc.Input[refIdx].GetSubgraphIndex(),
refData
);
if (refData.GetArraySize() == 0)
continue;
if (refData[idx] != 0.0f)
shortScore += static_cast<float>(weight);
}
// -------------------------------------------------------------------------
// 3. Persist per-bar scores for rolling lookback
// -------------------------------------------------------------------------
sg_LongSignal.Arrays[0][idx] = longScore;
sg_LongSignal.Arrays[1][idx] = shortScore;
// -------------------------------------------------------------------------
// 4. Rolling lookback sum over the last N bars (inclusive of current bar)
// -------------------------------------------------------------------------
const int lookback = in_LookbackBars.GetInt();
const int startBar = (idx - lookback + 1 > 0) ? (idx - lookback + 1) : 0;
float rollingLong = 0.0f;
float rollingShort = 0.0f;
for (int b = startBar; b <= idx; ++b)
{
rollingLong += sg_LongSignal.Arrays[0][b];
rollingShort += sg_LongSignal.Arrays[1][b];
}
// -------------------------------------------------------------------------
// 5. Apply scoring mode and evaluate thresholds
// -------------------------------------------------------------------------
const int scoringMode = in_ScoringMode.GetIndex(); // 0=shared, 1=independent
const float longThresh = static_cast<float>(in_LongEntryThreshold.GetInt());
const float shortThresh = static_cast<float>(in_ShortEntryThreshold.GetInt());
const float longLBThresh = static_cast<float>(in_LongLookbackThresh.GetInt());
const float shortLBThresh = static_cast<float>(in_ShortLookbackThresh.GetInt());
bool fireLongImmediate = false;
bool fireShortImmediate = false;
bool fireLongLookback = false;
bool fireShortLookback = false;
if (scoringMode == 0)
{
// -- Shared mode: one combined score per bar -------------------------
// Bull adds positive points; bear subtracts.
const float combined = longScore - shortScore;
const float rollingCombined = rollingLong - rollingShort;
fireLongImmediate = (combined >= longThresh);
fireShortImmediate = (combined <= -shortThresh);
fireLongLookback = (rollingCombined >= longLBThresh);
fireShortLookback = (rollingCombined <= -shortLBThresh);
}
else
{
// -- Independent mode: separate pools --------------------------------
// Each direction evaluated on its own accumulated positive score.
fireLongImmediate = (longScore >= longThresh);
fireShortImmediate = (shortScore >= shortThresh);
fireLongLookback = (rollingLong >= longLBThresh);
fireShortLookback = (rollingShort >= shortLBThresh);
}
// -------------------------------------------------------------------------
// 6. Write to subgraphs
//
// Arrow subgraphs: value = price level where arrow is drawn.
// Long arrow (ARROWUP) → placed at sc.Low[idx] (below bar, pointing up)
// Short arrow (ARROWDOWN) → placed at sc.High[idx] (above bar, pointing down)
// Set to 0 when not firing; DrawZeros = 0 suppresses drawing.
//
// Background subgraphs: any non-zero value activates the background shade.
// -------------------------------------------------------------------------
sg_LongSignal[idx] = fireLongImmediate ? sc.Low[idx] : 0.0f;
sg_ShortSignal[idx] = fireShortImmediate ? sc.High[idx] : 0.0f;
sg_LongConfluence[idx] = fireLongLookback ? 1.0f : 0.0f;
sg_ShortConfluence[idx] = fireShortLookback ? 1.0f : 0.0f;
}