Reorganized PNL calculator and associated types

This commit is contained in:
Taha Afzal authored and John Colvin committed 2022-08-01 11:14:28 -04:00
1 parent f95b0c19d7
commit f604db6517
6 files changed
+150 -100

No files matched your search

@@ -11,18 +11,22 @@ namespace mbts.analytics {
// tags, into IReadOnlyDictionary of symbol vs data publsihed under one tag
public class SymbolAggregationPublisher<TData> : IDisposable {
private readonly ThreadedBlackboard _bb;
private readonly ITag<SystemContractData> _sc_data_tag;
Func<string, SecurityType, ITag<TData>> _get_tag;
private readonly SecurityType _sec_type;
Func<string, ITag<TData>> _get_tag;
// the tag on which the output will be published
ITag<IReadOnlyDictionary<string, TData>> _out_tag;
private readonly ITag<SystemContractData> _sc_data_tag;
private Dictionary<string, ITag<TData>> _tag_map =
new Dictionary<string, ITag<TData>>();
private HashSet<object> _obj_tags = new HashSet<object>();
private bool _dirty = true;
public SymbolAggregationPublisher(
ThreadedBlackboard bb,
Func<string, SecurityType, ITag<TData>> get_tag,
SecurityType sec_type,
Func<string, ITag<TData>> get_tag,
ITag<IReadOnlyDictionary<string, TData>> out_tag) {
_bb = bb;
_sec_type = sec_type;
_get_tag = get_tag;
_out_tag = out_tag;
_sc_data_tag = SparkTags.SystemContractsTag;
@@ -48,13 +52,14 @@ namespace mbts.analytics {
return;
}
foreach (var st in sc_data.sectup_map) {
if (st.security_type != _sec_type) {
continue;
}
// this is not correct since it is assuming symbol/name is
// unique across securty_tuples
string symbol = st.security_name;
var sec_type = st.security_type;
var key = String.Format("{0}_{1}", symbol, sec_type.ToString());
if (!_tag_map.ContainsKey(key)) {
var tag = _get_tag(symbol, sec_type);
if (!_tag_map.ContainsKey(symbol)) {
var tag = _get_tag(symbol);
if (tag != null) {
_tag_map.Add(symbol, tag);
_obj_tags.Add(tag);
@@ -102,16 +102,13 @@ namespace mbts.spark {
_subs.Add(new AnalyticsParametersManager(bus, _analytics_service));
_subs.Add(new ResourceAllocator(bus, _bb));
_subs.Add(new SymbolAggregationPublisher<OptionDriverPriceMap>(
_bb, (sym, sec_type) =>
sec_type == SecurityType.Option ? SparkTags.OptionDriverBBOTag(sym) : null,
SparkTags.AllOptionDriverBBOTag));
_bb, SecurityType.Option, sym => SparkTags.OptionDriverBBOTag(sym),
SparkTags.AllOptionDriverBBOTag));
_subs.Add(new SymbolAggregationPublisher<OptionTheoInterpolationData>(
_bb, (sym, sec_type) =>
sec_type == SecurityType.Option ? SparkTags.SymbolInterpGreeksTag(sym) : null,
_bb, SecurityType.Option, sym => SparkTags.SymbolInterpGreeksTag(sym),
SparkTags.AllInterpGreeks));
_subs.Add(new SymbolAggregationPublisher<ExpStrikeKeyedData<TheoPoint>>(
_bb, (sym, sec_type) =>
sec_type == SecurityType.Option ? SparkTags.TC2Tags.LiveTheosTag(sym) : null,
_bb, SecurityType.Option, sym => SparkTags.TC2Tags.LiveTheosTag(sym),
SparkTags.TC2Tags.AllLiveTheosTag));
_subs.Add(new LiveTheoPublisher(_bb));
_subs.Add(new VolSurfaceInterpGreeksMultiplexer(_bb));
@@ -238,6 +238,15 @@ namespace mbts.spark {
return rv;
}
internal static string log_string(this IPNLUnit p) {
string rv = "IPNLUnit:";
rv += $" account: {p.account} sec: {p.security.security_desc()} driver: {p.driver} theo: {p.theo} settle: {p.settle_price}" +
$" start_pos: {p.start_long_pos - p.start_short_pos} day_pos: {p.day_long_pos - p.day_short_pos}" +
$" start_delta: {p.start_delta} start_vega: {p.start_vega}" +
$" day_delta: {p.day_delta} day_vega: {p.day_vega}";
return rv;
}
internal static int tid() {
return System.Threading.Thread.CurrentThread.ManagedThreadId;
}
+28
View File
@@ -0,0 +1,28 @@
namespace mbts.analytics {
using bts.api;
// This contains all the PNL data, divided up per start of day and day,
// for an account, security
public interface IPNLUnit {
public string account { get; }
public SecurityImmutable security { get; }
public double driver { get; }
public double theo { get; }
public double settle_price { get; }
public double start_long_pos { get; }
public double start_short_pos { get; }
public double day_long_pos { get; }
public double day_short_pos { get; }
public double start_pnl { get; }
public double day_pnl { get; }
public double start_delta { get; }
public double start_gamma { get; }
public double start_vega { get; }
public double start_theta { get; }
public double day_delta { get; }
public double day_gamma { get; }
public double day_vega { get; }
public double day_theta { get; }
}
}
@@ -1,6 +1,3 @@
using mbts;
using mbts.analytics;
using mbts.utils;
using System;
using System.Collections.Generic;
@@ -8,43 +5,16 @@ using System.Linq;
using mbts.spark;
using bts.api;
using mbts.ui;
using mbts.spark.filters;
using bts.api.analytics;
using bts;
using System.Reactive.Linq;
using bts.collections;
using BCG;
using bts.api.admin;
using NUnit.Framework.Internal;
namespace mbts.analytics {
public interface IPNLUnit {
public string account { get; }
public uint security_key { get; }
public double driver { get; }
public double theo { get; }
public double settle_price { get; }
public double start_long_pos { get; }
public double start_short_pos { get; }
public double day_long_pos { get; }
public double day_short_pos { get; }
public double start_pnl { get; }
public double day_pnl { get; }
public double start_delta { get; }
public double start_gamma { get; }
public double start_vega { get; }
public double start_theta { get; }
public double day_delta { get; }
public double day_gamma { get; }
public double day_vega { get; }
public double day_theta { get; }
}
internal class PNLReportCalculator : IDisposable {
private class PNLUnit : IPNLUnit {
public string account { get; init; }
public uint security_key { get; init; }
public SecurityImmutable security { get; init; }
public double driver { get; set; }
public double theo { get; set; }
public double settle_price { get; set; }
@@ -54,18 +24,20 @@ namespace mbts.analytics {
public double day_short_pos { get; set; }
public double start_pnl { get; set; }
public double day_pnl { get; set; }
// start greeks
public double start_delta { get; set; }
public double start_gamma { get; set; }
public double start_vega { get; set; }
public double start_theta { get; set; }
// day greeks
public double day_delta { get; set; }
public double day_gamma { get; set; }
public double day_vega { get; set; }
public double day_theta { get; set; }
internal void add_tl(PNLTradeRow tl) {
internal void absorb_trade(PNLTradeRow tl) {
bts.Log.assert(tl.tl_row.account == account);
bts.Log.assert(tl.tl_row.security_key == security_key);
bts.Log.assert(tl.tl_row.security_key == security.Id);
driver = tl.driver;
theo = tl.theo;
settle_price = tl.settle_price;
@@ -90,15 +62,17 @@ namespace mbts.analytics {
}
IBus _bus;
IDisposable _dispose;
// TODO, get it from report/user ?
readonly TimeSpan _sod_time_utc = TimeSpan.FromHours(8);
readonly TheoPoint _def_opt_theo = TheoPoint.getNulledObject();
readonly FutureTheoStructure _def_fut_theo =
new FutureTheoStructure(DateTime.MinValue, Double.NaN, Double.NaN, Double.NaN, 0);
readonly SecurityMD _def_md = new SecurityMD();
readonly bool _log_output = true;
public PNLReportCalculator(bts.api.IBus bus) {
Log.Ui.info("PNLReportCalculator init'ed");
_bus = bus;
HighPerformanceTimer timer = new HighPerformanceTimer();
_dispose =
bus.subscribe(SparkTags.TradeLogTag)
.CombineLatest(
@@ -108,27 +82,83 @@ namespace mbts.analytics {
(trades, all_theos, fut_theos, secs) => (trades, all_theos, fut_theos, secs))
.BufferLatest(TimeSpan.FromSeconds(2))
.Subscribe(tup => {
var pnl_trades =
_create_pnl_rows(tup.trades, tup.all_theos, tup.fut_theos, tup.secs);
timer.start();
var pnl_trades = _create_pnl_rows(tup.trades, tup.all_theos, tup.fut_theos, tup.secs);
var output = _create_output(pnl_trades);
timer.stop();
Log.Ui.info($"PNLReportCalculator n_rows: {output.Count()} time(msecs): {timer.lastIntervalTime}");
_log_before_posting(output);
_bus.post(SparkTags.ReportBlackboardTags.PNL.PNLData, output);
});
}
private void _re_organize_and_publish(IEnumerable<PNLTradeRow> tl_rows) {
IDictionary<string, PNLUnit> keyed_map = new Dictionary<string, PNLUnit>();
private IEnumerable<PNLTradeRow> _create_pnl_rows(
IEnumerable<mbts.ITradeLogRow> trades,
IReadOnlyDictionary<string, ExpStrikeKeyedData<TheoPoint>> option_theos,
IReadOnlyDictionary<SecurityExpiration, FutureTheoStructure> future_theos,
IReadOnlyDictionary<uint, SecurityMD> md) {
trades = trades.Where(t => t.is_active);
List<PNLTradeRow> pnl_trades = new List<PNLTradeRow>();
int n_option_theos = 0;
option_theos.ForEach(t => n_option_theos += t.Value.count);
Log.Ui.info($"PNLReportCalculator._compute_and_fire n_trades: {trades.Count()} " +
$"option_theos: {n_option_theos * 2} future_theos: {future_theos.Count()} md: {md.Count()}");
foreach (ITradeLogRow tl in trades) {
SecurityMD tl_md;
var found_sec = _get_md(tl, md, out tl_md);
if (!found_sec) {
Log.Ui.error($"Could not find security for trade id: {tl.trade_id}" +
$"key: {tl.key} sec_key: {tl.security_key}");
continue;
}
if (tl.security_type == SecurityType.Option) {
PNLTradeRow pnl_tl = new PNLTradeRow() {
tl_row = tl,
opt_theo = _get_option_theo(tl, option_theos),
fut_theo = null,
md = tl_md,
is_day_trade = _is_day_trade(tl)
};
pnl_trades.Add(pnl_tl);
} else if (tl.security_type == SecurityType.Future) {
PNLTradeRow pnl_tl = new PNLTradeRow() {
tl_row = tl,
opt_theo = null,
fut_theo = _get_fut_theo(tl, future_theos),
md = tl_md,
is_day_trade = _is_day_trade(tl)
};
pnl_trades.Add(pnl_tl);
}
}
return pnl_trades;
}
private IEnumerable<IPNLUnit> _create_output(IEnumerable<PNLTradeRow> tl_rows) {
var keyed_map = new Dictionary<string, PNLUnit>();
foreach (PNLTradeRow tl in tl_rows) {
string key = $"{tl.tl_row.account}+_{tl.tl_row.security_key}";
string key = $"{tl.tl_row.account}_{tl.tl_row.security_key}";
PNLUnit u = null;
if (keyed_map.TryGetValue(key, out u)) {
if (!keyed_map.TryGetValue(key, out u)) {
u = new PNLUnit() {
account = tl.tl_row.account,
security_key = tl.tl_row.security_key
security = tl.security
};
keyed_map.Add(key, u);
}
u.add_tl(tl);
u.absorb_trade(tl);
}
return keyed_map.Values.Select(t => t as IPNLUnit);
}
private void _log_before_posting(IEnumerable<IPNLUnit> output) {
if (_log_output) {
string rv = "PNL:";
foreach (var p in output) {
rv += "\n" + p.log_string();
}
Log.Ui.info(rv);
}
_bus.post(SparkTags.ReportBlackboardTags.PNL.PNLData,
keyed_map.Values.Select(t => t as IPNLUnit));
}
private static DateTime _get_sod_time(TimeSpan sod_time_utc) {
@@ -164,50 +194,19 @@ namespace mbts.analytics {
return _def_fut_theo;
}
private SecurityMD _get_md(
private bool _get_md(
ITradeLogRow tl,
IReadOnlyDictionary<uint, SecurityMD> md) {
return _def_md;
IReadOnlyDictionary<uint, SecurityMD> md_map, out SecurityMD md) {
if (md_map.TryGetValue(tl.security_key, out md)) {
return true;
}
return false;
}
private bool _is_day_trade(ITradeLogRow tl) {
return tl.time_entered.ToUniversalTime() > _get_sod_time(_sod_time_utc);
}
private IEnumerable<PNLTradeRow> _create_pnl_rows(
IEnumerable<mbts.ITradeLogRow> trades,
IReadOnlyDictionary<string, ExpStrikeKeyedData<TheoPoint>> option_theos,
IReadOnlyDictionary<SecurityExpiration, FutureTheoStructure> future_theos,
IReadOnlyDictionary<uint, SecurityMD> md) {
List<PNLTradeRow> pnl_trades = new List<PNLTradeRow>();
int n_option_theos = 0;
option_theos.ForEach(t => n_option_theos += t.Value.count);
trades = trades.Where(t => t.is_active);
Log.Ui.info($"PNLReportCalculator._compute_and_fire n_trades: {trades.Count()} " +
$"option_theos: {n_option_theos * 2} future_theos: {future_theos.Count()} md: {md.Count()}");
foreach (ITradeLogRow tl in trades) {
if (tl.security_type == SecurityType.Option) {
PNLTradeRow pnl_tl = new PNLTradeRow() {
tl_row = tl,
opt_theo = _get_option_theo(tl, option_theos),
fut_theo = null,
md = _get_md(tl, md),
is_day_trade = _is_day_trade(tl)
};
pnl_trades.Add(pnl_tl);
} else if (tl.security_type == SecurityType.Future) {
PNLTradeRow pnl_tl = new PNLTradeRow() {
tl_row = tl,
opt_theo = null,
fut_theo = _get_fut_theo(tl, future_theos),
md = _get_md(tl, md),
is_day_trade = _is_day_trade(tl)
};
}
}
return pnl_trades;
}
public void Dispose() =>
Disposable.dispose(ref _dispose);
}
+17 -5
View File
@@ -5,15 +5,18 @@ using bts.api;
using BCG;
namespace mbts.analytics {
// Pairs ITradeLogRow along with the data theo, md that is needed to compute
// the PNL fields
// Pairs ITradeLogRow along with the data (theo, md) that is needed to compute
// the PNL fields. The getters are simple redirection and aggregation functions
internal class PNLTradeRow {
public ITradeLogRow tl_row { get; init; }
// One of the opt_theo or fut_theo will be populated
public TheoPoint opt_theo { get; init; }
public FutureTheoStructure fut_theo { get; init; }
public SecurityMD md { get; init; }
public bool is_day_trade { get; init; }
public SecurityImmutable security { get { return md.security; } }
public double pnl {
get {
if (is_day_trade) {
@@ -36,7 +39,8 @@ namespace mbts.analytics {
public double theo {
get {
if (tl_row.security_type == SecurityType.Option) {
return tl_row.call_put == PutOrCall.Call ? opt_theo.fwd_normalized_call : opt_theo.fwd_normalized_put;
return tl_row.call_put == PutOrCall.Call ?
opt_theo.fwd_normalized_call : opt_theo.fwd_normalized_put;
} else if (tl_row.security_type == SecurityType.Future) {
return fut_theo.theo;
}
@@ -44,14 +48,16 @@ namespace mbts.analytics {
}
}
public double settle_price {
get { return md.md.settlement_price; }
get {
return md.md == null ? Double.NaN : md.md.settlement_price;
}
}
public double pos_delta {
get {
if (tl_row.security_type == SecurityType.Option)
return (tl_row.call_put == PutOrCall.Call ? opt_theo.cdelta : opt_theo.pdelta) * tl_row.size;
else if (tl_row.security_type == SecurityType.Future)
return tl_row.size * (tl_row.price / fut_theo.theo);
return tl_row.size / fut_theo.theo;
return 0;
}
}
@@ -73,6 +79,12 @@ namespace mbts.analytics {
(tl_row.call_put == PutOrCall.Call ? opt_theo.c_theta : opt_theo.p_theta) * tl_row.size : 0;
}
}
public double pos_oev {
get {
return tl_row.security_type == SecurityType.Option ?
(tl_row.call_put == PutOrCall.Call ? opt_theo.c_oev : opt_theo.p_oev) * tl_row.size : 0;
}
}
}
}