-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfloor_trader_pivots_ex.txt
2018 lines (1746 loc) · 60.2 KB
/
floor_trader_pivots_ex.txt
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
{
This study is an indicator used to plot floor trader pivots for Daily pivots,
Weekly pivots, Monthly pivots, Quarterly pivots, or Yearly pivots. The
'PivotInterval' input is used to specify which pivot interval to use.
The 'PivotCalcType' input is used to specify the type of pivot calculations to
use. The indicator can calculate classic pivots, traditional pivots, or
Camarilla pivots.
There are several inputs that control which pivot levels to plot, which, if any,
text labels to display, pivot colors and the color transparency value to use.
At the end of the session, pivot levels are calculated for tomorrow and are
shown. The values for tomorrow are plotted in RadarScreen.
我修改了
1、增加了报警代码,r1,s1以及pivot point不报警
2、15分钟的时候显示了R4和S4
}
#region Namespaces
using tsdata.marketdata;
using elsystem;
using tsdata.common;
using elsystem.drawing;
using elsystem.drawingobjects;
using elsystem.collections;
#endregion
inputs:
int PivotCalcType( 1 ) [
DisplayName = "PivotCalcType",
ToolTip = "Pivot Calculation Type. Enter 1 for Classic pivots; enter 2 for Traditional pivots; enter 3 for Camarilla pivots."],
int PivotInterval( 1 ) [
DisplayName = "PivotInterval",
ToolTip = "Enter 1 for Daily pivots; enter 2 for Weekly pivots, enter 3 for Monthly pivots, enter 4 for Quarterly pivots, enter 5 for Yearly pivots."],
int ShowPivotLabels( 1 ) [
DisplayName = "ShowPivotLabels",
ToolTip = "Enter 1 to show text labels for the pivot levels. Enter 0 to not show text labels."],
int ShowCurrentPeriodLabelsOnly( 1 ) [
DisplayName = "ShowCurrentPeriodLabelsOnly",
ToolTip = "Enter 1 to show text labels for the current period only. Enter 0 to show text labels for all pivot levels."],
int ShowPriceLevelsInLabels( 1 ) [
DisplayName = "ShowPriceLevelsInLabels",
ToolTip = "Enter 1 to include the pivot price values in the text labels. Enter 0 to not include pivot price values."],
int ShowPivotIntervalInLabels( 1 ) [
DisplayName = "ShowPivotIntervalInLabels",
ToolTip = "Enter 1 to show the pivot interval in the text labels. Enter 0 to not show the pivot interval."],
double PercentTransparency( 0 ) [
DisplayName = "PercentTransparency",
ToolTip = "Percentage Transparency. Enter the desired percentage transparency of the text labels (0 = solid, 100 = clear)."],
int R5Color( Red ) [
DisplayName = "R5Color",
ToolTip = "Enter the color to use for the R5 pivot plot and associated text label."],
int R4Color( MyColors( "OrangeRed" ) ) [
DisplayName = "R4Color",
ToolTip = "Enter the color to use for the R4 pivot plot and associated text label."],
int R3Color( MyColors( "Tomato" ) ) [
DisplayName = "R3Color",
ToolTip = "Enter the color to use for the R3 pivot plot and associated text label."],
int R2Color( MyColors( "DarkOrange" ) ) [
DisplayName = "R2Color",
ToolTip = "Enter the color to use for the R2 pivot plot and associated text label."],
int R1Color( MyColors( "Orange" ) ) [
DisplayName = "R1Color",
ToolTip = "Enter the color to use for the R1 pivot plot and associated text label."],
int PivotColor( MyColors( "NavajoWhite" ) ) [
DisplayName = "PivotColor",
ToolTip = "Enter the color to use for the pivot point plot and associated text label."],
int S1Color( MyColors( "PaleGreen" ) ) [
DisplayName = "S1Color",
ToolTip = "Enter the color to use for the S1 pivot plot and associated text label."],
int S2Color( MyColors( "SpringGreen" ) ) [
DisplayName = "S2Color",
ToolTip = "Enter the color to use for the S2 pivot plot and associated text label."],
int S3Color( MyColors( "LimeGreen" ) ) [
DisplayName = "S3Color",
ToolTip = "Enter the color to use for the S3 pivot plot and associated text label."],
int S4Color( MyColors( "Lime" ) ) [
DisplayName = "S4Color",
ToolTip = "Enter the color to use for the S4 pivot plot and associated text label."],
int S5Color( Green ) [
DisplayName = "S5Color",
ToolTip = "Enter the color to use for the S5 pivot plot and associated text label."],
int ShowR5( 1 ) [
DisplayName = "ShowR5",
ToolTip = "Enter 1 to plot the R5 pivot level and draw the associated text label (if text labels are set to show); enter 0 to not plot the pivot value."],
int ShowR4( 1 ) [
DisplayName = "ShowR4",
ToolTip = "Enter 1 to plot the R4 pivot level and draw the associated text label (if text labels are set to show); enter 0 to not plot the pivot value."],
int ShowR3( 1 ) [
DisplayName = "ShowR3",
ToolTip = "Enter 1 to plot the R3 pivot level and draw the associated text label (if text labels are set to show); enter 0 to not plot the pivot value."],
int ShowR2( 1 ) [
DisplayName = "ShowR2",
ToolTip = "Enter 1 to plot the R2 pivot level and draw the associated text label (if text labels are set to show); enter 0 to not plot the pivot value."],
int ShowR1( 1 ) [
DisplayName = "ShowR1",
ToolTip = "Enter 1 to plot the R1 pivot level and draw the associated text label (if text labels are set to show); enter 0 to not plot the pivot value."],
int ShowPP( 1 ) [
DisplayName = "ShowPP",
ToolTip = "Enter 1 to plot the pivot point level and draw the associated text label (if text labels are set to show); enter 0 to not plot the pivot value."],
int ShowS1( 1 ) [
DisplayName = "ShowS1",
ToolTip = "Enter 1 to plot the S1 pivot level and draw the associated text label (if text labels are set to show); enter 0 to not plot the pivot value."],
int ShowS2( 1 ) [
DisplayName = "ShowS2",
ToolTip = "Enter 1 to plot the S2 pivot level and draw the associated text label (if text labels are set to show); enter 0 to not plot the pivot value."],
int ShowS3( 1 ) [
DisplayName = "ShowS3",
ToolTip = "Enter 1 to plot the S3 pivot level and draw the associated text label (if text labels are set to show); enter 0 to not plot the pivot value."],
int ShowS4( 1 ) [
DisplayName = "ShowS4",
ToolTip = "Enter 1 to plot the S4 pivot level and draw the associated text label (if text labels are set to show); enter 0 to not plot the pivot value."],
int ShowS5( 1 ) [
DisplayName = "ShowS5",
ToolTip = "Enter 1 to plot the S5 pivot level and draw the associated text label (if text labels are set to show); enter 0 to not plot the pivot value."],
double TextLabelFontSize( 8.0 ) [
DisplayName = "TextLabelFontSize",
ToolTip = "Enter the font size for the text labels on the lines"];
constants:
int DAILY_PIVOTS( 1 ),
int WEEKLY_PIVOTS( 2 ),
int MONTHLY_PIVOTS( 3 ),
int QUARTERLY_PIVOTS( 4 ),
int YEARLY_PIVOTS( 5 ),
int MAX_TEXT_LABELS_ALLOWED( 4000 ),
string TRENDLINE_LEVEL_KEY( "TLLevel" ),
string TRENDLINE_COLOR_KEY( "TLColor" ),
string R5_KEY( "R5" ),
string R4_KEY( "R4" ),
string R3_KEY( "R3" ),
string R2_KEY( "R2" ),
string R1_KEY( "R1" ),
string PP_KEY( "PP" ),
string S1_KEY( "S1" ),
string S2_KEY( "S2" ),
string S3_KEY( "S3" ),
string S4_KEY( "S4" ),
string S5_KEY( "S5" );
variables:
TokenList TokenListOfLevels( NULL ),
Vector CurrentTextLabelVector( NULL ),
Vector AllTextLabelsVector( NULL ),
Vector TrendlineVector( NULL ),
Vector TomorrowTextLabelVector( NULL ),
Dictionary DOColorDict( NULL ),
Dictionary PlotColorDict( NULL ),
PriceSeriesProvider PivotsPSP( NULL ),
QuotesProvider QP1( NULL ),
intrabarpersist bool InputsAreOkay( true ),
intrabarpersist bool UseBNPoint( false ),
intrabarpersist bool RealTime( false ),
intrabarpersist bool InAChart( false ),
intrabarpersist bool AllowTransparentPlots( true ),
intrabarpersist int DecimalPlaces( 0 ),
intrabarpersist int BT( 0 ),
intrabarpersist bool OkToCalc( false ),
intrabarpersist int PSPIndexOffset( 0 ),
intrabarpersist string PivotIntervalString( "" ),
intrabarpersist double S1( 0 ),
intrabarpersist double S2( 0 ),
intrabarpersist double S3( 0 ),
intrabarpersist double S4( 0 ),
intrabarpersist double S5( 0 ),
intrabarpersist double R1( 0 ),
intrabarpersist double R2( 0 ),
intrabarpersist double R3( 0 ),
intrabarpersist double R4( 0 ),
intrabarpersist double R5( 0 ),
intrabarpersist double PP( 0 ),
intrabarpersist double S1Tom( 0 ),
intrabarpersist double S2Tom( 0 ),
intrabarpersist double S3Tom( 0 ),
intrabarpersist double S4Tom( 0 ),
intrabarpersist double S5Tom( 0 ),
intrabarpersist double R1Tom( 0 ),
intrabarpersist double R2Tom( 0 ),
intrabarpersist double R3Tom( 0 ),
intrabarpersist double R4Tom( 0 ),
intrabarpersist double R5Tom( 0 ),
intrabarpersist double PPTom( 0 ),
intrabarpersist int TransparencyValue( 255 ),
intrabarpersist int ReuseIndex( 0 ),
intrabarpersist bool TomorrowsPivotsShowing( true );
method void Init()
begin
BT = BarType;
InAChart = GetAppInfo( aiApplicationType ) = cChart;
ErrorCheckInputValues();
CreateTokenListOfLevels();
{
the value of PercentTransparency, which is a percentage, must be
converted to a value between 0 and 255 for use in the ARGB statements,
below
}
if InAChart = false then
begin
{
in grid applications such as RadarScreen, we will not use any transparency
value so that the values are more easily seen (the colors used for the plots
in both Charting and RadarScreen are set by the inputs, which includes the
PercentTransparency input)
}
TransparencyValue = 255;
end
else
begin
if PercentTransparency < 0 then
TransparencyValue = 255
else if PercentTransparency > 100 then
TransparencyValue = 0
else
TransparencyValue = Round( 0.01 * ( 100 - PercentTransparency ) *
255, 0 ) astype int;
end;
{
now that we have the transparency value, we will set the colors to be used
for the plots, trendlines, and text labels
}
LoadDOColorsIntoDictionary();
LoadPlotColorsIntoDictionary();
CreateTrendlinesForTomorrowsPivots();
CreateTextLabelsForTomorrowsPivots();
{
if the bar interval and pivot interval do not allow for transparent colors,
we will set the 'AllowTransparentPlots' variable to false, otherwise it is
set to true; for example, if the chart interval is Daily and we have
the pivots set for Daily pivots, setting the transparent color will remove
all plots from being seen since we have a new period starting on each bar;
for this case, the 'AllowTransparentPlots' variable is set to false
}
if ( BT = 2 and PivotInterval = DAILY_PIVOTS )
or ( BT = 3 and PivotInterval = WEEKLY_PIVOTS )
or ( BT = 4 and PivotInterval = MONTHLY_PIVOTS )
or ( BT = 0 ) { tick bar or volume bar }
or ( BT > 4 and BT <> 14 ) then { advanced bar }
begin
AllowTransparentPlots = false;
end
else
begin
AllowTransparentPlots = true;
end;
PivotIntervalString = GetPivotIntervalString();
{
When this study is applied to tick bars or advanced bars, we use BNPoint
positioning of text labels. This ensures that the text labels are positioned
on the correct bar even when multiple bars occur in the same second (as can
occur with very short-term tick bars). When positioning time-based bars, on
the other hand, we’ll use DTPoint positioning. This type of positioning can
account for missing bars in the data stream (periods of no trading activity,
as can occur with ‘thin’ issues). Missing bars, of course, cannot occur when
using tick-based charts or advanced bars, since these bar-building mechanisms
do not move along the x-axis until enough trades have occurred to complete a
bar. Thus, the approach illustrated here gives us the best of both worlds
– sub-second positioning and automatic accounting for missing bars.
}
UseBNPoint = BT = 0 or ( BT > 4 and BT <> 14 );
DecimalPlaces = NumDecimals( PriceScale );
CreateAndLoadPSP();
end;
#region DataProviders
{
we will use a PriceSeriesProvider object for the pivot level calculations so
we don't have to rely on intraday data on the chart; this allows us to use
the settlement prices as well as allows the chart to have less data on it
depending on the type of pivots; for example, if Monthly pivots are desired,
an intraday chart would need at least one month of data on the chart
}
method void CreateAndLoadPSP()
begin
PivotsPSP = new PriceSeriesProvider();
PivotsPSP.Symbol = Symbol;
ConfigureDataIntervalAndRange();
{ we don't need volume for this indicator }
PivotsPSP.IncludeVolumeInfo = false;
PivotsPSP.IncludeTicksInfo = false;
PivotsPSP.UseNaturalHours = false;
PivotsPSP.Realtime = true;
PivotsPSP.TimeZone = ConvertTimeZone();
PivotsPSP.StateChanged += PivotsPSP_StateChanged;
PivotsPSP.Updated += PivotsPSP_Updated;
PivotsPSP.LoadProvider();
Value1 = PivotsPSP.Count; { force PriceSeriesProvider to load }
end;
method void PivotsPSP_StateChanged( Object sender, StateChangedEventArgs args )
begin
{ if the PriceSeriesProvider object fails to load, we will throw an exception
to inform the user }
switch ( args.NewState )
begin
case DataState.Failed:
throw Exception.Create( !( "PriceSeriesProvider failed to load." ) );
end;
end;
method void PivotsPSP_Updated( Object sender, PriceSeriesUpdatedEventArgs args )
begin
switch ( args.Reason )
begin
case PriceSeriesUpdateReason.BarClose:
{
if the bar that just closed is the last bar of the session,
we will show the next period pivots; this allows the user
to see the upcoming pivot levels for the next period; for
example, if the chart is a 5-min chart with Daily pivots
applied, when the Daily bar closes for today, this code will
calculate and show the pivots that are calculated for tomorrow
}
if PivotsPSP.LastBarIsSessionClosed then
begin
ShowTomorrowsPivots();
{
after the bar closes at session end, the exchange can update the
settlement prices; we will use a QuotesProvider to detect a change
in DailyClose and update the pivot levels for tomorrow
}
CreateAndLoadQuotesProvider();
end;
default:
{
for other than a session close, we will stop the QuotesProvider; this provider
is used to detect changes to DailyClose quote field so we can update the
pivot values after hours as settlement values arrive; once we have an event
other than bar close, we don't need the QuotesProvider loaded any more since
the main code will run and update the pivot levels to the appropriate values
}
if QP1 <> NULL then
QP1.CloseProvider();
end;
end;
method void ConfigureDataIntervalAndRange()
variables: DateTime tempDT;
begin
{
we will load some additional bars of data so that we have pivot levels on
CurrentBar 1; we will use tempDT and additional bars of data and then use
tempDT as the 'FirstDate' of the PSP Range property; note that adding a
negative time frame (e.g., AddDays( - 10 )), moves the DateTime to an earlier
DateTime by the amount specified
}
tempDT = BarDateTime[MaxBarsBack];
switch ( PivotInterval )
begin
case DAILY_PIVOTS:
PivotsPSP.Interval.SetAsDailyBarChart();
tempDT.AddDays( -10 );
PivotsPSP.Range.FirstDate = tempDT;
case WEEKLY_PIVOTS:
PivotsPSP.Interval.SetAsWeeklyBarChart();
tempDT.AddWeeks( -2 );
PivotsPSP.Range.FirstDate = tempDT;
case MONTHLY_PIVOTS:
PivotsPSP.Interval.SetAsMonthlyBarChart();
tempDT.AddMonths( -2 );
PivotsPSP.Range.FirstDate = tempDT;
case QUARTERLY_PIVOTS:
{ for quarterly pivots, we will use a monthly PSP since that is the
biggest available bar interval }
PivotsPSP.Interval.SetAsMonthlyBarChart();
tempDT.AddMonths( -4 );
PivotsPSP.Range.FirstDate = tempDT;
case YEARLY_PIVOTS:
{ for yearly pivots, we will use a monthly PSP since that is the
biggest available bar interval }
PivotsPSP.Interval.SetAsMonthlyBarChart();
tempDT.AddYears( -2 );
PivotsPSP.Range.FirstDate = tempDT;
end;
end;
{
the data stream TimeZone and the TimeZone used in DataProviders are from
different classes; this method is used to provide the cross-reference between the
two so that the DataProvider TimeZone can be set to the same TimeZone as the
data stream to which this indicator is applied
}
method tsdata.common.TimeZone ConvertTimeZone()
begin
switch ( AnalysisTechnique.DataStreams.DefaultStream.TimeZone )
begin
case elsystem.TimeZone.Exchange:
return tsdata.common.TimeZone.Exchange;
case elsystem.TimeZone.Local:
return tsdata.common.TimeZone.Local;
end;
end;
{
the QuotesProvider is for the quote field 'DailyClose'; this is used to detect
a change to the settlement price after hours; if we get an udpated event, it
means the DailyClose has been updated; we need to update and show tomorrow's
pivots
}
method void CreateAndLoadQuotesProvider()
begin
QP1 = new QuotesProvider();
QP1.Symbol = Symbol;
QP1.Realtime = true;
QP1.TimeZone = ConvertTimeZone();
QP1.Fields += QuoteFields.DailyClose;
QP1.Updated += QP1_Updated;
QP1.LoadProvider();
end;
method void QP1_Updated( Object sender, QuoteUpdatedEventArgs args )
begin
{ see code commentary for the QuotesProvider creation }
ShowTomorrowsPivots();
end;
{ DataProviders region }
#endregion
#region TextLabels
method void SetupNewTextLabels()
variables:
TextLabel tempTextLabel,
string Spacer,
string LabelTextString,
string PivotLevelString,
double tempPivotPrice;
begin
{
if the DrawingObjects collection is NULL (drawing objects are not available
in applications other than Charting) or the user has selected to NOT draw
text labels, just exit this method
}
if DrawingObjects = NULL or ShowPivotLabels <> 1 then
return;
{
if we are showing the current period labels only and we have already created
the labels (i.e., CurrentTextLabelVector <> NULL), we will move the text labels
to the new period and exit since we don't need to create more labels
}
if ShowCurrentPeriodLabelsOnly = 1 and CurrentTextLabelVector <> NULL then
begin
MoveTextLabelsToNewPeriod();
return;
end;
Spacer = Spaces( 1 );
if CurrentTextLabelVector = NULL then
CurrentTextLabelVector = new Vector();
if AllTextLabelsVector = NULL then
AllTextLabelsVector = new Vector();
{
the CurrentTextLabelVector vector holds the text labels for the current pivot
period; we are about to create new labels for the new period, so we need to
clear the prior period text labels out of the Vector and we will add the new
labels to the Vector when we create them below
}
CurrentTextLabelVector.Clear();
if ShowS1 = 1 then
begin
tempTextLabel = GetTextLabelForPivotLevel( S1_KEY );
SetTextLabelColor( tempTextLabel );
CurrentTextLabelVector.push_back( tempTextLabel );
end;
if ShowS2 = 1 then
begin
tempTextLabel = GetTextLabelForPivotLevel( S2_KEY );
SetTextLabelColor( tempTextLabel );
CurrentTextLabelVector.push_back( tempTextLabel );
end;
if ShowS3 = 1 then
begin
tempTextLabel = GetTextLabelForPivotLevel( S3_KEY );
SetTextLabelColor( tempTextLabel );
CurrentTextLabelVector.push_back( tempTextLabel );
end;
if ShowR1 = 1 then
begin
tempTextLabel = GetTextLabelForPivotLevel( R1_KEY );
SetTextLabelColor( tempTextLabel );
CurrentTextLabelVector.push_back( tempTextLabel );
end;
if ShowR2 = 1 then
begin
tempTextLabel = GetTextLabelForPivotLevel( R2_KEY );
SetTextLabelColor( tempTextLabel );
CurrentTextLabelVector.push_back( tempTextLabel );
end;
if ShowR3 = 1 then
begin
tempTextLabel = GetTextLabelForPivotLevel( R3_KEY );
SetTextLabelColor( tempTextLabel );
CurrentTextLabelVector.push_back( tempTextLabel );
end;
{ don't show PP for camarilla pivots }
if ShowPP = 1 and PivotCalcType <> 3 then
begin
tempTextLabel = GetTextLabelForPivotLevel( PP_KEY );
SetTextLabelColor( tempTextLabel );
CurrentTextLabelVector.push_back( tempTextLabel );
end;
{
Levels S4, S5, R4, and R5 apply only to certain pivot types (e.g., traditional,
camarilla pivots, etc.); therefore, we need to ensure they are not zero (i.e.,
they have been calculated) before we create text labels for them
}
if ShowR4 = 1 and R4 <> 0 then
begin
tempTextLabel = GetTextLabelForPivotLevel( R4_KEY );
SetTextLabelColor( tempTextLabel );
CurrentTextLabelVector.push_back( tempTextLabel );
end;
if ShowR5 = 1 and R5 <> 0 then
begin
tempTextLabel = GetTextLabelForPivotLevel( R5_KEY );
SetTextLabelColor( tempTextLabel );
CurrentTextLabelVector.push_back( tempTextLabel );
end;
if ShowS4 = 1 and S4 <> 0 then
begin
tempTextLabel = GetTextLabelForPivotLevel( S4_KEY );
SetTextLabelColor( tempTextLabel );
CurrentTextLabelVector.push_back( tempTextLabel );
end;
if ShowS5 = 1 and S5 <> 0 then
begin
tempTextLabel = GetTextLabelForPivotLevel( S5_KEY );
SetTextLabelColor( tempTextLabel );
CurrentTextLabelVector.push_back( tempTextLabel );
end;
end;
{ this method is called when we need to get a text label for a new period }
method TextLabel GetTextLabelForPivotLevel( string iPivotLevel )
variables: TextLabel tempTextLabel;
begin
{
to prevent getting the warning message when 5,000 text labels are created,
we will check to see what the text label count is; if the count is less than
the value specified by the 'MAX_TEXT_LABELS_ALLOWED' constant, we will create
a new text label and return it from this method; if the count exceeds the
value specified by the 'MAX_TEXT_LABELS_ALLOWED' constant, we will get a text
label created earlier and 'recycle' it to be used for the new period
note that we have no way to know which study or strategy created the text
labels returnd by the DrawingObjects collection; therefore, this method only
tries to ensure that this study does not cause the triggering of the 5,000
text labels warning; note that if the number of text labels on the chart
(from all sources, including manually drawn text labels and text labels drawn
by other studies/strategies) is already greater than the value specified by
the 'MAX_TEXT_LABELS_ALLOWED' constant when this study is placed on the chart,
no text labels will be displayed by this indicator (except for tomorrow's
pivots)
}
if DrawingObjects[ObjectCategory.TextLabel].Count < MAX_TEXT_LABELS_ALLOWED then
begin
tempTextLabel = new TextLabel();
AllTextLabelsVector.push_back( tempTextLabel );
{
to 'show' the text label, the text label is added to the DrawingObjects
collection; if you want to remove the text label, you can use the Delete
method of the DrawingObjects class; DrawingObjects collection is not
available in RadarScreen (it is NULL), so we only add the TextLabel to
the collection if the DrawingObjects collection is not NULL
}
if DrawingObjects <> NULL then
DrawingObjects.Add( tempTextLabel );
end
else
begin
tempTextLabel = GetTextLabelFromVector();
end;
ConfigureTextLabel( tempTextLabel, iPivotLevel );
return tempTextLabel;
end;
method void ConfigureTextLabel( TextLabel tempTextLabel, string iPivotLevel )
variables: double Price;
begin
if tempTextLabel = NULL then
return;
tempTextLabel.Tag = iPivotLevel;
Price = GetPriceForPivotLabel( iPivotLevel );
{
see code commentary in once block that describes how we will anchor text
labels
}
if UseBNPoint then
tempTextLabel.SetPointValue( BNPoint.Create( GetDOBarNumberForCurrentBar(),
Price ) )
else
tempTextLabel.SetPointValue( DTPoint.Create( BarDateTime, Price ) );
tempTextLabel.TextString = GetTextLabelText( iPivotLevel );
tempTextLabel.HStyle = HorizontalStyle.Left;
{
we will place support levels (e.g., S1, S2, etc.) above the plotted line
and the resistance levels (e.g., R1, R2, etc. ) and the pivot level below
the plotted line so they are visible in the case where the plotted value
is at the bottom (in the case of support levels) or top (in the case of
resistance levels) so that the text label is visible
}
if LeftStr( iPivotLevel, 1 ) = "S" then
tempTextLabel.VStyle = VerticalStyle.Bottom
else
tempTextLabel.VStyle = VerticalStyle.Top;
tempTextLabel.Lock = true; { prevent inadvertent moving }
{
Setting 'Persist' to false causes the text label to be deleted on an
intrabar tick. When set to false, a text label that is created on the
closing tick of the bar is saved/retained.
}
tempTextLabel.Persist = false;
if TextLabelFontSize > 0 then
begin
tempTextLabel.Font = Font.Create( tempTextLabel.Font.Name,
TextLabelFontSize );
end;
end;
{
in the case where we are not creating new text labels due to limiting the
number of text labels created by this indicator, we will 'recycle' one of
the text labels created earlier; this method will return a text label from
the vector containing all text labels (excluding text labels for tomorrow's
pvitos)
for more, see code commentary for 'GetTextLabelForPivotLevel' method above
}
method TextLabel GetTextLabelFromVector()
variables: TextLabel tempLabel;
begin
{
once we get to the end of the labels in the Vector, we need to
reset the index to start at 0 again to start over
}
if ReuseIndex >= AllTextLabelsVector.Count - 1 then
ReuseIndex = 0;
tempLabel = AllTextLabelsVector[ReuseIndex] astype TextLabel;
ReuseIndex += 1;
return TempLabel;
end;
{ text used in pivot level text label }
method string GetTextLabelText( string iPivotLevel )
variables: string LabelTextString, string PivotLevel;
begin
PivotLevel = iPivotLevel;
LabelTextString = !( PivotLevel );
if ShowPivotIntervalInLabels = 1 then
LabelTextString += "(" + PivotIntervalString + ")";
if ShowPriceLevelsInLabels = 1 then
LabelTextString += ": " + NumToStr( GetPriceForPivotLabel( PivotLevel ),
DecimalPlaces );
return LabelTextString;
end;
{
in the case where we are only displaying text labels for the current period,
when a new period begins, this method is called to move the text labels to
the new period pivot values and update the text string
}
method void MoveTextLabelsToNewPeriod()
variables: int Counter, TextLabel tempLabel;
begin
{ if the text label vector is NULL (we are not drawing text labels), or we
are not just showing the current period labels, we will just exit }
if CurrentTextLabelVector = NULL or ShowCurrentPeriodLabelsOnly <> 1 then
return;
for Counter = 0 to CurrentTextLabelVector.Count - 1
begin
tempLabel = CurrentTextLabelVector[Counter] astype TextLabel;
if UseBNPoint then
begin
tempLabel.SetPointValue( BNPoint.Create( GetDOBarNumberForCurrentBar(),
GetPriceForTextLabel( tempLabel ) ) );
end
else
begin
tempLabel.SetPointValue( DTPoint.Create( BarDateTime,
GetPriceForTextLabel( tempLabel ) ) );
end;
tempLabel.TextString = GetTextLabelText( tempLabel.Tag astype string );
end;
end;
method double GetPriceForTextLabel( TextLabel iTextLabel )
variables: double textLabelPrice;
begin
textLabelPrice = GetPriceForPivotLabel( iTextLabel.Tag astype string );
return textLabelPrice;
end;
method double GetPriceForPivotLabel( string iPivotLevel )
variables: double textLabelPrice;
begin
switch ( iPivotLevel astype string )
begin
case R5_KEY:
textLabelPrice = R5;
case R4_KEY:
textLabelPrice = R4;
case R3_KEY:
textLabelPrice = R3;
case R2_KEY:
textLabelPrice = R2;
case R1_KEY:
textLabelPrice = R1;
case PP_KEY:
textLabelPrice = PP;
case S1_KEY:
textLabelPrice = S1;
case S2_KEY:
textLabelPrice = S2;
case S3_KEY:
textLabelPrice = S3;
case S4_KEY:
textLabelPrice = S4;
case S5_KEY:
textLabelPrice = S5;
end;
return textLabelPrice;
end;
method double GetPriceForTomorrowPivotLabel( string iPivotLevel )
variables: double textLabelPrice;
begin
switch ( iPivotLevel astype string )
begin
case R5_KEY:
textLabelPrice = R5Tom;
case R4_KEY:
textLabelPrice = R4Tom;
case R3_KEY:
textLabelPrice = R3Tom;
case R2_KEY:
textLabelPrice = R2Tom;
case R1_KEY:
textLabelPrice = R1Tom;
case PP_KEY:
textLabelPrice = PPTom;
case S1_KEY:
textLabelPrice = S1Tom;
case S2_KEY:
textLabelPrice = S2Tom;
case S3_KEY:
textLabelPrice = S3Tom;
case S4_KEY:
textLabelPrice = S4Tom;
case S5_KEY:
textLabelPrice = S5Tom;
end;
return textLabelPrice;
end;
method void SetTextLabelColor( TextLabel tempLabel )
variables: string ColorKey;
begin
if tempLabel = NULL or tempLabel.Tag = NULL then
return;
tempLabel.Color = DOColorDict[tempLabel.Tag astype string] astype Color;
end;
method void ShowTodaysLabels()
variables: int Counter, TextLabel tempLabel;
begin
if CurrentTextLabelVector = NULL then
return;
for Counter = 0 to CurrentTextLabelVector.Count - 1
begin
tempLabel = CurrentTextLabelVector[Counter] astype TextLabel;
SetTextLabelColor( tempLabel );
end;
end;
method void HideTodaysLabels()
variables: int Counter, TextLabel tempLabel;
begin
if CurrentTextLabelVector = NULL then
return;
for Counter = 0 to CurrentTextLabelVector.Count - 1
begin
tempLabel = CurrentTextLabelVector[Counter] astype TextLabel;
tempLabel.Color = Color.Transparent;
end;
end;
{ TextLabels region }
#endregion
#region PivotCalcs
method void CalculateLevels()
begin
switch ( PivotInterval )
begin
case DAILY_PIVOTS,WEEKLY_PIVOTS,MONTHLY_PIVOTS:
CalcPivotsUsingLastClosedPSPBar( PivotsPSP );
{
for quarterly and yearly pivots, we are using a Monthly PSP interval since
that is the largest available interval; to calculate quarterly and yearly
pivots using monthly bars, we will need to loop through the bars and
determine the period high, low, and close; therefore, we will call the
quarterly and yearly methods below
}
case QUARTERLY_PIVOTS:
if GetQuarterForDateTime( BarDateTime ) = 1 then
begin
CalcQuarterlyPivots( PivotsPSP, BarDateTime.Year - 1, 4 );
end
else
begin
CalcQuarterlyPivots( PivotsPSP, BarDateTime.Year,
GetQuarterForDateTime( BarDateTime ) - 1 );
end;
case YEARLY_PIVOTS:
CalcYearlyPivots( PivotsPSP, BarDateTime.Year - 1 );
end;
end;
method void CalcPivotsUsingLastClosedPSPBar( PriceSeriesProvider PSP )
begin
{
we will calculate the PSP pivot values for the last 'closed' PSP bar;
when we are in history, the 'PSPIndexOffset' variable value is zero,
which references the last closed PSP bar; when we are in real-time, the
'PSPIndexOffset' variable value is 1, so that we are calculating the
pivot values based on the last 'closed' PSP bar; note that in real time,
the zero index values in the PSP reference the currently forming PSP bar,
so to use the last 'closed' PSP bar, we need to use the PSP values of 1
PSP bar ago, which is why we set 'PSPIndexOffset' to 1 in real time
}
CalcPivotLevels( PSP.High[PSPIndexOffset], PSP.Low[PSPIndexOffset],
PSP.Close[PSPIndexOffset] );
end;
method bool CalcQuarterlyPivots( PriceSeriesProvider PSP, int iYear, int iQuarter )
variables:
double PeriodHigh,
double PeriodLow,
double PeriodClose,
int Counter,
int LastMonth,
int NumMonthsFound;
begin
PeriodHigh = double.MinValue();
PeriodLow = double.MaxValue();
LastMonth = -1;
{
for quarterly pivots, we are using a Monthly PSP interval since that is the
largest available interval; to calculate quarterly pivots using monthly bars,
we will loop through the bars and determine the quarterly high, low, and close
}
for Counter = 0 to PSP.Count - 1
begin
if PSP.Time[Counter].Year = iYear and
GetQuarterForDateTime( PSP.Time[Counter] ) = iQuarter then
begin
NumMonthsFound += 1;
if PSP.High[Counter] > PeriodHigh then
PeriodHigh = PSP.High[Counter];
if PivotsPSP.Low[Counter] < PeriodLow then
PeriodLow = PSP.Low[Counter];
if PSP.Time[Counter].Month > LastMonth then
begin
PeriodClose = PSP.Close[Counter];
LastMonth = PSP.Time[Counter].Month;
end;
end;
end;
{ calculate the pivot levels using the quarterly high, low, and close }
CalcPivotLevels( PeriodHigh, PeriodLow, PeriodClose );
return PeriodClose <> 0 and PeriodHigh <> double.MinValue()
and PeriodLow <> double.MaxValue() and NumMonthsFound = 3;
end;
method bool CalcYearlyPivots( PriceSeriesProvider PSP, int tempYear )
variables:
double PeriodHigh,
double PeriodLow,
double PeriodClose,
int Counter,
int NumMonthsFound;
begin
PeriodHigh = double.MinValue();
PeriodLow = double.MaxValue();
PeriodClose = 0;
{
for yearly pivots, we are using a Monthly PSP interval since that is the
largest available interval; to calculate yearly pivots using monthly bars,
we will loop through the bars and determine the yearly high, low, and close
}
for Counter = 0 to PSP.Count - 1
begin
{
we will only evaluate PSP bars where the year matches the year we are
interested in calculating
}
if PSP.Time[Counter].Year = tempYear then
begin
NumMonthsFound += 1;