PNL calculator pushed in
This commit is contained in:
1 parent
dd1d9b5df8
commit
38caa7f892
7 files changed
+487
-23
No files matched your search
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ProtoBuf;
|
||||
using bts.api;
|
||||
|
||||
namespace mbts
|
||||
{
|
||||
/// Wraps up a security type, security name and exchange
|
||||
[ProtoContract]
|
||||
[Serializable]
|
||||
public class PNLRolloverData
|
||||
{
|
||||
[ProtoMember(10)]
|
||||
public readonly DateTime security_expiration;
|
||||
[ProtoMember(20)]
|
||||
public readonly double security_strike;
|
||||
[ProtoMember(30)]
|
||||
public readonly PutOrCall security_cp;
|
||||
[ProtoMember(40)]
|
||||
public readonly double driver_price;
|
||||
[ProtoMember(50)]
|
||||
public readonly double theo;
|
||||
[ProtoMember(60)]
|
||||
public readonly double vol;
|
||||
|
||||
public PNLRolloverData() { }
|
||||
|
||||
public PNLRolloverData(DateTime security_expiration,
|
||||
double security_strike,
|
||||
PutOrCall security_cp,
|
||||
double driver_price,
|
||||
double theo,
|
||||
double vol)
|
||||
{
|
||||
this.security_expiration = security_expiration;
|
||||
this.security_strike = security_strike;
|
||||
this.security_cp = security_cp;
|
||||
this.driver_price = driver_price;
|
||||
this.theo = theo;
|
||||
this.vol = vol;
|
||||
}
|
||||
|
||||
public static byte[] to_bytes(IEnumerable<PNLRolloverData> items)
|
||||
{
|
||||
byte[] rv = mbts.utils.MiscUtils.serialize_proto_list(items);
|
||||
return mbts.utils.MiscUtils.compress(rv);
|
||||
}
|
||||
|
||||
public static IEnumerable<PNLRolloverData> from_bytes(byte[] data)
|
||||
{
|
||||
data = mbts.utils.MiscUtils.decompress(data);
|
||||
return mbts.utils.MiscUtils.deserialize_proto_list<PNLRolloverData>(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using bts.api;
|
||||
|
||||
using mbts;
|
||||
using mbts.spark;
|
||||
|
||||
using System;
|
||||
using System.Reactive.Linq;
|
||||
|
||||
namespace BCG {
|
||||
internal class PnLDataModel : IDisposable {
|
||||
public PnLDataModel(string name, IBus bus) {
|
||||
_name = name;
|
||||
_bus = bus;
|
||||
data_stream = _bus.subscribe<PnLFullData>(SparkTags.ReportBlackboardTags.PNL.SyncCalculationResponse(name)).AsObservable();
|
||||
_bus.post(SparkTags.ReportBlackboardTags.PNL.Activate, _name);
|
||||
// TODO
|
||||
//_dispose = new DisposableSingle(GetType().FullName, () => _bus.post(new ReportBlackboardTags.PNL.Deactivate(), _name));
|
||||
}
|
||||
|
||||
private readonly string _name;
|
||||
private readonly IBus _bus;
|
||||
//private readonly DisposableSingle _dispose;
|
||||
|
||||
public readonly IObservable<PnLFullData> data_stream;
|
||||
|
||||
public void calculate() {
|
||||
_bus.post(SparkTags.ReportBlackboardTags.PNL.SyncCalculationRequest(_name), true);
|
||||
}
|
||||
public void set_ui_overrides(PnLModelUIOverrides ui_overrides) {
|
||||
_bus.post(SparkTags.ReportBlackboardTags.PNL.UIOverrides(_name), ui_overrides);
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
//_dispose.check_not_disposed();
|
||||
//_dispose.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
using System;
|
||||
using mbts.analytics;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using bts.api;
|
||||
|
||||
namespace mbts.analytics {
|
||||
/// Represents the PNL/greeks for one position (option/future etc)
|
||||
public class UnitPositionPNL {
|
||||
public readonly mbts.SecurityStrike contract;
|
||||
public readonly PutOrCall call_put;
|
||||
|
||||
public readonly double ro_driver, ro_theo, driver, theo, position;
|
||||
public readonly IEnumerable<double> pos_greeks;
|
||||
|
||||
public UnitPositionPNL(mbts.SecurityStrike contract, PutOrCall call_put,
|
||||
double ro_driver = Double.NaN, double ro_theo = Double.NaN,
|
||||
double driver = Double.NaN, double theo = Double.NaN,
|
||||
double position = Double.NaN,
|
||||
IEnumerable<double> pos_greeks = null) {
|
||||
this.contract = contract;
|
||||
this.call_put = call_put;
|
||||
this.ro_driver = ro_driver;
|
||||
this.ro_theo = ro_theo;
|
||||
this.driver = driver;
|
||||
this.theo = theo;
|
||||
this.position = position;
|
||||
this.pos_greeks = pos_greeks;
|
||||
}
|
||||
}
|
||||
|
||||
/// PNL/Greek for a trade
|
||||
public class UnitDayPNL {
|
||||
public readonly TradeLogRow trade;
|
||||
public readonly double driver, live_theo;
|
||||
public readonly IEnumerable<double> pos_greeks;
|
||||
|
||||
public UnitDayPNL(TradeLogRow row, double driver, double live_theo,
|
||||
IEnumerable<double> pos_greeks) {
|
||||
trade = row;
|
||||
this.driver = driver;
|
||||
this.live_theo = live_theo;
|
||||
this.pos_greeks = pos_greeks;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace BCG {
|
||||
/// All this is is an x-y grid of PNLGreekUnit's, with the
|
||||
/// expirations (and total) on the y-axis, and greeks on the x
|
||||
public class PnLFullData {
|
||||
public static PnLFullData empty(ReportParameters report_params = null) {
|
||||
return new PnLFullData(report_params ?? new ReportParameters(ReportType.PnL, string.Empty),
|
||||
new PnLGreekData[] { },
|
||||
new Dictionary<VolSurfaceExpiration, IEnumerable<PnLGreekData>>(),
|
||||
Enumerable.Empty<UnitPositionPNL>(),
|
||||
Enumerable.Empty<UnitDayPNL>(),
|
||||
DateTime.MinValue,
|
||||
null);
|
||||
}
|
||||
|
||||
private readonly ReportParameters _report_parameters;
|
||||
private readonly IEnumerable<PnLGreekData> _totals_greeks;
|
||||
private readonly IReadOnlyDictionary<VolSurfaceExpiration, IEnumerable<PnLGreekData>> _expiration_greeks;
|
||||
private readonly IEnumerable<UnitPositionPNL> _position_pnl;
|
||||
private readonly IEnumerable<UnitDayPNL> _day_pnl;
|
||||
private readonly DateTime _last_volsave_time;
|
||||
private readonly mbts.TradeLogRow _last_trade;
|
||||
private readonly int[] _indexer;
|
||||
|
||||
public PnLFullData(ReportParameters report_parameters,
|
||||
IEnumerable<PnLGreekData> total_greeks,
|
||||
IReadOnlyDictionary<VolSurfaceExpiration, IEnumerable<PnLGreekData>> expiration_greeks,
|
||||
IEnumerable<UnitPositionPNL> position_pnl,
|
||||
IEnumerable<UnitDayPNL> day_pnl,
|
||||
DateTime last_volsave_time,
|
||||
mbts.TradeLogRow latest_trade) {
|
||||
_report_parameters = report_parameters;
|
||||
_totals_greeks = total_greeks;
|
||||
_expiration_greeks = expiration_greeks;
|
||||
_position_pnl = position_pnl;
|
||||
_day_pnl = day_pnl;
|
||||
_last_volsave_time = last_volsave_time;
|
||||
_last_trade = latest_trade;
|
||||
_indexer = Enumerable.Repeat<int>(-1, System.Enum.GetNames(typeof(VolSurfaceGreeks)).Length).ToArray();
|
||||
for (int i = 0; i < _report_parameters.ordered_greeks.Length; i++)
|
||||
_indexer[(int)_report_parameters.ordered_greeks[i]] = i;
|
||||
}
|
||||
|
||||
public ReportParameters report_parameters { get { return _report_parameters; } }
|
||||
public DateTime last_volsave_time { get { return _last_volsave_time; } }
|
||||
public mbts.TradeLogRow last_trade { get { return _last_trade; } }
|
||||
|
||||
public IEnumerable<VolSurfaceExpiration> ordered_expirations {
|
||||
get { return _expiration_greeks.Keys.OrderBy(k => k); }
|
||||
}
|
||||
|
||||
public PnLGreekData get_totals_greek(VolSurfaceGreeks greek) { return _get_greek(greek, _totals_greeks); }
|
||||
|
||||
private PnLGreekData _get_greek(VolSurfaceGreeks g, IEnumerable<PnLGreekData> greeks) {
|
||||
int i = _indexer[(int)g];
|
||||
return greeks != null && i > -1 && i < greeks.Count() ? greeks.ElementAt(i) : PnLGreekData.empty;
|
||||
}
|
||||
|
||||
public PnLGreekData get_expiration_greek(VolSurfaceExpiration expiration, VolSurfaceGreeks greek) {
|
||||
IEnumerable<PnLGreekData> exp_greeks = _expiration_greeks.TryGetValue(expiration, out exp_greeks) ? exp_greeks : null;
|
||||
return _get_greek(greek, exp_greeks);
|
||||
}
|
||||
|
||||
public IEnumerable<UnitPositionPNL> position_pnl { get { return _position_pnl; } }
|
||||
|
||||
public IEnumerable<UnitDayPNL> day_pnl { get { return _day_pnl; } }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
namespace BCG
|
||||
{
|
||||
public class PnLGreekData
|
||||
{
|
||||
public static PnLGreekData empty = new PnLGreekData(double.NaN, double.NaN);
|
||||
|
||||
public PnLGreekData(double position_val, double day_val) {
|
||||
this.position_val = position_val;
|
||||
this.day_val = day_val;
|
||||
}
|
||||
|
||||
public double position_val { get; private set; }
|
||||
public double day_val { get; private set; }
|
||||
public double total_val { get { return position_val + day_val; } }
|
||||
|
||||
public static PnLGreekData operator +(PnLGreekData d1, PnLGreekData d2) {
|
||||
return new PnLGreekData(d1.position_val + d2.position_val, d1.day_val + d2.day_val);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using mbts.ui;
|
||||
|
||||
namespace BCG
|
||||
{
|
||||
internal class PnLModelUIOverrides
|
||||
{
|
||||
public readonly double driver_move, vol_move, time_move;
|
||||
public readonly bool compute_on_theos, compute_on_positions, use_candidate_values;
|
||||
|
||||
public PnLModelUIOverrides(double driver_move, double vol_move, double time_move, bool compute_on_theos, bool compute_on_positions, bool use_candidate_values) {
|
||||
this.driver_move = driver_move;
|
||||
this.vol_move = vol_move;
|
||||
this.time_move = time_move;
|
||||
this.compute_on_theos = compute_on_theos;
|
||||
this.compute_on_positions = compute_on_positions;
|
||||
this.use_candidate_values = use_candidate_values;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using bts.api;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace mbts.analytics
|
||||
{
|
||||
public static class VolSurfacePNLCalculator
|
||||
{
|
||||
private static readonly PNLRolloverData _nan_ro_data = VolSurfaceUtilityFunctions.nan_out_fields(() => new PNLRolloverData());
|
||||
|
||||
public static IDictionary<mbts.SecurityStrike, Theos> get_pnl_theos(VolSurfaceInput vs_input,
|
||||
IDictionary<VolSurfaceExpiration, VolSurfaceExpirationAuxiliaryData> aux_data_map,
|
||||
HashSet<SecurityStrike> all_contracts,
|
||||
HashSet<SecurityStrike> pos_contracts)
|
||||
{
|
||||
VolSurfaceTimer timer = new VolSurfaceTimer();
|
||||
var vs_exp_strike_map = VolSurfaceUtilityFunctions.convert_from_strike_map(all_contracts).ToDictionary(kv => kv.Key, kv => kv.Value as IEnumerable<double>);
|
||||
var pos_exp_strike_map = VolSurfaceUtilityFunctions.convert_from_strike_map(pos_contracts);
|
||||
vs_input = VolSurfaceDataFunctions.sync_input_expirations_to_master_list(vs_input, vs_exp_strike_map.Keys);
|
||||
aux_data_map = VolSurfaceUtilityFunctions.sync_map_exps(vs_exp_strike_map.Keys, aux_data_map, true, vs_exp => VolSurfaceExpirationAuxiliaryData.empty);
|
||||
timer.start(); IDictionary<VolSurfaceExpiration, IEnumerable<VolMatrix>> exp_vm_map = VolSurfaceVolMatrixCalculator.generate_volmatrix(vs_input, vs_exp_strike_map, aux_data_map, new double[] { 0.0 }, false, false, false, false, false, pos_exp_strike_map);
|
||||
bts.Log.Gen.info(String.Format("VolSurfaceSlideTheosProducer.get_option_slide_theos volmat time {0}", timer.last_interval_time_ms));
|
||||
timer.start(); IDictionary<VolSurfaceExpiration, IEnumerable<Theos>> exp_theo_map = VolSurfaceTheoCalculator.compute_standard_greeks(vs_input.surface_settings, exp_vm_map, false); timer.stop();
|
||||
bts.Log.Gen.info(String.Format("VolSurfaceSlideTheosProducer.get_option_slide_theos theo time {0}", timer.last_interval_time_ms));
|
||||
IDictionary<mbts.SecurityStrike, Theos> rv = new Dictionary<mbts.SecurityStrike, Theos>();
|
||||
foreach (var exp_kv in exp_theo_map.ToDictionary(kv => new mbts.SecurityExpiration(Venue.Unset, SecurityType.Option, kv.Key.symbol, kv.Key.expiration), kv => kv.Value))
|
||||
foreach (var t in exp_kv.Value)
|
||||
rv[new SecurityStrike(exp_kv.Key, t.strike)] = t;
|
||||
return rv;
|
||||
}
|
||||
|
||||
#region Position PNL
|
||||
|
||||
public static IEnumerable<UnitPositionPNL> compute_position_pnl(// options
|
||||
IDictionary<mbts.SecurityStrike, Tuple<double, double>> option_pos_map,
|
||||
IDictionary<mbts.SecurityStrike, Tuple<mbts.PNLRolloverData, mbts.PNLRolloverData>> option_rollover_data_map,
|
||||
IDictionary<mbts.SecurityStrike, mbts.analytics.Theos> option_theo_map,
|
||||
// futures
|
||||
IDictionary<mbts.SecurityExpiration, double> fut_pos_map,
|
||||
IDictionary<mbts.SecurityExpiration, mbts.PNLRolloverData> fut_rollover_data_map,
|
||||
IDictionary<mbts.SecurityExpiration, VolSurfaceDriverTheo> fut_theo_map,
|
||||
// what to calc and what terms
|
||||
IEnumerable<VolSurfaceGreeks> greeks_to_calc,
|
||||
IDictionary<mbts.SecurityTuple, double> cpp_map,
|
||||
double delta_target_cpp,
|
||||
double oev_target_cpp)
|
||||
{
|
||||
IEnumerable<UnitPositionPNL> option_pnl_list = compute_option_position_pnl(option_pos_map, option_rollover_data_map, option_theo_map, greeks_to_calc,
|
||||
cpp_map.Where(kv => kv.Key.security_type == SecurityType.Option).ToDictionary(kv => kv.Key.security_name, kv => kv.Value),
|
||||
delta_target_cpp, oev_target_cpp),
|
||||
future_pnl_list = compute_future_position_pnl(fut_pos_map, fut_rollover_data_map, fut_theo_map, greeks_to_calc,
|
||||
cpp_map.Where(kv => kv.Key.security_type == SecurityType.Future).ToDictionary(kv => kv.Key.security_name, kv => kv.Value),
|
||||
delta_target_cpp);
|
||||
return Enumerable.Concat(option_pnl_list, future_pnl_list);
|
||||
}
|
||||
|
||||
public static IEnumerable<UnitPositionPNL> compute_option_position_pnl(IDictionary<mbts.SecurityStrike, Tuple<double, double>> pos_map,
|
||||
IDictionary<mbts.SecurityStrike, Tuple<mbts.PNLRolloverData, mbts.PNLRolloverData>> rollover_data_map,
|
||||
IDictionary<mbts.SecurityStrike, mbts.analytics.Theos> theo_map,
|
||||
IEnumerable<VolSurfaceGreeks> greeks_to_calc,
|
||||
IDictionary<string, double> cpp_map,
|
||||
double delta_target_cpp,
|
||||
double oev_target_cpp)
|
||||
{
|
||||
List<UnitPositionPNL> rv = new List<UnitPositionPNL>();
|
||||
foreach (var kv in pos_map)
|
||||
{
|
||||
Theos k_theo = theo_map.TryGetValue(kv.Key, out k_theo) ? k_theo : VolSurfaceRiskCalculator.nan_option_theos;
|
||||
Tuple<mbts.PNLRolloverData, mbts.PNLRolloverData> k_ro_theo = rollover_data_map.TryGetValue(kv.Key, out k_ro_theo) ? k_ro_theo : Tuple.Create(_nan_ro_data, _nan_ro_data);
|
||||
rv.Add(_compute_single_opt_pos_pnl(kv.Key, PutOrCall.Call, kv.Value.Item1, k_theo, k_ro_theo.Item1, greeks_to_calc, cpp_map[kv.Key.expiration.symbol], delta_target_cpp, oev_target_cpp));
|
||||
rv.Add(_compute_single_opt_pos_pnl(kv.Key, PutOrCall.Put, kv.Value.Item2, k_theo, k_ro_theo.Item2, greeks_to_calc, cpp_map[kv.Key.expiration.symbol], delta_target_cpp, oev_target_cpp));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
public static IEnumerable<UnitPositionPNL> compute_future_position_pnl(IDictionary<mbts.SecurityExpiration, double> pos_map,
|
||||
IDictionary<mbts.SecurityExpiration, mbts.PNLRolloverData> rollover_data_map,
|
||||
IDictionary<mbts.SecurityExpiration, VolSurfaceDriverTheo> theo_map,
|
||||
IEnumerable<VolSurfaceGreeks> greeks_to_calc,
|
||||
IDictionary<string, double> cpp_map,
|
||||
double delta_target_cpp)
|
||||
{
|
||||
List<UnitPositionPNL> rv = new List<UnitPositionPNL>();
|
||||
foreach (var kv in pos_map)
|
||||
{
|
||||
VolSurfaceDriverTheo exp_curr_theo = theo_map.TryGetValue(kv.Key, out exp_curr_theo) ? exp_curr_theo : VolSurfaceRiskCalculator.nan_future_theos;
|
||||
mbts.PNLRolloverData exp_ro_theo = rollover_data_map.TryGetValue(kv.Key, out exp_ro_theo) ? exp_ro_theo : _nan_ro_data;
|
||||
rv.Add(_compute_single_future_pos_pnl(kv.Key, kv.Value, exp_curr_theo, exp_ro_theo, greeks_to_calc, cpp_map[kv.Key.symbol], delta_target_cpp));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
private static UnitPositionPNL _compute_single_opt_pos_pnl(SecurityStrike strike, PutOrCall cp,
|
||||
double ro_position,
|
||||
Theos theo,
|
||||
mbts.PNLRolloverData ro_theo,
|
||||
IEnumerable<VolSurfaceGreeks> greeks_to_calc,
|
||||
double symbol_cpp,
|
||||
double delta_target_cpp,
|
||||
double oev_target_cpp)
|
||||
{
|
||||
bool is_call = cp == PutOrCall.Call;
|
||||
IEnumerable<double> pos_greeks = VolSurfaceRiskCalculator.normalize_greeks(greeks_to_calc, g =>
|
||||
{
|
||||
if (g == VolSurfaceGreeks.PNL)
|
||||
return VolSurfaceUtilityFunctions.get_greek(VolSurfaceGreeks.Theo, theo, is_call) - ro_theo.theo;
|
||||
else
|
||||
return VolSurfaceUtilityFunctions.get_greek(g, theo, is_call);
|
||||
},
|
||||
ro_position, symbol_cpp, delta_target_cpp, oev_target_cpp);
|
||||
return new UnitPositionPNL(strike, cp, ro_theo.driver_price, ro_theo.theo, theo.driver, cp == PutOrCall.Call ? theo.call : theo.put, ro_position, pos_greeks);
|
||||
}
|
||||
|
||||
private static UnitPositionPNL _compute_single_future_pos_pnl(SecurityExpiration expiration,
|
||||
double ro_position,
|
||||
VolSurfaceDriverTheo theo,
|
||||
mbts.PNLRolloverData ro_theo,
|
||||
IEnumerable<VolSurfaceGreeks> greeks_to_calc,
|
||||
double cpp,
|
||||
double delta_target_cpp)
|
||||
{
|
||||
IEnumerable<double> pos_greeks = VolSurfaceRiskCalculator.normalize_greeks(greeks_to_calc, g =>
|
||||
{
|
||||
if (g == VolSurfaceGreeks.PNL)
|
||||
return theo.effective_theo - ro_theo.theo;
|
||||
else if (g == VolSurfaceGreeks.Delta || g == VolSurfaceGreeks.BSMDelta || g == VolSurfaceGreeks.FlatVolDelta || g == VolSurfaceGreeks.ExpirationDelta)
|
||||
return theo.effective_delta;
|
||||
else
|
||||
return 0.0;
|
||||
},
|
||||
ro_position, cpp, delta_target_cpp, 1);
|
||||
return new UnitPositionPNL(new SecurityStrike(expiration, Double.NaN), PutOrCall.Call /*TODO: bts.o2.msgs.PutOrCall.type.BOTH*/, ro_theo.driver_price, ro_theo.theo, theo.driver_market_price, theo.effective_theo, ro_position, pos_greeks);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region DayPNL
|
||||
|
||||
public static IEnumerable<UnitDayPNL> compute_day_pnl(IEnumerable<TradeLogRow> trades,
|
||||
IDictionary<mbts.SecurityStrike, mbts.analytics.Theos> option_theo_map,
|
||||
IDictionary<mbts.SecurityExpiration, VolSurfaceDriverTheo> future_theo_map,
|
||||
IEnumerable<VolSurfaceGreeks> greeks_to_calc,
|
||||
IDictionary<mbts.SecurityTuple, double> cpp_map,
|
||||
double delta_target_cpp,
|
||||
double oev_target_cpp)
|
||||
{
|
||||
IEnumerable<UnitDayPNL> option_pnl_list = compute_option_day_pnl(trades.Where(t => t.security_type == SecurityType.Option),
|
||||
option_theo_map, greeks_to_calc,
|
||||
cpp_map.Where(kv => kv.Key.security_type == SecurityType.Option).ToDictionary(kv => kv.Key.security_name, kv => kv.Value),
|
||||
delta_target_cpp, oev_target_cpp),
|
||||
future_pnl_list = compute_future_day_pnl(trades.Where(t => t.security_type ==SecurityType.Future),
|
||||
future_theo_map, greeks_to_calc,
|
||||
cpp_map.Where(kv => kv.Key.security_type == SecurityType.Future).ToDictionary(kv => kv.Key.security_name, kv => kv.Value),
|
||||
delta_target_cpp);
|
||||
return Enumerable.Concat(option_pnl_list, future_pnl_list);
|
||||
}
|
||||
|
||||
public static IEnumerable<UnitDayPNL> compute_option_day_pnl(IEnumerable<TradeLogRow> trades,
|
||||
IDictionary<mbts.SecurityStrike, mbts.analytics.Theos> theo_map,
|
||||
IEnumerable<VolSurfaceGreeks> greeks_to_calc,
|
||||
IDictionary<string, double> cpp_map,
|
||||
double delta_target_cpp,
|
||||
double oev_target_cpp)
|
||||
{
|
||||
List<UnitDayPNL> rv = new List<UnitDayPNL>();
|
||||
foreach (var t in trades.Where(i => i.security_type == SecurityType.Option))
|
||||
{
|
||||
Theos k_theo = theo_map.TryGetValue(t.security_strike, out k_theo) ? k_theo : VolSurfaceRiskCalculator.nan_option_theos;
|
||||
rv.Add(_compute_single_opt_day_pnl(t, k_theo, greeks_to_calc, cpp_map[t.symbol], delta_target_cpp, oev_target_cpp));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
public static IEnumerable<UnitDayPNL> compute_future_day_pnl(IEnumerable<TradeLogRow> trades,
|
||||
IDictionary<mbts.SecurityExpiration, VolSurfaceDriverTheo> theo_map,
|
||||
IEnumerable<VolSurfaceGreeks> greeks_to_calc,
|
||||
IDictionary<string, double> cpp_map,
|
||||
double delta_target_cpp)
|
||||
{
|
||||
List<UnitDayPNL> rv = new List<UnitDayPNL>();
|
||||
foreach (var t in trades.Where(i => i.security_type == SecurityType.Future))
|
||||
{
|
||||
VolSurfaceDriverTheo exp_theo = theo_map.TryGetValue(t.security_expiration, out exp_theo) ? exp_theo : VolSurfaceRiskCalculator.nan_future_theos;
|
||||
rv.Add(_compute_single_future_day_pnl(t, exp_theo, greeks_to_calc, cpp_map[t.symbol], delta_target_cpp));
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
private static UnitDayPNL _compute_single_opt_day_pnl(TradeLogRow row, Theos theo, IEnumerable<VolSurfaceGreeks> greeks_to_calc,
|
||||
double cpp, double delta_target_cpp, double oev_target_cpp)
|
||||
{
|
||||
bool is_call = row.call_put == PutOrCall.Call;
|
||||
IEnumerable<double> pos_greeks = VolSurfaceRiskCalculator.normalize_greeks(greeks_to_calc, g =>
|
||||
{
|
||||
if (g == VolSurfaceGreeks.PNL)
|
||||
return VolSurfaceUtilityFunctions.get_greek(VolSurfaceGreeks.Theo, theo, is_call) - row.price;
|
||||
else
|
||||
return VolSurfaceUtilityFunctions.get_greek(g, theo, is_call);
|
||||
},
|
||||
row.size, cpp, delta_target_cpp, oev_target_cpp);
|
||||
return new UnitDayPNL(row, theo.driver, is_call ? theo.call : theo.put, pos_greeks);
|
||||
}
|
||||
|
||||
private static UnitDayPNL _compute_single_future_day_pnl(TradeLogRow row, VolSurfaceDriverTheo theo, IEnumerable<VolSurfaceGreeks> greeks_to_calc,
|
||||
double cpp, double delta_target_cpp)
|
||||
{
|
||||
IEnumerable<double> pos_greeks = VolSurfaceRiskCalculator.normalize_greeks(greeks_to_calc, g =>
|
||||
{
|
||||
if (g == VolSurfaceGreeks.PNL)
|
||||
return theo.effective_theo - row.price;
|
||||
else if (g == VolSurfaceGreeks.Delta || g == VolSurfaceGreeks.BSMDelta || g == VolSurfaceGreeks.FlatVolDelta || g == VolSurfaceGreeks.ExpirationDelta)
|
||||
return theo.effective_delta;
|
||||
else
|
||||
return 0.0;
|
||||
},
|
||||
row.size, cpp, delta_target_cpp, 1);
|
||||
return new UnitDayPNL(row, theo.driver_market_price, theo.effective_theo, pos_greeks);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -398,33 +398,27 @@ namespace mbts.spark {
|
||||
_mk_tag<string>();
|
||||
}
|
||||
|
||||
// public static class PNL {
|
||||
// /// Publisher: PNLDataModel
|
||||
// /// Data: string (report_name)
|
||||
// public class Activate : EqualsHelper, IBlackboardTag { }
|
||||
public static class PNL {
|
||||
// Publisher: PNLDataModel
|
||||
public static ITag<string> Activate { get; } =
|
||||
_mk_tag<string>();
|
||||
|
||||
// /// Publisher: PNLDataModel
|
||||
// /// Data: string (report_name)
|
||||
// public class Deactivate : EqualsHelper, IBlackboardTag { }
|
||||
// Publisher: PNLDataModel
|
||||
public static ITag<string> Deactivate { get; } =
|
||||
_mk_tag<string>();
|
||||
|
||||
// /// Publisher: PnLDataModel
|
||||
// /// Data: [bool] (just a placeholder type)
|
||||
// public class SyncCalculationRequest : KeyedTag<string>, IBlackboardTag {
|
||||
// public SyncCalculationRequest(string report_name) : base(report_name) { }
|
||||
// }
|
||||
// Publisher: PnLDataModel
|
||||
public static ITag<bool> SyncCalculationRequest(string id) =>
|
||||
_mk_tag<bool, string>(id);
|
||||
|
||||
// /// Publisher: SinglePnLReportCalculator
|
||||
// /// Data: PnLFullData
|
||||
// public class SyncCalculationResponse : KeyedTag<string>, IBlackboardTag {
|
||||
// public SyncCalculationResponse(string report_name) : base(report_name) { }
|
||||
// }
|
||||
// Publisher: SinglePnLReportCalculator
|
||||
public static ITag<PnLFullData> SyncCalculationResponse(string id) =>
|
||||
_mk_tag<PnLFullData, string>(id);
|
||||
|
||||
// /// Publisher: PnLDataModel
|
||||
// /// Data: PnLModelUIOverrides
|
||||
// public class UIOverrides : KeyedTag<string>, IBlackboardTag {
|
||||
// public UIOverrides(string report_name) : base(report_name) { }
|
||||
// }
|
||||
// }
|
||||
// Publisher: PnLDataModel
|
||||
public static ITag<PnLModelUIOverrides> UIOverrides(string id) =>
|
||||
_mk_tag<PnLModelUIOverrides, string>(id);
|
||||
}
|
||||
|
||||
// /// Publisher: ReportConfigurationEditorVM
|
||||
// /// Data: ReportPersistence.SaveReportRequest
|
||||
|
||||
Reference in new issue
Block a user