-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerMeterFeedJS.cpp
More file actions
368 lines (309 loc) · 13.3 KB
/
PowerMeterFeedJS.cpp
File metadata and controls
368 lines (309 loc) · 13.3 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
// PowerMeterFeedJS.cpp
//
// SierraChart ACSIL study - writes live DOM data to the PowerMeter
// Win32 overlay via a named Windows shared-memory segment.
//
// Jigsaw-like simplified mode:
// col0Red (TBV) - Executed Bid Volume: sell-aggressor trades (sc.BidVolume[idx])
// col0Blue (TAV) - Executed Ask Volume: buy-aggressor trades (sc.AskVolume[idx])
// col1Red (Bear P/S) - plain bid-pull sum + ask-stack sum
// col1Blue (Bull P/S) - plain ask-pull sum + bid-stack sum
// col2Red (ASK) - total ask-side DOM volume across configured levels
// col2Blue (BID) - total bid-side DOM volume across configured levels
//
// Column 0 uses sc.BidVolume[idx] / sc.AskVolume[idx] – Sierra Chart's standard
// bar-level bid/ask execution arrays. PowerMeter.cpp applies delta accumulation
// and persistent reset on top of these raw values.
//
// *** Place this file in your SierraChart ACS_Source directory ***
// Compile from SierraChart: Analysis > Build Custom Studies DLL
#include "sierrachart.h"
#include <windows.h>
SCDLLName("PowerMeter Feed JS")
// -----------------------------------------------------------------------------
// Shared memory layout - must match the PMSharedData struct in PowerMeter.cpp
// -----------------------------------------------------------------------------
#define PM_SHM_NAME L"Local\\PowerMeterLiveData"
#pragma pack(push, 4)
struct PMSharedData
{
volatile LONG sequence; // odd while writing, even when stable (seqlock)
float col0Red; // TBV - Executed at Bid Volume (sell-aggressor, sc.BidVolume[idx])
float col0Blue; // TAV - Executed at Ask Volume (buy-aggressor, sc.AskVolume[idx])
float col1Red; // Bear P/S
float col1Blue; // Bull P/S
float col2Red; // ASK DOM total volume
float col2Blue; // BID DOM total volume
DWORD tickCount; // GetTickCount() at last write
};
#pragma pack(pop)
// IPC handle wrapper kept alive across UpdateAlways ticks via persistent memory
struct PMIpcContext
{
HANDLE hMapFile;
PMSharedData* pData;
};
// -----------------------------------------------------------------------------
SCSFExport scsf_PowerMeterFeedJS(SCStudyInterfaceRef sc)
{
// Subgraphs (DRAWSTYLE_IGNORE - output is IPC only, not plotted on chart)
SCSubgraphRef SG_ExecBid = sc.Subgraph[0]; // col0Red - TBV (Executed Bid Vol, sell-aggressor)
SCSubgraphRef SG_ExecAsk = sc.Subgraph[1]; // col0Blue - TAV (Executed Ask Vol, buy-aggressor)
SCSubgraphRef SG_BearPS = sc.Subgraph[2]; // col1Red - Bear P/S
SCSubgraphRef SG_BullPS = sc.Subgraph[3]; // col1Blue - Bull P/S
SCSubgraphRef SG_AskDOM = sc.Subgraph[4]; // col2Red - ASK DOM vol
SCSubgraphRef SG_BidDOM = sc.Subgraph[5]; // col2Blue - BID DOM vol
// Inputs
SCInputRef i_NumLevels = sc.Input[0];
SCInputRef i_RecentVolLevels = sc.Input[1];
SCInputRef i_UseBestLevel = sc.Input[2];
SCInputRef i_DepthLevels = sc.Input[3];
SCInputRef i_SnapshotLevels = sc.Input[4];
SCInputRef i_DebugLog = sc.Input[5];
// -------------------------------------------------------------------------
if (sc.SetDefaults)
{
sc.GraphName = "PowerMeter Feed JS";
sc.StudyDescription =
"Writes live DOM and execution data to the PowerMeter Win32 overlay via "
"the named shared memory segment Local\\PowerMeterLiveData. "
"Column 0 uses sc.BidVolume[idx] / sc.AskVolume[idx] for executed-at-bid "
"(sell-aggressor) and executed-at-ask (buy-aggressor) volume per bar. "
"Column 1 uses plain Pull/Stack sums without persistence, burst, "
"continuation, decay, or extra weighting. "
"Column 2 uses plain DOM snapshot totals. "
"PowerMeter.cpp applies delta accumulation and persistent reset for Column 0.";
sc.GraphRegion = 0;
sc.AutoLoop = 0;
sc.UpdateAlways = 1;
sc.UsesMarketDepthData = 1;
SG_ExecBid.Name = "TBV - Executed Bid Vol (sell-aggressor)";
SG_ExecBid.DrawStyle = DRAWSTYLE_IGNORE;
SG_ExecAsk.Name = "TAV - Executed Ask Vol (buy-aggressor)";
SG_ExecAsk.DrawStyle = DRAWSTYLE_IGNORE;
SG_BearPS.Name = "Bear P/S (plain bid pulls + ask stacks)";
SG_BearPS.DrawStyle = DRAWSTYLE_IGNORE;
SG_BullPS.Name = "Bull P/S (plain ask pulls + bid stacks)";
SG_BullPS.DrawStyle = DRAWSTYLE_IGNORE;
SG_AskDOM.Name = "ASK DOM Total Vol";
SG_AskDOM.DrawStyle = DRAWSTYLE_IGNORE;
SG_BidDOM.Name = "BID DOM Total Vol";
SG_BidDOM.DrawStyle = DRAWSTYLE_IGNORE;
i_NumLevels.Name = "DOM Levels Per Side Cap (0 = No Cap)";
i_NumLevels.SetInt(10);
i_RecentVolLevels.Name = "(unused) Legacy Recent Vol Levels";
i_RecentVolLevels.SetInt(5);
i_UseBestLevel.Name = "Include Best Bid/Ask Level (level 0)";
i_UseBestLevel.SetYesNo(0);
i_DepthLevels.Name = "Depth Levels (P/S)";
i_DepthLevels.SetInt(4);
i_SnapshotLevels.Name = "Snapshot Levels (DOM)";
i_SnapshotLevels.SetInt(6);
i_DebugLog.Name = "Debug Log";
i_DebugLog.SetYesNo(0);
return;
}
// -------------------------------------------------------------------------
// Release OS handles on study removal / SC shutdown
// -------------------------------------------------------------------------
if (sc.LastCallToFunction)
{
void*& pIpcRaw = sc.GetPersistentPointer(1);
if (pIpcRaw != NULL)
{
PMIpcContext* ctx = reinterpret_cast<PMIpcContext*>(pIpcRaw);
if (ctx->pData != NULL)
{
UnmapViewOfFile(ctx->pData);
ctx->pData = NULL;
}
if (ctx->hMapFile != NULL)
{
CloseHandle(ctx->hMapFile);
ctx->hMapFile = NULL;
}
}
return;
}
sc.SetUseMarketDepthPullingStackingData(1);
if (sc.ArraySize < 1)
return;
const int idx = sc.ArraySize - 1;
// -------------------------------------------------------------------------
// Read inputs
// -------------------------------------------------------------------------
int numLevels = i_NumLevels.GetInt();
(void)i_RecentVolLevels.GetInt(); // input slot reserved for backward compatibility
const bool includeBest = (i_UseBestLevel.GetYesNo() != 0);
const bool debugLog = (i_DebugLog.GetYesNo() != 0);
int depthLevels = i_DepthLevels.GetInt();
if (depthLevels < 1)
depthLevels = 1;
int snapshotLevels = i_SnapshotLevels.GetInt();
if (snapshotLevels < 1)
snapshotLevels = 1;
if (numLevels > 0)
{
if (depthLevels > numLevels)
depthLevels = numLevels;
if (snapshotLevels > numLevels)
snapshotLevels = numLevels;
}
// -------------------------------------------------------------------------
// DOM availability check
// -------------------------------------------------------------------------
const int bidLevelsAvail = sc.GetBidMarketDepthNumberOfLevels();
const int askLevelsAvail = sc.GetAskMarketDepthNumberOfLevels();
if (bidLevelsAvail <= 0 || askLevelsAvail <= 0)
return;
const int startLevel = includeBest ? 0 : 1;
s_MarketDepthEntry de;
// -------------------------------------------------------------------------
// Column 0: Executed Bid/Ask Volume (Trade Execution style)
//
// Source: sc.BidVolume[idx] / sc.AskVolume[idx]
// sc.BidVolume[idx] = volume executed at bid (sell-aggressors hitting bid)
// sc.AskVolume[idx] = volume executed at ask (buy-aggressors lifting ask)
//
// Sierra Chart's standard bar-level bid/ask execution arrays.
// No inactivity-clear timer. No GetRecentBidVolumeAtPrice used.
// PowerMeter.cpp applies delta accumulation and persistent reset on top.
// -------------------------------------------------------------------------
const double execBidVol = static_cast<double>(sc.BidVolume[idx]); // sell-aggressor
const double execAskVol = static_cast<double>(sc.AskVolume[idx]); // buy-aggressor
// -------------------------------------------------------------------------
// Column 1: Plain Pull/Stack directional composite
//
// Bear P/S = bid pulls + ask stacks
// Bull P/S = ask pulls + bid stacks
//
// No decay, no burst logic, no continuation logic, no persistence weighting.
// -------------------------------------------------------------------------
double bearPSSum = 0.0;
double bullPSSum = 0.0;
double totalAskVol = 0.0;
double totalBidVol = 0.0;
// Ask side
for (int level = startLevel; level < startLevel + depthLevels && level < askLevelsAvail; ++level)
{
if (!sc.GetAskMarketDepthEntryAtLevel(de, level))
continue;
const int val = sc.GetAskMarketDepthStackPullValueAtPrice(static_cast<float>(de.AdjustedPrice));
if (val < 0)
{
// Ask pulling -> bullish
bullPSSum += static_cast<double>(-val);
}
else if (val > 0)
{
// Ask stacking -> bearish
bearPSSum += static_cast<double>(val);
}
}
// Bid side
for (int level = startLevel; level < startLevel + depthLevels && level < bidLevelsAvail; ++level)
{
if (!sc.GetBidMarketDepthEntryAtLevel(de, level))
continue;
const int val = sc.GetBidMarketDepthStackPullValueAtPrice(static_cast<float>(de.AdjustedPrice));
if (val < 0)
{
// Bid pulling -> bearish
bearPSSum += static_cast<double>(-val);
}
else if (val > 0)
{
// Bid stacking -> bullish
bullPSSum += static_cast<double>(val);
}
}
// -------------------------------------------------------------------------
// Column 2: Snapshot (DOM totals)
// -------------------------------------------------------------------------
for (int level = startLevel; level < startLevel + snapshotLevels && level < askLevelsAvail; ++level)
{
if (sc.GetAskMarketDepthEntryAtLevel(de, level))
totalAskVol += static_cast<double>(de.Quantity);
}
for (int level = startLevel; level < startLevel + snapshotLevels && level < bidLevelsAvail; ++level)
{
if (sc.GetBidMarketDepthEntryAtLevel(de, level))
totalBidVol += static_cast<double>(de.Quantity);
}
// Write subgraphs
SG_ExecBid[idx] = static_cast<float>(execBidVol);
SG_ExecAsk[idx] = static_cast<float>(execAskVol);
SG_BearPS[idx] = static_cast<float>(bearPSSum);
SG_BullPS[idx] = static_cast<float>(bullPSSum);
SG_AskDOM[idx] = static_cast<float>(totalAskVol);
SG_BidDOM[idx] = static_cast<float>(totalBidVol);
// -------------------------------------------------------------------------
// Shared memory IPC - create on first call, reuse on every tick
// -------------------------------------------------------------------------
void*& pIpcRaw = sc.GetPersistentPointer(1);
if (pIpcRaw == NULL)
{
pIpcRaw = sc.AllocateMemory(sizeof(PMIpcContext));
if (pIpcRaw == NULL)
return;
PMIpcContext* ctx = reinterpret_cast<PMIpcContext*>(pIpcRaw);
ctx->hMapFile = NULL;
ctx->pData = NULL;
}
PMIpcContext* ipc = reinterpret_cast<PMIpcContext*>(pIpcRaw);
if (ipc->hMapFile == NULL)
{
ipc->hMapFile = CreateFileMappingW(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
static_cast<DWORD>(sizeof(PMSharedData)),
PM_SHM_NAME);
if (ipc->hMapFile != NULL)
{
ipc->pData = reinterpret_cast<PMSharedData*>(
MapViewOfFile(ipc->hMapFile, FILE_MAP_WRITE, 0, 0, sizeof(PMSharedData)));
if (ipc->pData == NULL)
{
CloseHandle(ipc->hMapFile);
ipc->hMapFile = NULL;
}
else
{
memset(ipc->pData, 0, sizeof(PMSharedData));
}
}
}
if (ipc->pData != NULL)
{
PMSharedData* d = ipc->pData;
InterlockedIncrement(&d->sequence); // write start (odd)
d->col0Red = static_cast<float>(execBidVol);
d->col0Blue = static_cast<float>(execAskVol);
d->col1Red = static_cast<float>(bearPSSum);
d->col1Blue = static_cast<float>(bullPSSum);
d->col2Red = static_cast<float>(totalAskVol);
d->col2Blue = static_cast<float>(totalBidVol);
d->tickCount = GetTickCount();
InterlockedIncrement(&d->sequence); // write done (even)
}
// -------------------------------------------------------------------------
// Debug log
// -------------------------------------------------------------------------
if (debugLog)
{
SCString msg;
msg.Format(
"PMJS | ExecAsk=%.0f ExecBid=%.0f | BearPS=%.1f BullPS=%.1f | "
"AskDOM=%.0f BidDOM=%.0f | IPC=%s",
execAskVol,
execBidVol,
bearPSSum,
bullPSSum,
totalAskVol,
totalBidVol,
(ipc->pData != NULL) ? "OK" : "FAIL");
sc.AddMessageToLog(msg, 0);
}
}