Repository navigation
Expand file tree
/
Copy pathCombinedDashboard.cpp
More file actions
1733 lines (1429 loc) · 53.2 KB
/
Copy pathCombinedDashboard.cpp
File metadata and controls
1733 lines (1429 loc) · 53.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
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
#include "sierrachart.h"
#include <fstream>
#include <string>
#include <map>
SCDLLName("Combined Chart Dashboard")
//============================================================================
// COMBINED CHART DASHBOARD
//
// One study replacing nine. Nine lines of text pinned to the upper-left of
// the chart, one per original study, all updating live:
//
// 0 QUOTE symbol, last, bid/ask, bar count (FourLineChartText)
// 1 MIDNIGHT midnight close, distance, relation (MidnightLevel)
// 2 SUNDAY OPEN 5pm Sunday open, distance, relation (SundayOpenLevel)
// 3 PREV CLOSE previous 5pm close, distance, trend (PrevSessionCloseTracker)
// 4 1ST HOUR first-hour direction, green/red day (FirstHourTrend)
// 5 OPENING RANGE which extreme formed first (OneHourOR_HighLow_First)
// 6 DAY BIAS historical green/red % for the day (DayOfWeekBias)
// 7 TIME SLOT value for the current slot from CSV (TimeSlotValue)
// 8 INSIDE BAR inside-bar scan across Daily charts (InsideBarScanner)
//
// Each line can be switched off individually; a hidden line collapses so the
// remaining ones close up rather than leaving a gap.
//
// SETUP
// Apply to an INTRADAY chart with the timezone set to Central, so 08:30 is
// the NY open and 17:00 is the 5pm settlement / evening reopen.
// Load at least 7 days of history so the Sunday open is reachable on Friday.
// For the inside-bar line, open one Daily chart per symbol in this
// Chartbook (they can be hidden) and set the "IB Chart 1..12" inputs.
//
//----------------------------------------------------------------------------
// WHAT WAS SHARED
//
// The nine originals each carried their own copy of the same plumbing. Here
// there is exactly one of each:
//
// SetTextLine() one text renderer for all nine lines, with the color
// folded into the cache key so a red/green flip on
// identical text still redraws
// DrawLevel() one horizontal-line + right-hand-label renderer,
// used by both the midnight and Sunday levels
// FindBoundaryIndex() one backwards session-boundary search replacing the
// four near-identical finders. Mode and an optional
// required weekday cover every case:
// midnight -> window at 00:00, date change
// Sunday -> window at 17:00, date change, Sunday
// RTH open -> window at 08:30, window entry
// prev close -> window at 17:00, window entry, -1 bar
// HHMMToMinutes / MinutesOfDay / AbsValue / DayOfWeekFromDate /
// TicksBetween / FormatSigned one copy each
//============================================================================
// If sc.GetChartName() does not exist in your Sierra Chart version, change
// this to 0 and the inside-bar line falls back to "Chart #N" labels.
#define HAVE_GET_CHART_NAME 1
namespace {
//========================================================================
// LAYOUT CONSTANTS
//========================================================================
const int NUM_LINES = 9; // one status line per original study
const int MAX_SYMBOLS = 12; // inside-bar scanner chart slots
const int LINE_NUM_BASE = 99000; // drawing IDs: base + 0 .. base + 8
const int MID_LEVEL_LINE_NUM = LINE_NUM_BASE + 50;
const int MID_LABEL_LINE_NUM = LINE_NUM_BASE + 51;
const int SUN_LEVEL_LINE_NUM = LINE_NUM_BASE + 52;
const int SUN_LABEL_LINE_NUM = LINE_NUM_BASE + 53;
// Persistent SCString slots: 0..8 are the text-line caches.
const int MID_LEVEL_CACHE_IX = NUM_LINES + 0; // 9
const int MID_LABEL_CACHE_IX = NUM_LINES + 1; // 10
const int SUN_LEVEL_CACHE_IX = NUM_LINES + 2; // 11
const int SUN_LABEL_CACHE_IX = NUM_LINES + 3; // 12
const int IB_TEXT_CACHE_IX = NUM_LINES + 4; // 13 - last scan result
const int NUM_CACHE_SLOTS = NUM_LINES + 5;
// Persistent ints
const int PI_IB_SCAN_BARCOUNT = 0;
const int PI_IB_HIT_COUNT = 1;
//========================================================================
// INPUTS
//========================================================================
enum InputIndex
{
// ---- shared display ----
IN_HORIZ_POS = 0,
IN_VERT_POS,
IN_LINE_SPACING,
IN_FONT_SIZE,
IN_BOLD,
IN_TRANSPARENT_BG,
IN_GREEN_COLOR,
IN_RED_COLOR,
IN_FLAT_COLOR,
IN_INFO_COLOR,
// ---- shared session times ----
IN_RTH_OPEN_HHMM,
IN_RTH_CLOSE_HHMM,
IN_EVENING_OPEN_HHMM,
IN_DETECT_WINDOW_MINUTES,
// ---- line visibility ----
IN_SHOW_QUOTE,
IN_SHOW_MIDNIGHT,
IN_SHOW_SUNDAY,
IN_SHOW_PREV_CLOSE,
IN_SHOW_FIRST_HOUR,
IN_SHOW_OR_SEQUENCE,
IN_SHOW_DAY_BIAS,
IN_SHOW_TIME_SLOT,
IN_SHOW_INSIDE_BAR,
// ---- level lines (midnight + Sunday) ----
IN_DRAW_LEVEL_LINES,
IN_LEVEL_LINE_WIDTH,
IN_LEVEL_DASHED,
IN_MIDNIGHT_LABEL,
IN_SUNDAY_LABEL,
IN_REQUIRE_SUNDAY,
IN_USE_COMPLETED_BAR,
IN_INVERT_LEVEL_COLORS,
// ---- previous session close ----
IN_PREV_CLOSE_ONLY_RTH,
IN_TREND_LOOKBACK,
// ---- first hour / opening range ----
IN_WINDOW_MINUTES,
IN_MIN_TICKS,
IN_RESOLVE_SAME_BAR,
// ---- day of week bias ----
IN_MON_GREEN,
IN_TUE_GREEN,
IN_WED_GREEN,
IN_THU_GREEN,
IN_FRI_GREEN,
IN_DOW_ROLL_EVENING,
IN_DOW_USE_SYSTEM_DATE,
// ---- time slot value ----
IN_SLOT_FILE_PATH,
IN_SLOT_SUBTRACT_MINUTES,
IN_SLOT_MINUTES,
IN_SLOT_USE_SYSTEM_CLOCK,
IN_SLOT_SHOW_NEXT,
IN_SLOT_RELOAD,
IN_SLOT_RED_THRESHOLD,
IN_SLOT_GREY_THRESHOLD,
IN_SLOT_GREY_COLOR,
// ---- inside bar scanner ----
IN_IB_STRICT,
IN_IB_SKIP_TODAY,
IN_IB_MAX_NAMES,
IN_IB_CHART_FIRST // .. IN_IB_CHART_FIRST + MAX_SYMBOLS - 1
};
//========================================================================
// SHARED SMALL HELPERS (one copy of what used to live in nine files)
//========================================================================
// HHMM integer (830 = 08:30) -> minutes since midnight
int HHMMToMinutes(int HHMM)
{
return (HHMM / 100) * 60 + (HHMM % 100);
}
int MinutesOfDay(const SCDateTime& DT)
{
return DT.GetHour() * 60 + DT.GetMinute();
}
double AbsValue(double Value)
{
return (Value < 0.0) ? -Value : Value;
}
// Sakamoto's method. Returns 0 = Sunday .. 6 = Saturday.
int DayOfWeekFromDate(int Year, int Month, int Day)
{
static const int MonthTable[12] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
if (Month < 1 || Month > 12)
return -1;
if (Month < 3)
Year -= 1;
return (Year + Year / 4 - Year / 100 + Year / 400 + MonthTable[Month - 1] + Day) % 7;
}
int TicksBetween(SCStudyInterfaceRef sc, double Distance)
{
if (sc.TickSize <= 0.0)
return 0;
return (int)(AbsValue(Distance) / sc.TickSize + 0.5);
}
SCString FormatSigned(SCStudyInterfaceRef sc, double Value, int ValueFormat)
{
SCString Out;
Out.Format("%s%s",
(Value >= 0.0) ? "+" : "-",
sc.FormatGraphValue(AbsValue(Value), ValueFormat).GetChars());
return Out;
}
//========================================================================
// SHARED TEXT RENDERER
//
// Every visible line gets a FIXED LineNumber, and UTAM_ADD_OR_ADJUST
// replaces that drawing in place, so text can never overlap or be left
// behind. The color is part of the cache key, otherwise a red-to-green
// flip with identical text would never be redrawn. "" erases the line.
//========================================================================
void SetTextLine(SCStudyInterfaceRef sc, int LineIndex, const SCString& Text, COLORREF TextColor)
{
if (LineIndex < 0 || LineIndex >= NUM_LINES)
return;
SCString CacheKey;
CacheKey.Format("%u|%s", (unsigned int)TextColor, Text.GetChars());
SCString& LastKey = sc.GetPersistentSCString(LineIndex);
if (strcmp(LastKey.GetChars(), CacheKey.GetChars()) == 0)
return;
LastKey = CacheKey;
const int LineNumber = LINE_NUM_BASE + LineIndex;
if (Text.GetLength() == 0)
{
sc.DeleteACSChartDrawing(sc.ChartNumber, TOOL_DELETE_CHARTDRAWING, LineNumber);
return;
}
s_UseTool Tool;
Tool.Clear();
Tool.ChartNumber = sc.ChartNumber;
Tool.Region = sc.GraphRegion;
Tool.DrawingType = DRAWING_TEXT;
Tool.LineNumber = LineNumber;
Tool.AddMethod = UTAM_ADD_OR_ADJUST;
Tool.AddAsUserDrawnDrawing = 0;
Tool.UseRelativeVerticalValues = 1; // horiz 0-150, vert 0-100 (100 = top)
Tool.BeginDateTime = sc.Input[IN_HORIZ_POS].GetInt();
Tool.BeginValue = sc.Input[IN_VERT_POS].GetFloat()
- LineIndex * sc.Input[IN_LINE_SPACING].GetFloat();
Tool.Text = Text;
Tool.Color = TextColor;
Tool.FontSize = sc.Input[IN_FONT_SIZE].GetInt();
Tool.FontBold = sc.Input[IN_BOLD].GetYesNo();
Tool.TransparentLabelBackground = sc.Input[IN_TRANSPARENT_BG].GetYesNo();
Tool.TextAlignment = DT_LEFT | DT_TOP;
sc.UseTool(Tool);
}
//========================================================================
// SHARED LEVEL RENDERER
//
// A horizontal line in real chart coordinates plus a label anchored at
// the newest bar, left-aligned so it runs out into the empty space on the
// right. Used by the midnight level and the Sunday open level.
//========================================================================
struct s_LevelIds
{
int LineNumber;
int LabelNumber;
int LineCacheIndex;
int LabelCacheIndex;
};
const s_LevelIds MIDNIGHT_LEVEL =
{ MID_LEVEL_LINE_NUM, MID_LABEL_LINE_NUM, MID_LEVEL_CACHE_IX, MID_LABEL_CACHE_IX };
const s_LevelIds SUNDAY_LEVEL =
{ SUN_LEVEL_LINE_NUM, SUN_LABEL_LINE_NUM, SUN_LEVEL_CACHE_IX, SUN_LABEL_CACHE_IX };
void DrawLevel(SCStudyInterfaceRef sc, const s_LevelIds& Ids, int AnchorIndex,
double Price, COLORREF LevelColor, const SCString& LabelText)
{
const int LastIndex = sc.ArraySize - 1;
if (LastIndex < 0 || AnchorIndex < 0)
return;
//---- the line ----
SCString LineKey;
LineKey.Format("L|%u|%.8f|%d", (unsigned int)LevelColor, Price, AnchorIndex);
SCString& LastLineKey = sc.GetPersistentSCString(Ids.LineCacheIndex);
if (strcmp(LastLineKey.GetChars(), LineKey.GetChars()) != 0)
{
LastLineKey = LineKey;
s_UseTool Tool;
Tool.Clear();
Tool.ChartNumber = sc.ChartNumber;
Tool.Region = sc.GraphRegion;
Tool.DrawingType = DRAWING_HORIZONTALLINE;
Tool.LineNumber = Ids.LineNumber;
Tool.AddMethod = UTAM_ADD_OR_ADJUST;
Tool.AddAsUserDrawnDrawing = 0;
Tool.BeginDateTime = sc.BaseDateTimeIn[AnchorIndex];
Tool.BeginValue = (float)Price;
Tool.Color = LevelColor;
Tool.LineWidth = sc.Input[IN_LEVEL_LINE_WIDTH].GetInt();
Tool.LineStyle = sc.Input[IN_LEVEL_DASHED].GetYesNo()
? LINESTYLE_DASH : LINESTYLE_SOLID;
sc.UseTool(Tool);
}
//---- the right hand label ----
SCString LabelKey;
LabelKey.Format("T|%u|%.8f|%s",
(unsigned int)LevelColor, Price, LabelText.GetChars());
SCString& LastLabelKey = sc.GetPersistentSCString(Ids.LabelCacheIndex);
if (strcmp(LastLabelKey.GetChars(), LabelKey.GetChars()) != 0)
{
LastLabelKey = LabelKey;
s_UseTool Tool;
Tool.Clear();
Tool.ChartNumber = sc.ChartNumber;
Tool.Region = sc.GraphRegion;
Tool.DrawingType = DRAWING_TEXT;
Tool.LineNumber = Ids.LabelNumber;
Tool.AddMethod = UTAM_ADD_OR_ADJUST;
Tool.AddAsUserDrawnDrawing = 0;
Tool.BeginDateTime = sc.BaseDateTimeIn[LastIndex];
Tool.BeginValue = (float)Price;
Tool.Text = LabelText;
Tool.Color = LevelColor;
Tool.FontSize = sc.Input[IN_FONT_SIZE].GetInt();
Tool.FontBold = sc.Input[IN_BOLD].GetYesNo();
Tool.TransparentLabelBackground = 1;
Tool.TextAlignment = DT_LEFT | DT_VCENTER;
sc.UseTool(Tool);
}
}
void EraseLevel(SCStudyInterfaceRef sc, const s_LevelIds& Ids)
{
SCString& LastLineKey = sc.GetPersistentSCString(Ids.LineCacheIndex);
SCString& LastLabelKey = sc.GetPersistentSCString(Ids.LabelCacheIndex);
if (LastLineKey.GetLength() == 0 && LastLabelKey.GetLength() == 0)
return;
LastLineKey = "";
LastLabelKey = "";
sc.DeleteACSChartDrawing(sc.ChartNumber, TOOL_DELETE_CHARTDRAWING, Ids.LineNumber);
sc.DeleteACSChartDrawing(sc.ChartNumber, TOOL_DELETE_CHARTDRAWING, Ids.LabelNumber);
}
void ClearAll(SCStudyInterfaceRef sc)
{
for (int i = 0; i < NUM_LINES; ++i)
sc.DeleteACSChartDrawing(sc.ChartNumber, TOOL_DELETE_CHARTDRAWING, LINE_NUM_BASE + i);
sc.DeleteACSChartDrawing(sc.ChartNumber, TOOL_DELETE_CHARTDRAWING, MID_LEVEL_LINE_NUM);
sc.DeleteACSChartDrawing(sc.ChartNumber, TOOL_DELETE_CHARTDRAWING, MID_LABEL_LINE_NUM);
sc.DeleteACSChartDrawing(sc.ChartNumber, TOOL_DELETE_CHARTDRAWING, SUN_LEVEL_LINE_NUM);
sc.DeleteACSChartDrawing(sc.ChartNumber, TOOL_DELETE_CHARTDRAWING, SUN_LABEL_LINE_NUM);
for (int i = 0; i < NUM_CACHE_SLOTS; ++i)
sc.GetPersistentSCString(i) = "";
}
//========================================================================
// SHARED SESSION BOUNDARY SEARCH
//
// One backwards walk replacing the four separate finders. Returns the
// index of the newest bar that opens the requested window, or -1.
//
// BOUNDARY_WINDOW_ENTRY : the bar before it is earlier in the day or on
// a different date. This is the "first bar at or
// just after the RTH open" test.
// BOUNDARY_DATE_CHANGE : the bar before it is on a different date, so
// the bar is the first of a session.
//
// WindowMinutes is what rejects the wrong boundary: a 17:00 reopen is a
// date change but is not inside a 00:00 window, and Friday's 08:30 is not
// inside a 17:00 window.
//
// RequiredWeekday is -1 for "any", else 0 = Sunday .. 6 = Saturday.
//========================================================================
enum e_BoundaryMode
{
BOUNDARY_WINDOW_ENTRY = 0,
BOUNDARY_DATE_CHANGE
};
int FindBoundaryIndex(SCStudyInterfaceRef sc, int StartMinutes, int WindowMinutes,
int Mode, int RequiredWeekday)
{
const int LastIndex = sc.ArraySize - 1;
if (LastIndex < 1 || WindowMinutes < 1)
return -1;
for (int i = LastIndex; i >= 1; --i)
{
const int CurrentMinutes = MinutesOfDay(sc.BaseDateTimeIn[i]);
if (CurrentMinutes < StartMinutes || CurrentMinutes >= StartMinutes + WindowMinutes)
continue;
const int DateChanged =
(sc.BaseDateTimeIn[i].GetDate() != sc.BaseDateTimeIn[i - 1].GetDate()) ? 1 : 0;
if (Mode == BOUNDARY_DATE_CHANGE)
{
if (!DateChanged)
continue;
}
else
{
const int PreviousMinutes = MinutesOfDay(sc.BaseDateTimeIn[i - 1]);
if (PreviousMinutes >= StartMinutes && !DateChanged)
continue;
}
if (RequiredWeekday >= 0)
{
const SCDateTime& DT = sc.BaseDateTimeIn[i];
if (DayOfWeekFromDate(DT.GetYear(), DT.GetMonth(), DT.GetDay()) != RequiredWeekday)
continue;
}
return i;
}
return -1;
}
//========================================================================
// SHARED LEVEL RELATION
//
// Midnight and Sunday both color by "level below price = red, level above
// price = green", with an invert option.
//========================================================================
COLORREF LevelRelation(SCStudyInterfaceRef sc, double Distance, const char* LevelName,
SCString& r_RelationText)
{
COLORREF AboveColor = sc.Input[IN_GREEN_COLOR].GetColor();
COLORREF BelowColor = sc.Input[IN_RED_COLOR].GetColor();
if (sc.Input[IN_INVERT_LEVEL_COLORS].GetYesNo())
{
const COLORREF Swap = AboveColor;
AboveColor = BelowColor;
BelowColor = Swap;
}
if (Distance > 0.0)
{
r_RelationText.Format("%s BELOW PRICE", LevelName);
return BelowColor;
}
if (Distance < 0.0)
{
r_RelationText.Format("%s ABOVE PRICE", LevelName);
return AboveColor;
}
r_RelationText.Format("PRICE AT %s", LevelName);
return sc.Input[IN_FLAT_COLOR].GetColor();
}
//========================================================================
// TIME SLOT CSV TABLE (from TimeSlotValue)
//
// FILE FORMAT - one line per slot, blank lines and # lines ignored:
// 09:30,22
// 09:35,12
// "Subtract From File Times" shifts those labels to the chart clock.
//========================================================================
std::map<int, int> g_SlotValues;
std::string g_LoadedPath;
int g_LoadedReloadFlag = -1;
int g_LoadFailed = 0;
int ParseSlotLine(const std::string& Line, int& r_Minutes, int& r_Value)
{
if (Line.empty() || Line[0] == '#')
return 0;
const size_t CommaPos = Line.find(',');
if (CommaPos == std::string::npos)
return 0;
const std::string TimePart = Line.substr(0, CommaPos);
const std::string ValuePart = Line.substr(CommaPos + 1);
int Hours = 0;
int Minutes = 0;
const size_t ColonPos = TimePart.find(':');
if (ColonPos != std::string::npos)
{
Hours = atoi(TimePart.substr(0, ColonPos).c_str());
Minutes = atoi(TimePart.substr(ColonPos + 1).c_str());
}
else
{
const int HHMM = atoi(TimePart.c_str());
Hours = HHMM / 100;
Minutes = HHMM % 100;
}
if (Hours < 0 || Hours > 23 || Minutes < 0 || Minutes > 59)
return 0;
r_Minutes = Hours * 60 + Minutes;
r_Value = atoi(ValuePart.c_str());
return 1;
}
void LoadSlotFile(const std::string& Path)
{
g_SlotValues.clear();
g_LoadFailed = 0;
std::ifstream File(Path.c_str());
if (!File.is_open())
{
g_LoadFailed = 1;
return;
}
std::string Line;
while (std::getline(File, Line))
{
while (!Line.empty()
&& (Line[Line.size() - 1] == '\r' || Line[Line.size() - 1] == ' '
|| Line[Line.size() - 1] == '\t'))
{
Line.erase(Line.size() - 1);
}
while (!Line.empty() && (Line[0] == ' ' || Line[0] == '\t'))
Line.erase(0, 1);
int Minutes = 0;
int Value = 0;
if (ParseSlotLine(Line, Minutes, Value))
g_SlotValues[Minutes] = Value;
}
File.close();
}
//========================================================================
// INSIDE BAR SCAN (from InsideBarScanner)
//
// A Daily chart only contains trading days, so "yesterday, or Friday if
// today is Monday" needs no weekday math: drop today's unfinished bar and
// compare the newest completed bar with the one before it.
//========================================================================
SCString GetChartLabel(SCStudyInterfaceRef sc, int ChartNumber)
{
SCString Label;
#if HAVE_GET_CHART_NAME
Label = sc.GetChartName(ChartNumber);
if (Label.GetLength() == 0)
Label.Format("Chart #%d", ChartNumber);
#else
Label.Format("Chart #%d", ChartNumber);
#endif
return Label;
}
struct s_ScanResult
{
int Valid;
int IsInsideBar;
int Month;
int Day;
};
s_ScanResult ScanChart(SCStudyInterfaceRef sc, int ChartNumber, int Strict, int SkipTodaysBar)
{
s_ScanResult Result;
Result.Valid = 0;
Result.IsInsideBar = 0;
Result.Month = 0;
Result.Day = 0;
SCGraphData BaseData;
sc.GetChartBaseData(ChartNumber, BaseData);
SCFloatArrayRef HighArray = BaseData[SC_HIGH];
SCFloatArrayRef LowArray = BaseData[SC_LOW];
const int ArraySize = HighArray.GetArraySize();
if (ArraySize < 2)
return Result;
SCDateTimeArray DateTimeArray;
sc.GetChartDateTimeArray(ChartNumber, DateTimeArray);
int TargetIndex = ArraySize - 1;
if (SkipTodaysBar
&& DateTimeArray.GetArraySize() == ArraySize
&& DateTimeArray[TargetIndex].GetDate() >= sc.CurrentSystemDateTime.GetDate())
{
--TargetIndex;
}
const int PriorIndex = TargetIndex - 1;
if (PriorIndex < 0)
return Result;
const float TargetHigh = HighArray[TargetIndex];
const float TargetLow = LowArray[TargetIndex];
const float PriorHigh = HighArray[PriorIndex];
const float PriorLow = LowArray[PriorIndex];
if (Strict)
Result.IsInsideBar = (TargetHigh < PriorHigh && TargetLow > PriorLow) ? 1 : 0;
else
Result.IsInsideBar = (TargetHigh <= PriorHigh && TargetLow >= PriorLow) ? 1 : 0;
if (DateTimeArray.GetArraySize() == ArraySize)
{
Result.Month = DateTimeArray[TargetIndex].GetMonth();
Result.Day = DateTimeArray[TargetIndex].GetDay();
}
Result.Valid = 1;
return Result;
}
//========================================================================
// LINE BUILDERS - one per original study, each producing ONE line
//========================================================================
//------------------------------------------------------------------------
// LINE 0 - QUOTE (FourLineChartText)
//------------------------------------------------------------------------
void BuildQuoteLine(SCStudyInterfaceRef sc, SCString& r_Text, COLORREF& r_Color)
{
const int LastIndex = sc.ArraySize - 1;
const int ValueFormat = sc.GetValueFormat();
r_Color = sc.Input[IN_INFO_COLOR].GetColor();
if (LastIndex >= 1)
{
const double Change = sc.Close[LastIndex] - sc.Close[LastIndex - 1];
if (Change > 0.0)
r_Color = sc.Input[IN_GREEN_COLOR].GetColor();
else if (Change < 0.0)
r_Color = sc.Input[IN_RED_COLOR].GetColor();
}
r_Text.Format("%s LAST %s BID/ASK %s / %s BARS %d",
sc.Symbol.GetChars(),
sc.FormatGraphValue(sc.Close[LastIndex], ValueFormat).GetChars(),
sc.FormatGraphValue(sc.Bid, ValueFormat).GetChars(),
sc.FormatGraphValue(sc.Ask, ValueFormat).GetChars(),
sc.ArraySize);
}
//------------------------------------------------------------------------
// LINE 1 - MIDNIGHT LEVEL (MidnightLevel)
//
// The midnight bar is the first bar of a new calendar date. Its close is
// the 00:00-00:01 closing price on a 1 minute chart.
//------------------------------------------------------------------------
void BuildMidnightLine(SCStudyInterfaceRef sc, int PriceIndex,
SCString& r_Text, COLORREF& r_Color)
{
const int MidnightIndex = FindBoundaryIndex(
sc, 0, sc.Input[IN_DETECT_WINDOW_MINUTES].GetInt(), BOUNDARY_DATE_CHANGE, -1);
if (MidnightIndex < 0)
{
EraseLevel(sc, MIDNIGHT_LEVEL);
r_Text = "MIDNIGHT: bar not in loaded data";
r_Color = sc.Input[IN_FLAT_COLOR].GetColor();
return;
}
const double MidnightClose = sc.Close[MidnightIndex];
const double Distance = sc.Close[PriceIndex] - MidnightClose;
SCString Relation;
r_Color = LevelRelation(sc, Distance, "MIDNIGHT", Relation);
if (sc.Input[IN_DRAW_LEVEL_LINES].GetYesNo())
{
SCString LabelText = sc.Input[IN_MIDNIGHT_LABEL].GetString();
DrawLevel(sc, MIDNIGHT_LEVEL, MidnightIndex, MidnightClose, r_Color, LabelText);
}
else
{
EraseLevel(sc, MIDNIGHT_LEVEL);
}
const int ValueFormat = sc.GetValueFormat();
const SCDateTime MidnightDT = sc.BaseDateTimeIn[MidnightIndex];
r_Text.Format("MIDNIGHT %02d/%02d %02d:%02d %s DIST %s (%d t) %s",
MidnightDT.GetMonth(), MidnightDT.GetDay(),
MidnightDT.GetHour(), MidnightDT.GetMinute(),
sc.FormatGraphValue(MidnightClose, ValueFormat).GetChars(),
FormatSigned(sc, Distance, ValueFormat).GetChars(),
TicksBetween(sc, Distance),
Relation.GetChars());
}
//------------------------------------------------------------------------
// LINE 2 - SUNDAY OPEN LEVEL (SundayOpenLevel)
//
// The first bar of the most recent Sunday evening session. The level is
// that bar's OPEN, i.e. the first print of the trading week.
//------------------------------------------------------------------------
void BuildSundayLine(SCStudyInterfaceRef sc, int PriceIndex,
SCString& r_Text, COLORREF& r_Color)
{
const int OpenIndex = FindBoundaryIndex(
sc,
HHMMToMinutes(sc.Input[IN_EVENING_OPEN_HHMM].GetInt()),
sc.Input[IN_DETECT_WINDOW_MINUTES].GetInt(),
BOUNDARY_DATE_CHANGE,
sc.Input[IN_REQUIRE_SUNDAY].GetYesNo() ? 0 : -1);
if (OpenIndex < 0)
{
EraseLevel(sc, SUNDAY_LEVEL);
r_Text = "SUNDAY OPEN: not in loaded data - raise Days to Load to 7+";
r_Color = sc.Input[IN_FLAT_COLOR].GetColor();
return;
}
const double SundayOpen = sc.Open[OpenIndex];
const double Distance = sc.Close[PriceIndex] - SundayOpen;
SCString Relation;
r_Color = LevelRelation(sc, Distance, "SUNDAY OPEN", Relation);
if (sc.Input[IN_DRAW_LEVEL_LINES].GetYesNo())
{
SCString LabelText = sc.Input[IN_SUNDAY_LABEL].GetString();
DrawLevel(sc, SUNDAY_LEVEL, OpenIndex, SundayOpen, r_Color, LabelText);
}
else
{
EraseLevel(sc, SUNDAY_LEVEL);
}
const int ValueFormat = sc.GetValueFormat();
const SCDateTime OpenDT = sc.BaseDateTimeIn[OpenIndex];
r_Text.Format("SUNDAY OPEN %02d/%02d %02d:%02d %s DIST %s (%d t) %s",
OpenDT.GetMonth(), OpenDT.GetDay(),
OpenDT.GetHour(), OpenDT.GetMinute(),
sc.FormatGraphValue(SundayOpen, ValueFormat).GetChars(),
FormatSigned(sc, Distance, ValueFormat).GetChars(),
TicksBetween(sc, Distance),
Relation.GetChars());
}
//------------------------------------------------------------------------
// LINE 3 - PREVIOUS SESSION CLOSE (PrevSessionCloseTracker)
//
// The close of the last bar BEFORE the most recent 17:00 boundary.
// Finding it by boundary crossing rather than by calendar date is what
// makes Monday work: walking back from Monday morning, the bar before the
// Sunday reopen is Friday's final bar.
//------------------------------------------------------------------------
void BuildPrevCloseLine(SCStudyInterfaceRef sc, SCString& r_Text, COLORREF& r_Color)
{
const int LastIndex = sc.ArraySize - 1;
const COLORREF Flat = sc.Input[IN_FLAT_COLOR].GetColor();
//---- display window ----
const int NowMinutes = MinutesOfDay(sc.BaseDateTimeIn[LastIndex]);
const int StartMinutes = HHMMToMinutes(sc.Input[IN_RTH_OPEN_HHMM].GetInt());
const int EndMinutes = HHMMToMinutes(sc.Input[IN_RTH_CLOSE_HHMM].GetInt());
if (sc.Input[IN_PREV_CLOSE_ONLY_RTH].GetYesNo())
{
int InWindow;
if (StartMinutes <= EndMinutes)
InWindow = (NowMinutes >= StartMinutes && NowMinutes <= EndMinutes) ? 1 : 0;
else // window wraps past midnight
InWindow = (NowMinutes >= StartMinutes || NowMinutes <= EndMinutes) ? 1 : 0;
if (!InWindow)
{
r_Text = "PREV CLOSE: outside the RTH display window";
r_Color = Flat;
return;
}
}
//---- reference close ----
const int RefMinutes = HHMMToMinutes(sc.Input[IN_EVENING_OPEN_HHMM].GetInt());
int RefIndex = FindBoundaryIndex(
sc, RefMinutes, 24 * 60 - RefMinutes, BOUNDARY_WINDOW_ENTRY, -1);
if (RefIndex > 0)
{
--RefIndex; // the bar BEFORE the boundary is the closing bar
}
else
{
// Fallback for charts carrying no evening session: last bar of
// the previous calendar day.
RefIndex = -1;
const int TodayDate = sc.BaseDateTimeIn[LastIndex].GetDate();
for (int i = LastIndex; i >= 0; --i)
{
if (sc.BaseDateTimeIn[i].GetDate() < TodayDate)
{
RefIndex = i;
break;
}
}
}
if (RefIndex < 0)
{
r_Text = "PREV CLOSE: not in loaded data";
r_Color = Flat;
return;
}
const double RefClose = sc.Close[RefIndex];
const double CurrentClose = sc.Close[LastIndex];
const double Distance = CurrentClose - RefClose;
//---- trend and approach ----
int Lookback = sc.Input[IN_TREND_LOOKBACK].GetInt();
if (Lookback > LastIndex)
Lookback = LastIndex;
const double PastClose = sc.Close[LastIndex - Lookback];
const double TrendChange = CurrentClose - PastClose;
const double PastDistance = AbsValue(PastClose - RefClose);
const double NowDistance = AbsValue(Distance);
const char* TrendText = "TREND FLAT";
r_Color = Flat;
if (TrendChange > 0.0)
{
r_Color = sc.Input[IN_GREEN_COLOR].GetColor();
TrendText = "TREND UP";
}
else if (TrendChange < 0.0)
{
r_Color = sc.Input[IN_RED_COLOR].GetColor();
TrendText = "TREND DOWN";
}
const char* ApproachText = "HOLDING";
if (NowDistance < PastDistance)
ApproachText = "MOVING TOWARD";
else if (NowDistance > PastDistance)
ApproachText = "MOVING AWAY";
const int ValueFormat = sc.GetValueFormat();
const SCDateTime RefDT = sc.BaseDateTimeIn[RefIndex];
r_Text.Format("PREV CLOSE %02d/%02d %02d:%02d %s DIST %s (%d t) %s %s",
RefDT.GetMonth(), RefDT.GetDay(),
RefDT.GetHour(), RefDT.GetMinute(),
sc.FormatGraphValue(RefClose, ValueFormat).GetChars(),
FormatSigned(sc, Distance, ValueFormat).GetChars(),
TicksBetween(sc, Distance),
ApproachText,
TrendText);
}
//------------------------------------------------------------------------
// LINE 4 - FIRST HOUR TREND (FirstHourTrend)
//
// Open-to-close direction of the first hour after the RTH open.
//------------------------------------------------------------------------
void BuildFirstHourLine(SCStudyInterfaceRef sc, int OpenIndex,
SCString& r_Text, COLORREF& r_Color)
{
const int LastIndex = sc.ArraySize - 1;
r_Color = sc.Input[IN_FLAT_COLOR].GetColor();
if (OpenIndex < 0)
{
r_Text = "1ST HOUR: RTH open not found in loaded data";
return;
}
const int OpenMinutes = HHMMToMinutes(sc.Input[IN_RTH_OPEN_HHMM].GetInt());
const int EndMinutes = OpenMinutes + sc.Input[IN_WINDOW_MINUTES].GetInt();
const int SessionDate = sc.BaseDateTimeIn[OpenIndex].GetDate();
int WindowEndIndex = OpenIndex;
double WindowHigh = sc.High[OpenIndex];
double WindowLow = sc.Low[OpenIndex];
for (int i = OpenIndex; i <= LastIndex; ++i)
{
if (sc.BaseDateTimeIn[i].GetDate() != SessionDate)
break;
if (MinutesOfDay(sc.BaseDateTimeIn[i]) >= EndMinutes)
break;
if (sc.High[i] > WindowHigh)
WindowHigh = sc.High[i];
if (sc.Low[i] < WindowLow)
WindowLow = sc.Low[i];
WindowEndIndex = i;
}
const int IsComplete = (WindowEndIndex < LastIndex) ? 1 : 0;
const double WindowOpen = sc.Open[OpenIndex];
const double WindowClose = sc.Close[WindowEndIndex];
const double Net = WindowClose - WindowOpen;
const int NetTicks = TicksBetween(sc, Net);
const int MinTicks = sc.Input[IN_MIN_TICKS].GetInt();
SCString Verdict;
if (Net > 0.0 && NetTicks >= MinTicks)
{
r_Color = sc.Input[IN_GREEN_COLOR].GetColor();
Verdict = IsComplete ? "GREEN DAY" : "LEANING GREEN";
}
else if (Net < 0.0 && NetTicks >= MinTicks)
{
r_Color = sc.Input[IN_RED_COLOR].GetColor();
Verdict = IsComplete ? "RED DAY" : "LEANING RED";
}
else
{
Verdict = IsComplete ? "NEUTRAL DAY" : "NEUTRAL SO FAR";
}
if (!IsComplete)
Verdict += " (still running)";
const int ValueFormat = sc.GetValueFormat();
r_Text.Format("1ST HOUR %02d:%02d-%02d:%02d O %s C %s NET %s (%d t) %s",
OpenMinutes / 60, OpenMinutes % 60,
(EndMinutes / 60) % 24, EndMinutes % 60,
sc.FormatGraphValue(WindowOpen, ValueFormat).GetChars(),
sc.FormatGraphValue(WindowClose, ValueFormat).GetChars(),
FormatSigned(sc, Net, ValueFormat).GetChars(),
NetTicks,
Verdict.GetChars());
}
//------------------------------------------------------------------------
// LINE 5 - OPENING RANGE SEQUENCE (OneHourOR_HighLow_First)
//
// Which of the opening range extremes was put in first. Strict > and <
// are what make a later retest leave the recorded index alone.
//------------------------------------------------------------------------
void BuildOpeningRangeLine(SCStudyInterfaceRef sc, int OpenIndex,
SCString& r_Text, COLORREF& r_Color)
{
const int LastIndex = sc.ArraySize - 1;
r_Color = sc.Input[IN_FLAT_COLOR].GetColor();