-
Notifications
You must be signed in to change notification settings - Fork 91
/
OrderFlow.cs
1184 lines (1093 loc) · 47.4 KB
/
OrderFlow.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
#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
using NinjaTrader.NinjaScript.AddOns.SightEngine;
using NinjaTrader.NinjaScript.AddOns.WyckoffRenderUtils;
public static class _OrderFlowEnums
{
public enum Calculation
{
BidAsk,
TotalDelta,
Total,
Delta
}
public enum Representation
{
Volume,
Percent
}
public enum Style
{
Profile,
HeatMap
}
public enum Position
{
Left,
//Center,
Right
}
}
namespace NinjaTrader.NinjaScript.Indicators.WyckoffZen
{
public class OrderFlow : Indicator
{
#region MAIN
private class WyckoffOrderFlow : WyckoffRenderControl
{
private SharpDX.RectangleF Rect, Rect2;
private Action _renderOFText;
private Action<float, float> renderClusterRect;
private SharpDX.DirectWrite.TextFormat volumeTextFormat;
private SharpDX.Vector2 beg_maxClusterVec;
private SharpDX.Vector2 end_maxClusterVec;
// !- setup brushes
private SharpDX.Color colorBidClusterColor;
private SharpDX.Color colorAskClusterColor;
private SharpDX.Color colorTotalClusterColor;
private SharpDX.Color colorBidFontColor;
private SharpDX.Color colorAskFontColor;
private SharpDX.Color colorTotalFontColor;
private SharpDX.Color colorMaxVolumeClusterColor;
private SharpDX.Color colorMinVolumeClusterColor;
private SharpDX.Color colorPOCLines;
private SharpDX.Color colorPOILines;
// !- setup opacity
private float brushBidFontOpacity;
private float brushAskFontOpacity;
private float totalFontOpacity;
private float maxClusterOpacity;
private float minClusterOpacity;
private float clustersOpacity;
private float POCLinesOpacity;
private float POILinesOpacity;
// !- setup calculation
private float minFontWidth;
private float minFontHeight;
private _OrderFlowEnums.Position orderFlowPosition;
private _OrderFlowEnums.Style orderFlowStyle;
private _OrderFlowEnums.Calculation orderFlowCalculation;
private _OrderFlowEnums.Representation orderFlowRepresentation;
// !- setup renders
private bool showClusterPOC;
private bool showClusterPOI;
private bool showPOCSLines;
private bool showPOISLines;
private bool showText;
private bool showOrderFlow;
// !- setup lines style
private float POCLines_strokeWidth;
private SharpDX.Direct2D1.StrokeStyle POCLines_strokeStyle;
private float POILines_strokeWidth;
private SharpDX.Direct2D1.StrokeStyle POILines_strokeStyle;
// !- Para copiado de datos internos;
private float barX;
private float barY;
private VolumeAnalysis.WyckoffBars wyckoffBars;
private VolumeAnalysis.WyckoffBars.Bar currentBar;
private VolumeAnalysis.MarketOrder volumeInfo;
private bool realTime;
public WyckoffOrderFlow()
{
this.volumeInfo = new VolumeAnalysis.MarketOrder();
this.Rect = new SharpDX.RectangleF();
this.Rect2= new SharpDX.RectangleF();
this.realTime = false;
this.beg_maxClusterVec = new SharpDX.Vector2();
this.end_maxClusterVec = new SharpDX.Vector2();
}
#region SET_ORDER_FLOW_STYLE
public void setFontStyle(SimpleFont font)
{
base.setFontStyle(font, out volumeTextFormat);
}
public void setBidAskClusterColor(Brush brushBidClusterColor, Brush brushAskClusterColor)
{
this.colorBidClusterColor = WyckoffRenderControl.BrushToColor(brushBidClusterColor);
this.colorAskClusterColor = WyckoffRenderControl.BrushToColor(brushAskClusterColor);
}
public void setMaxMinVolumeClusterColor(
Brush brushMaxVolumeClusterColor, float maxClusterOpacity,
Brush brushMinVolumeClusterColor, float minClusterOpacity
)
{
this.colorMaxVolumeClusterColor = WyckoffRenderControl.BrushToColor(brushMaxVolumeClusterColor);
this.maxClusterOpacity = maxClusterOpacity / 100f;
this.colorMinVolumeClusterColor = WyckoffRenderControl.BrushToColor(brushMinVolumeClusterColor);
this.minClusterOpacity = minClusterOpacity / 100f;
}
public void setPOCPOILines(
Brush brushPOCLines, float POCLines_strokeWidth, DashStyleHelper POCstrokeStyle, float POCLinesOpacity,
Brush brushPOILines, float POILines_strokeWidth, DashStyleHelper POIstrokeStyle, float POILinesOpacity
)
{
this.colorPOCLines = WyckoffRenderControl.BrushToColor(brushPOCLines);
this.POCLinesOpacity = POCLinesOpacity / 100;
this.POCLines_strokeWidth = POCLines_strokeWidth;
SharpDX.Direct2D1.StrokeStyleProperties POCLines_strokeStyleProperties = new SharpDX.Direct2D1.StrokeStyleProperties();
POCLines_strokeStyleProperties.DashStyle = DashStyleHelperToDX(POCstrokeStyle);
this.POCLines_strokeStyle = new SharpDX.Direct2D1.StrokeStyle(NinjaTrader.Core.Globals.D2DFactory, POCLines_strokeStyleProperties);
this.colorPOILines = WyckoffRenderControl.BrushToColor(brushPOILines);
this.POILinesOpacity = POILinesOpacity / 100;
this.POILines_strokeWidth = POCLines_strokeWidth;
SharpDX.Direct2D1.StrokeStyleProperties POILines_strokeStyleProperties = new SharpDX.Direct2D1.StrokeStyleProperties();
POILines_strokeStyleProperties.DashStyle = DashStyleHelperToDX(POIstrokeStyle);
this.POILines_strokeStyle = new SharpDX.Direct2D1.StrokeStyle(NinjaTrader.Core.Globals.D2DFactory, POILines_strokeStyleProperties);
}
public void setBidAskFontColor(
Brush brushBidFontColor, float bidOpacity,
Brush brushAskFontColor, float askOpacity)
{
this.colorBidFontColor = WyckoffRenderControl.BrushToColor(brushBidFontColor);
this.brushBidFontOpacity = bidOpacity / 100f;
this.colorAskFontColor = WyckoffRenderControl.BrushToColor(brushAskFontColor);
this.brushAskFontOpacity = askOpacity / 100f;
}
public void setTotalFontColor(Brush brushTotalClusterColor, Brush brushTotalFontColor, float totalOpacity)
{
this.colorTotalClusterColor = WyckoffRenderControl.BrushToColor(brushTotalClusterColor);
this.colorTotalFontColor = WyckoffRenderControl.BrushToColor(brushTotalFontColor);
this.totalFontOpacity = totalOpacity / 100f;
}
public void setMinSizeFont(float minFontWidth, float minFontHeight)
{
this.minFontWidth = minFontWidth;
this.minFontHeight = minFontHeight;
}
public void setShows(
bool showClusterPOC, bool showClusterPOI,
bool showPOCSLines, bool showPOISLines,
bool showText, bool showOrderFlow)
{
this.showClusterPOC = showClusterPOC;
this.showClusterPOI = showClusterPOI;
this.showPOCSLines = showPOCSLines;
this.showPOISLines = showPOISLines;
this.showText = showText;
this.showOrderFlow = showOrderFlow;
}
public void setPosition(_OrderFlowEnums.Position orderFlowPosition)
{
this.orderFlowPosition = orderFlowPosition;
}
public void setStyle(_OrderFlowEnums.Style orderFlowStyle)
{
this.orderFlowStyle = orderFlowStyle;
}
public void setClustersOpacity(float clustersOpacity)
{
this.clustersOpacity = clustersOpacity / 100f;
}
#endregion
#region RENDER_INFORMATION
private void __renderBidAskText()
{
long B = 0;
long A = 0;
string C = "";
switch( this.orderFlowRepresentation )
{
case _OrderFlowEnums.Representation.Volume:
{
B = this.volumeInfo.Bid;
A = this.volumeInfo.Ask;
break;
}
case _OrderFlowEnums.Representation.Percent:
{
long total = this.volumeInfo.Total;
B = (long)Math2.Percent(total, this.volumeInfo.Bid);
A = (long)Math2.Percent(total, this.volumeInfo.Ask);
// !- agregamos el simbolo de %
C = "%";
break;
}
}
long D = volumeInfo.Delta;
if( D > 0 ){
myDrawText(string.Format("{0}x{1}"+C, B, A), ref Rect, colorAskFontColor, -1,-1, volumeTextFormat, brushAskFontOpacity);
}
else{
myDrawText(string.Format("{0}x{1}"+C, B, A), ref Rect, colorBidFontColor, -1,-1, volumeTextFormat, brushBidFontOpacity);
}
}
private void __renderTotalDeltaText()
{
long T = 0;
long D = 0;
string C = "";
switch( this.orderFlowRepresentation )
{
case _OrderFlowEnums.Representation.Volume:
{
T = this.volumeInfo.Total;
D = this.volumeInfo.Delta;
break;
}
case _OrderFlowEnums.Representation.Percent:
{
long total = this.volumeInfo.Total;
T = total;
D = (long)Math2.Percent(total, Math.Abs(this.volumeInfo.Delta));
// !- agregamos el simbolo de %
C = "%";
break;
}
}
if( this.volumeInfo.Delta >= 0 ){
myDrawText(string.Format("{0}x{1}"+C, T, D), ref Rect, colorAskFontColor, -1,-1, volumeTextFormat, brushAskFontOpacity);
}
else{
myDrawText(string.Format("{0}x{1}"+C, T, D), ref Rect, colorBidFontColor, -1,-1, volumeTextFormat, brushBidFontOpacity);
}
}
private void __renderTotalText()
{
myDrawText(volumeInfo.Total.ToString(), ref Rect, colorTotalFontColor, -1,-1, volumeTextFormat, totalFontOpacity);
}
private void __renderDeltaText()
{
long D = this.volumeInfo.Delta;
string s_D = string.Empty;
switch( this.orderFlowRepresentation )
{
case _OrderFlowEnums.Representation.Volume:
{
s_D = D.ToString();
break;
}
case _OrderFlowEnums.Representation.Percent:
{
long total = this.volumeInfo.Total;
s_D = Math2.Percent(total, Math.Abs(D)).ToString();
// !- agregamos el simbolo de %
s_D+= "%";
break;
}
}
if( D >= 0 ){
myDrawText(s_D.ToString(), ref Rect, colorAskFontColor, -1,-1, volumeTextFormat, brushAskFontOpacity);
}
else{
myDrawText(s_D.ToString(), ref Rect, colorBidFontColor, -1,-1, volumeTextFormat, brushBidFontOpacity);
}
}
public void setCalculation(_OrderFlowEnums.Calculation orderFlowCalculation)
{
switch( orderFlowCalculation )
{
//case _OrderFlowEnums.Calculation.TotalBidAsk:
case _OrderFlowEnums.Calculation.BidAsk:
{
this._renderOFText = this.__renderBidAskText;
this.renderClusterRect = this.__renderBidAsk;
break;
}
case _OrderFlowEnums.Calculation.TotalDelta:
{
this._renderOFText = this.__renderTotalDeltaText;
this.renderClusterRect = this.__renderDelta;
break;
}
case _OrderFlowEnums.Calculation.Total:
{
this._renderOFText = this.__renderTotalText;
this.renderClusterRect = this.__renderTotal;
break;
}
case _OrderFlowEnums.Calculation.Delta:
{
this._renderOFText = this.__renderDeltaText;
this.renderClusterRect = this.__renderDelta;
break;
}
}
this.orderFlowCalculation = orderFlowCalculation;
}
public void setRepresentation(_OrderFlowEnums.Representation orderFlowRepresentation)
{
this.orderFlowRepresentation = orderFlowRepresentation;
}
#endregion
public void setWyckoffBars(VolumeAnalysis.WyckoffBars wyckoffBars)
{
this.wyckoffBars = wyckoffBars;
}
public void setRealtime(bool isRealtime)
{
this.realTime = isRealtime;
}
public bool IsRealtime
{
get{ return this.realTime; }
}
// *- para la posicion del order flow, clusters y texto, si el resultado es negativo
// se invierte la posicion
private bool calculateXPosition(out float barXpos)
{
barXpos = 0;
switch(orderFlowPosition)
{
case _OrderFlowEnums.Position.Right:
{
barXpos = (W / 4f);// - (BarW*4f);
break;
}
case _OrderFlowEnums.Position.Left:
{
barXpos = -(W / 4f);
return true;
}
// case _OrderFlowEnums.Position.Center:
// {
// barXpos = -(W / 4f);
// break;
// }
}
return false;
}
private float calculateXPositionFont()
{
if( orderFlowPosition == _OrderFlowEnums.Position.Left )//switch(orderFlowPosition)
{
return -W;
// !- No necesitamos calcular la fuente
//case _OrderFlowEnums.Position.Right:{ return 0; }
//case _OrderFlowEnums.Position.Left:{ return -W; }
//case _OrderFlowEnums.Position.Center:{ return -(W / 2f); }
}
return 0;
}
private float calculateClusterPOCPercent()
{
switch( this.orderFlowCalculation )
{
// case _OrderFlowEnums.Calculation.TotalBidAsk:
// {
// long vol;
// if( this.volumeInfo.Delta >= 0 )
// vol = this.volumeInfo.Ask;
// else
// vol = this.volumeInfo.Bid;
// return (float)Math2.Percent(this.currentBar.MaxClusterVolume.Total, vol);
// }
case _OrderFlowEnums.Calculation.BidAsk:
case _OrderFlowEnums.Calculation.Total:
{
// !- Obtenemos el porcentaje de volumen a partir del cluster maximo, en este punto
// @maxClusterVolume representa el 100% y @vs.Total es el volumen en cada nivel
// de precio, entonces si: maxClusterVolume == vs.Total el porcentaje sera 100%
return (float)Math2.Percent(this.currentBar.MaxClusterVolume.Total, this.volumeInfo.Total);
}
case _OrderFlowEnums.Calculation.TotalDelta:
{
return (float)Math2.Percent(this.currentBar.MaxClusterVolume.Total, Math.Abs(this.volumeInfo.Delta));
}
case _OrderFlowEnums.Calculation.Delta:
{
return (float)Math2.Percent(Math.Abs(this.currentBar.MaxClusterVolume.Delta), Math.Abs(this.volumeInfo.Delta));
}
}
return 0;
}
private void __renderBidAsk(float clusterPOC_per, float opacity)
{
bool invertSign = false;
long vol = this.currentBar.MaxClusterVolume.Total;
float bidPer = (float)Math2.Percent(vol, this.volumeInfo.Bid) / 100f;
float askPer = (float)Math2.Percent(vol, this.volumeInfo.Ask) / 100f;
this.Rect2.Y = this.Rect.Y;
this.Rect2.Height = this.Rect.Height;
// !- calculamos el estilo(profile, heatmap)
switch( orderFlowStyle )
{
case _OrderFlowEnums.Style.Profile:
{
float bar_x;
invertSign = this.calculateXPosition(out bar_x);
Rect.X = barX + bar_x;//barX + (W / 4f);
Rect2.X= Rect.X;
// !- Anchura maxima: W / 2
// el cual representa la anchura total de cada cluster, a partir de esto el
// calculo (% * W) nos dara que cantidad de pixeles corresponde a cada cluster
float width = W / 2f;
float width_per, width_per2;
width_per = (float)Math.Round(bidPer * width);
width_per2= (float)Math.Round(askPer * width);
if( invertSign ){
width_per = -width_per;
width_per2= -width_per2;
}
Rect.Width = width_per;
Rect2.Width= width_per2;
break;
}
case _OrderFlowEnums.Style.HeatMap:
{
Rect.X = barX - (W / 2f);
Rect2.X= Rect.X;
//float width = W; // 2f;
//if( invertSign )
//width = -width;
Rect.Width = this.W;
Rect2.Width= this.W;
break;
}
}
myFillRectangle(ref Rect, colorBidClusterColor, bidPer);
myFillRectangle(ref Rect2,colorAskClusterColor, askPer);
}
private void __renderDelta(float clusterPOC_per, float opacity)
{
long D = this.volumeInfo.Delta;
if( D == 0 )
return;
switch( this.orderFlowStyle )
{
case _OrderFlowEnums.Style.Profile:
{
float bar_x;
bool invertSign = this.calculateXPosition(out bar_x);
Rect.X = barX + bar_x;//barX + (W / 4f);
// !- Anchura maxima: W / 2
// el cual representa la anchura total de cada cluster, a partir de esto el
// calculo (% * W) nos dara que cantidad de pixeles corresponde a cada cluster
float width = W / 2f;
float width_per = (float)Math.Round((clusterPOC_per * width) / 100);
if( invertSign )
width_per= -width_per;
Rect.Width = width_per;
break;
}
case _OrderFlowEnums.Style.HeatMap:
{
Rect.X = barX - (W / 2f);
Rect.Width = W;
break;
}
}
if( D >= 0 )
myFillRectangle(ref Rect, colorAskClusterColor, clustersOpacity);
else
myFillRectangle(ref Rect, colorBidClusterColor, clustersOpacity);
}
private void __renderTotal(float clusterPOC_per, float opacity)
{
switch( this.orderFlowStyle )
{
case _OrderFlowEnums.Style.Profile:
{
float bar_x;
bool invertSign = this.calculateXPosition(out bar_x);
Rect.X = barX + bar_x;//barX + (W / 4f);
// !- Anchura maxima: W / 2
// el cual representa la anchura total de cada cluster, a partir de esto el
// calculo (% * W) nos dara que cantidad de pixeles corresponde a cada cluster
float width = W / 2f;
float width_per = (float)Math.Round((clusterPOC_per * width) / 100);
if( invertSign )
width_per= -width_per;
Rect.Width = width_per;
break;
}
case _OrderFlowEnums.Style.HeatMap:
{
Rect.X = barX - (W / 2f);
Rect.Width = W;
break;
}
}
myFillRectangle(ref Rect, colorTotalClusterColor, opacity);
}
private void renderCluster()
{
if( !this.showOrderFlow ){
return;
}
this.Rect.Y = barY - (this.H / 2f);
this.Rect.Height = this.H;
float clusterPOC_per = this.calculateClusterPOCPercent();
float opacity = this.clustersOpacity == 0 ? 1.0f : (clusterPOC_per / 100f) * this.clustersOpacity;
this.renderClusterRect(clusterPOC_per, opacity);
}
private void renderMinMaxCluster(double price)
{
Rect.Y = this.barY - (H / 2f);
Rect.Height = H;
switch( orderFlowStyle )
{
case _OrderFlowEnums.Style.Profile:{
float bar_x;
bool invertSign = this.calculateXPosition(out bar_x);
Rect.X = barX + bar_x;
float minmax_clus_width = W / 2f;
if( invertSign )
minmax_clus_width = -minmax_clus_width;
Rect.Width = minmax_clus_width;
break;
}
case _OrderFlowEnums.Style.HeatMap:{
Rect.X = barX - (W / 2f);
Rect.Width = W;
break;
}
}
// !- cluster maximo, es necesario que el precio actual coincida con el precio del cluster maximo
// de otro modo si el volumen total es identico(se repite en la vela mas de una vez) el renderizado
// mostratra mas de un cluster maximo confundiendo cual fue el ultimo maximo...
if( this.showClusterPOC && Math2.Percent(currentBar.MaxClusterVolume.Total, volumeInfo.Total) == 100 && currentBar.MaxClusterPrice == price ){
myDrawRectangle(ref Rect, colorMaxVolumeClusterColor, maxClusterOpacity, 1.5f);
}
// !- cluster minimo, misma logica
if( this.showClusterPOI && Math2.Percent(currentBar.MinClusterVolume.Total, volumeInfo.Total) == 100 && currentBar.MinClusterPrice == price ){
myFillRectangle(ref Rect, colorMinVolumeClusterColor, minClusterOpacity);
}
}
private void renderText()
{
// !- renderizamos el texto
if( this.showText && W >= minFontWidth && H >= minFontHeight )
{
switch( orderFlowStyle )
{
case _OrderFlowEnums.Style.Profile:
{
Rect.X = this.barX + this.calculateXPositionFont();
break;
}
case _OrderFlowEnums.Style.HeatMap:
{
Rect.X = this.barX - (W / 2f);
break;
}
}
Rect.Y = this.barY - (H / 2f);
Rect.Width = W;
Rect.Height = H;
this._renderOFText();//RenderTarget.DrawText(string.Format("{0}x{1}", T, D), volumeTextFormat, Rect, Brushes.CornflowerBlue.ToDxBrush(RenderTarget, 1.0f));
}
}
// !- Renderizado para onRender
private void renderBarCluster(double price)
{
renderCluster();
renderMinMaxCluster(price);
renderText();
}
private void renderLines(int barIndex)
{
if( barIndex > 0 ){
int prevBarIndex = barIndex - 1;
VolumeAnalysis.WyckoffBars.Bar prevBar = this.wyckoffBars[prevBarIndex];
float prevBarX = CHART_CONTROL.GetXByBarIndex(CHART_BARS, prevBarIndex);
beg_maxClusterVec.X = this.barX;
end_maxClusterVec.X = prevBarX;
if( this.orderFlowStyle == _OrderFlowEnums.Style.Profile )
{
switch( this.orderFlowPosition )
{
case _OrderFlowEnums.Position.Right:
{
float barW = W / 2f;
beg_maxClusterVec.X += barW;//(W / 4f);
end_maxClusterVec.X += barW;
break;
}
case _OrderFlowEnums.Position.Left:
{
float barW = W / 2f;
beg_maxClusterVec.X -= barW;
end_maxClusterVec.X -= barW;
break;
}
/* // podemos omitirlo es redundante
case _OrderFlowEnums.Position.Center:
{
beg_maxClusterVec.X = this.barX;
end_maxClusterVec.X = prevBarX;
break;
}*/
}
/* // podemos omitirlo es redundante
case _OrderFlowEnums.Style.HeatMap:
{
break;
}*/
}
// !- POC Lines
if(this.showPOCSLines){
beg_maxClusterVec.Y = CHART_SCALE.GetYByValue(currentBar.MaxClusterPrice);
end_maxClusterVec.Y = CHART_SCALE.GetYByValue(prevBar.MaxClusterPrice);
myDrawLine(ref beg_maxClusterVec, ref end_maxClusterVec,
colorPOCLines, POCLinesOpacity,
POCLines_strokeWidth, POCLines_strokeStyle);
}
// !- POI lines
if(this.showPOISLines){
beg_maxClusterVec.Y = CHART_SCALE.GetYByValue(currentBar.MinClusterPrice);
end_maxClusterVec.Y = CHART_SCALE.GetYByValue(prevBar.MinClusterPrice);
myDrawLine(ref beg_maxClusterVec, ref end_maxClusterVec,
colorPOILines, POILinesOpacity,
POILines_strokeWidth, POILines_strokeStyle);
}
}
}
public void renderBarClusters(int barIndex, bool realtimeCalculation)
{
// !- Optimizamos para calculos en tiempo-real
if( realtimeCalculation ){
//if( !this.wyckoffBars.BarExists(barIndex) ) return;
this.currentBar = this.wyckoffBars.CurrentBar;//this.currentBar = this.wyckoffBars[barIndex];
this.currentBar.CalculateMinAndMaxCluster();
}
else{
this.currentBar = this.wyckoffBars[barIndex];
}
this.barX = CHART_CONTROL.GetXByBarIndex(CHART_BARS, barIndex);
// -- renderizamos las lineas de POCs y POIs si estas fueron calculadas
renderLines(barIndex);
double price;
foreach(var wb in currentBar){
price = wb.Key;
this.barY = CHART_SCALE.GetYByValue(price);
// !- informacion de volumen
this.volumeInfo = wb.Value;
// !- renderizamos el cluster precio a precio
renderBarCluster(price);
}
}
}
#endregion
#region GLOBAL_VARIABLES
private VolumeAnalysis.WyckoffBars wyckoffBars;
private WyckoffOrderFlow wyckoffOF;
#endregion
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
wyckoffOF = new WyckoffOrderFlow();
Description = @"";
Name = "Order Flow";
Calculate = Calculate.OnEachTick;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
//See Help Guide for additional information.
IsSuspendedWhileInactive = false;
// !- Setup de estilo
_TextFont = new SimpleFont();
_TextFont.Family = new FontFamily("Arial");
_TextFont.Size = 10f;
_TextFont.Bold = false;
_TextFont.Italic= false;
_MinFontWidth = 10f; _MinFontHeight= 10f;
_BidTextVolumeColor = Brushes.LightCoral;
_BidTextOpacity = 100f;
_AskTextVolumeColor = Brushes.DarkSeaGreen;
_AskTextOpacity = 100f;
_TotalTextVolumeColor = Brushes.Beige;
_TotalTextOpacity = 80f;
_AskClusterVolumeColor = Brushes.SeaGreen;
_BidClusterVolumeColor = Brushes.Red;
_TotalClusterVolumeColor = Brushes.LightSkyBlue;
_MaxClusterVolumeColor = Brushes.PowderBlue;
_MaxClusterOpacity = 60f;
_MinClusterVolumeColor = Brushes.Violet;
_MinClusterOpacity = 20f;
// !- Por defecto tiene un 70% de opacidad cada nivel de cluster
_ClustersOpacity = 70f;
// !- Estilo de lineas POCs y POIs
_POCLinesColor = Brushes.WhiteSmoke;
_POCLinesOpacity = 70f;
_POCLines_strokeWidth = 1.0f;
_POCLinesStrokeStyle = DashStyleHelper.Solid;//SharpDX.Direct2D1.DashStyle.Solid;
_POILinesColor = Brushes.Violet;
_POILinesOpacity = 40f;
_POILines_strokeWidth = 1.0f;
_POILinesStrokeStyle = DashStyleHelper.Dash;//SharpDX.Direct2D1.DashStyle.Dash;
// !- Calculos del order flow por defecto
_OrderFlowCalculation = _OrderFlowEnums.Calculation.BidAsk;
_OrderFlowRepresentation = _OrderFlowEnums.Representation.Volume;
_OrderFlowPosition = _OrderFlowEnums.Position.Right;
_ShowClusterPOC = true;
_ShowPOCSLines = false;
_ShowClusterPOI = false;
_ShowPOISLines = false;
_ShowText = true;
_ShowOrderFlow = true;
// !- calculamos la ultima barra creada en tiempo real/mercado (?)
_RealtimeHeuristic = true;
}
else if (State == State.Configure)
{
wyckoffOF.setFontStyle(_TextFont);
wyckoffOF.setCalculation(_OrderFlowCalculation);
wyckoffOF.setRepresentation(_OrderFlowRepresentation);
wyckoffOF.setPosition(_OrderFlowPosition);
wyckoffOF.setStyle(_OrderFlowStyle);
wyckoffOF.setBidAskClusterColor(_BidClusterVolumeColor, _AskClusterVolumeColor);
wyckoffOF.setBidAskFontColor(_BidTextVolumeColor, _BidTextOpacity,
_AskTextVolumeColor, _AskTextOpacity);
wyckoffOF.setTotalFontColor(_TotalClusterVolumeColor, _TotalTextVolumeColor, _TotalTextOpacity);
wyckoffOF.setMaxMinVolumeClusterColor(_MaxClusterVolumeColor, _MaxClusterOpacity,
_MinClusterVolumeColor, _MinClusterOpacity);
wyckoffOF.setPOCPOILines(
_POCLinesColor, _POCLines_strokeWidth, _POCLinesStrokeStyle, _POCLinesOpacity,
_POILinesColor, _POILines_strokeWidth, _POILinesStrokeStyle, _POILinesOpacity);
wyckoffOF.setMinSizeFont(_MinFontWidth, _MinFontHeight);
wyckoffOF.setClustersOpacity(_ClustersOpacity);
wyckoffOF.setShows(_ShowClusterPOC, _ShowClusterPOI,
_ShowPOCSLines, _ShowPOISLines,
_ShowText, _ShowOrderFlow);
Calculate= Calculate.OnEachTick;
}
else if(State == State.DataLoaded)
{
wyckoffBars = new VolumeAnalysis.WyckoffBars(Bars);
wyckoffOF.setWyckoffBars(wyckoffBars);
}
else if(State == State.Realtime)
{
wyckoffOF.setRealtime(true);
}
else if(State == State.Terminated)
{
if( ChartControl != null )
ChartControl.Properties.BarMarginRight = 0;
}
}
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
{
base.OnRender(chartControl, chartScale);
if( !wyckoffOF.IsRealtime || IsInHitTest == null ||
chartControl == null || ChartBars.Bars == null ){
return;
}
#region RENDER_ORDER_FLOW
float W = chartControl.Properties.BarDistance;
if( this._OrderFlowStyle == _OrderFlowEnums.Style.HeatMap ){
ChartControl.Properties.BarMarginRight = (int)(W / 4);
}
if( this._OrderFlowPosition == _OrderFlowEnums.Position.Right ){
ChartControl.Properties.BarMarginRight = (int)(W / 1.5);
}
//renderOF.BarW = (float)chartControl.BarWidth;
// 1- Altura minima de un tick
// 2- Ancho de barra en barra
wyckoffOF.setHW(chartScale.GetPixelsForDistance(TickSize), W);
// !- Apuntamos al target de renderizado
wyckoffOF.setRenderTarget(chartControl, chartScale, ChartBars, RenderTarget);
int fromIndex = ChartBars.FromIndex;
int toIndex = ChartBars.ToIndex;
// ?- es la ultima barra generada
if( toIndex == wyckoffBars.CurrentBarIndex )
{
if( _RealtimeHeuristic ){
wyckoffOF.renderBarClusters(toIndex, true);
}
// -- no cargamos la ultima barra en creacion
toIndex--;
}
for(int barIndex = fromIndex; barIndex <= toIndex; barIndex++){
wyckoffOF.renderBarClusters(barIndex, false);
}
#endregion
}
protected override void OnMarketData(MarketDataEventArgs MarketArgs){
wyckoffBars.onMarketData(MarketArgs);
}
#region Properties
[NinjaScriptProperty]
[Display(Name="Formula", Order=0, GroupName="Order flow calculation")]
public _OrderFlowEnums.Calculation _OrderFlowCalculation
{ get; set; }
[NinjaScriptProperty]
[Display(Name="Representation", Order=1, GroupName="Order flow calculation")]
public _OrderFlowEnums.Representation _OrderFlowRepresentation
{ get; set; }
[NinjaScriptProperty]
[Display(Name="Position(Profile)", Order=2, GroupName="Order flow calculation")]
public _OrderFlowEnums.Position _OrderFlowPosition
{ get; set; }
[NinjaScriptProperty]
[Display(Name="Style", Order=3, GroupName="Order flow calculation")]
public _OrderFlowEnums.Style _OrderFlowStyle
{ get; set; }
[NinjaScriptProperty]
[Display(Name="Realtime heuristic", Order=4, GroupName="Order flow calculation")]
public bool _RealtimeHeuristic
{ get; set; }
[XmlIgnore]
[Display(Name="Ask clusters", Order=0, GroupName="Order flow style")]
public Brush _AskClusterVolumeColor
{ get; set; }
[Browsable(false)]
public string _AskClusterVolumeColorSerializable
{
get { return Serialize.BrushToString(_AskClusterVolumeColor); }
set { _AskClusterVolumeColor = Serialize.StringToBrush(value); }
}
[XmlIgnore]
[Display(Name="Bid clusters", Order=1, GroupName="Order flow style")]
public Brush _BidClusterVolumeColor
{ get; set; }
[Browsable(false)]
public string _BidClusterVolumeColorSerializable
{
get { return Serialize.BrushToString(_BidClusterVolumeColor); }
set { _BidClusterVolumeColor = Serialize.StringToBrush(value); }
}
[XmlIgnore]
[Display(Name="Total clusters", Order=2, GroupName="Order flow style")]
public Brush _TotalClusterVolumeColor
{ get; set; }
[Browsable(false)]
public string _TotalClusterVolumeColorSerializable
{
get { return Serialize.BrushToString(_TotalClusterVolumeColor); }
set { _TotalClusterVolumeColor = Serialize.StringToBrush(value); }
}
[XmlIgnore]
[Display(Name="Max cluster", Order=3, GroupName="Order flow style")]
public Brush _MaxClusterVolumeColor
{ get; set; }
[Browsable(false)]
public string _MaxClusterVolumeColorSerializable
{
get { return Serialize.BrushToString(_MaxClusterVolumeColor); }
set { _MaxClusterVolumeColor = Serialize.StringToBrush(value); }
}
[XmlIgnore]
[Display(Name="Min cluster", Order=4, GroupName="Order flow style")]
public Brush _MinClusterVolumeColor
{ get; set; }
[Browsable(false)]
public string _MinClusterVolumeColorSerializable
{
get { return Serialize.BrushToString(_MinClusterVolumeColor); }
set { _MinClusterVolumeColor = Serialize.StringToBrush(value); }
}
[NinjaScriptProperty]
[Range(0.0f, 100f)]
[Display(Name="Max cluster opacity", Order=5, GroupName="Order flow style")]
public float _MaxClusterOpacity
{ get; set; }
[NinjaScriptProperty]
[Range(0.0f, 100f)]
[Display(Name="Min cluster opacity", Order=6, GroupName="Order flow style")]
public float _MinClusterOpacity
{ get; set; }
[NinjaScriptProperty]
[Range(0.0f, 100f)]
[Display(Name="Clusters opacity", Order=7, GroupName="Order flow style")]
public float _ClustersOpacity
{ get; set; }
[XmlIgnore]
[Display(Name="POC lines", Order=8, GroupName="Order flow style")]
public Brush _POCLinesColor
{ get; set; }
[Browsable(false)]
public string _POCLinesColorSerializable
{
get { return Serialize.BrushToString(_POCLinesColor); }
set { _POCLinesColor = Serialize.StringToBrush(value); }
}
[XmlIgnore]
[Display(Name="POI lines", Order=9, GroupName="Order flow style")]
public Brush _POILinesColor
{ get; set; }
[Browsable(false)]
public string _POILinesColorSerializable
{
get { return Serialize.BrushToString(_POILinesColor); }
set { _POILinesColor = Serialize.StringToBrush(value); }
}