253 lines
8.8 KiB
C#
253 lines
8.8 KiB
C#
using bts.SilverPine;
|
|
|
|
namespace bts.SilverPine {
|
|
using bts.api;
|
|
using bts.spark;
|
|
|
|
using System;
|
|
|
|
using ZstdSharp.Unsafe;
|
|
|
|
public static class ContractUtils {
|
|
public class FutureTradableContract {
|
|
public string symbol;
|
|
public DateTime expiration;
|
|
public Exchange exchange;
|
|
public string contract_text;
|
|
}
|
|
public class FutureSpreadContract {
|
|
public List<Tuple<double, bts.SilverPine.ContractUtils.FutureTradableContract>> legs;
|
|
public string symbol {
|
|
get {
|
|
return legs == null || legs.Count == 0 ? String.Empty : legs.ElementAt(0).Item2.symbol;
|
|
}
|
|
}
|
|
public string contract_text;
|
|
}
|
|
public class FutureContinousContract {
|
|
public string symbol;
|
|
public Exchange exchange;
|
|
}
|
|
// this is for all future conytracts belonging to a symbol
|
|
public class FutureWildcardContract {
|
|
public string symbol;
|
|
public Exchange exchange;
|
|
}
|
|
public class FutureContract {
|
|
public FutureWildcardContract wildcard_contract = null;
|
|
public FutureTradableContract future_tradable_contract = null;
|
|
public FutureSpreadContract spread_tradable_contract = null;
|
|
public FutureContinousContract continous_constract = null;
|
|
|
|
public FutureContract(string txt) {
|
|
wildcard_contract = parse_wildcard_contract(txt);
|
|
if (wildcard_contract != null)
|
|
return;
|
|
FutureTradableContract fc = parse_future_contract(txt);
|
|
if (fc != null) {
|
|
future_tradable_contract = fc;
|
|
return;
|
|
}
|
|
FutureSpreadContract sc = parse_future_spread_contract(txt);
|
|
if (sc != null) {
|
|
spread_tradable_contract = sc;
|
|
return;
|
|
}
|
|
// this is not totally true, since ParseFutureSpreadContract does not
|
|
// parse ICS spreads
|
|
continous_constract = new FutureContinousContract() { symbol = txt };
|
|
}
|
|
public bool is_continous_contract => continous_constract != null;
|
|
public string symbol() {
|
|
if (wildcard_contract != null)
|
|
return wildcard_contract.symbol;
|
|
if (continous_constract != null) {
|
|
return continous_constract.symbol;
|
|
} else if (future_tradable_contract != null) {
|
|
return future_tradable_contract.symbol;
|
|
} else if (spread_tradable_contract != null) {
|
|
return spread_tradable_contract.symbol;
|
|
}
|
|
Log.assert(false);
|
|
return String.Empty;
|
|
}
|
|
public string contract_text {
|
|
get {
|
|
if (future_tradable_contract != null)
|
|
return future_tradable_contract.contract_text;
|
|
if (spread_tradable_contract != null)
|
|
return spread_tradable_contract.contract_text;
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
#region Parsing
|
|
public static FutureWildcardContract parse_wildcard_contract(string contract_txt, Exchange exchange = Exchange.CME) {
|
|
if (contract_txt.EndsWith("*")) {
|
|
contract_txt = contract_txt.Replace("*", String.Empty);
|
|
return new FutureWildcardContract() {
|
|
symbol = contract_txt,
|
|
exchange = exchange
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
public static FutureTradableContract parse_future_contract(string contract_txt, Exchange exchange = Exchange.CME) {
|
|
// Validate the future string length
|
|
if (!contract_txt.All(c => Char.IsLetterOrDigit(c)))
|
|
return null;
|
|
if (contract_txt.Length < 4) {
|
|
return null;
|
|
}
|
|
|
|
// Extract the symbol and the expiration month/year codes
|
|
string symbol = contract_txt.Substring(0, contract_txt.Length - 2);
|
|
char monthCode = contract_txt[contract_txt.Length - 2];
|
|
char yearCode = contract_txt[contract_txt.Length - 1];
|
|
|
|
// Map month code to month, return null if the month code is invalid
|
|
int month = monthCode switch {
|
|
'F' => 1, // January
|
|
'G' => 2, // February
|
|
'H' => 3, // March
|
|
'J' => 4, // April
|
|
'K' => 5, // May
|
|
'M' => 6, // June
|
|
'N' => 7, // July
|
|
'Q' => 8, // August
|
|
'U' => 9, // September
|
|
'V' => 10, // October
|
|
'X' => 11, // November
|
|
'Z' => 12, // December
|
|
_ => 0 // Return 0 for invalid month code
|
|
};
|
|
|
|
if (month == 0) {
|
|
return null; // Invalid month code
|
|
}
|
|
|
|
// Calculate the year based on the current decade
|
|
int currentYear = DateTime.Now.Year;
|
|
int decadeStart = currentYear / 10 * 10;
|
|
int year = decadeStart + (yearCode - '0');
|
|
|
|
// If the year is in the past, assume the next decade
|
|
if (year < currentYear) {
|
|
year += 10;
|
|
}
|
|
|
|
// Create the FutureContract instance and set its properties
|
|
DateTime expiration = new DateTime(year, month, 1);
|
|
var futureContract = new FutureTradableContract {
|
|
symbol = symbol,
|
|
expiration = expiration,
|
|
exchange = exchange,
|
|
contract_text = contract_txt
|
|
};
|
|
|
|
return futureContract;
|
|
}
|
|
// Method to parse a futures spread, e.g. "CLX4-CLZ4"
|
|
public static FutureSpreadContract parse_future_spread_contract(string contract_txt, Exchange exchange = Exchange.CME) {
|
|
// Split the string by '-' to handle each leg
|
|
var spreadParts = contract_txt.Split('-');
|
|
if (spreadParts.Length < 2) {
|
|
return null; // Not a valid spread format
|
|
}
|
|
|
|
var spreadContract = new FutureSpreadContract { legs = new List<Tuple<double, FutureTradableContract>>() };
|
|
|
|
foreach (var part in spreadParts) {
|
|
// Split each leg by '*', assuming the format is weight*future or just future
|
|
string[] legParts = part.Split('*');
|
|
double weight = 1.0;
|
|
string futurePart;
|
|
|
|
if (legParts.Length == 2) {
|
|
if (!double.TryParse(legParts[0], out weight)) {
|
|
return null; // Invalid weight, parsing failed
|
|
}
|
|
futurePart = legParts[1];
|
|
} else if (legParts.Length == 1) {
|
|
futurePart = legParts[0];
|
|
} else {
|
|
return null; // Invalid format, too many '*' parts
|
|
}
|
|
|
|
// Parse the future contract
|
|
var futureContract = parse_future_contract(futurePart, exchange);
|
|
if (futureContract == null) {
|
|
return null; // Failed to parse the future part
|
|
}
|
|
|
|
// Add the leg to the spread contract
|
|
spreadContract.legs.Add(new Tuple<double, FutureTradableContract>(weight, futureContract));
|
|
}
|
|
return spreadContract;
|
|
}
|
|
|
|
// public static ContractType DetermineFutureOrSpread(string contract) {
|
|
// if (contract.Contains("-")) {
|
|
// return ContractType.FutureSpread;
|
|
// } else {
|
|
// return ContractType.Future;
|
|
// }
|
|
// }
|
|
#endregion
|
|
|
|
public static ContractType get_type(string contract_txt) {
|
|
FutureTradableContract fc = parse_future_contract(contract_txt);
|
|
if (fc != null) {
|
|
return ContractType.Future;
|
|
}
|
|
FutureSpreadContract sc = parse_future_spread_contract(contract_txt);
|
|
if (sc != null) {
|
|
return ContractType.FutureSpread;
|
|
}
|
|
// tehre are some non calendar spreads we cant parse
|
|
//Log.assert(false);
|
|
return bts.SilverPine.ContractUtils.ContractType.FutureSpread;
|
|
}
|
|
|
|
public static ContractCategory get_category(string symbol) {
|
|
var tp = get_symbol_category(symbol);
|
|
return tp.Item1;
|
|
}
|
|
|
|
public static Tuple<ContractCategory, string> get_symbol_category(string symbol) {
|
|
ContractUtils.FutureTradableContract fc = parse_future_contract(symbol);
|
|
if (fc != null) {
|
|
return Tuple.Create(ContractCategory.FutureString, fc.symbol);
|
|
}
|
|
var sc = parse_future_spread_contract(symbol);
|
|
if (sc != null) {
|
|
return Tuple.Create(ContractCategory.FutureString, sc.symbol);
|
|
}
|
|
return Tuple.Create(ContractCategory.Symbol, symbol);
|
|
}
|
|
|
|
|
|
|
|
public enum ContractType {
|
|
Spot,
|
|
Option,
|
|
Future,
|
|
FutureSpread,
|
|
OptionSpread
|
|
}
|
|
public enum ContractCategory {
|
|
Symbol = 0,
|
|
FutureString,
|
|
SpreadString
|
|
}
|
|
public enum CallPut {
|
|
Call,
|
|
Put
|
|
}
|
|
public enum Exchange {
|
|
CME
|
|
}
|
|
|
|
}
|
|
}
|