71 lines
2.9 KiB
C#
71 lines
2.9 KiB
C#
namespace mbts.spark {
|
|
using bts.api;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
|
|
// A lot of options data (migrated from edge) is not normalized
|
|
// on the sec_id, but rather on a combination of symbol, exp, strike.
|
|
// Additionally the exchange is missing. This class serves as that
|
|
// one since point to resolve that.
|
|
// (Over time we will normalize all the data and this wont be needed)
|
|
public static class SecurityNormalizer {
|
|
// f(option_exp:driver_md) -> (sec_id:driver_md)
|
|
|
|
public static IDictionary<UInt32, DriverMD> normalize_drivers(
|
|
OptionDriverPriceMap driver_map, SystemContractData sc_data) {
|
|
if (driver_map is null) {
|
|
return new Dictionary<uint, DriverMD>(0);
|
|
}
|
|
Dictionary<UInt32, DriverMD> rv = new Dictionary<uint, DriverMD>();
|
|
foreach (var kv in driver_map.get_price_map()) {
|
|
var exp = kv.Key;
|
|
var dmd = kv.Value;
|
|
IEnumerable<SecurityMD> exp_secs = sc_data.get_securities(
|
|
(sec_md) => {
|
|
var s = sec_md.security;
|
|
return s.security_type() == SecurityType.Option &&
|
|
s.Symbol == driver_map.option_symbol &&
|
|
s.earliest_expiration() == exp.expiration;
|
|
}
|
|
);
|
|
foreach (var sec_md in exp_secs) {
|
|
rv.Add(sec_md.security.Id, kv.Value);
|
|
}
|
|
}
|
|
return rv;
|
|
}
|
|
public static IEnumerable<Tuple<SecurityImmutable, TheoPoint>> normalize_interp_theos(
|
|
OptionTheoInterpolationData symbol_theos, SystemContractData sc_data) {
|
|
if (symbol_theos is null) {
|
|
return Enumerable.Empty<Tuple<SecurityImmutable, TheoPoint>>();
|
|
}
|
|
var rv = new List<Tuple<SecurityImmutable, TheoPoint>>();
|
|
foreach (DateTime exp in symbol_theos.get_expirations()) {
|
|
Dictionary<double, Dictionary<double, TheoPoint>> exp_data =
|
|
symbol_theos.get_expiration_data(exp);
|
|
// exp_data is driver vs strike vs TP
|
|
foreach (var exp_kv in exp_data) {
|
|
foreach (var k_kv in exp_kv.Value) {
|
|
double strike = k_kv.Key;
|
|
TheoPoint tp = k_kv.Value;
|
|
IEnumerable<SecurityMD> calls = sc_data.get_options(
|
|
null, symbol_theos.symbol, exp, strike, PutOrCall.Call);
|
|
IEnumerable<SecurityMD> puts = sc_data.get_options(
|
|
null, symbol_theos.symbol, exp, strike, PutOrCall.Put);
|
|
foreach (SecurityMD sec_md in calls) {
|
|
rv.Add(Tuple.Create(sec_md.security, tp));
|
|
}
|
|
foreach (SecurityMD sec_md in puts) {
|
|
rv.Add(Tuple.Create(sec_md.security, tp));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return rv;
|
|
}
|
|
}
|
|
}
|