Future delta fixed in Slide

This commit is contained in:
Taha Afzal committed 2022-06-08 15:56:24 -04:00
1 parent e14b1e0b07
commit 7b04a7849b
1 file changed
+22 -13
@@ -303,6 +303,8 @@ namespace mbts.analytics {
return handle_greeks;
}
public static IDictionary<VolSurfaceExpiration, IEnumerable<SlideGreekUnit>> compute_future_slide(IEnumerable<VolSurfaceGreeks> ordered_greeks,
IEnumerable<double> handles,
mbts.SecurityTuple delta_terms_symbol,
@@ -316,12 +318,6 @@ namespace mbts.analytics {
IDictionary<VolSurfaceExpiration, IEnumerable<SlideGreekUnit>> sym_rv = _compute_single_symbol_future_slide(ordered_greeks, handles, future_symbol, fut_theo_map, fut_pos_map, cpp, delta_terms_symbol_cpp);
sym_rv.ToList().ForEach(kv => rv.Add(kv));
}
//rv = rv.ToDictionary(kv => new VolSurfaceExpiration()
//{
// symbol = kv.Key.symbol,
// expiration = mbts.ui.utility.ExpirationFormatter.convert_real_to_mapped(SecurityType.type.FUTURE, kv.Key.symbol, kv.Key.expiration)
//},
//kv => kv.Value);
return rv;
}
@@ -335,7 +331,9 @@ namespace mbts.analytics {
IDictionary<VolSurfaceExpiration, IEnumerable<SlideGreekUnit>> rv = new Dictionary<VolSurfaceExpiration, IEnumerable<SlideGreekUnit>>();
foreach (KeyValuePair<VolSurfaceExpiration, double> kv in future_pos_map.Where(kv => kv.Key.symbol == fut_sym)) {
VolSurfaceDriverTheo theo_tup = fut_theo_map.TryGetValue(kv.Key, out theo_tup) ? theo_tup : null;
IEnumerable<SlideGreekUnit> exp_pos_greeks = _compute_single_exp_future_slide(ordered_greeks, handles,
IEnumerable<SlideGreekUnit> exp_pos_greeks = _compute_single_exp_future_slide(
kv.Key,
ordered_greeks, handles,
theo_tup != null ? theo_tup.effective_theo : Double.NaN,
theo_tup != null ? theo_tup.effective_delta : Double.NaN,
theo_tup != null ? theo_tup.roll : Double.NaN,
@@ -348,8 +346,18 @@ namespace mbts.analytics {
}
/// Computes the handles for one exp
private static IEnumerable<SlideGreekUnit> _compute_single_exp_future_slide(IEnumerable<VolSurfaceGreeks> ordered_greeks, IEnumerable<double> handles,
double theo, double delta, double roll, double pos) {
private static IEnumerable<SlideGreekUnit> _compute_single_exp_future_slide(
VolSurfaceExpiration vs_exp,
IEnumerable<VolSurfaceGreeks> ordered_greeks, IEnumerable<double> handles,
double theo, double delta, double roll, double pos) {
Func<double, double, double> normalize_delta = (delta, handle_theo) => {
// TODO:
bool qty_in_usd = vs_exp.symbol == "BTC" || vs_exp.symbol == "ETH";
if (qty_in_usd) {
return handle_theo == 0 ? Double.NaN : delta / handle_theo;
}
return delta;
};
IEnumerable<Tuple<double, double, double>> handle_theos = _compute_future_slide_theos(handles, theo, delta, roll);
int pnl_index = Array.FindIndex(ordered_greeks.ToArray(), g => g == VolSurfaceGreeks.Pnl),
delta_index = Array.FindIndex(ordered_greeks.ToArray(), g => g == VolSurfaceGreeks.Delta),
@@ -358,17 +366,18 @@ namespace mbts.analytics {
flat_vol_delta_index = Array.FindIndex(ordered_greeks.ToArray(), g => g == VolSurfaceGreeks.FlatVolDelta);
List<SlideGreekUnit> rv_handle_pos_greeks = new List<SlideGreekUnit>();
foreach (Tuple<double, double, double> t in handle_theos) {
double handle_theo = t.Item1;
double[] position_greeks = Enumerable.Repeat<double>(0.0, ordered_greeks.Count()).ToArray();
if (pnl_index >= 0)
position_greeks[pnl_index] = t.Item1 * pos;
if (delta_index >= 0)
position_greeks[delta_index] = t.Item2 * pos;
position_greeks[delta_index] = normalize_delta(t.Item2 * pos, handle_theo);
if (bsm_delta_index >= 0)
position_greeks[bsm_delta_index] = t.Item2 * pos;
position_greeks[bsm_delta_index] = normalize_delta(t.Item2 * pos, handle_theo);
if (flat_vol_delta_index >= 0)
position_greeks[flat_vol_delta_index] = t.Item2 * pos;
position_greeks[flat_vol_delta_index] = normalize_delta(t.Item2 * pos, handle_theo);
if (exp_delta_index >= 0)
position_greeks[exp_delta_index] = t.Item2 * pos;
position_greeks[exp_delta_index] = normalize_delta(t.Item2 * pos, handle_theo);
rv_handle_pos_greeks.Add(new SlideGreekUnit(t.Item3, position_greeks));
}
return rv_handle_pos_greeks;