Repository navigation
Expand file tree
/
Copy pathFlipperStudies.cpp
More file actions
806 lines (693 loc) · 27.5 KB
/
Copy pathFlipperStudies.cpp
File metadata and controls
806 lines (693 loc) · 27.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
// The top of every source code file must include this line
#include "sierrachart.h"
SCDLLName("FlipperStudies")
SCSFExport scsf_TheFlipper(SCStudyInterfaceRef sc)
{
// Persistent variables
float& PrevDelta = sc.GetPersistentFloat(0);
int& PotentialBearishIndex = sc.GetPersistentInt(1);
int& PotentialBullishIndex = sc.GetPersistentInt(2);
int& LastProcessedIndex = sc.GetPersistentInt(3);
int& IsStrongFlip = sc.GetPersistentInt(4);
// Input parameters
SCInputRef LookBackPeriod = sc.Input[0];
SCInputRef DeltaFlipThreshold = sc.Input[1];
SCInputRef StrongDeltaFlipFactor = sc.Input[2];
SCInputRef RequireNextBarConfirmation = sc.Input[3];
SCInputRef ConfirmationPointThreshold = sc.Input[4];
// Subgraphs for final signals
SCSubgraphRef BearishMarker = sc.Subgraph[0];
SCSubgraphRef BullishMarker = sc.Subgraph[1];
SCSubgraphRef StrongBearishMarker = sc.Subgraph[2];
SCSubgraphRef StrongBullishMarker = sc.Subgraph[3];
if (sc.SetDefaults)
{
sc.GraphName = "The Flipper";
sc.StudyDescription = "Marks flips with strength detection and optional next bar confirmation.";
sc.AutoLoop = 1;
sc.UpdateAlways = 1;
sc.GraphRegion = 0;
// Input settings
LookBackPeriod.Name = "LookBack Period";
LookBackPeriod.SetInt(15);
DeltaFlipThreshold.Name = "Flip Threshold";
DeltaFlipThreshold.SetFloat(100.0f);
StrongDeltaFlipFactor.Name = "Strong Flip Multiplier";
StrongDeltaFlipFactor.SetFloat(2.5f);
StrongDeltaFlipFactor.SetDescription("Multiple of threshold to consider as strong flip (0 = disabled)");
RequireNextBarConfirmation.Name = "Require Next Bar Confirmation";
RequireNextBarConfirmation.SetYesNo(0);
ConfirmationPointThreshold.Name = "Confirmation Points Required";
ConfirmationPointThreshold.SetFloat(0.0f);
BearishMarker.Name = "Bearish Marker";
BearishMarker.DrawStyle = DRAWSTYLE_POINT_ON_HIGH;
BearishMarker.PrimaryColor = RGB(255, 0, 0);
BearishMarker.LineWidth = 5;
BearishMarker.DrawZeros = false;
BullishMarker.Name = "Bullish Marker";
BullishMarker.DrawStyle = DRAWSTYLE_POINT_ON_LOW;
BullishMarker.PrimaryColor = RGB(0, 255, 0);
BullishMarker.LineWidth = 5;
BullishMarker.DrawZeros = false;
StrongBearishMarker.Name = "Strong Bearish Marker";
StrongBearishMarker.DrawStyle = DRAWSTYLE_POINT_ON_HIGH;
StrongBearishMarker.PrimaryColor = RGB(255, 128, 0); // Orange
StrongBearishMarker.LineWidth = 6;
StrongBearishMarker.DrawZeros = false;
StrongBullishMarker.Name = "Strong Bullish Marker";
StrongBullishMarker.DrawStyle = DRAWSTYLE_POINT_ON_LOW;
StrongBullishMarker.PrimaryColor = RGB(0, 128, 255); // Cyan-blue
StrongBullishMarker.LineWidth = 6;
StrongBullishMarker.DrawZeros = false;
// Initialize persistent variables
PrevDelta = 0.0f;
PotentialBearishIndex = -1;
PotentialBullishIndex = -1;
LastProcessedIndex = -1;
IsStrongFlip = 0;
return;
}
// Calculate the current bar's delta (ask volume minus bid volume).
float currentDelta = sc.AskVolume[sc.Index] - sc.BidVolume[sc.Index];
float deltaChange = currentDelta - PrevDelta;
// On the first bar, initialize PrevDelta and exit.
if (sc.Index == 0)
{
PrevDelta = currentDelta;
LastProcessedIndex = 0;
return;
}
// Get bar status
int barStatus = sc.GetBarHasClosedStatus(sc.Index);
bool isBarClosed = (barStatus == BHCS_BAR_HAS_CLOSED);
// Skip processing if bar hasn't closed
if (!isBarClosed) {
return;
}
// Get confirmation parameters
float pointThreshold = ConfirmationPointThreshold.GetFloat();
/* ---------- CONFIRMATION LOGIC ---------- */
if (RequireNextBarConfirmation.GetYesNo() && sc.Index > LastProcessedIndex && isBarClosed)
{
// Check for bearish signal confirmation
if (PotentialBearishIndex >= 0 && PotentialBearishIndex < sc.Index)
{
// Confirm if current bar's close is lower than the signal bar's close
if (sc.Close[sc.Index] < (sc.Close[PotentialBearishIndex] - pointThreshold))
{
// Confirmed - display the proper marker
if (IsStrongFlip == 1)
{
StrongBearishMarker[PotentialBearishIndex] = sc.High[PotentialBearishIndex];
}
else
{
BearishMarker[PotentialBearishIndex] = sc.High[PotentialBearishIndex];
}
}
// Reset potential signal
PotentialBearishIndex = -1;
IsStrongFlip = 0;
}
// Check for bullish signal confirmation
if (PotentialBullishIndex >= 0 && PotentialBullishIndex < sc.Index)
{
// Confirm if current bar's close is higher than the signal bar's close
if (sc.Close[sc.Index] > (sc.Close[PotentialBullishIndex] + pointThreshold))
{
// Confirmed - display the proper marker
if (IsStrongFlip == 1)
{
StrongBullishMarker[PotentialBullishIndex] = sc.Low[PotentialBullishIndex];
}
else
{
BullishMarker[PotentialBullishIndex] = sc.Low[PotentialBullishIndex];
}
}
// Reset potential signal
PotentialBullishIndex = -1;
IsStrongFlip = 0;
}
LastProcessedIndex = sc.Index;
}
/* ---------- END CONFIRMATION LOGIC ---------- */
/* FLIP LOGIC */
float flipThreshold = DeltaFlipThreshold.GetFloat();
// Calculate strong flip threshold
float strongFlipThreshold = flipThreshold * StrongDeltaFlipFactor.GetFloat();
bool isStrongFlip = (fabs(deltaChange) >= strongFlipThreshold && StrongDeltaFlipFactor.GetFloat() > 0);
// Only proceed if the change in delta exceeds the flip threshold.
if (fabs(deltaChange) < flipThreshold)
{
PrevDelta = currentDelta;
return;
}
/* END FLIP LOGIC */
// Get the highest high and lowest low over the LookBackPeriod.
int lookBack = LookBackPeriod.GetInt();
float highestValue = sc.GetHighest(sc.BaseDataIn[SC_HIGH], lookBack);
float lowestValue = sc.GetLowest(sc.BaseDataIn[SC_LOW], lookBack);
bool isExtremeHigh = (sc.High[sc.Index] >= highestValue);
bool isExtremeLow = (sc.Low[sc.Index] <= lowestValue);
// Check for a bearish or bullish delta flip.
if (PrevDelta > 0 && currentDelta < 0 && isExtremeHigh)
{
if (RequireNextBarConfirmation.GetYesNo())
{
// Store potential signal info
PotentialBearishIndex = sc.Index;
IsStrongFlip = isStrongFlip ? 1 : 0;
}
else
{
// No confirmation required
if (isStrongFlip)
{
StrongBearishMarker[sc.Index] = sc.High[sc.Index];
}
else
{
BearishMarker[sc.Index] = sc.High[sc.Index];
}
}
}
else if (PrevDelta < 0 && currentDelta > 0 && isExtremeLow)
{
if (RequireNextBarConfirmation.GetYesNo())
{
// Store potential signal info
PotentialBullishIndex = sc.Index;
IsStrongFlip = isStrongFlip ? 1 : 0;
}
else
{
// No confirmation required
if (isStrongFlip)
{
StrongBullishMarker[sc.Index] = sc.Low[sc.Index];
}
else
{
BullishMarker[sc.Index] = sc.Low[sc.Index];
}
}
}
// Update PrevDelta for the next bar.
PrevDelta = currentDelta;
}
SCSFExport scsf_DeltaColoredCandles(SCStudyInterfaceRef sc)
{
// Input parameters
SCInputRef PositiveDeltaThreshold = sc.Input[0];
SCInputRef NegativeDeltaThreshold = sc.Input[1];
SCInputRef UseAbsoluteValue = sc.Input[2];
SCInputRef CVDStudyID = sc.Input[3];
SCInputRef RSIPeriod = sc.Input[4];
// Main subgraphs for candle data
SCSubgraphRef OpenGraph = sc.Subgraph[0];
SCSubgraphRef HighGraph = sc.Subgraph[1];
SCSubgraphRef LowGraph = sc.Subgraph[2];
SCSubgraphRef CloseGraph = sc.Subgraph[3];
SCSubgraphRef RSIGraph = sc.Subgraph[4];
// Color subgraphs
SCSubgraphRef BuyMaxColorGraph = sc.Subgraph[5];
SCSubgraphRef BuyMedColorGraph = sc.Subgraph[6];
SCSubgraphRef BuyMinColorGraph = sc.Subgraph[7];
SCSubgraphRef SellMaxColorGraph = sc.Subgraph[8];
SCSubgraphRef SellMedColorGraph = sc.Subgraph[9];
SCSubgraphRef SellMinColorGraph = sc.Subgraph[10];
SCSubgraphRef NeutralUpColorGraph = sc.Subgraph[11];
SCSubgraphRef NeutralDownColorGraph = sc.Subgraph[12];
SCSubgraphRef PositiveDeltaColorGraph = sc.Subgraph[13];
SCSubgraphRef NegativeDeltaColorGraph = sc.Subgraph[14];
if (sc.SetDefaults)
{
sc.GraphName = "Delta Candles";
sc.StudyDescription = "Colors candles based on delta thresholds.";
sc.AutoLoop = 1;
sc.GraphRegion = 0;
// Input settings
PositiveDeltaThreshold.Name = "Positive Delta Threshold";
PositiveDeltaThreshold.SetFloat(150.0f);
PositiveDeltaThreshold.SetDescription("Bar changes color when delta exceeds this value");
NegativeDeltaThreshold.Name = "Negative Delta Threshold";
NegativeDeltaThreshold.SetFloat(-150.0f);
NegativeDeltaThreshold.SetDescription("Bar changes color when delta is below this value");
UseAbsoluteValue.Name = "Use Absolute Value for Both Thresholds";
UseAbsoluteValue.SetYesNo(0);
UseAbsoluteValue.SetDescription("If Yes, Negative Threshold becomes absolute value");
CVDStudyID.Name = "CVD Study ID";
CVDStudyID.SetStudyID(7);
CVDStudyID.SetDescription("Set to the Study ID of the CVD study");
// Setup color subgraphs for RSI
BuyMaxColorGraph.Name = "Buy Max Color";
BuyMaxColorGraph.DrawStyle = DRAWSTYLE_HIDDEN;
BuyMaxColorGraph.PrimaryColor = RGB(64, 224, 208);
BuyMedColorGraph.Name = "Buy Med Color";
BuyMedColorGraph.DrawStyle = DRAWSTYLE_HIDDEN;
BuyMedColorGraph.PrimaryColor = RGB(0, 128, 128);
BuyMinColorGraph.Name = "Buy Min Color";
BuyMinColorGraph.DrawStyle = DRAWSTYLE_HIDDEN;
BuyMinColorGraph.PrimaryColor = RGB(0, 64, 64);
SellMaxColorGraph.Name = "Sell Max Color";
SellMaxColorGraph.DrawStyle = DRAWSTYLE_HIDDEN;
SellMaxColorGraph.PrimaryColor = RGB(255, 181, 218);
SellMedColorGraph.Name = "Sell Med Color";
SellMedColorGraph.DrawStyle = DRAWSTYLE_HIDDEN;
SellMedColorGraph.PrimaryColor = RGB(255, 0, 0);
SellMinColorGraph.Name = "Sell Min Color";
SellMinColorGraph.DrawStyle = DRAWSTYLE_HIDDEN;
SellMinColorGraph.PrimaryColor = RGB(128, 0, 0);
NeutralUpColorGraph.Name = "Neutral Up Color";
NeutralUpColorGraph.DrawStyle = DRAWSTYLE_HIDDEN;
NeutralUpColorGraph.PrimaryColor = RGB(50, 54, 69);
NeutralDownColorGraph.Name = "Neutral Down Color";
NeutralDownColorGraph.DrawStyle = DRAWSTYLE_HIDDEN;
NeutralDownColorGraph.PrimaryColor = RGB(67, 73, 84);
PositiveDeltaColorGraph.Name = "Positive Delta Color";
PositiveDeltaColorGraph.DrawStyle = DRAWSTYLE_HIDDEN;
PositiveDeltaColorGraph.PrimaryColor = RGB(120, 200, 50);
NegativeDeltaColorGraph.Name = "Negative Delta Color";
NegativeDeltaColorGraph.DrawStyle = DRAWSTYLE_HIDDEN;
NegativeDeltaColorGraph.PrimaryColor = RGB(130, 50, 200);
RSIPeriod.Name = "RSI Period";
RSIPeriod.SetInt(24);
RSIPeriod.SetIntLimits(1, 100);
RSIPeriod.SetDescription("Period for RSI calculation");
// Set graph type to OHLC Bar
sc.GraphDrawType = GDT_OHLCBAR;
// Set to use data from main price graph
sc.ValueFormat = VALUEFORMAT_INHERITED;
return;
}
// Copy price data to our subgraphs
OpenGraph[sc.Index] = sc.Open[sc.Index];
HighGraph[sc.Index] = sc.High[sc.Index];
LowGraph[sc.Index] = sc.Low[sc.Index];
CloseGraph[sc.Index] = sc.Close[sc.Index];
// Default color setup - use neutral colors based on bar direction
bool isUpBar = sc.Close[sc.Index] >= sc.Open[sc.Index];
COLORREF barColor;
if (isUpBar)
{
barColor = NeutralUpColorGraph.PrimaryColor;
}
else
{
barColor = NeutralDownColorGraph.PrimaryColor;
}
// Get input values for delta thresholds
float posThreshold = PositiveDeltaThreshold.GetFloat();
float negThreshold = NegativeDeltaThreshold.GetFloat();
bool useAbsValue = UseAbsoluteValue.GetYesNo();
// If using absolute value, adjust the negative threshold
if (useAbsValue)
{
negThreshold = -fabs(posThreshold);
}
// Calculate delta (ask volume minus bid volume)
float currentDelta = sc.AskVolume[sc.Index] - sc.BidVolume[sc.Index];
// Flag to track if delta coloring should be applied
bool applyDeltaColor = false;
COLORREF deltaColor = barColor;
// Check if delta exceeds thresholds
if (currentDelta >= posThreshold)
{
// Positive delta above threshold
applyDeltaColor = true;
deltaColor = PositiveDeltaColorGraph.PrimaryColor;
}
else if (currentDelta <= negThreshold)
{
// Negative delta below threshold
applyDeltaColor = true;
deltaColor = NegativeDeltaColorGraph.PrimaryColor;
}
// Flag to track if RSI coloring should be applied
bool applyRSIColor = false;
COLORREF rsiColor = barColor;
// Apply RSI-based coloring
// Get CVD study ID
int cvdStudyID = CVDStudyID.GetStudyID();
if (cvdStudyID > 0)
{
// Get CVD values from referenced study
SCFloatArray cvdArray;
sc.GetStudyArrayUsingID(cvdStudyID, 0, cvdArray);
if (cvdArray.GetArraySize() > sc.Index)
{
// Calculate RSI on CVD data
sc.RSI(cvdArray, RSIGraph, MOVAVGTYPE_EXPONENTIAL, RSIPeriod.GetInt());
float currentRSI = RSIGraph[sc.Index];
// Define static thresholds
const float BUY_MAX_THRESHOLD = 80.0f;
const float BUY_MED_THRESHOLD = 75.0f;
const float BUY_MIN_THRESHOLD = 70.0f;
const float SELL_MIN_THRESHOLD = 30.0f;
const float SELL_MED_THRESHOLD = 25.0f;
const float SELL_MAX_THRESHOLD = 20.0f;
// For buy conditions (RSI above thresholds)
if (currentRSI >= BUY_MAX_THRESHOLD)
{
// Max buy signal (RSI >= 80)
applyRSIColor = true;
rsiColor = BuyMaxColorGraph.PrimaryColor;
}
else if (currentRSI >= BUY_MED_THRESHOLD)
{
// Medium buy signal (RSI >= 75)
applyRSIColor = true;
rsiColor = BuyMedColorGraph.PrimaryColor;
}
else if (currentRSI >= BUY_MIN_THRESHOLD)
{
// Minimum buy signal (RSI >= 70)
applyRSIColor = true;
rsiColor = BuyMinColorGraph.PrimaryColor;
}
// For sell conditions (RSI below thresholds)
else if (currentRSI <= SELL_MAX_THRESHOLD)
{
// Max sell signal (RSI <= 20)
applyRSIColor = true;
rsiColor = SellMaxColorGraph.PrimaryColor;
}
else if (currentRSI <= SELL_MED_THRESHOLD)
{
// Medium sell signal (RSI <= 25)
applyRSIColor = true;
rsiColor = SellMedColorGraph.PrimaryColor;
}
else if (currentRSI <= SELL_MIN_THRESHOLD)
{
// Minimum sell signal (RSI <= 30)
applyRSIColor = true;
rsiColor = SellMinColorGraph.PrimaryColor;
}
}
else if (sc.Index == 0)
{
sc.AddMessageToLog("CVD Study data not available", 1);
}
}
else if (sc.Index == 0)
{
sc.AddMessageToLog("Please set a valid CVD Study ID for RSI coloring", 1);
}
// Determine final color - always prioritize delta coloring
if (applyDeltaColor)
{
barColor = deltaColor;
}
else if (applyRSIColor)
{
barColor = rsiColor;
}
// Set the color for all data points
OpenGraph.DataColor[sc.Index] = barColor;
HighGraph.DataColor[sc.Index] = barColor;
LowGraph.DataColor[sc.Index] = barColor;
CloseGraph.DataColor[sc.Index] = barColor;
}
SCSFExport scsf_DynamicFlipper(SCStudyInterfaceRef sc)
{
// Persistent variables
float& PrevDelta = sc.GetPersistentFloat(0);
int& PotentialBearishIndex = sc.GetPersistentInt(1);
int& PotentialBullishIndex = sc.GetPersistentInt(2);
int& LastProcessedIndex = sc.GetPersistentInt(3);
int& IsStrongFlip = sc.GetPersistentInt(4);
float& SignalBarRSI = sc.GetPersistentFloat(5);
float& PrevFlipThreshold = sc.GetPersistentFloat(6);
int& StudyVersion = sc.GetPersistentInt(7);
// Input parameters
SCInputRef DeltaFlipThreshold = sc.Input[0];
SCInputRef StrongDeltaFlipFactor = sc.Input[1];
SCInputRef RequireNextBarConfirmation = sc.Input[3];
SCInputRef ConfirmationPointThreshold = sc.Input[4];
SCInputRef CVDStudyID = sc.Input[5];
SCInputRef LookbackPeriod = sc.Input[6];
SCInputRef ThresholdMultiplier = sc.Input[7];
SCInputRef RSIPeriod = sc.Input[8];
// Subgraphs
SCSubgraphRef BearishMarker = sc.Subgraph[0];
SCSubgraphRef BullishMarker = sc.Subgraph[1];
SCSubgraphRef StrongBearishMarker = sc.Subgraph[2];
SCSubgraphRef StrongBullishMarker = sc.Subgraph[3];
SCSubgraphRef RSISubgraph = sc.Subgraph[4];
SCSubgraphRef RSIUpperLine = sc.Subgraph[5];
SCSubgraphRef RSILowerLine = sc.Subgraph[6];
SCSubgraphRef DeltaStorage = sc.Subgraph[7];
if (sc.SetDefaults)
{
sc.GraphName = "Dynamic Flipper";
sc.StudyDescription = "Marks flips with strength detection, optional next bar confirmation, and dynamic threshold.";
sc.AutoLoop = 1;
sc.UpdateAlways = 1;
sc.GraphRegion = 0;
DeltaFlipThreshold.Name = "Default Flip Threshold";
DeltaFlipThreshold.SetFloat(100.0f);
StrongDeltaFlipFactor.Name = "Strong Flip Multiplier";
StrongDeltaFlipFactor.SetFloat(2.0f);
RequireNextBarConfirmation.Name = "Require Next Bar Confirmation";
RequireNextBarConfirmation.SetYesNo(1);
ConfirmationPointThreshold.Name = "Confirmation Points Required";
ConfirmationPointThreshold.SetFloat(2.0f);
CVDStudyID.Name = "CVD Study ID";
CVDStudyID.SetStudyID(7);
LookbackPeriod.Name = "Lookback Period for Dynamic Threshold";
LookbackPeriod.SetInt(100);
LookbackPeriod.SetIntLimits(5, 100);
ThresholdMultiplier.Name = "Dynamic Threshold Multiplier";
ThresholdMultiplier.SetFloat(1.5f);
ThresholdMultiplier.SetFloatLimits(0.5f, 10.0f);
BearishMarker.Name = "Bearish Marker";
BearishMarker.DrawStyle = DRAWSTYLE_POINT_ON_HIGH;
BearishMarker.PrimaryColor = RGB(255, 0, 0);
BearishMarker.LineWidth = 5;
BearishMarker.DrawZeros = false;
BullishMarker.Name = "Bullish Marker";
BullishMarker.DrawStyle = DRAWSTYLE_POINT_ON_LOW;
BullishMarker.PrimaryColor = RGB(0, 255, 0);
BullishMarker.LineWidth = 5;
BullishMarker.DrawZeros = false;
StrongBearishMarker.Name = "Strong Bearish Marker";
StrongBearishMarker.DrawStyle = DRAWSTYLE_POINT_ON_HIGH;
StrongBearishMarker.PrimaryColor = RGB(255, 128, 0);
StrongBearishMarker.LineWidth = 6;
StrongBearishMarker.DrawZeros = false;
StrongBullishMarker.Name = "Strong Bullish Marker";
StrongBullishMarker.DrawStyle = DRAWSTYLE_POINT_ON_LOW;
StrongBullishMarker.PrimaryColor = RGB(0, 128, 255);
StrongBullishMarker.LineWidth = 6;
StrongBullishMarker.DrawZeros = false;
DeltaStorage.Name = "Delta Storage";
DeltaStorage.DrawStyle = DRAWSTYLE_IGNORE;
DeltaStorage.DrawZeros = false;
RSIPeriod.Name = "RSI Period";
RSIPeriod.SetInt(24);
RSIPeriod.SetIntLimits(1, 100);
RSIPeriod.SetDescription("Period for RSI calculation");
// Initialize persistent variables
PrevDelta = 0.0f;
PotentialBearishIndex = -1;
PotentialBullishIndex = -1;
LastProcessedIndex = -1;
IsStrongFlip = 0;
SignalBarRSI = 0.0f;
PrevFlipThreshold = 0.0f;
StudyVersion = 1;
return;
}
// Calculate and store the current bar's delta
float currentDelta = sc.AskVolume[sc.Index] - sc.BidVolume[sc.Index];
DeltaStorage[sc.Index] = currentDelta;
float deltaChange = currentDelta - PrevDelta;
// --- Dynamic Flip Threshold ---
float flipThreshold = DeltaFlipThreshold.GetFloat();
int lookback = LookbackPeriod.GetInt();
bool isDynamic = false;
float stdDev = 0.0f;
if (lookback > 0 && sc.Index >= lookback)
{
// Calculate mean of deltas
float sum = 0.0f;
for (int i = 0; i < lookback; ++i)
{
int arrayIndex = sc.Index - i;
if (arrayIndex >= 0)
{
sum += DeltaStorage[arrayIndex];
}
}
float mean = sum / lookback;
// Calculate variance
float sumSquaredDiff = 0.0f;
for (int i = 0; i < lookback; ++i)
{
int arrayIndex = sc.Index - i;
if (arrayIndex >= 0)
{
float diff = DeltaStorage[arrayIndex] - mean;
sumSquaredDiff += diff * diff;
}
}
float variance = sumSquaredDiff / lookback;
// Calculate standard deviation
stdDev = sqrtf(variance);
// Set dynamic threshold
if (stdDev > 0.0f)
{
flipThreshold = stdDev * ThresholdMultiplier.GetFloat();
isDynamic = true;
// Ensure threshold isn't too small
if (flipThreshold < DeltaFlipThreshold.GetFloat() * 0.5f)
{
flipThreshold = DeltaFlipThreshold.GetFloat();
isDynamic = false;
}
}
// Debug logging
SCString debugMessage;
debugMessage.Format("Index %d: FlipThreshold=%.2f, StdDev=%.2f, IsDynamic=%s",
sc.Index, flipThreshold, stdDev, isDynamic ? "Yes" : "No");
sc.AddMessageToLog(debugMessage, 0);
}
PrevFlipThreshold = flipThreshold;
// --- Rest of the code ---
int cvdStudyID = CVDStudyID.GetStudyID();
float currentRSI = 50.0f;
float rsiUpper = 70.0f;
float rsiLower = 30.0f;
if (cvdStudyID != 0)
{
SCFloatArray cvdArray;
sc.GetStudyArrayUsingID(cvdStudyID, 0, cvdArray);
if (cvdArray.GetArraySize() > 0)
{
sc.RSI(cvdArray, RSISubgraph, MOVAVGTYPE_EXPONENTIAL, RSIPeriod.GetInt());
currentRSI = RSISubgraph[sc.Index];
RSIUpperLine[sc.Index] = rsiUpper;
RSILowerLine[sc.Index] = rsiLower;
}
}
if (sc.Index == 0)
{
PrevDelta = currentDelta;
LastProcessedIndex = 0;
return;
}
if (sc.GetBarHasClosedStatus(sc.Index) == BHCS_BAR_HAS_NOT_CLOSED)
{
return;
}
float pointThreshold = ConfirmationPointThreshold.GetFloat();
// Confirmation logic
if (RequireNextBarConfirmation.GetYesNo() && sc.Index > LastProcessedIndex)
{
if (PotentialBearishIndex >= 0 && PotentialBearishIndex < sc.Index)
{
bool rsiConditionMet = true;
if (cvdStudyID != 0)
{
rsiConditionMet = (SignalBarRSI >= rsiUpper);
}
if (rsiConditionMet && sc.Close[sc.Index] < (sc.Close[PotentialBearishIndex] - pointThreshold))
{
if (IsStrongFlip == 1)
{
StrongBearishMarker[sc.Index] = sc.High[sc.Index];
}
else
{
BearishMarker[sc.Index] = sc.High[sc.Index];
}
}
PotentialBearishIndex = -1;
IsStrongFlip = 0;
SignalBarRSI = 0.0f;
}
if (PotentialBullishIndex >= 0 && PotentialBullishIndex < sc.Index)
{
bool rsiConditionMet = true;
if (cvdStudyID != 0)
{
rsiConditionMet = (SignalBarRSI <= rsiLower);
}
if (rsiConditionMet && sc.Close[sc.Index] > (sc.Close[PotentialBullishIndex] + pointThreshold))
{
if (IsStrongFlip == 1)
{
StrongBullishMarker[sc.Index] = sc.Low[sc.Index];
}
else
{
BullishMarker[sc.Index] = sc.Low[sc.Index];
}
}
PotentialBullishIndex = -1;
IsStrongFlip = 0;
SignalBarRSI = 0.0f;
}
LastProcessedIndex = sc.Index;
}
// FLIP LOGIC
float strongFlipThreshold = flipThreshold * StrongDeltaFlipFactor.GetFloat();
bool isStrongFlip = (fabs(deltaChange) >= strongFlipThreshold && StrongDeltaFlipFactor.GetFloat() > 0);
if (fabs(deltaChange) < flipThreshold)
{
PrevDelta = currentDelta;
return;
}
bool bearishRSICondition = true;
bool bullishRSICondition = true;
if (cvdStudyID != 0)
{
bearishRSICondition = (currentRSI >= rsiUpper);
bullishRSICondition = (currentRSI <= rsiLower);
}
bool bearishDeltaCondition = (PrevDelta > 0 && currentDelta < 0);
bool bullishDeltaCondition = (PrevDelta < 0 && currentDelta > 0);
if (bearishDeltaCondition && bearishRSICondition)
{
if (RequireNextBarConfirmation.GetYesNo())
{
PotentialBearishIndex = sc.Index;
IsStrongFlip = isStrongFlip ? 1 : 0;
SignalBarRSI = currentRSI;
}
else
{
if (isStrongFlip)
{
StrongBearishMarker[sc.Index] = sc.High[sc.Index];
}
else
{
BearishMarker[sc.Index] = sc.High[sc.Index];
}
}
}
else if (bullishDeltaCondition && bullishRSICondition)
{
if (RequireNextBarConfirmation.GetYesNo())
{
PotentialBullishIndex = sc.Index;
IsStrongFlip = isStrongFlip ? 1 : 0;
SignalBarRSI = currentRSI;
}
else
{
if (isStrongFlip)
{
StrongBullishMarker[sc.Index] = sc.Low[sc.Index];
}
else
{
BullishMarker[sc.Index] = sc.Low[sc.Index];
}
}
}
PrevDelta = currentDelta;
}