-
Notifications
You must be signed in to change notification settings - Fork 11
/
@CandleStickPattern.cs
1005 lines (869 loc) · 31.1 KB
/
@CandleStickPattern.cs
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
//
// Copyright (C) 2008, NinjaTrader LLC <www.ninjatrader.com>.
// NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
//
#region Using declarations
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
#endregion
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// Detects common candlestick patterns and marks them on the chart.
/// </summary>
[Description("Detects common candlestick patterns and marks them on the chart.")]
public class CandleStickPattern : Indicator
{
#region Variables
private Color downColor;
private bool downTrend;
private ChartPattern pattern = ChartPattern.MorningStar;
private int patternsFound;
private Font textFont = new Font("Arial", 12, FontStyle.Bold);
private int trendStrength = 4;
private Color txtColor;
private Color upColor;
private bool upTrend;
#endregion
/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
Add(new Plot(Color.Transparent, "Pattern Found"));
Overlay = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (CurrentBar == 0 && ChartControl != null)
{
downColor = ChartControl.GetAxisBrush(ChartControl.BackColor).Color;
txtColor = downColor;
if (downColor == Color.Black)
upColor = Color.Transparent;
else
upColor = Color.Black;
}
// Calculates trend lines and prevailing trend for patterns that require a trend
if (TrendStrength > 0 && CurrentBar >= TrendStrength)
CalculateTrendLines();
Value.Set(0);
switch (pattern)
{
case ChartPattern.BearishBeltHold:
{
#region Bearish Belt Hold
if (CurrentBar < 1 || (TrendStrength > 0 && !upTrend))
return;
if (Close[1] > Open[1] && Open[0] > Close[1] + 5 * TickSize && Open[0] == High[0] && Close[0] < Open[0])
{
if (ChartControl != null)
{
BarColorSeries.Set(CurrentBar - 1, upColor);
CandleOutlineColorSeries.Set(CurrentBar - 1, downColor);
BarColor = downColor;
}
DrawText("Bearish Belt Hold" + CurrentBar, false, "Bearish Belt Hold", 0, High[0], 10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.BearishEngulfing:
{
#region Bearish Engulfing
if (CurrentBar < 1 || (TrendStrength > 0 && !upTrend))
return;
if (Close[1] > Open[1] && Close[0] < Open[0] && Open[0] > Close[1] && Close[0] < Open[1])
{
BarColor = downColor;
DrawText("Bearish Engulfing" + CurrentBar, false, "Bearish Engulfing", 0, Low[0], -10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.BearishHarami:
{
#region Bearish Harami
if (CurrentBar < 1 || (TrendStrength > 0 && !upTrend))
return;
if (Close[0] < Open[0] && Close[1] > Open[1] && Low[0] >= Open[1] && High[0] <= Close[1])
{
BarColor = downColor;
DrawText("Bearish Harami" + CurrentBar, false, "Bearish Harami", 0, Low[0], -10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.BearishHaramiCross:
{
#region Bearish Harami Cross
if (CurrentBar < 1 || (TrendStrength > 0 && !upTrend))
return;
if ((High[0] <= Close[1]) && (Low[0] >= Open[1]) && Open[0] <= Close[1] && Close[0] >= Open[1] && ((Close[0] >= Open[0] && Close[0] <= Open[0] + TickSize) || (Close[0] <= Open[0] && Close[0] >= Open[0] - TickSize)))
{
BarColor = downColor;
DrawText("Bearish Harami Cross" + CurrentBar, false, "Bearish Harami Cross", 0, Low[0], -10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.BullishBeltHold:
{
#region Bullish Belt Hold
if (CurrentBar < 1 || (TrendStrength > 0 && !downTrend))
return;
if (Close[1] < Open[1] && Open[0] < Close[1] - 5 * TickSize && Open[0] == Low[0] && Close[0] > Open[0])
{
if (ChartControl != null)
{
BarColorSeries.Set(CurrentBar - 1, downColor);
BarColor = upColor;
CandleOutlineColorSeries.Set(CurrentBar, downColor);
}
DrawText("Bullish Belt Hold" + CurrentBar, false, "Bullish Belt Hold", 0, Low[0], -10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.BullishEngulfing:
{
#region Bullish Engulfing
if (CurrentBar < 1 || (TrendStrength > 0 && !downTrend))
return;
if (Close[1] < Open[1] && Close[0] > Open[0] && Close[0] > Open[1] && Open[0] < Close[1])
{
if (ChartControl != null)
{
BarColor = upColor;
CandleOutlineColorSeries.Set(CurrentBar, downColor);
}
DrawText("Bullish Engulfing" + CurrentBar, false, "Bullish Engulfing", 0, Low[0], -10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.BullishHarami:
{
#region Bullish Harami
if (CurrentBar < 1 || (TrendStrength > 0 && !downTrend))
return;
if (Close[0] > Open[0] && Close[1] < Open[1] && Low[0] >= Close[1] && High[0] <= Open[1])
{
if (ChartControl != null)
{
BarColor = upColor;
CandleOutlineColorSeries.Set(CurrentBar, downColor);
}
DrawText("Bullish Harami" + CurrentBar, false, "Bullish Harami", 0, Low[0], -10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.BullishHaramiCross:
{
#region Bullish Harami Cross
if (CurrentBar < 1 || (TrendStrength > 0 && !downTrend))
return;
if ((High[0] <= Open[1]) && (Low[0] >= Close[1]) && Open[0] >= Close[1] && Close[0] <= Open[1] && ((Close[0] >= Open[0] && Close[0] <= Open[0] + TickSize) || (Close[0] <= Open[0] && Close[0] >= Open[0] - TickSize)))
{
if (ChartControl != null)
{
BarColor = upColor;
CandleOutlineColorSeries.Set(CurrentBar, downColor);
}
DrawText("Bullish Harami Cross" + CurrentBar, false, "Bullish Harami Cross", 0, Low[0], -10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.DarkCloudCover:
{
#region Dark Cloud Cover
if (CurrentBar < 1 || (TrendStrength > 0 && !upTrend))
return;
if (Open[0] > High[1] && Close[1] > Open[1] && Close[0] < Open[0] && Close[0] <= Close[1] - (Close[1] - Open[1]) / 2 && Close[0] >= Open[1])
{
if (ChartControl != null)
{
CandleOutlineColorSeries.Set(CurrentBar - 1, downColor);
BarColorSeries.Set(CurrentBar - 1, upColor);
BarColor = downColor;
}
DrawText("Dark Cloud Cover" + CurrentBar, false, "Dark Cloud Cover", 1, High[0], 10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.Doji:
{
#region Doji
if (Math.Abs(Close[0] - Open[0]) <= (High[0] - Low[0]) * 0.07)
{
if (ChartControl != null)
{
BarColor = upColor;
CandleOutlineColorSeries.Set(CurrentBar, downColor);
}
int yOffset = Close[0] > Close[Math.Min(1, CurrentBar)] ? 10 : -10;
DrawText("Doji Text" + CurrentBar, false, "Doji", 0, (yOffset > 0 ? High[0] : Low[0]), yOffset, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.DownsideTasukiGap:
{
#region Downside Tasuki Gap
if (CurrentBar < 2)
return;
if (Close[2] < Open[2] && Close[1] < Open[1] && Close[0] > Open[0]
&& High[1] < Low[2]
&& Open[0] > Close[1] && Open[0] < Open[1]
&& Close[0] > Open[1] && Close[0] < Close[2])
{
if (ChartControl != null)
{
BarColor = upColor;
CandleOutlineColorSeries.Set(CurrentBar, downColor);
BarColorSeries.Set(CurrentBar - 1, downColor);
BarColorSeries.Set(CurrentBar - 2, downColor);
}
DrawText("Downside Tasuki Gap" + CurrentBar, false, "Downside Tasuki Gap", 1, High[2], 10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.EveningStar:
{
#region Evening Star
if (CurrentBar < 2)
return;
if (Close[2] > Open[2] && Close[1] > Close[2] && Open[0] < (Math.Abs((Close[1] - Open[1])/2) + Open[1]) && Close[0] < Open[0])
{
if (ChartControl != null)
{
if (Close[0] > Open[0])
{
BarColor = upColor;
CandleOutlineColorSeries.Set(CurrentBar, downColor);
}
else
BarColor = downColor;
if (Close[1] > Open[1])
{
BarColorSeries.Set(CurrentBar - 1, upColor);
CandleOutlineColorSeries.Set(CurrentBar - 1, downColor);
}
else
BarColorSeries.Set(CurrentBar - 1, downColor);
if (Close[2] > Open[2])
{
BarColorSeries.Set(CurrentBar - 2, upColor);
CandleOutlineColorSeries.Set(CurrentBar - 2, downColor);
}
else
BarColorSeries.Set(CurrentBar - 2, downColor);
}
DrawText("Evening Star Text" + CurrentBar, false, "Evening Star", 1, High[1], 10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.FallingThreeMethods:
{
#region Falling Three Methods
if (CurrentBar < 5)
return;
if (Close[4] < Open[4] && Close[0] < Open[0] && Close[0] < Low[4]
&& High[3] < High[4] && Low[3] > Low[4]
&& High[2] < High[4] && Low[2] > Low[4]
&& High[1] < High[4] && Low[1] > Low[4])
{
if (ChartControl != null)
{
BarColor = downColor;
BarColorSeries.Set(CurrentBar - 4, downColor);
int x = 1;
while (x < 4)
{
if (Close[x] > Open[x])
{
BarColorSeries.Set(CurrentBar - x, upColor);
CandleOutlineColorSeries.Set(CurrentBar - x, downColor);
}
else
BarColorSeries.Set(CurrentBar - x, downColor);
x++;
}
}
DrawText("Falling Three Methods" + CurrentBar, false, "Falling Three Methods", 2, Math.Max(High[0], High[4]), 10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.Hammer:
{
#region Hammer
if (TrendStrength > 0)
{
if (!downTrend || MIN(Low, TrendStrength)[0] != Low[0])
return;
}
if (Low[0] < Open[0] - 5 * TickSize && Math.Abs(Open[0] - Close[0]) < (0.10 * (High[0] - Low[0])) && (High[0] - Close[0]) < (0.25 * (High[0] - Low[0])))
{
if (ChartControl != null)
{
if (Close[0] > Open[0])
{
BarColor = upColor;
CandleOutlineColorSeries.Set(CurrentBar, downColor);
}
else
BarColor = downColor;
}
DrawText("Hammer" + CurrentBar, false, "Hammer", 0, Low[0], -10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.HangingMan:
{
#region Hanging Man
if (TrendStrength > 0)
{
if (!upTrend || MAX(High, TrendStrength)[0] != High[0])
return;
}
if (Low[0] < Open[0] - 5 * TickSize && Math.Abs(Open[0] - Close[0]) < (0.10 * (High[0] - Low[0])) && (High[0] - Close[0]) < (0.25 * (High[0] - Low[0])))
{
if (ChartControl != null)
{
if (Close[0] > Open[0])
{
BarColor = upColor;
CandleOutlineColorSeries.Set(CurrentBar, downColor);
}
else
BarColor = downColor;
}
DrawText("Hanging Man" + CurrentBar, false, "Hanging Man", 0, Low[0], -10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.InvertedHammer:
{
#region Inverted Hammer
if (TrendStrength > 0)
{
if (!upTrend || MAX(High, TrendStrength)[0] != High[0])
return;
}
if (High[0] > Open[0] + 5 * TickSize && Math.Abs(Open[0] - Close[0]) < (0.10 * (High[0] - Low[0])) && (Close[0] - Low[0]) < (0.25 * (High[0] - Low[0])))
{
if (ChartControl != null)
{
if (Close[0] > Open[0])
{
BarColor = upColor;
CandleOutlineColorSeries.Set(CurrentBar, downColor);
}
else
BarColor = downColor;
}
DrawText("Inverted Hammer" + CurrentBar, false, "InvertedHammer", 0, High[0] + 5 * TickSize, 0, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.MorningStar:
{
#region Morning Star
if (CurrentBar < 2)
return;
if (Close[2] < Open[2] && Close[1] < Close[2] && Open[0] > (Math.Abs((Close[1] - Open[1])/2) + Open[1]) && Close[0] > Open[0])
{
if (ChartControl != null)
{
if (Close[0] > Open[0])
{
BarColor = upColor;
CandleOutlineColorSeries.Set(CurrentBar, downColor);
}
else
BarColor = downColor;
if (Close[1] > Open[1])
{
BarColorSeries.Set(CurrentBar - 1, upColor);
CandleOutlineColorSeries.Set(CurrentBar - 1, downColor);
}
else
BarColorSeries.Set(CurrentBar - 1, downColor);
if (Close[2] > Open[2])
{
BarColorSeries.Set(CurrentBar - 2, upColor);
CandleOutlineColorSeries.Set(CurrentBar - 2, downColor);
}
else
BarColorSeries.Set(CurrentBar - 2, downColor);
}
DrawText("Morning Star Text" + CurrentBar, false, "Morning Star", 1, Low[1], -10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.PiercingLine:
{
#region Piercing Line
if (CurrentBar < 1 || (TrendStrength > 0 && !downTrend))
return;
if (Open[0] < Low[1] && Close[1] < Open[1] && Close[0] > Open[0] && Close[0] >= Close[1] + (Open[1] - Close[1]) / 2 && Close[0] <= Open[1])
{
if (ChartControl != null)
{
CandleOutlineColorSeries.Set(CurrentBar - 1, downColor);
BarColorSeries.Set(CurrentBar - 1, upColor);
BarColor = downColor;
}
DrawText("Piercing Line" + CurrentBar, false, "Piercing Line", 1, Low[0], -10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.RisingThreeMethods:
{
#region Rising Three Methods
if (CurrentBar < 5)
return;
if (Close[4] > Open[4] && Close[0] > Open[0] && Close[0] > High[4]
&& High[3] < High[4] && Low[3] > Low[4]
&& High[2] < High[4] && Low[2] > Low[4]
&& High[1] < High[4] && Low[1] > Low[4])
{
if (ChartControl != null)
{
BarColor = upColor;
CandleOutlineColorSeries.Set(CurrentBar, downColor);
BarColorSeries.Set(CurrentBar - 4, upColor);
CandleOutlineColorSeries.Set(CurrentBar - 4, downColor);
int x = 1;
while (x < 4)
{
if (Close[x] > Open[x])
{
BarColorSeries.Set(CurrentBar - x, upColor);
CandleOutlineColorSeries.Set(CurrentBar - x, downColor);
}
else
BarColorSeries.Set(CurrentBar - x, downColor);
x++;
}
}
DrawText("Rising Three Methods" + CurrentBar, false, "Rising Three Methods", 2, Math.Min(Low[0], Low[4]), -10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.ShootingStar:
{
#region Shooting Star
if (CurrentBar < 1 || (TrendStrength > 0 && !upTrend))
return;
if (High[0] > Open[0] && (High[0] - Open[0]) >= 2 * (Open[0] - Close[0]) && Close[0] < Open[0] && (Close[0] - Low[0]) <= 2 * TickSize)
{
if (ChartControl != null)
BarColor = downColor;
DrawText("Shooting Star" + CurrentBar, false, "Shooting Star", 0, Low[0], -10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.StickSandwich:
{
#region Stick Sandwich
if (CurrentBar < 2)
return;
if (Close[2] == Close[0] && Close[2] < Open[2] && Close[1] > Open[1] && Close[0] < Open[0])
{
if (ChartControl != null)
{
BarColor = downColor;
BarColorSeries.Set(CurrentBar - 1, upColor);
CandleOutlineColorSeries.Set(CurrentBar - 1, downColor);
BarColorSeries.Set(CurrentBar - 2, downColor);
}
DrawText("Stick Sandwich" + CurrentBar, false, "Stick Sandwich", 1, Math.Min(Low[0], Math.Min(Low[1], Low[2])), -10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.ThreeBlackCrows:
{
#region Three Black Crows
if (CurrentBar < 2 || (TrendStrength > 0 && !upTrend))
return;
if (Value[1] == 0 && Value[2] == 0
&& Close[0] < Open[0] && Close[1] < Open[1] && Close[2] < Open[2]
&& Close[0] < Close[1] && Close[1] < Close[2]
&& Open[0] < Open[1] && Open[0] > Close[1]
&& Open[1] < Open[2] && Open[1] > Close[2])
{
if (ChartControl != null)
{
BarColor = downColor;
BarColorSeries.Set(CurrentBar - 1, downColor);
BarColorSeries.Set(CurrentBar - 2, downColor);
}
DrawText("Three Black Crows" + CurrentBar, false, "Three Black Crows", 1, High[2], 10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.ThreeWhiteSoldiers:
{
#region Three White Soldiers
if (CurrentBar < 2 || (TrendStrength > 0 && !downTrend))
return;
if (Value[1] == 0 && Value[2] == 0
&& Close[0] > Open[0] && Close[1] > Open[1] && Close[2] > Open[2]
&& Close[0] > Close[1] && Close[1] > Close[2]
&& Open[0] < Close[1] && Open[0] > Open[1]
&& Open[1] < Close[2] && Open[1] > Open[2])
{
if (ChartControl != null)
{
BarColor = upColor;
CandleOutlineColorSeries.Set(CurrentBar, downColor);
BarColorSeries.Set(CurrentBar - 1, upColor);
CandleOutlineColorSeries.Set(CurrentBar - 1, downColor);
BarColorSeries.Set(CurrentBar - 2, upColor);
CandleOutlineColorSeries.Set(CurrentBar - 2, downColor);
}
DrawText("Three White Soldiers" + CurrentBar, false, "Three White Soldiers", 1, Low[2], -10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.UpsideGapTwoCrows:
{
#region Upside Gap Two Crows
if (CurrentBar < 2 || (TrendStrength > 0 && !upTrend))
return;
if (Close[2] > Open[2] && Close[1] < Open[1] && Close[0] < Open[0]
&& Low[1] > High[2]
&& Close[0] > High[2]
&& Close[0] < Close[1] && Open[0] > Open[1])
{
if (ChartControl != null)
{
BarColor = downColor;
BarColorSeries.Set(CurrentBar - 1, downColor);
BarColorSeries.Set(CurrentBar - 2, upColor);
CandleOutlineColorSeries.Set(CurrentBar - 2, downColor);
}
DrawText("Upside Gap Two Crows" + CurrentBar, false, "Upside Gap Two Crows", 1, Math.Max(High[0], High[1]), 10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
case ChartPattern.UpsideTasukiGap:
{
#region Upside Tasuki Gap
if (CurrentBar < 2)
return;
if (Close[2] > Open[2] && Close[1] > Open[1] && Close[0] < Open[0]
&& Low[1] > High[2]
&& Open[0] < Close[1] && Open[0] > Open[1]
&& Close[0] < Open[1] && Close[0] > Close[2])
{
if (ChartControl != null)
{
BarColor = downColor;
BarColorSeries.Set(CurrentBar - 1, upColor);
CandleOutlineColorSeries.Set(CurrentBar - 1, downColor);
BarColorSeries.Set(CurrentBar - 2, upColor);
CandleOutlineColorSeries.Set(CurrentBar - 2, downColor);
}
DrawText("Upside Tasuki Gap" + CurrentBar, false, "Upside Tasuki Gap", 1, Math.Max(High[0], High[1]), 10, txtColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
patternsFound++;
Value.Set(1);
}
#endregion
break;
}
}
DrawTextFixed("Count", patternsFound.ToString() + " patterns found", TextPosition.BottomRight);
}
#region Properties
/// <summary>
/// Gets a value indicating if a pattern was found
/// </summary>
[Browsable(false)]
[XmlIgnore()]
public DataSeries PatternFound
{
get { return Values[0]; }
}
[Description("Number of bars required to define a trend when a pattern requires a prevailing trend. A value of zero will disable trend requirement.")]
[GridCategory("Parameters")]
[Gui.Design.DisplayName("Trend strength")]
public int TrendStrength
{
get { return trendStrength; }
set { trendStrength = Math.Max(0, value); }
}
[Description("Choose a candlestick pattern to chart.")]
[GridCategory("Parameters")]
[Gui.Design.DisplayName("Chart Pattern")]
public ChartPattern Pattern
{
get { return pattern; }
set { pattern = value; }
}
#endregion
#region Misc
public override string ToString()
{
return Name + "(" + pattern + ")";
}
// Calculate trend lines and prevailing trend
private void CalculateTrendLines()
{
// Calculate up trend line
int upTrendStartBarsAgo = 0;
int upTrendEndBarsAgo = 0;
int upTrendOccurence = 1;
while (Low[upTrendEndBarsAgo] <= Low[upTrendStartBarsAgo])
{
upTrendStartBarsAgo = Swing(TrendStrength).SwingLowBar(0, upTrendOccurence + 1, CurrentBar);
upTrendEndBarsAgo = Swing(TrendStrength).SwingLowBar(0, upTrendOccurence, CurrentBar);
if (upTrendStartBarsAgo < 0 || upTrendEndBarsAgo < 0)
break;
upTrendOccurence++;
}
// Calculate down trend line
int downTrendStartBarsAgo = 0;
int downTrendEndBarsAgo = 0;
int downTrendOccurence = 1;
while (High[downTrendEndBarsAgo] >= High[downTrendStartBarsAgo])
{
downTrendStartBarsAgo = Swing(TrendStrength).SwingHighBar(0, downTrendOccurence + 1, CurrentBar);
downTrendEndBarsAgo = Swing(TrendStrength).SwingHighBar(0, downTrendOccurence, CurrentBar);
if (downTrendStartBarsAgo < 0 || downTrendEndBarsAgo < 0)
break;
downTrendOccurence++;
}
if (upTrendStartBarsAgo > 0 && upTrendEndBarsAgo > 0 && upTrendStartBarsAgo < downTrendStartBarsAgo)
{
upTrend = true;
downTrend = false;
}
else if (downTrendStartBarsAgo > 0 && downTrendEndBarsAgo > 0 && upTrendStartBarsAgo > downTrendStartBarsAgo)
{
upTrend = false;
downTrend = true;
}
else
{
upTrend = false;
downTrend = false;
}
}
#endregion
}
}
public enum ChartPattern
{
BearishBeltHold,
BearishEngulfing,
BearishHarami,
BearishHaramiCross,
BullishBeltHold,
BullishEngulfing,
BullishHarami,
BullishHaramiCross,
DarkCloudCover,
Doji,
DownsideTasukiGap,
EveningStar,
FallingThreeMethods,
Hammer,
HangingMan,
InvertedHammer,
MorningStar,
PiercingLine,
RisingThreeMethods,
ShootingStar,
StickSandwich,
ThreeBlackCrows,
ThreeWhiteSoldiers,
UpsideGapTwoCrows,
UpsideTasukiGap,
}
#region NinjaScript generated code. Neither change nor remove.
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
public partial class Indicator : IndicatorBase
{
private CandleStickPattern[] cacheCandleStickPattern = null;
private static CandleStickPattern checkCandleStickPattern = new CandleStickPattern();
/// <summary>
/// Detects common candlestick patterns and marks them on the chart.
/// </summary>
/// <returns></returns>
public CandleStickPattern CandleStickPattern(ChartPattern pattern, int trendStrength)
{
return CandleStickPattern(Input, pattern, trendStrength);
}
/// <summary>
/// Detects common candlestick patterns and marks them on the chart.
/// </summary>
/// <returns></returns>
public CandleStickPattern CandleStickPattern(Data.IDataSeries input, ChartPattern pattern, int trendStrength)
{
if (cacheCandleStickPattern != null)
for (int idx = 0; idx < cacheCandleStickPattern.Length; idx++)
if (cacheCandleStickPattern[idx].Pattern == pattern && cacheCandleStickPattern[idx].TrendStrength == trendStrength && cacheCandleStickPattern[idx].EqualsInput(input))
return cacheCandleStickPattern[idx];
lock (checkCandleStickPattern)
{
checkCandleStickPattern.Pattern = pattern;
pattern = checkCandleStickPattern.Pattern;
checkCandleStickPattern.TrendStrength = trendStrength;
trendStrength = checkCandleStickPattern.TrendStrength;
if (cacheCandleStickPattern != null)
for (int idx = 0; idx < cacheCandleStickPattern.Length; idx++)
if (cacheCandleStickPattern[idx].Pattern == pattern && cacheCandleStickPattern[idx].TrendStrength == trendStrength && cacheCandleStickPattern[idx].EqualsInput(input))
return cacheCandleStickPattern[idx];
CandleStickPattern indicator = new CandleStickPattern();
indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
#if NT7
indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
indicator.MaximumBarsLookBack = MaximumBarsLookBack;
#endif
indicator.Input = input;
indicator.Pattern = pattern;
indicator.TrendStrength = trendStrength;
Indicators.Add(indicator);
indicator.SetUp();
CandleStickPattern[] tmp = new CandleStickPattern[cacheCandleStickPattern == null ? 1 : cacheCandleStickPattern.Length + 1];
if (cacheCandleStickPattern != null)
cacheCandleStickPattern.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cacheCandleStickPattern = tmp;
return indicator;
}
}
}
}
// This namespace holds all market analyzer column definitions and is required. Do not change it.
namespace NinjaTrader.MarketAnalyzer
{
public partial class Column : ColumnBase
{
/// <summary>
/// Detects common candlestick patterns and marks them on the chart.
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.CandleStickPattern CandleStickPattern(ChartPattern pattern, int trendStrength)
{
return _indicator.CandleStickPattern(Input, pattern, trendStrength);
}
/// <summary>
/// Detects common candlestick patterns and marks them on the chart.
/// </summary>
/// <returns></returns>
public Indicator.CandleStickPattern CandleStickPattern(Data.IDataSeries input, ChartPattern pattern, int trendStrength)
{
return _indicator.CandleStickPattern(input, pattern, trendStrength);
}
}
}
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// Detects common candlestick patterns and marks them on the chart.
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.CandleStickPattern CandleStickPattern(ChartPattern pattern, int trendStrength)
{
return _indicator.CandleStickPattern(Input, pattern, trendStrength);
}
/// <summary>
/// Detects common candlestick patterns and marks them on the chart.
/// </summary>
/// <returns></returns>
public Indicator.CandleStickPattern CandleStickPattern(Data.IDataSeries input, ChartPattern pattern, int trendStrength)
{
if (InInitialize && input == null)
throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");