-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModelClass.cs
682 lines (544 loc) · 17.4 KB
/
ModelClass.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
using System;
using System.Collections.Generic;
using System.Linq;
using NinjaTrader.NinjaScript.DrawingTools;
using System.Windows.Media;
using System.IO;
using System.Xml.Serialization;
namespace NinjaTrader.NinjaScript.Indicators.MRPack
{
[Serializable]
public class Instrument_Data
{
public string InstrumentName;
public List<RangeProfile_Data> RangeProfilesList;
public Instrument_Data()
{}
public Instrument_Data(string name)
{
InstrumentName = name;
RangeProfilesList= new List<RangeProfile_Data>();
}
}
[Serializable]
public class RangeProfile_Data
{
public string Tag;
public double Top_price;
public double Bottom_price;
public DateTime Left_Time;
public DateTime Right_Time;
public int ProfileType;
public bool IsExtended;
public RangeProfile_Data()
{}
public RangeProfile_Data(string tag,double topPrice, DateTime startTime, double botPrice, DateTime endTime, int type, bool extend)
{
Tag = tag;
Top_price=topPrice;
Bottom_price=botPrice;
Left_Time=startTime;
Right_Time=endTime;
ProfileType = type;
IsExtended= extend;
}
}
public class Model
{
public List<Bar> ListOfBar;
public CurrentBar currentBar;
private Stack<Print> stackForMarketStop;
public List<MarketStop> ListOfMarketStop;
public HistogrammClass Histogramm;
public DayProfileClass DayProfile;
public DateTime MaxVerticalVolumeBar;
public MRIndicator parent;
public List<RangeProfile2> RangeProfiles;
public List<double> TandS_AllPrints_price;
public List<int> TandS_AllPrints_volume;
public List<double> TandS_FilterPrints_price;
public List<int> TandS_FilterPrints_volume;
public List<DateTime> TandS_FilterPrints_time;
private Stack<Print> stackForTickAggregator;
private double TickAggregator_TopPrice;
private double TickAggregator_BotPrice;
public List<TickAggregatorElement> TickAggregatorElements;
public int MaxTickAggregatorVolume;
public bool Range_Profile_Text_OnOff; // Edited by PD
public Brush Range_Profile_Text_Color; // Edited by PD
public Brush Input_ProfileRange_Inside_Color;
public Brush Input_ProfileRange_POC_Color;
public bool Input_RangeProfile_BidAsk_OnOff;
public bool Input_RangeProfile_ExtendedLine_OnOff;
public Brush Input_ProfileRange_Border_Color;
public Brush Input_ProfileRange_Inside_Bid_Color;
public Brush Input_ProfileRange_Inside_Ask_Color;
public int Profile_Text_Opacity;
public int Profile_Opacity;
public Model()
{}
public Model(MRIndicator indic)
{
ListOfBar = new List<Bar>();
currentBar = new CurrentBar();
stackForMarketStop = new Stack<Print>();
ListOfMarketStop = new List<MarketStop>();
Histogramm = new HistogrammClass();
DayProfile = new DayProfileClass();
MaxVerticalVolumeBar = new DateTime();
parent = indic;
RangeProfiles= new List<RangeProfile2>();
TandS_AllPrints_price = new List<double>();
TandS_AllPrints_volume = new List<int>();
TandS_FilterPrints_price = new List<double>();
TandS_FilterPrints_volume = new List<int>();
TandS_FilterPrints_time = new List<DateTime>();
stackForTickAggregator= new Stack<Print>();
TickAggregatorElements = new List<TickAggregatorElement>();
}
public void DeleteProfile(string tag)
{
RangeProfiles.RemoveAll(p=>p.Tag==tag);
}
public void LoadProfiles()
{
if(File.Exists("RangeProfiles.xml"))
{
List<Instrument_Data> lst = new List<Instrument_Data>();
XmlSerializer formatter = new XmlSerializer(typeof(List<Instrument_Data>));
Instrument_Data instrument;
using (FileStream fs = new FileStream("RangeProfiles.xml", FileMode.OpenOrCreate))
{
lst = (List<Instrument_Data>)formatter.Deserialize(fs);
if(lst.Exists(i=>i.InstrumentName==parent.Instrument.FullName))
instrument = lst.Find(i=>i.InstrumentName==parent.Instrument.FullName);
else
instrument = new Instrument_Data(parent.Instrument.FullName);
foreach(RangeProfile_Data profile in instrument.RangeProfilesList)
{
parent.CreateNewRangeProfile(profile.Tag, profile.Left_Time,profile.Top_price,profile.Right_Time,profile.Bottom_price, profile.ProfileType,profile.IsExtended);
}
}
}
}
public void SaveProfiles()
{
List<RangeProfile_Data> profiles = new List<RangeProfile_Data>();
foreach(DrawingTools.RangeProfile2 profile in RangeProfiles)
{
RangeProfile_Data RP = new RangeProfile_Data(profile.Tag,
profile.StartAnchor.Price,
profile.StartAnchor.Time,
profile.EndAnchor.Price,
profile.EndAnchor.Time,
profile.ProfileType,
profile.ExtendedLine);
profiles.Add(RP);
}
if(File.Exists("RangeProfiles.xml"))
{
List<Instrument_Data> lst = new List<Instrument_Data>();
XmlSerializer formatter = new XmlSerializer(typeof(List<Instrument_Data>));
Instrument_Data instrument;
using (FileStream fs = new FileStream("RangeProfiles.xml", FileMode.OpenOrCreate))
{
lst = (List<Instrument_Data>)formatter.Deserialize(fs);
if(lst.Exists(i=>i.InstrumentName==parent.Instrument.FullName))
{
instrument = lst.Find(i=>i.InstrumentName==parent.Instrument.FullName);
lst.Remove(instrument);
}
else
instrument = new Instrument_Data(parent.Instrument.FullName);
instrument.RangeProfilesList = profiles;
lst.Add(instrument);
}
using (FileStream fs = new FileStream("RangeProfiles.xml", FileMode.Create))
{
formatter.Serialize(fs, lst);
}
}
else
{
List<Instrument_Data> lst = new List<Instrument_Data>();
XmlSerializer formatter = new XmlSerializer(typeof(List<Instrument_Data>));
Instrument_Data instrument = new Instrument_Data(parent.Instrument.FullName);
lst.Add(instrument);
using (FileStream fs = new FileStream("RangeProfiles.xml", FileMode.Create))
{
formatter.Serialize(fs, lst);
}
}
}
public bool CloseBar(DateTime date)
{
currentBar.Time = date;
ListOfBar.Add(currentBar.GetStruct());
/*if(!(ListOfBar.ContainsKey(MaxVerticalVolumeBar)))
MaxVerticalVolumeBar = date;
if(ListOfBar[date].Volume_sum>=ListOfBar[MaxVerticalVolumeBar].Volume_sum)
MaxVerticalVolumeBar = date;*/
currentBar.Clear();
return true;
}
public void CloseDay()
{
DayProfile.Clear();
}
public void AddPrintToTickAggregator(DateTime time, int volume, double price, PrintType printType)
{
if(volume>=parent.Input_TickAggregator_TickLimit)
{
if(stackForTickAggregator.Count==0)
{
stackForTickAggregator.Push(new Print(time, volume, price, printType));
TickAggregator_TopPrice = TickAggregator_BotPrice = price;
return;
}
Print p = stackForTickAggregator.Peek();
TimeSpan t = time - p.Time;
if(t.TotalMilliseconds>parent.Input_TickAggregator_Delay)
{
SumTickAggregatorStack();
AddPrintToTickAggregator(time,volume,price, printType);
return;
}
if(price>TickAggregator_TopPrice)
{
if(((int)((price-TickAggregator_BotPrice)/parent.TickSize))>parent.Input_TickAggregator_Range)
{
SumTickAggregatorStack();
AddPrintToTickAggregator(time,volume,price, printType);
return;
}
}
if(price<TickAggregator_BotPrice)
{
if(((int)((TickAggregator_TopPrice-price)/parent.TickSize))>parent.Input_TickAggregator_Range)
{
SumTickAggregatorStack();
AddPrintToTickAggregator(time,volume,price, printType);
return;
}
}
stackForTickAggregator.Push(new Print(time, volume, price, printType));
if(price>TickAggregator_TopPrice)TickAggregator_TopPrice=price;
if(price<TickAggregator_BotPrice)TickAggregator_BotPrice=price;
}
}
public void SumTickAggregatorStack()
{
int sum = 0;//stackForTickAggregator.Sum(p=>p.Volume);
int sumAsk = 0;
int sumBid = 0;
List<Print> tmp = stackForTickAggregator.ToList();
foreach(Print p in tmp)
{
if(p.Type==PrintType.ASK) sumAsk+=p.Volume;
if(p.Type==PrintType.BID) sumBid+=p.Volume;
sum+=p.Volume;
}
if(sum>=parent.Input_TickAggregator_SummLimit)
{
Print l = stackForTickAggregator.Peek();
List<Print> SortedList = tmp.OrderBy(o=>o.Volume).ToList();
SortedList.Reverse();
if(SortedList[0].Volume>=parent.Input_TickAggregator_BigPrint)
{
TickAggregatorElement t = new TickAggregatorElement(l.Time,sum,sumAsk,sumBid,l.Price,TickAggregator_BotPrice,TickAggregator_TopPrice,SortedList);
TickAggregatorElements.Add(t);
if(sum>=MaxTickAggregatorVolume)
MaxTickAggregatorVolume=sum;
if(parent.State==State.Realtime)
{
parent.CreateNewTickAggregatorEllipse(l.Time,l.Price,t);
if(parent.Input_TickAggregator_AlertOnOff){
//parent.PlaySound(NinjaTrader.Core.Globals.InstallDir + @"\sounds\AutoTrail.wav");
parent.Alert("MS:"+l.Time.ToString(), Priority.High, "TiCkAggregator", NinjaTrader.Core.Globals.InstallDir+@"\sounds\AutoTrail.wav", 2, Brushes.Black, Brushes.Yellow);
}
}
}
}
stackForTickAggregator.Clear();
}
public void AddPrintToBar(double price, int volume, PrintType typeOfPrint)
{
if(currentBar.ListOfCurrentBar.ContainsKey(price))
{
currentBar.ListOfCurrentBar[price].AddPrintOnClaster(volume, typeOfPrint);
}else{
CurrentClaster claster = new CurrentClaster();
currentBar.ListOfCurrentBar.Add(price,claster);
currentBar.ListOfCurrentBar[price].AddPrintOnClaster(volume, typeOfPrint);
}
if(currentBar.pocPrice==0)
{
currentBar.pocPrice = price;
}
else
{
if(currentBar.pocPrice != price && currentBar.ListOfCurrentBar[price].Volume_sum>=currentBar.ListOfCurrentBar[currentBar.pocPrice].Volume_sum)
{
currentBar.pocPrice = price;
}
}
currentBar.dayPocPrice = DayProfile.pocPrice;
}
public void AddPrintToMarketStopStack(DateTime time, int volume, double price, PrintType printType)
{
if(stackForMarketStop.Count!=0)
{
Print p = stackForMarketStop.Peek();
TimeSpan ts = timeDelta(p.Time, time);
if(!(ts.TotalSeconds<1))
{
int sum = stackForMarketStop.Sum(pr => pr.Volume);
if(sum>=parent.Input_MS_VolumeLimit)
{
double minPrice = stackForMarketStop.Min(pr => pr.Price);
double maxPrice = stackForMarketStop.Max(pr => pr.Price);
ListOfMarketStop.Add(new MarketStop(p.Time,sum,minPrice,maxPrice));
if(parent.Input_MSAlert_OnOff){
//parent.PlaySound(NinjaTrader.Core.Globals.InstallDir + @"\sounds\AutoTrail.wav");
parent.Alert("MS:"+p.Time.ToString(), Priority.High, "MarketStop", NinjaTrader.Core.Globals.InstallDir+@"\sounds\AutoTrail.wav", 2, Brushes.Black, Brushes.Yellow);
}
}
stackForMarketStop.Clear();
}
}
stackForMarketStop.Push(new Print(time,volume,price,printType));
}
public List<Bar> GetBarRange(int firstTime, int lastTime)
{
Bar b = currentBar.GetStruct();
List<Bar> tmp;
//return this.ListOfBar.Where(bar => bar.Key>=firstTime && bar.Key<=lastTime);
if(firstTime<0)firstTime=0;
if(lastTime<0)lastTime=0;
int count=lastTime-firstTime;
if(lastTime>this.ListOfBar.Count)
count=this.ListOfBar.Count-firstTime;
if(firstTime>this.ListOfBar.Count)
{
firstTime=this.ListOfBar.Count-1;
count =0;
}
if(firstTime==lastTime)
count=0;
tmp = this.ListOfBar.GetRange(firstTime, count);
if(lastTime>this.ListOfBar.Count && firstTime<=this.ListOfBar.Count-1)
tmp.Add(this.currentBar.GetStruct());
return tmp;
}
public void AddPrintToTandS(DateTime time,int volume, double price)
{
TandS_AllPrints_price.Add(price);
TandS_AllPrints_volume.Add(volume);
if(TandS_AllPrints_price.Count>parent.Input_TandS_CountOrders)
{
TandS_AllPrints_price.RemoveAt(0);
TandS_AllPrints_volume.RemoveAt(0);
}
if((volume>0 && volume>=parent.Input_TandS_FilterAsk)||(volume<0 && volume*(-1)>=parent.Input_TandS_FilterBid))
{
TandS_FilterPrints_price.Add(price);
TandS_FilterPrints_volume.Add(volume);
TandS_FilterPrints_time.Add(time);
}
}
public TimeSpan timeDelta(DateTime prev, DateTime next)
{
DateTime d1 = new DateTime(next.Year,next.Month,next.Day,
next.Hour,next.Minute,next.Second);
DateTime d2 = new DateTime(prev.Year,prev.Month,prev.Day,
prev.Hour,prev.Minute,prev.Second);
return d1-d2;
}
public int Claster_Height;
public int Claster_Width_Max;
public void SetGraficDimensions(int cena1Y, int cena2Y, int bar1X, int bar2X)
{
int priceDelta = cena1Y-cena2Y;
Claster_Height = priceDelta % 2 == 0 ? priceDelta - 1 : priceDelta - 2;
if(Claster_Height<1) Claster_Height=1;
//if(priceDelta==3) Claster_Height=3;
int barDelta = bar1X-bar2X;
Claster_Width_Max = barDelta -1;
if(Claster_Width_Max<1) Claster_Width_Max=1;
}
public class CurrentBar
{
public SortedDictionary<double,CurrentClaster> ListOfCurrentBar;
public double pocPrice;
public double dayPocPrice;
public DateTime Time;
public CurrentBar()
{
ListOfCurrentBar = new SortedDictionary<double,CurrentClaster>();
}
public Bar GetStruct()
{
Bar b = new Bar(this);
return b;
}
public void Clear()
{
this.ListOfCurrentBar.Clear();
this.pocPrice = 0;
this.dayPocPrice = 0;
}
}
public class HistogrammClass : CurrentBar
{
public void AddPrintToHistogramm(double price, int volume, PrintType typeOfPrint)
{
if(this.ListOfCurrentBar.ContainsKey(price))
{
this.ListOfCurrentBar[price].AddPrintOnClaster(volume, typeOfPrint);
}else{
CurrentClaster claster = new CurrentClaster();
this.ListOfCurrentBar.Add(price,claster);
this.ListOfCurrentBar[price].AddPrintOnClaster(volume, typeOfPrint);
}
if(this.pocPrice==0)
{
this.pocPrice = price;
}
else
{
if(this.pocPrice != price && this.ListOfCurrentBar[price].Volume_sum>=this.ListOfCurrentBar[this.pocPrice].Volume_sum)
{
this.pocPrice = price;
}
}
}
}
public class DayProfileClass : HistogrammClass
{
}
public class CurrentClaster
{
public int Volume_sum;
public int Volume_Ask_sum;
public int Volume_Bid_sum;
public void AddPrintOnClaster(int volume, PrintType typeOfPrint)
{
switch(typeOfPrint)
{
case PrintType.ASK:
{
this.Volume_sum+=volume;
this.Volume_Ask_sum+=volume;
}break;
case PrintType.BID:
{
this.Volume_sum+=volume;
this.Volume_Bid_sum+=volume;
}break;
}
}
public Claster GetStruct()
{
Claster claster = new Claster();
claster.Volume_sum = this.Volume_sum;
claster.Volume_Ask_sum = this.Volume_Ask_sum;
claster.Volume_Bid_sum = this.Volume_Bid_sum;
return claster;
}
}
public struct Bar
{
public int Volume_sum;
public int Volume_Ask_sum;
public int Volume_Bid_sum;
public double PocPrice;
public double DayPocPrice;
public DateTime Time;
public SortedDictionary<double, Claster> ListOfClasters;
public Bar(CurrentBar curBar)
{
ListOfClasters = new SortedDictionary<double,Claster>();
int Volume_sum_tmp = 0;
int Volume_Ask_sum_tmp = 0;
int Volume_Bid_sum_tmp = 0;
foreach(KeyValuePair<double, Model.CurrentClaster> kvp in curBar.ListOfCurrentBar)
{
ListOfClasters.Add(kvp.Key,kvp.Value.GetStruct());
Volume_sum_tmp+=kvp.Value.Volume_sum;
Volume_Ask_sum_tmp+=kvp.Value.Volume_Ask_sum;
Volume_Bid_sum_tmp+=kvp.Value.Volume_Bid_sum;
}
Volume_sum = Volume_sum_tmp;
Volume_Ask_sum = Volume_Ask_sum_tmp;
Volume_Bid_sum = Volume_Bid_sum_tmp;
PocPrice = curBar.pocPrice;
DayPocPrice = curBar.dayPocPrice;
Time = curBar.Time;
}
}
public struct Claster
{
public int Volume_sum;
public int Volume_Ask_sum;
public int Volume_Bid_sum;
}
public struct Print
{
public DateTime Time;
public int Volume;
public double Price;
public PrintType Type;
public Print(DateTime time, int volume, double price, PrintType type)
{
Time = time;
Volume = volume;
Price = price;
Type = type;
}
}
public struct MarketStop
{
public DateTime Time;
public int Volume;
public double Price_low;
public double Price_high;
public MarketStop(DateTime time,int volume, double price_low, double price_high)
{
Time = time;
Volume = volume;
Price_low = price_low;
Price_high = price_high;
}
}
public struct TickAggregatorElement
{
public DateTime Time;
public int Volume;
public int Volume_Ask;
public int Volume_Bid;
public int Volume_Delta;
public double Price;
public double LowPrice;
public double TopPrice;
public List<Print> PrintList;
public TickAggregatorElement(DateTime time,int volume,int ask, int bid, double price, double lowPrice, double topPrice,List<Print> printList)
{
Time = time;
Volume = volume;
Price = price;
LowPrice=lowPrice;
TopPrice=topPrice;
Volume_Ask = ask*100/volume;
Volume_Bid = 100-Volume_Ask;
Volume_Delta = Math.Abs(Volume_Ask-Volume_Bid);
PrintList = printList;
}
}
}
public enum PrintType
{
ASK,
BID,
NONE
}
}