forked from MeowBot/OpenQuant.API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBarCompressor.cs
More file actions
99 lines (99 loc) · 2.7 KB
/
Copy pathBarCompressor.cs
File metadata and controls
99 lines (99 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using SmartQuant.Data;
using System;
namespace OpenQuant.API.Compression
{
internal abstract class BarCompressor
{
protected long oldBarSize;
protected long newBarSize;
protected global::OpenQuant.API.Bar bar;
private event CompressedBarEventHandler NewCompressedBar;
protected BarCompressor()
{
this.bar = null;
}
public static BarCompressor GetCompressor(global::OpenQuant.API.BarType barType, long oldBarSize, long newBarSize)
{
BarCompressor barCompressor;
switch (barType)
{
case global::OpenQuant.API.BarType.Time:
barCompressor = new TimeBarCompressor();
break;
case global::OpenQuant.API.BarType.Tick:
barCompressor = new TickBarCompressor();
break;
case global::OpenQuant.API.BarType.Volume:
barCompressor = new VolumeBarCompressor();
break;
case global::OpenQuant.API.BarType.Range:
barCompressor = new RangeBarCompressor();
break;
default:
throw new ArgumentException(string.Format("Unknown bar type - {0}", barType));
}
barCompressor.oldBarSize = oldBarSize;
barCompressor.newBarSize = newBarSize;
return barCompressor;
}
protected abstract void Add(DataEntry entry);
protected void AddItemsToBar(PriceSizeItem[] items)
{
for (int i = 0; i < items.Length; i++)
{
PriceSizeItem item = items[i];
this.AddItemToBar(item);
}
}
protected void CreateNewBar(global::OpenQuant.API.BarType barType, DateTime beginTime, DateTime endTime, double price)
{
if (barType == global::OpenQuant.API.BarType.Time && this.newBarSize == 86400L)
{
this.bar = new global::OpenQuant.API.Bar(new Daily(beginTime, price, price, price, price, 0L));
return;
}
this.bar = new global::OpenQuant.API.Bar(new SmartQuant.Data.Bar(EnumConverter.Convert(barType), this.newBarSize, beginTime, endTime, price, price, price, price, 0L, 0L));
}
protected void EmitNewCompressedBar()
{
if (this.NewCompressedBar != null)
{
this.NewCompressedBar(this, new CompressedBarEventArgs(this.bar));
}
}
public BarSeries Compress(DataEntryEnumerator enumerator)
{
BarSeries series = new BarSeries();
this.NewCompressedBar += delegate(object sender, CompressedBarEventArgs args)
{
series.Add(args.Bar);
};
while (enumerator.MoveNext())
{
this.Add(enumerator.Current);
}
this.Flush();
return series;
}
private void AddItemToBar(PriceSizeItem item)
{
if (item.Price < this.bar.Low)
{
this.bar.bar.Low = item.Price;
}
if (item.Price > this.bar.High)
{
this.bar.bar.High = item.Price;
}
this.bar.bar.Close = item.Price;
this.bar.bar.Volume += (long)item.Size;
}
private void Flush()
{
if (this.bar != null)
{
this.EmitNewCompressedBar();
}
}
}
}