forked from EvgBitWhiskey/BitWhiskey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chart.cs
1201 lines (1079 loc) · 41.9 KB
/
Chart.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
namespace BitWhiskey
{
public class BChartStyle
{
public Pen drawPen;
public Color backColor;
public Pen barUp = new Pen(Color.FromArgb(63, 170, 57));// new Pen(Color.FromArgb(73,192,67));
public Pen barDown = new Pen(Color.FromArgb(200, 11,11));
public Pen SelLine = Pens.Pink;
public Brush barUpBrush;
public Brush barDownBrush;
public Brush strLabelBrush;
public Brush strLabelSelBrush;
public Font strLabelFont=new Font(FontFamily.GenericSansSerif,8);
public Font strLabelSelFont = new Font(FontFamily.GenericSansSerif, 8);
public BChartStyle()
{
drawPen = new Pen(Color.Red);
backColor = Color.Black;
barUpBrush = new SolidBrush(barUp.Color);
barDownBrush = new SolidBrush(barDown.Color);
strLabelBrush = new SolidBrush(Color.LightGray);
strLabelSelBrush = new SolidBrush(Color.Green);
}
}
public enum ConvertDataType
{
BAR_5,
BAR_15,
BAR_HOUR,
BAR_DAY,
BAR_WEEK,
BAR_MONTH
}
public class ConvertData
{
ConvertDataType datatype;
public bool created = false;
// public int hour = -1;
public int lastperiod = -1;
public DateTime lastdate = DateTime.Now;
public PriceCandle lastprice = new PriceCandle();
public double prevclose = 0;
PriceCandle lastprice15 = new PriceCandle();
DateTime dt;
public ConvertData(ConvertDataType datatype_)
{
datatype = datatype_;
}
public int HourPeriodNumber(DateTime date, int period)
{
int minute = date.Minute;
int periodnum = (minute /period);
return periodnum;
}
public void ProcessBar(Dictionary<int, PriceCandle> pricedata)
{
int period = 0;
switch (datatype)
{
case ConvertDataType.BAR_15:
period = HourPeriodNumber(dt,15);
break;
case ConvertDataType.BAR_HOUR:
period = dt.Hour;
break;
case ConvertDataType.BAR_DAY:
period = dt.Day;
break;
case ConvertDataType.BAR_WEEK:
period = Helper.WeekNumber(dt);
break;
case ConvertDataType.BAR_MONTH:
period = dt.Month;
break;
}
if (period != lastperiod)
{
AddPrice(pricedata);
CreatePrice();
}
else
AddBar();
}
public void SetCurBar(DateTime dt_, PriceCandle lastprice15_)
{
lastprice15 = lastprice15_;
dt = dt_;
}
public void CreatePrice()
{
created = true;
switch (datatype)
{
case ConvertDataType.BAR_15:
lastperiod = HourPeriodNumber(dt, 15);
lastdate = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, lastperiod*15, 0);
break;
case ConvertDataType.BAR_HOUR:
lastperiod = dt.Hour;
lastdate = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, 0, 0);
break;
case ConvertDataType.BAR_DAY:
lastperiod = dt.Day;
lastdate = new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0);
break;
case ConvertDataType.BAR_WEEK:
lastperiod = Helper.WeekNumber(dt);
lastdate = Helper.WeekDate(dt.Year, lastperiod).AddDays(-1);
/*
DateTime d1 = new DateTime(2017, 1, 1).AddDays(34 * 7);
DateTime d2 = new DateTime(2017, 1, 1).AddDays(52 * 7);
lastperiod = Helper.WeekNumber(new DateTime(2017, 1, 15));
DateTime d3 = new DateTime(2017, 1, 1).AddDays(lastperiod * 7-1);
*/
break;
case ConvertDataType.BAR_MONTH:
lastperiod = dt.Month;
lastdate = new DateTime(dt.Year, dt.Month, 1, 0, 0, 0);
break;
}
lastprice = new PriceCandle();
lastprice.open = lastprice15.open;
lastprice.high = lastprice15.high;
lastprice.low = lastprice15.low;
lastprice.volume = lastprice15.volume;
lastprice.date = lastprice15.date;
}
public void AddPrice(Dictionary<int, PriceCandle> pricedata)
{
if (created)
{
lastprice.close = prevclose;
pricedata.Add(Helper.ToUnixTimeStamp(lastdate), lastprice);
}
}
public void AddBar()
{
lastprice.high = Math.Max(lastprice.high, lastprice15.high);
lastprice.low = Math.Min(lastprice.low, lastprice15.low);
lastprice.volume += lastprice15.volume;
}
}
public class PriceData
{
List<int> keys;
Dictionary<int, PriceCandle> pricedata;
Dictionary<int, PriceCandle> normpricedata;
Dictionary<int, PriceCandle> origpricedata;
Dictionary<int, PriceCandle> min15pricedata;
Dictionary<int, PriceCandle> hourpricedata;
Dictionary<int, PriceCandle> daypricedata;
Dictionary<int, PriceCandle> weekpricedata;
Dictionary<int, PriceCandle> monthpricedata;
ConvertDataType curViewType;
public PriceData()
{
}
public void SetData(Dictionary<int, PriceCandle> pricedata_, ConvertDataType datatype, ConvertDataType viewType)
{
origpricedata = pricedata_;
curViewType = viewType;
Convert(viewType);
keys = new List<int>(pricedata.Keys);
// SelectPeriod(datatype);
}
public bool UpdatePrice(double lastPrice)
{
bool barAdded = false;
PriceCandle last = pricedata.Last().Value;
int lastPriceDateUnix = pricedata.Keys.Max();
DateTime lastPriceDate = Helper.UnixToDateTime(lastPriceDateUnix);
int unixPeriod = 0;
switch (curViewType)
{
case ConvertDataType.BAR_5:
unixPeriod = 15 * 60;
break;
case ConvertDataType.BAR_15:
unixPeriod = 15 * 60;
break;
case ConvertDataType.BAR_HOUR:
unixPeriod = 60 * 60;
break;
case ConvertDataType.BAR_DAY:
unixPeriod = 24*60 * 60;
break;
case ConvertDataType.BAR_WEEK:
unixPeriod = 7*24 * 60 * 60;
break;
case ConvertDataType.BAR_MONTH:
unixPeriod = DateTime.DaysInMonth(lastPriceDate.Year, lastPriceDate.Month) * 24 * 60 * 60;
break;
}
int curDateUnix = Helper.ToUnixTimeStamp(DateTime.UtcNow);
if (curDateUnix >= lastPriceDateUnix + unixPeriod)
{ //create new bar
PriceCandle newbar = new PriceCandle();
newbar.close = lastPrice;
newbar.open = last.close;
newbar.high =Math.Max(last.close, lastPrice);
newbar.low = Math.Min(last.close, lastPrice);
newbar.volume = 666;
newbar.date = lastPriceDateUnix + unixPeriod;
pricedata.Add(newbar.date, newbar);
barAdded = true;
}
// else
{ //update last bar
// int rn = Helper.GetRandomExact(0, 1);
// if (rn==0)
// {
double newClose = lastPrice;// + 100;
last.close = newClose;
if (last.high < newClose)
last.high = newClose;
if (last.low > newClose)
last.low = newClose;
}
keys = new List<int>(pricedata.Keys);
return barAdded;
}
public void SelectPeriod1(ConvertDataType datatype)
{
switch (datatype)
{
case ConvertDataType.BAR_5:
pricedata = origpricedata;
break;
case ConvertDataType.BAR_15:
if (min15pricedata != null)
pricedata = min15pricedata;
break;
case ConvertDataType.BAR_HOUR:
if (hourpricedata != null)
pricedata = hourpricedata;
break;
case ConvertDataType.BAR_DAY:
if (daypricedata != null)
pricedata = daypricedata;
break;
case ConvertDataType.BAR_WEEK:
if (weekpricedata != null)
pricedata = weekpricedata;
break;
case ConvertDataType.BAR_MONTH:
if (monthpricedata != null)
pricedata = monthpricedata;
break;
}
keys = new List<int>(pricedata.Keys);
}
public void Convert(ConvertDataType viewType)
{
List<int> keys15;
keys15 = new List<int>(origpricedata.Keys);
min15pricedata = new Dictionary<int, PriceCandle>();
hourpricedata = new Dictionary<int, PriceCandle>();
daypricedata = new Dictionary<int, PriceCandle>();
weekpricedata = new Dictionary<int, PriceCandle>();
monthpricedata = new Dictionary<int, PriceCandle>();
DateTime dt;
PriceCandle lastprice15 = new PriceCandle();
ConvertData min15data = new ConvertData(ConvertDataType.BAR_15);
ConvertData hourdata = new ConvertData(ConvertDataType.BAR_HOUR);
ConvertData daydata = new ConvertData(ConvertDataType.BAR_DAY);
ConvertData weekdata = new ConvertData(ConvertDataType.BAR_WEEK);
ConvertData monthdata = new ConvertData(ConvertDataType.BAR_MONTH);
for (int n = 0; n < keys15.Count; n++)
{
if (n == 486)
{ }
dt = Helper.UnixToDateTime(keys15[n].ToString());
// if (dt.Month == 9 && dt.Day == 15)
min15data.prevclose = lastprice15.close;
hourdata.prevclose = lastprice15.close;
daydata.prevclose = lastprice15.close;
weekdata.prevclose = lastprice15.close;
monthdata.prevclose = lastprice15.close;
lastprice15 = origpricedata[keys15[n]];
if (viewType == ConvertDataType.BAR_15)
{
min15data.SetCurBar(dt, lastprice15);
min15data.ProcessBar(min15pricedata);
}
else if (viewType == ConvertDataType.BAR_HOUR)
{
hourdata.SetCurBar(dt, lastprice15);
hourdata.ProcessBar(hourpricedata);
}
else if (viewType == ConvertDataType.BAR_DAY)
{
daydata.SetCurBar(dt, lastprice15);
daydata.ProcessBar(daypricedata);
}
else if (viewType == ConvertDataType.BAR_WEEK)
{
weekdata.SetCurBar(dt, lastprice15);
weekdata.ProcessBar(weekpricedata);
}
else if (viewType == ConvertDataType.BAR_MONTH)
{
monthdata.SetCurBar(dt, lastprice15);
monthdata.ProcessBar(monthpricedata);
}
}
min15data.prevclose = lastprice15.close;
hourdata.prevclose = lastprice15.close;
daydata.prevclose = lastprice15.close;
weekdata.prevclose = lastprice15.close;
monthdata.prevclose = lastprice15.close;
if (viewType == ConvertDataType.BAR_15)
{
min15data.AddPrice(min15pricedata);
pricedata = min15pricedata;
}
else if (viewType == ConvertDataType.BAR_HOUR)
{
hourdata.AddPrice(hourpricedata);
pricedata = hourpricedata;
}
else if (viewType == ConvertDataType.BAR_DAY)
{
daydata.AddPrice(daypricedata);
pricedata = daypricedata;
}
else if (viewType == ConvertDataType.BAR_WEEK)
{
weekdata.AddPrice(weekpricedata);
pricedata = weekpricedata;
}
else if (viewType == ConvertDataType.BAR_MONTH)
{
monthdata.AddPrice(monthpricedata);
pricedata = monthpricedata;
}
}
public void Convert1(bool fromday)
{
List<int> keys15 ;
keys15 = new List<int>(origpricedata.Keys);
min15pricedata = new Dictionary<int, PriceCandle>();
hourpricedata = new Dictionary<int, PriceCandle>();
if (!fromday)
daypricedata = new Dictionary<int, PriceCandle>();
weekpricedata = new Dictionary<int, PriceCandle>();
monthpricedata = new Dictionary<int, PriceCandle>();
DateTime dt;
PriceCandle lastprice15 = new PriceCandle();
ConvertData min15data = new ConvertData(ConvertDataType.BAR_15);
ConvertData hourdata = new ConvertData(ConvertDataType.BAR_HOUR);
ConvertData daydata = new ConvertData(ConvertDataType.BAR_DAY);
ConvertData weekdata = new ConvertData(ConvertDataType.BAR_WEEK);
ConvertData monthdata = new ConvertData(ConvertDataType.BAR_MONTH);
for (int n = 0; n < keys15.Count; n++)
{
if(n==486)
{ }
dt = Helper.UnixToDateTime(keys15[n].ToString());
// if (dt.Month == 9 && dt.Day == 15)
min15data.prevclose = lastprice15.close;
hourdata.prevclose = lastprice15.close;
daydata.prevclose = lastprice15.close;
weekdata.prevclose = lastprice15.close;
monthdata.prevclose = lastprice15.close;
lastprice15 = origpricedata[keys15[n]];
if (!fromday)
{
min15data.SetCurBar(dt, lastprice15);
min15data.ProcessBar(min15pricedata);
hourdata.SetCurBar(dt, lastprice15);
hourdata.ProcessBar(hourpricedata);
daydata.SetCurBar(dt, lastprice15);
daydata.ProcessBar(daypricedata);
}
weekdata.SetCurBar(dt, lastprice15);
weekdata.ProcessBar(weekpricedata);
monthdata.SetCurBar(dt, lastprice15);
monthdata.ProcessBar(monthpricedata);
}
if (!fromday)
{
min15data.prevclose = lastprice15.close;
hourdata.prevclose = lastprice15.close;
daydata.prevclose = lastprice15.close;
min15data.AddPrice(min15pricedata);
hourdata.AddPrice(hourpricedata);
daydata.AddPrice(daypricedata);
}
weekdata.prevclose = lastprice15.close;
monthdata.prevclose = lastprice15.close;
weekdata.AddPrice(weekpricedata);
monthdata.AddPrice(monthpricedata);
}
public List<int> GetKeys()
{
return keys;
}
public Dictionary<int, PriceCandle> GetData()
{
return pricedata;
}
public Dictionary<int, PriceCandle> GetNormalizeData()
{
return normpricedata;
}
public int Count()
{
return keys.Count;
}
public void NormalizeData(double minprice, double yzoomcoeff)
{
normpricedata = new Dictionary<int, PriceCandle>();
foreach (int key in keys)
{
PriceCandle p = pricedata[key];
PriceCandle pnew = p.Copy();
pnew.open = (pnew.open - minprice) / yzoomcoeff;
pnew.high = (pnew.high - minprice) / yzoomcoeff;
pnew.low = (pnew.low - minprice) / yzoomcoeff;
pnew.close = (pnew.close - minprice) / yzoomcoeff;
// pnew.date = key;
normpricedata.Add(key, pnew);
}
}
}
public class ChartButton
{
public PointF pos ;
public PointF size;
public string text = "";
public bool clicked = false;
public ChartButton(PointF pos_, PointF size_, string text_)
{
pos = pos_;
size = size_;
text = text_;
}
public void Click()
{
clicked = true;
}
public void Reset()
{
clicked = false;
}
}
public class PriceChart
{
string chartCaption;
Graphics gform;
Graphics g = null;
Bitmap bmap ;
BChartStyle style = new BChartStyle();
PriceData data;
float xFormPos = 0;
float yFormPos = 0;
float areaxmax = 0;
float areaymax = 0;
// draw rect
float xstart = 0;
float ystart = 0;
float xend = 0;
float yend = 0;
float draww = 0;
float drawh = 0;
//markers labels
float LeftOffset = 0;
float RightOffset = 0;
float TopOffset = 0;
float DownOffset = 0;
float yzoomcoeff = 1;
// max min data
double maxprice = 0;
double minprice = double.MaxValue;
float barsizex = 5;
int lasttick =0;
int starttick =0;
bool scrollActive = false;
PointF scrollStart;
PointF scrollEnd;
PointF scrollChange;
//mouse selection
int mousex=200;
int mousey=200;
float mousedrawx =0;
float mousedrawy = 0;
float mouseareax = 0;
float mouseareay = 0;
int buttonstate = 0;
int selectedPriceKey = -1;
//buttons
/*
ChartButton buttonMonth;
ChartButton buttonWeek;
ChartButton buttonDay;
ChartButton buttonHour;
ChartButton buttonMin15;
ChartButton buttonMin5;
*/
ChartButton buttonZoomPlus;
ChartButton buttonZoomMinus;
public PriceChart(string chartCaption_,Graphics gform_, float xFormPos_, float yFormPos_, float xmax_, float ymax_)
{
chartCaption = chartCaption_;
Init(gform_,xFormPos_, yFormPos_, xmax_, ymax_);
}
public void Init(Graphics gform_,float xFormPos_, float yFormPos_, float xmax_, float ymax_)
{
gform = gform_;
xFormPos = xFormPos_;
yFormPos = yFormPos_;
areaxmax = xmax_;
areaymax = ymax_;
LeftOffset = 5;
RightOffset = 85;
TopOffset = 22;
DownOffset = 23;
xstart = LeftOffset;
ystart = DownOffset;
xend = areaxmax - RightOffset;
yend = areaymax - TopOffset;
draww = xend - xstart;
drawh = yend - ystart;
ChartButton selButton = GetSelectedButton();
float bx = 4;
float bxadd = 3;
float sx = 26;
float bsizeh = 18;
/*
buttonMonth = new ChartButton(new PointF(bx, areaymax - bsizeh), new PointF(sx, bsizeh-2), "M");
bx += sx + bxadd;
buttonWeek = new ChartButton(new PointF(bx, areaymax - bsizeh), new PointF(sx, bsizeh - 2), "W");
bx += sx + bxadd;
buttonDay = new ChartButton(new PointF(bx, areaymax - bsizeh), new PointF(sx, bsizeh - 2), "D");
bx += sx + bxadd;
buttonHour = new ChartButton(new PointF(bx, areaymax - bsizeh), new PointF(sx, bsizeh - 2), "H");
bx += sx + bxadd;
buttonMin15 = new ChartButton(new PointF(bx, areaymax - bsizeh), new PointF(sx, bsizeh - 2), "M15");
bx += sx + bxadd;
buttonMin5 = new ChartButton(new PointF(bx, areaymax - bsizeh), new PointF(sx, bsizeh - 2), "M5");
bx += sx + 12;
// sx = 30;
*/
buttonZoomPlus = new ChartButton(new PointF(bx, areaymax - bsizeh), new PointF(sx, bsizeh - 2), "+");
bx += sx + bxadd;
buttonZoomMinus = new ChartButton(new PointF(bx, areaymax - bsizeh), new PointF(sx, bsizeh - 2), "-");
bmap = new Bitmap((int)areaxmax, (int)areaymax);
g = Graphics.FromImage(bmap);
if(selButton!=null)
SelectButton(selButton.text);
}
public void SetData(Dictionary<int, PriceCandle> pricedata_, ConvertDataType convertFromType, ConvertDataType viewType)
{
data = new PriceData();
data.SetData(pricedata_, convertFromType, viewType);
/*
switch (viewType)
{
case ConvertDataType.BAR_5:
SelectButton(buttonMin5.text);
break;
case ConvertDataType.BAR_15:
SelectButton(buttonMin15.text);
break;
case ConvertDataType.BAR_HOUR:
SelectButton(buttonHour.text);
break;
case ConvertDataType.BAR_DAY:
SelectButton(buttonDay.text);
break;
case ConvertDataType.BAR_WEEK:
SelectButton(buttonWeek.text);
break;
case ConvertDataType.BAR_MONTH:
SelectButton(buttonMonth.text);
break;
}
*/
//data.SelectPeriod(viewType);
ScrollToEnd();
ReCalc();
}
public void Dispose()
{
gform.Dispose();
}
public void SaveImage(string imagepathfile)
{
ReDrawFull();
bmap.Save(imagepathfile, ImageFormat.Png);
}
public PriceData GetPriceData()
{
return data;
}
public void UpdatePrice(double lastPrice)
{
bool isChartEnd = (lasttick == data.Count() - 1);
bool barAdded=data.UpdatePrice(lastPrice);
if(barAdded && isChartEnd)
ScrollToEnd();
}
public void Zoom(bool zoomin)
{
if (zoomin)
{
barsizex+=1;
}
else
{
barsizex-=1;
if (barsizex < 1)
barsizex = 1;
}
}
public void ScrollToEnd()
{
lasttick = data.Count() - 1;
}
public void CalcScroll()
{
if(scrollActive)
{
lasttick = lasttick - (int)(scrollChange.X);
scrollChange = new PointF(0, 0);
}
if (lasttick >= data.Count())
lasttick = data.Count() - 1;
starttick = lasttick - (int)(draww / barsizex);
if (starttick < 0)
starttick = 0;
}
public void MouseMove(int x, int y)
{
mousex = x;
mousey = y;
CalcMouseSelection();
if (scrollActive)
{
scrollEnd = new PointF(x, y);
float needBarSize=2;
float barSizeFactor=barsizex/needBarSize;
if(barSizeFactor<1)
barSizeFactor=1;
float scrollInPixels = scrollStart.X - scrollEnd.X;
// float scrollUnitInPixels = barsizex;
float scrollInBars=scrollInPixels/barSizeFactor;
scrollChange = new PointF(scrollInBars, scrollEnd.Y - scrollStart.Y);
scrollStart = new PointF(x, y);
ReCalc();
}
ReDraw();
}
public void MouseClick(int x, int y,int buttonstate_)
{
buttonstate = buttonstate_;
bool redraw = false;
scrollActive = false;
if (buttonstate != 1)
return;
scrollActive = true;
scrollStart = new PointF(x, y);
scrollEnd = new PointF(x, y);
scrollChange = new PointF(0,0);
/*
if( MouseOnButton(buttonMonth))
{
SelectButton(buttonMonth.text);
data.SelectPeriod(ConvertDataType.BAR_MONTH);
redraw = true;
}
else if (MouseOnButton(buttonWeek))
{
SelectButton(buttonWeek.text);
data.SelectPeriod(ConvertDataType.BAR_WEEK);
redraw = true;
}
else if (MouseOnButton(buttonDay))
{
SelectButton(buttonDay.text);
data.SelectPeriod(ConvertDataType.BAR_DAY);
redraw = true;
}
else if (MouseOnButton(buttonHour))
{
SelectButton(buttonHour.text);
data.SelectPeriod(ConvertDataType.BAR_HOUR);
redraw = true;
}
else if (MouseOnButton(buttonMin15))
{
SelectButton(buttonMin15.text);
data.SelectPeriod(ConvertDataType.BAR_15);
redraw = true;
}
else if (MouseOnButton(buttonMin5))
{
SelectButton(buttonMin5.text);
data.SelectPeriod(ConvertDataType.BAR_5);
redraw = true;
}
*/
if (MouseOnButton(buttonZoomPlus))
{
Zoom(true);
ReDrawFull();
}
if (MouseOnButton(buttonZoomMinus))
{
Zoom(false);
ReDrawFull();
}
if (redraw)
{
ScrollToEnd();
ReDrawFull();
}
}
void SelectButton(string btext)
{
/*
if(btext==null)
return ;
buttonMonth.Reset();
buttonWeek.Reset();
buttonDay.Reset();
buttonHour.Reset();
buttonMin15.Reset();
buttonMin5.Reset();
if (btext== buttonMonth.text) buttonMonth.Click();
if (btext == buttonWeek.text) buttonWeek.Click();
if (btext == buttonDay.text) buttonDay.Click();
if (btext == buttonHour.text) buttonHour.Click();
if (btext == buttonMin15.text) buttonMin15.Click();
if (btext == buttonMin5.text) buttonMin5.Click();
*/
}
ChartButton GetSelectedButton()
{
/*
if(buttonMonth==null)
return null;
if (buttonMonth.clicked) return buttonMonth;
if (buttonWeek.clicked) return buttonWeek;
if (buttonDay.clicked) return buttonDay;
if (buttonHour.clicked) return buttonHour;
if (buttonMin15.clicked) return buttonMin15;
if (buttonMin5.clicked) return buttonMin5;
*/
return null;
}
void CalcMouseSelection()
{
mousedrawx = -1;
mousedrawy = -1;
mouseareax = -1;
mouseareay = -1;
if (mousex >= xFormPos + LeftOffset && mousex <= xFormPos + LeftOffset + draww)
if (mousey >= yFormPos + TopOffset && mousey <= yFormPos + TopOffset + drawh)
{
mousedrawx =(int)( mousex - (xFormPos + LeftOffset));
mousedrawy = (int)((yFormPos + TopOffset+drawh) - mousey);
}
if (mousex >= xFormPos && mousex <= xFormPos + areaxmax)
if (mousey >= yFormPos && mousey <= yFormPos + areaymax)
{
mouseareax = (int)(mousex - xFormPos);
mouseareay = (int)((yFormPos + areaymax) - mousey);
}
}
public void ReDrawFull()
{
ReCalc();
ReDraw();
}
public void ReCalc()
{
CalcScroll();
PrepareChart();
}
public void Resize(Graphics gform_, float xFormPos_, float yFormPos_, float xmax_, float ymax_)
{
Init(gform_,xFormPos_, yFormPos_, xmax_, ymax_);
}
public void ReDraw()
{
g.Clear(style.backColor);
DrawMainChart();
DrawPrice();
DrawUpdate();
}
void DrawUpdate()
{
gform.DrawImage(bmap, (int)xFormPos, (int)yFormPos);
}
void PrepareChart()
{
maxprice = 0;
minprice = double.MaxValue;
Dictionary<int, PriceCandle> pricedata = data.GetData();
List<int> keys = data.GetKeys();
for (int n = starttick; n <= lasttick; n++)
{
int key1 = keys[n];
// int date1 = key1;
PriceCandle price1 = pricedata[key1];
if (price1.open > maxprice)
maxprice = price1.open;
if (price1.high > maxprice)
maxprice = price1.high;
if (price1.low > maxprice)
maxprice = price1.low;
if (price1.close > maxprice)
maxprice = price1.close;
if (price1.open < minprice)
minprice = price1.open;
if (price1.high < minprice)
minprice = price1.high;
if (price1.low < minprice)
minprice = price1.low;
if (price1.close < minprice)
minprice = price1.close;
}
yzoomcoeff = (float)(maxprice-minprice) / drawh;
data.NormalizeData(minprice, yzoomcoeff);
}
double NormalizePrice(double pricedata)
{
return (pricedata - minprice) / yzoomcoeff;
}
double DeNormalizePrice(double pricedata)
{
return (pricedata * yzoomcoeff) + minprice;
}
void DrawPrice()
{
Dictionary<int, PriceCandle> pricedata = data.GetNormalizeData();
List<int> keys = data.GetKeys();
float pos = draww;
selectedPriceKey=-1;
for (int n = lasttick; n > starttick; n--)
{
Pen pen = style.barUp;
Brush brush = style.barUpBrush;
int key1 = keys[n - 1];
int date1 = key1;
PriceCandle price1 = pricedata[key1];
int key2 = keys[n];
int date2 = key2;
PriceCandle price2 = pricedata[key2];
bool fill = false;
if (price2.close < price2.open)
{
pen = style.barDown;
brush = style.barDownBrush;
fill = true;
}
float barspace = 0;
if (mousedrawx != -1)
{
if (mousedrawx >= pos - barsizex && mousedrawx <pos)
{
pen = new Pen(Color.Yellow);
brush = new SolidBrush(Color.Yellow);
selectedPriceKey = key2;
}
}
if (barsizex > 2)
{
barspace = 2;
float realbarsize = barsizex - barspace;
PointF p = new PointF(pos - barsizex, (float)Math.Min(price2.close, price2.open));
PointF size = new PointF(realbarsize, (float)Math.Abs(price2.close - price2.open));
AreaDrawRectangle(pen, brush, p, size, fill);
PointF p1 = new PointF(pos - barsizex / 2 - barspace / 2, (float)price2.high);
PointF p2 = new PointF(pos - barsizex / 2 - barspace / 2, (float)Math.Max(price2.close, price2.open));
AreaDrawLine(pen, p1, p2);
p1 = new PointF(pos - barsizex / 2 - barspace / 2, (float)price2.low);
p2 = new PointF(pos - barsizex / 2 - barspace / 2, (float)Math.Min(price2.close, price2.open));
if (p1.X == p2.X && p1.Y == p2.Y)
p2.X += 1;
AreaDrawLine(pen, p1, p2);
/*
if(price2.high==price2.low)
{
p1 = new PointF(pos - barsizex / 2 - barspace / 2-1, (float)price2.close);
p2 = new PointF(pos - barsizex / 2 - barspace / 2+1, (float)price2.close);
AreaDrawLine(pen, p1, p2);
}