127 lines
4.6 KiB
C#
127 lines
4.6 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Concurrent;
|
|
using bts.api.core;
|
|
using System.Linq;
|
|
using System.Reactive.Disposables;
|
|
using bts.api;
|
|
using Google.Protobuf.WellKnownTypes;
|
|
using System.Threading;
|
|
|
|
namespace bts.analytics
|
|
{
|
|
class VolTermStructureServer : VolTermStructureServiceBase
|
|
{
|
|
static bts.config.system_symbols.OptionDriverMap _driver =
|
|
mbts.config.Configuration.get<bts.config.system_symbols.OptionDriverMap>();
|
|
|
|
readonly Vol_term_structurePublisher _publisher;
|
|
readonly StatefulPublisher<VolTermStructure, string> _stateful;
|
|
readonly SortedDictionary<DateTime, StrikeVolCurve> _vols = new SortedDictionary<DateTime, StrikeVolCurve>();
|
|
readonly Dictionary<string, VolService> _services = new Dictionary<string, VolService>();
|
|
readonly ServiceContext _context;
|
|
readonly CompositeDisposable _disp = new CompositeDisposable();
|
|
readonly Timer _timer;
|
|
readonly string _driver_symbol;
|
|
VolTermStructure _last_term = new VolTermStructure();
|
|
|
|
bool _dirty;
|
|
public VolTermStructureServer(ServiceContext context, string driver_symbol) : base(context, driver_symbol)
|
|
{
|
|
_context = context;
|
|
_driver_symbol = driver_symbol;
|
|
_publisher = make_vol_term_structure_publisher(driver_symbol);
|
|
_stateful = new StatefulPublisher<VolTermStructure, string>(_publisher.publish, x => x.DriverSymbol);
|
|
|
|
foreach (var d in _driver.option_driver)
|
|
{
|
|
if (_driver_symbol == d.driver_sym)
|
|
{
|
|
var vs = _services[d.option_sym] = context.locate<VolService>(d.option_sym);
|
|
_disp.Add( vs.subscribe_implied_vol_curve(d.option_sym).Subscribe( x =>
|
|
{
|
|
lock(_vols)
|
|
{
|
|
foreach(var val in x.all)
|
|
_vols[val.Expiration.ToDateTime()] = val;
|
|
}
|
|
_dirty = true;
|
|
}));
|
|
}
|
|
}
|
|
_timer = new Timer(on_timer, null, 0, 5000);
|
|
}
|
|
|
|
void on_timer(object _)
|
|
{
|
|
publish();
|
|
}
|
|
|
|
void publish()
|
|
{
|
|
if (!_dirty)
|
|
return;
|
|
VolTermStructure term = new VolTermStructure() { DriverSymbol = _driver_symbol };
|
|
lock(_vols)
|
|
{
|
|
foreach (var exp in _vols)
|
|
{
|
|
VolTermInfo info = new VolTermInfo
|
|
{
|
|
CalT = exp.Value.CalT,
|
|
AtmVol = exp.Value.AtmVol,
|
|
Skewness = 0.0,
|
|
};
|
|
term.Terms.Add(info);
|
|
}
|
|
_stateful.add_or_update(term);
|
|
_last_term = term;
|
|
Log.Gen.info($"Publishing Vol Term Structure {_driver_symbol} ");
|
|
|
|
_stateful.flush();
|
|
}
|
|
_dirty = false;
|
|
}
|
|
|
|
async public override Task<ResultMessage> global_shift(VolShiftQuery request)
|
|
{
|
|
if (request.Symbol != _driver_symbol)
|
|
throw new NotSupportedException($"can only global shift vol for {_driver_symbol}");
|
|
|
|
double[] x = _last_term.Terms.Select( z => z.CalT).ToArray();
|
|
double[] v = _last_term.Terms.Select( z => z.AtmVol).ToArray();
|
|
double firstnotnan = v.FirstOrDefault(xx => !double.IsNaN(xx));
|
|
const double vftime = 1/12.0;
|
|
double v30day = Interpolation.interp_quadratic(vftime, x, v);
|
|
if(double.IsNaN(v30day))
|
|
v30day = Math.Max(firstnotnan, 0.05);
|
|
foreach(var vs in _services)
|
|
{
|
|
List<VolShiftQuery> shifts = new List<VolShiftQuery>();
|
|
lock(_vols)
|
|
{
|
|
foreach(var curve in _vols)
|
|
{
|
|
Timestamp exp = DateTime.SpecifyKind(curve.Key, DateTimeKind.Utc).ToTimestamp();
|
|
double vf = VegaFactor.vega_factor_shortcut(vftime, v30day, curve.Value.CalT, curve.Value.AtmVol);
|
|
double shift = request.Shift * vf;
|
|
shifts.Add(new VolShiftQuery { Symbol = vs.Key, Expiration = exp, Shift = shift});
|
|
}
|
|
}
|
|
//can't await in lock
|
|
foreach(var s in shifts)
|
|
if(!double.IsNaN(s.Shift))
|
|
await vs.Value.shift_vol_async(s);
|
|
}
|
|
_dirty = true;
|
|
return new ResultMessage() { Success = true };
|
|
}
|
|
|
|
public override void Dispose()
|
|
{
|
|
if (_disp != null)
|
|
_disp.Dispose();
|
|
}
|
|
}
|
|
} |