105 lines
4.6 KiB
C#
105 lines
4.6 KiB
C#
namespace bts.SilverPine {
|
|
using BCG;
|
|
|
|
using bts;
|
|
// Given volume data per date for all futures (all exps + spreads) it selects the
|
|
// the future with the highest volume
|
|
[DataStreamUserRespresentation("FuturesActiveContract")]
|
|
public class FuturesActiveContractStream : ViewStream, INonIncGroupByEvaluator {
|
|
|
|
public FuturesActiveContractStream(string future_name)
|
|
// the active contract's resoulution is a day, so we used
|
|
: this(new OHLCVStream(future_name,
|
|
// uses Historic Daily bars to find the acive contract
|
|
new OHLCVStream.OHLCVParams(OHLCVStream.Source.SQL, OHLCVStream.OHLCVType.HISTORIC, OHLCVStream.OHLCVBarSize.ONE_DAY,
|
|
OHLCVStream.get_default_historic_range()))) { }
|
|
|
|
// simply finds the most active (highest volume) contract per day
|
|
public FuturesActiveContractStream(ViewStream ohlcv_stream) {
|
|
var rs = new FilterStream(ohlcv_stream, r => {
|
|
ContractUtils.ContractType t = ContractUtils.get_type(r.get<string>("contract"));
|
|
return t == ContractUtils.ContractType.Future;
|
|
});
|
|
mySetRealSource(new NonIncGroupByStream(rs, this));
|
|
}
|
|
|
|
public IBlueRowCollection evaluate(IDictionary<int, List<IBlueRow>> grouped_rows) {
|
|
Func<string, bool> is_future = (c) => ContractUtils.get_category(c) == ContractUtils.ContractCategory.FutureString;
|
|
BlueTable rv = null;
|
|
Func<List<IBlueRow>, IBlueRow> reduce = (rows) => {
|
|
var future_rows = rows.Where(r => is_future(r.get<string>("contract")));
|
|
IBlueRow rv = future_rows.ElementAt(0);
|
|
foreach (IBlueRow r in future_rows) {
|
|
if (r.get<int>("volume") > rv.get<int>("volume"))
|
|
rv = r;
|
|
}
|
|
return rv;
|
|
};
|
|
foreach (var kv in grouped_rows) {
|
|
var rows = kv.Value;
|
|
if (rows == null || rows.Count == 0)
|
|
continue;
|
|
IBlueRow rr = reduce(rows);
|
|
rv = rv ?? new BlueTable(rr.schema);
|
|
rv.addRow(rr);
|
|
}
|
|
return rv;
|
|
}
|
|
public string[] get_group_by_cols() {
|
|
return new string[] { "ts_event" };
|
|
}
|
|
}
|
|
|
|
// Reduces the OHLCV stream by date and by either contract (if given explicty) or active contract
|
|
// is the first arg is a symbol and not a contract
|
|
public class FilteredOHLCVStream : ViewStream {
|
|
public FilteredOHLCVStream(string symbol_or_contract)
|
|
:
|
|
this(symbol_or_contract, DateTime.Today.Date.Subtract(TimeSpan.FromDays(1)), DateTime.MinValue) { }
|
|
|
|
public FilteredOHLCVStream(string symbol_or_contract, DateTime dt1, DateTime dt2) {
|
|
Tuple<ContractUtils.ContractCategory, string> tup = ContractUtils.get_symbol_category(symbol_or_contract);
|
|
var ohlcv_params = OHLCVStream.get_params(dt1, dt2);
|
|
ViewStream rs = new OHLCVStream(tup.Item2, ohlcv_params);
|
|
switch (tup.Item1) {
|
|
case ContractUtils.ContractCategory.FutureString:
|
|
case ContractUtils.ContractCategory.SpreadString:
|
|
rs = new FilterStream(rs, $"contract = '{symbol_or_contract}'");
|
|
break;
|
|
case ContractUtils.ContractCategory.Symbol:
|
|
rs = new FuturesActiveContractStream(rs);
|
|
break;
|
|
}
|
|
rs = new FilterStream(rs, r => r.get_datetime("ts_event") <= dt1 &&
|
|
r.get_datetime("ts_event") >= dt2);
|
|
mySetRealSource(rs);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
namespace bts.SilverPine {
|
|
using BCG;
|
|
|
|
using bts.api;
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
[DataStreamUserRespresentation("FuturesContinousContract")]
|
|
public class FuturesContinousContractStream : ViewStream {
|
|
// the argument is symbol, like "ES" etc and not the contract itself
|
|
public FuturesContinousContractStream(string symbol) : this(symbol, ViewStreamDatabase.instance) { }
|
|
public FuturesContinousContractStream(string symbol, ViewStreamDatabase db) {
|
|
symbol = symbol.EndsWith("*") ? symbol : symbol + "*";
|
|
ViewStream r = new FuturesHistorical1DayBarsStream(symbol);
|
|
r = new ContractExpanderStream(r, "symbol");
|
|
r = new FilterStream(r, "contract_type='Future'");
|
|
// finding the max volume across symbols, if we need to do this for open_interest, then join here
|
|
// with the open interest stream on symbol/contract
|
|
r = new NonIncGroupByStream(r, "ts_event", "volume:Max");
|
|
r = new NewProjectStream(r, "ts_event symbol !");
|
|
r = new SortStream(r, "-ts_event");
|
|
mySetRealSource(r);
|
|
}
|
|
}
|
|
} |