57 lines
2.4 KiB
C#
57 lines
2.4 KiB
C#
namespace bts.SilverPine {
|
|
using BCG;
|
|
|
|
using ZstdSharp.Unsafe;
|
|
|
|
[DataStreamUserRespresentation("AbsVolume")]
|
|
// Calculates the volume within a time window (this does not do rates, like highest daily volume etc,
|
|
// but with some thought can perhaps be extended)
|
|
public class AbsoluteVolumeStream : ViewStream {
|
|
// TODO, abstract this out
|
|
// adds contract_type to OHLCV stream
|
|
private static ViewStream _get_ohlcv_steam(string symbol, DateTime d1, DateTime d2) {
|
|
var ohlcv_params = OHLCVStream.get_params(d1, d2);
|
|
ViewStream rs = new OHLCVStream(symbol, ohlcv_params);
|
|
Func<IBlueTableSchema, IBlueTableSchema> create_schema =
|
|
s => BlueDataUtility.add_columns(s, Tuple.Create(typeof(string), "contract_type"));
|
|
rs = new SuperMapStream(rs,
|
|
s => create_schema(s),
|
|
r => {
|
|
IBlueTableSchema out_schema = create_schema(r.schema);
|
|
string contract_txt = r.get<string>("contract");
|
|
var contr_type = ContractUtils.get_type(contract_txt);
|
|
MutableBlueRow out_row = new MutableBlueRow(out_schema, r);
|
|
out_row.setValue("contract_type", contr_type.ToString());
|
|
return new IBlueRow[] { out_row };
|
|
});
|
|
return rs;
|
|
}
|
|
|
|
public AbsoluteVolumeStream(string symbol, string ctype, DateTime d1)
|
|
: this(symbol, ctype, d1, d1) { }
|
|
|
|
public AbsoluteVolumeStream(string symbol, string ctype, DateTime d1, DateTime d2)
|
|
: this(symbol, Enum.Parse<ContractUtils.ContractType>(ctype), d1, d2) {
|
|
}
|
|
|
|
static void swap_if_necessary(ref DateTime dt1, ref DateTime dt2) {
|
|
if (dt1 < dt2) {
|
|
DateTime aux = dt1;
|
|
dt1 = dt2;
|
|
dt2 = aux;
|
|
}
|
|
}
|
|
|
|
private AbsoluteVolumeStream(string symbol, ContractUtils.ContractType ctype, DateTime d1, DateTime d2) {
|
|
swap_if_necessary(ref d1, ref d2);
|
|
ViewStream ohlcv_str = _get_ohlcv_steam(symbol, d1, d2);
|
|
ohlcv_str = new FilterStream(ohlcv_str,
|
|
r => r.get_datetime("ts_event") <= d1 && r.get_datetime("ts_event") >= d2);
|
|
ohlcv_str = new FilterStream(ohlcv_str, r => String.Compare(ctype.ToString(), r.get<string>("contract_type"), true) == 0);
|
|
ViewStream rs = new MaxStream(ohlcv_str, "volume");
|
|
rs = new NewProjectStream(rs, "volume:total_volume contract !");
|
|
mySetRealSource(rs);
|
|
}
|
|
|
|
}
|
|
} |