32 lines
1.5 KiB
C#
32 lines
1.5 KiB
C#
namespace bts.SilverPine {
|
|
using BCG;
|
|
// Takes multiple bars and collapses them into one, which is aggregated over a bigger time period.
|
|
// All bars belonging to a common group_by_col, are aggregated
|
|
public class BarsAggregatorStream : ViewStream {
|
|
public BarsAggregatorStream(ViewStream ohlcv_stream, params string[] group_by_cols) {
|
|
ViewStream rs = new NonIncGroupByStream(ohlcv_stream,
|
|
() => { return group_by_cols; },
|
|
(group_rows) => {
|
|
var sorted_rows = group_rows.OrderBy(r => r.get_datetime("ts_event"));
|
|
IBlueRow first_row = sorted_rows.First(),
|
|
last_row = sorted_rows.Last();
|
|
double open = first_row.get<double>("open");
|
|
double close = last_row.get<double>("close");
|
|
double low = sorted_rows.Min(r => r.get<double>("low"));
|
|
double high = sorted_rows.Max(r => r.get<double>("high"));
|
|
double volume = 0.0;
|
|
foreach (IBlueRow r in sorted_rows)
|
|
volume += r.get<double>("volume");
|
|
MutableBlueRow rv = new MutableBlueRow(first_row);
|
|
rv.setValue("open", open);
|
|
rv.setValue("close", close);
|
|
rv.setValue("low", low);
|
|
rv.setValue("high", high);
|
|
rv.setValue("volume", volume);
|
|
return new IBlueRow[] { rv }.ToList();
|
|
}
|
|
);
|
|
mySetRealSource(rs);
|
|
}
|
|
}
|
|
} |