55 lines
1.8 KiB
C#
55 lines
1.8 KiB
C#
|
|
using Google.Protobuf.WellKnownTypes;
|
|
|
|
using System;
|
|
using BCG.Utilities.General;
|
|
using System.Data;
|
|
using bts.api.domain;
|
|
|
|
namespace BCG {
|
|
// Stream computes the EMA of a column in any stream. It takes in the base stream, the column name and a smoothing factor
|
|
// as args, and create one row with an ema column, which is the running ema of the column provided
|
|
[DataStreamUserRespresentation("ema")]
|
|
public class EMAStream : ViewStream {
|
|
private ViewStream _stream;
|
|
private readonly string _col_name;
|
|
private EMACalculator _calc;
|
|
private readonly IBlueTableSchema _schema = BlueDataUtility.form_schema(
|
|
"SMA", Tuple.Create("id", TypeCode.Int32),
|
|
Tuple.Create("sma", TypeCode.Double));
|
|
|
|
public EMAStream(ViewStream stream, string col_name, double smoothing_factor) {
|
|
_col_name = col_name;
|
|
_stream = stream;
|
|
_calc = new EMACalculator(smoothing_factor);
|
|
}
|
|
|
|
protected override void myOnFirstSubscriber() {
|
|
_stream.onChange += new ChangeDelegate(_onstream_change);
|
|
}
|
|
protected override void myOnLastSubscriber() {
|
|
_stream.onChange -= new ChangeDelegate(_onstream_change);
|
|
}
|
|
|
|
|
|
private void _onstream_change(object o) {
|
|
|
|
DataPacket dp = o as DataPacket;
|
|
foreach (IBlueRow r in dp.addedRows) {
|
|
double price = r.get<double>(_col_name);
|
|
if (!Double.IsNaN(price))
|
|
_calc.add(price);
|
|
}
|
|
IBlueRow nr = new BlueRow(_schema, 0, _calc.get_EMA());
|
|
BlueTable state = new BlueTable(_schema);
|
|
state.addRow(nr);
|
|
DataPacket dp_out = BlueDataUtility.build_dp_inc(state, myLastValue as DataPacket);
|
|
myFireEvent(dp_out);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|