fix: Don't assume venue is in spread def string

Pass it as an argument to the parse fn. This should be fine for now, but
obviously a singular venue param won't work for cross exchange spreads.
This commit is contained in:
John Colvin authored and Adeel Khan committed 2024-10-03 17:46:50 +05:00
1 parent 6bcce832ac
commit ec32078dd5
5 files changed
+43 -13

No files matched your search

@@ -539,7 +539,7 @@ namespace BCG {
p.contract.expiration.ToShortDateString() + " " + p.contract.strike.ToString() + " " +
(p.contract.isCall ? "C" : "P") + " " + p.position.ToString() + ";";
Type spread_type;
SpreadInterpreter.interpret(sprText, out detail, out summary, out spread_type);
SpreadInterpreter.interpret(sprText, out detail, out summary, out spread_type, spec.venue);
if (spread_type != null)
bts.Log.Gen.info($"{spread_type.ToString()}");
}
@@ -30,7 +30,7 @@ namespace BCG
size = theSize;
}
public static SingleSpreadLeg[] parse(string stringRep)
public static SingleSpreadLeg[] parse(string stringRep, Venue default_venue = Venue.Unset)
{
if(string.IsNullOrEmpty(stringRep))
return new SingleSpreadLeg[0];
@@ -39,7 +39,14 @@ namespace BCG
string[] toks = stringRep.Split(' ');
int i = 0;
string symbol = toks[i++];
Venue venue = Enum.Parse<Venue>(toks[i++], true);
// We recently combined venue with symbol as a key for theos/greeks data.
// Try to parse the venue, but if it isn't there just use default_venue
// Note: this parameter presupposes all legs are at the same venue.
if (Enum.TryParse<Venue>(toks[i], out var venue)) {
i++;
} else {
venue = default_venue;
}
DateTime exp = DateTime.Parse(toks[i++]);
double strike = Double.Parse(toks[i++]);
char callPut = char.Parse(toks[i++]);
@@ -1,28 +1,45 @@
using bts.api;
using System;
using System.Collections.Generic;
using System.Text;
namespace BCG {
public static class SpreadInterpreter {
public static void interpret(string bts_spread_def, out string detail) {
public static void interpret(string bts_spread_def, out string detail, Venue default_venue) {
string summary;
Type spread_type;
interpret(bts_spread_def, out detail, out summary, out spread_type);
interpret(bts_spread_def, out detail, out summary, out spread_type, default_venue);
}
public static void interpret(string blueSpreadDetail, out string detail, out string summary,
out Type spread_type) {
out Type spread_type,
Venue default_venue) {
string symbol;
List<string> symbols;
SingleSpreadLeg[] legs;
string longSpec;
interpret(blueSpreadDetail, out symbol, out symbols, out legs, out detail,
out summary, out longSpec, out spread_type);
out summary, out longSpec, out spread_type, default_venue);
}
/// <summary>
/// Parses spread parameters from a string.
/// </summary>
/// <param name="bts_spread_text">The string to parse</param>
/// <param name="symbol">The primary symbol in the spread</param>
/// <param name="symbols">All symbol(s) involved in the spread</param>
/// <param name="legs">The spread legs</param>
/// <param name="detail">A string that concatenates each spread leg in uniform format.</param>
/// <param name="summary">Shorter form representation of the spread.</param>
/// <param name="longSpec"></param>
/// <param name="spread_type">The implementation type (implements <see cref="ISpreadSpec"/>).</param>
/// <param name="default_venue">The venue common to all spread legs (because <see cref="SingleSpreadLeg"/> must be constructed with a venue).</param>
public static void interpret(string bts_spread_text, out string symbol, out List<string> symbols,
out SingleSpreadLeg[] legs, out string detail, out string summary, out string longSpec,
out Type spread_type) {
out Type spread_type,
Venue default_venue) {
// The venue arg is needed because bts_spread_text doesn't include the exchange
spread_type = null;
if (String.IsNullOrEmpty(bts_spread_text)) {
symbol = String.Empty;
@@ -31,7 +48,7 @@ namespace BCG {
detail = longSpec = "Empty Spread";
summary = String.Empty; ;
} else {
legs = SingleSpreadLeg.parse(bts_spread_text);
legs = SingleSpreadLeg.parse(bts_spread_text, default_venue);
bool is_future_spread = true;
foreach (SingleSpreadLeg l in legs)
is_future_spread &= l.callPut.ToString().ToLower().StartsWith("f");
+6
View File
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BCG;
using BCG.Utilities;
@@ -69,6 +70,11 @@ namespace BCG {
this.contracts = contracts;
}
// TODO: remove this property when we are ready to support cross-exchange spreads.
// Throws an exception if the legs are not all at the same exchange.
public Venue venue =>
contracts.Select(c => c.contract.exchange).Distinct().SingleOrDefault(Venue.Unset);
#region mutation objects
public SpreadSpec multiplyPosition(double byValue) {
@@ -162,14 +162,14 @@ namespace BCG {
/// the second and third are negated values
private static Dictionary<string, Triplet<bool, string, string>> _negated_spreads_detailed_map = new Dictionary<string, Triplet<bool, string, string>>();
/// Returns a null if theo is not valid
private static Triplet<bool, string, string> _get_spread_negation_detail(string bts_spread_def, double theo) {
private static Triplet<bool, string, string> _get_spread_negation_detail(string bts_spread_def, double theo, Venue venue) {
Triplet<bool, string, string> rv = null;
if (_is_theo_valid(theo)) {
lock (_negated_spreads_detailed_map) {
if (!_negated_spreads_detailed_map.TryGetValue(bts_spread_def, out rv)) {
rv = new Triplet<bool, string, string>(theo < 0, String.Empty, String.Empty);
rv.second = rv.first ? SingleSpreadLeg.negate(bts_spread_def) : bts_spread_def;
SpreadInterpreter.interpret(rv.second, out rv.third);
SpreadInterpreter.interpret(rv.second, out rv.third, venue);
_negated_spreads_detailed_map[bts_spread_def] = rv;
}
}
@@ -184,7 +184,7 @@ namespace BCG {
double bid, UInt32 bid_size, double ask, UInt32 ask_size,
out double normalized_theo, out double normalized_delta, out double normalized_gamma, out double normalized_vega, out double normalized_theta,
out double normalized_bid, out UInt32 normalized_bid_size, out double normalized_ask, out UInt32 normalized_ask_size) {
Triplet<bool, string, string> spread_negation_detail = _get_spread_negation_detail(bts_spread_def, theo);
Triplet<bool, string, string> spread_negation_detail = _get_spread_negation_detail(bts_spread_def, theo, Venue.Cboe);
if (spread_negation_detail != null && spread_negation_detail.first) {
/// theos/greeks
normalized_theo = -1.0 * theo; normalized_delta = -1.0 * delta; normalized_gamma = -1.0 * gamma; normalized_vega = -1.0 * vega; normalized_theta = -1.0 * theta;
@@ -206,7 +206,7 @@ namespace BCG {
public static bool normalize_cboe_spread_trade(string bts_spread_def, string spread_detail, double theo, double delta, double trade_price,
out string normalized_bts_desc, out string normalized_spread_detail, out double normalized_theo, out double normalized_delta, out double normalized_trade_price) {
Triplet<bool, string, string> spread_negation_detail = _get_spread_negation_detail(bts_spread_def, theo);
Triplet<bool, string, string> spread_negation_detail = _get_spread_negation_detail(bts_spread_def, theo, Venue.Cboe);
if (spread_negation_detail != null && spread_negation_detail.first) {
/// spread desc
normalized_bts_desc = spread_negation_detail.second;