Added PNL Calculation code
This commit is contained in:
1 parent
655bf87e92
commit
f95b0c19d7
9 files changed
+329
-11
No files matched your search
@@ -12,7 +12,7 @@ namespace mbts.analytics {
|
||||
public class SymbolAggregationPublisher<TData> : IDisposable {
|
||||
private readonly ThreadedBlackboard _bb;
|
||||
private readonly ITag<SystemContractData> _sc_data_tag;
|
||||
Func<string, ITag<TData>> _get_tag;
|
||||
Func<string, SecurityType, ITag<TData>> _get_tag;
|
||||
ITag<IReadOnlyDictionary<string, TData>> _out_tag;
|
||||
private Dictionary<string, ITag<TData>> _tag_map =
|
||||
new Dictionary<string, ITag<TData>>();
|
||||
@@ -20,7 +20,7 @@ namespace mbts.analytics {
|
||||
private bool _dirty = true;
|
||||
public SymbolAggregationPublisher(
|
||||
ThreadedBlackboard bb,
|
||||
Func<string, ITag<TData>> get_tag,
|
||||
Func<string, SecurityType, ITag<TData>> get_tag,
|
||||
ITag<IReadOnlyDictionary<string, TData>> out_tag) {
|
||||
_bb = bb;
|
||||
_get_tag = get_tag;
|
||||
@@ -51,12 +51,16 @@ namespace mbts.analytics {
|
||||
// this is not correct since it is assuming symbol/name is
|
||||
// unique across securty_tuples
|
||||
string symbol = st.security_name;
|
||||
if (!_tag_map.ContainsKey(symbol)) {
|
||||
var symbol_tag = _get_tag(symbol);
|
||||
_tag_map.Add(symbol, symbol_tag);
|
||||
_obj_tags.Add(symbol_tag);
|
||||
_bb.subscribe(symbol_tag);
|
||||
_dirty = true;
|
||||
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 != null) {
|
||||
_tag_map.Add(symbol, tag);
|
||||
_obj_tags.Add(tag);
|
||||
_bb.subscribe(tag);
|
||||
_dirty = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ using System.Collections;
|
||||
using mbts.spark;
|
||||
using System.DirectoryServices.ActiveDirectory;
|
||||
using Spark.Core.Protobuf;
|
||||
using mbts.ui;
|
||||
// using mbts.bb;
|
||||
|
||||
namespace mbts.spark {
|
||||
@@ -101,11 +102,17 @@ namespace mbts.spark {
|
||||
_subs.Add(new AnalyticsParametersManager(bus, _analytics_service));
|
||||
_subs.Add(new ResourceAllocator(bus, _bb));
|
||||
_subs.Add(new SymbolAggregationPublisher<OptionDriverPriceMap>(
|
||||
_bb, (sym) => SparkTags.OptionDriverBBOTag(sym),
|
||||
SparkTags.AllOptionDriverBBOTag));
|
||||
_bb, (sym, sec_type) =>
|
||||
sec_type == SecurityType.Option ? SparkTags.OptionDriverBBOTag(sym) : null,
|
||||
SparkTags.AllOptionDriverBBOTag));
|
||||
_subs.Add(new SymbolAggregationPublisher<OptionTheoInterpolationData>(
|
||||
_bb, (sym) => SparkTags.SymbolInterpGreeksTag(sym),
|
||||
_bb, (sym, sec_type) =>
|
||||
sec_type == SecurityType.Option ? SparkTags.SymbolInterpGreeksTag(sym) : null,
|
||||
SparkTags.AllInterpGreeks));
|
||||
_subs.Add(new SymbolAggregationPublisher<ExpStrikeKeyedData<TheoPoint>>(
|
||||
_bb, (sym, sec_type) =>
|
||||
sec_type == SecurityType.Option ? SparkTags.TC2Tags.LiveTheosTag(sym) : null,
|
||||
SparkTags.TC2Tags.AllLiveTheosTag));
|
||||
_subs.Add(new LiveTheoPublisher(_bb));
|
||||
_subs.Add(new VolSurfaceInterpGreeksMultiplexer(_bb));
|
||||
// usage
|
||||
|
||||
@@ -29,6 +29,7 @@ namespace bts.daemons {
|
||||
readonly OrderService _order_service;
|
||||
readonly ErrorReporter _error_reporter;
|
||||
readonly IBus _bus;
|
||||
readonly mbts.analytics.PNLReportCalculator _pnl_calc;
|
||||
|
||||
IReadOnlyDictionary<int, ReportImmutable> _report_parameters_map =
|
||||
new Dictionary<int, ReportImmutable>();
|
||||
@@ -48,6 +49,7 @@ namespace bts.daemons {
|
||||
_error_reporter = error_reporter;
|
||||
_order_service = order_service;
|
||||
_report_calculators = new(mk_report_calculator);
|
||||
_pnl_calc = new PNLReportCalculator(bus);
|
||||
|
||||
_dispose = Disposable.Create(
|
||||
bus.subscribe(SparkTags.ReportBlackboardTags.Create).Subscribe(rp => _create_report(rp).ignore()),
|
||||
|
||||
@@ -21,6 +21,7 @@ namespace mbts {
|
||||
) {
|
||||
this.trade_id = trade_id;
|
||||
this.key = String.Empty;
|
||||
security_key = 1;
|
||||
security_type = security_expiration.security_type;
|
||||
symbol = security_expiration.symbol;
|
||||
expiration = security_expiration.expiration;
|
||||
@@ -40,6 +41,7 @@ namespace mbts {
|
||||
|
||||
public int trade_id { get; }
|
||||
public string key { get; }
|
||||
public uint security_key { get; }
|
||||
public SecurityType security_type { get; }
|
||||
public string symbol { get; }
|
||||
public DateTime? expiration { get; }
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace mbts {
|
||||
public interface ITradeLogRow {
|
||||
int trade_id { get; }
|
||||
string key { get; }
|
||||
uint security_key { get; }
|
||||
SecurityType security_type { get; }
|
||||
string symbol { get; }
|
||||
DateTime? expiration { get; }
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
|
||||
using mbts;
|
||||
using mbts.analytics;
|
||||
using mbts.utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
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 double driver { get; set; }
|
||||
public double theo { get; set; }
|
||||
public double settle_price { get; set; }
|
||||
public double start_long_pos { get; set; }
|
||||
public double start_short_pos { get; set; }
|
||||
public double day_long_pos { get; set; }
|
||||
public double day_short_pos { get; set; }
|
||||
public double start_pnl { get; set; }
|
||||
public double day_pnl { get; set; }
|
||||
public double start_delta { get; set; }
|
||||
public double start_gamma { get; set; }
|
||||
public double start_vega { get; set; }
|
||||
public double start_theta { get; set; }
|
||||
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) {
|
||||
bts.Log.assert(tl.tl_row.account == account);
|
||||
bts.Log.assert(tl.tl_row.security_key == security_key);
|
||||
driver = tl.driver;
|
||||
theo = tl.theo;
|
||||
settle_price = tl.settle_price;
|
||||
if (!tl.is_day_trade) {
|
||||
if (tl.tl_row.size > 0) start_long_pos += tl.tl_row.size;
|
||||
else start_short_pos += Math.Abs(tl.tl_row.size);
|
||||
start_pnl += tl.pnl;
|
||||
start_delta += tl.pos_delta;
|
||||
start_gamma += tl.pos_gamma;
|
||||
start_vega += tl.pos_vega;
|
||||
start_theta += tl.pos_theta;
|
||||
} else {
|
||||
if (tl.tl_row.size > 0) day_long_pos += tl.tl_row.size;
|
||||
else day_short_pos += Math.Abs(tl.tl_row.size);
|
||||
day_pnl += tl.pnl;
|
||||
day_delta += tl.pos_delta;
|
||||
day_gamma += tl.pos_gamma;
|
||||
day_vega += tl.pos_vega;
|
||||
day_theta += tl.pos_theta;
|
||||
}
|
||||
}
|
||||
}
|
||||
IBus _bus;
|
||||
IDisposable _dispose;
|
||||
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();
|
||||
|
||||
public PNLReportCalculator(bts.api.IBus bus) {
|
||||
Log.Ui.info("PNLReportCalculator init'ed");
|
||||
_bus = bus;
|
||||
_dispose =
|
||||
bus.subscribe(SparkTags.TradeLogTag)
|
||||
.CombineLatest(
|
||||
bus.subscribe(SparkTags.TC2Tags.AllLiveTheosTag),
|
||||
bus.subscribe(SparkTags.TC2Tags.LiveFutureTheosTag),
|
||||
bus.subscribe(SparkTags.SecuritiesTag),
|
||||
(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);
|
||||
});
|
||||
}
|
||||
|
||||
private void _re_organize_and_publish(IEnumerable<PNLTradeRow> tl_rows) {
|
||||
IDictionary<string, PNLUnit> keyed_map = new Dictionary<string, PNLUnit>();
|
||||
foreach (PNLTradeRow tl in tl_rows) {
|
||||
string key = $"{tl.tl_row.account}+_{tl.tl_row.security_key}";
|
||||
PNLUnit u = null;
|
||||
if (keyed_map.TryGetValue(key, out u)) {
|
||||
u = new PNLUnit() {
|
||||
account = tl.tl_row.account,
|
||||
security_key = tl.tl_row.security_key
|
||||
};
|
||||
keyed_map.Add(key, u);
|
||||
}
|
||||
u.add_tl(tl);
|
||||
}
|
||||
_bus.post(SparkTags.ReportBlackboardTags.PNL.PNLData,
|
||||
keyed_map.Values.Select(t => t as IPNLUnit));
|
||||
}
|
||||
|
||||
private static DateTime _get_sod_time(TimeSpan sod_time_utc) {
|
||||
DateTime sod_dt = DateTime.UtcNow.Date + sod_time_utc;
|
||||
if (DateTime.UtcNow >= sod_dt) {
|
||||
return sod_dt;
|
||||
}
|
||||
return sod_dt.Subtract(TimeSpan.FromDays(1));
|
||||
}
|
||||
|
||||
private TheoPoint _get_option_theo(
|
||||
ITradeLogRow tl,
|
||||
IReadOnlyDictionary<string, ExpStrikeKeyedData<TheoPoint>> opt_theos) {
|
||||
ExpStrikeKeyedData<TheoPoint> sym_theos = null;
|
||||
if (opt_theos.TryGetValue(tl.symbol, out sym_theos)) {
|
||||
var rv = sym_theos.get_data(tl.expiration.Value, tl.strike);
|
||||
if (rv != null)
|
||||
return rv;
|
||||
}
|
||||
return _def_opt_theo;
|
||||
}
|
||||
|
||||
private FutureTheoStructure _get_fut_theo(
|
||||
ITradeLogRow tl,
|
||||
IReadOnlyDictionary<SecurityExpiration, FutureTheoStructure> fut_theos) {
|
||||
var exp = tl.security_expiration;
|
||||
if (exp == null)
|
||||
return _def_fut_theo;
|
||||
FutureTheoStructure rv = null;
|
||||
if (fut_theos.TryGetValue(exp, out rv)) {
|
||||
return rv == null ? _def_fut_theo : rv;
|
||||
}
|
||||
return _def_fut_theo;
|
||||
}
|
||||
|
||||
private SecurityMD _get_md(
|
||||
ITradeLogRow tl,
|
||||
IReadOnlyDictionary<uint, SecurityMD> md) {
|
||||
return _def_md;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using mbts.spark;
|
||||
using bts.api;
|
||||
|
||||
using BCG;
|
||||
|
||||
namespace mbts.analytics {
|
||||
// Pairs ITradeLogRow along with the data theo, md that is needed to compute
|
||||
// the PNL fields
|
||||
internal class PNLTradeRow {
|
||||
public ITradeLogRow tl_row { get; init; }
|
||||
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 double pnl {
|
||||
get {
|
||||
if (is_day_trade) {
|
||||
return (theo - tl_row.price) * tl_row.size;
|
||||
}
|
||||
// use settle price ?
|
||||
return (theo - tl_row.price) * tl_row.size;
|
||||
}
|
||||
}
|
||||
public double driver {
|
||||
get {
|
||||
if (tl_row.security_type == SecurityType.Option) {
|
||||
return opt_theo.driver;
|
||||
} else if (tl_row.security_type == SecurityType.Future) {
|
||||
return fut_theo.driver_price;
|
||||
}
|
||||
return Double.NaN;
|
||||
}
|
||||
}
|
||||
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;
|
||||
} else if (tl_row.security_type == SecurityType.Future) {
|
||||
return fut_theo.theo;
|
||||
}
|
||||
return Double.NaN;
|
||||
}
|
||||
}
|
||||
public double settle_price {
|
||||
get { return 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 0;
|
||||
}
|
||||
}
|
||||
public double pos_gamma {
|
||||
get {
|
||||
return tl_row.security_type == SecurityType.Option ?
|
||||
(tl_row.call_put == PutOrCall.Call ? opt_theo.cgamma : opt_theo.pgamma) * tl_row.size : 0;
|
||||
}
|
||||
}
|
||||
public double pos_vega {
|
||||
get {
|
||||
return tl_row.security_type == SecurityType.Option ?
|
||||
(tl_row.call_put == PutOrCall.Call ? opt_theo.cvega : opt_theo.pvega) * tl_row.size : 0;
|
||||
}
|
||||
}
|
||||
public double pos_theta {
|
||||
get {
|
||||
return tl_row.security_type == SecurityType.Option ?
|
||||
(tl_row.call_put == PutOrCall.Call ? opt_theo.c_theta : opt_theo.p_theta) * tl_row.size : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace mbts {
|
||||
public TradeLogRow(ManualFill m_fill, SecurityImmutable sec) {
|
||||
trade_id = m_fill.RecordId;
|
||||
key = $"ManualFill_{m_fill.RecordId}";
|
||||
security_key = m_fill.SecurityKey;
|
||||
// TODO
|
||||
trader = m_fill.UserId.ToString();
|
||||
security_type = sec.security_type();
|
||||
@@ -37,6 +38,7 @@ namespace mbts {
|
||||
public TradeLogRow(Fill fill, SecurityImmutable sec) {
|
||||
trade_id = Convert.ToInt32(fill.OrderKey);
|
||||
key = $"ElectronicFill_{fill.FillKey}";
|
||||
security_key = fill.SecurityKey;
|
||||
// TODO
|
||||
trader = fill.Account;
|
||||
security_type = sec.security_type();
|
||||
@@ -63,6 +65,7 @@ namespace mbts {
|
||||
|
||||
public int trade_id { get; }
|
||||
public string key { get; }
|
||||
public uint security_key { get; }
|
||||
public SecurityType security_type { get; }
|
||||
public string symbol { get; }
|
||||
public DateTime? expiration { get; }
|
||||
|
||||
@@ -357,6 +357,11 @@ namespace mbts.spark {
|
||||
public static ITag<ExpStrikeKeyedData<TheoPoint>> LiveTheosTag(string symbol) =>
|
||||
_mk_tag<ExpStrikeKeyedData<TheoPoint>, string>(symbol);
|
||||
|
||||
// Aggregated version of the above
|
||||
public static ITag<IReadOnlyDictionary<string, ExpStrikeKeyedData<TheoPoint>>>
|
||||
AllLiveTheosTag { get; } =
|
||||
_mk_tag<IReadOnlyDictionary<string, ExpStrikeKeyedData<TheoPoint>>>();
|
||||
|
||||
public static ITag<IReadOnlyDictionary<SecurityExpiration, FutureTheoStructure>>
|
||||
LiveFutureTheosTag { get; } =
|
||||
_mk_tag<IReadOnlyDictionary<SecurityExpiration, FutureTheoStructure>>();
|
||||
@@ -421,6 +426,8 @@ namespace mbts.spark {
|
||||
}
|
||||
|
||||
public static class PNL {
|
||||
public static ITag<IEnumerable<IPNLUnit>> PNLData { get; } =
|
||||
_mk_tag<IEnumerable<IPNLUnit>>();
|
||||
// Publisher: PNLDataModel
|
||||
public static ITag<string> Activate { get; } =
|
||||
_mk_tag<string>();
|
||||
|
||||
Reference in new issue
Block a user