Parent: Object
OptionsHelper is a static class with methods to assist with converting and parsing option symbols with different formats.
Returns an option symbol of the specified format from its parts identified in the parameters.
C2Symbol returns the Collective2 option symbol by specifying each parameter exactly or by converting an option symbol of another format (e.g., OptionSynthetic, IB, Tradier, TDA/Schwab) to a C2 option symbol.
Note:
- Collective2 and IQFeed option symbol formats are identical.
- If the format of optionSymbol is not recognized as an option symbol, the same optionSymbol will be returned.
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript123 { public class SyntheticOptionSymbol : UserStrategyBase { public override void Initialize(BarHistory bars) { //identify an ATM call with an expiration at least 5 days in the future string symbol = OptionSynthetic.GetOptionsSymbol(bars, OptionType.Call, bars.LastValue, bars.DateTimes[bars.Count - 1], 5); string c2sym = OptionsHelper.C2Symbol(symbol); //show the symbols DrawHeaderText($"Synthetic symbol is {symbol}", WLColor.Gold, 12, "Price"); DrawHeaderText($"C2 symbol is {c2sym}", WLColor.Gold, 12, "Price"); } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) { } } }
Converts optionSymbol to another option symbol format, corresponding to the OptionSymbolFormat enum. Use the resultType overload to convert symbol from a Call to a Put, or vice-versa.
Remarks
The example uses ConvertSymbol to convert an OptionSynthetic contract to an IB Provider contract to determine if the real contract data is available for backtesting.
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using WealthLab.InteractiveBrokers; namespace WealthScript2 { public class MyStrategy : UserStrategyBase { public override void Initialize(BarHistory bars) { // get the last expired call for the ATM strike on that day int bar = bars.Count - 1; DateTime nextEx = bars.NextOptionExpiryDate(bar); DateTime exp = nextEx; do { exp = bars.NextOptionExpiryDate(--bar); } while (exp == nextEx && bar > 0); DrawHeaderText($"Previous expiration was {exp:yyyy-MM-dd}", WLColor.NeonGreen, 14); int idx = bars.DateTimes.IndexOf(exp); if (idx == -1) { DrawHeaderText($"Expiration for {exp:yyyy-MM-dd} is not loaded in the chart"); return; } double strike = Math.Round(bars.Close[idx] / 5) * 5; string osym = OptionSynthetic.Symbol(bars, OptionType.Call, strike, exp); // first check if the IB real contract data is available in cache string ibsym = OptionsHelper.ConvertSymbol(osym, OptionSymbolFormat.Compact); BarHistory obars = GetHistory(bars, ibsym); if (obars == null) { // data wasn't in cache - we need to go synthetic obars = OptionSynthetic.GetHistory(bars, osym, 0.15); } PlotBarHistory(obars, "opane"); } public override void Execute(BarHistory bars, int idx) { } } }
Returns true if symbol matches one of the known option symbol formats, otherwise false.
Returns true if symbol is properly formatted for the specified OptionSymbolFormat, otherwise false.
Returns true if symbol is properly formatted for OptionSynthetic, Interactive Brokers, e.g., ABC230818C90.5, otherwise false.
Returns true if symbol is properly formatted Tradier, EODHD, or Schwab options, otherwise false. Schwab options use 6 characters for the underlying symbol, filled with spaces. See also IsOptionFormat5.
Example for TTD 105 Call with expiration 2024-08-16:
Tradier: TTD240816C00105000
Schwab: TTD 240816C00105000
Returns true if symbol is properly formatted for C2, IQFeed options, e.g., ABC2318H90.5, otherwise false.
Returns true if symbol is properly formatted for the former TD Ameritrade options, e.g., ABC_081823C90.5, otherwise false.
Returns true if symbol is properly formatted for Schwab options, otherwise false. Schwab uses the exchange symbol format with 6 fixed characters for the underlying symbol, right-filled with whitespaces.
Example for TTD 105 Call with expiration 2024-08-16:
Schwab: TTD 240816C00105000
Parses an option symbol for any format and returns a tuple containing the underlier, OptionType, strike, and expiration date.
Remarks:
- If symbol does not match a known option symbol format, the return values will be (String.Empty, OptionType.Call, 0, DateTime.MinValue).
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; namespace WealthScript123 { public class MyStrategy : UserStrategyBase { public override void Initialize(BarHistory bars) { _lastIdx = bars.Count - 1; string osym = OptionSynthetic.GetOptionsSymbol(bars, OptionType.Call, bars.Close[_lastIdx], bars.DateTimes[_lastIdx], 10); (string, OptionType, double, DateTime) optionParts = OptionsHelper.ParseSymbol(osym); string underlier = optionParts.Item1; OptionType otype = optionParts.Item2; double strike = optionParts.Item3; DateTime expiry = optionParts.Item4; WriteToDebugLog($"The underlier for {osym} is {underlier}"); WriteToDebugLog($"The option type is {otype}"); WriteToDebugLog($"The strike is {strike}"); WriteToDebugLog($"The expiration date is {expiry:yyyy-MM-dd}"); } public override void Execute(BarHistory bars, int idx) { } //declare private variables below int _lastIdx; } }
Returns the estimated underlying price at delta for the specified optionType, implied volatility iv and expiry. You can use the result to identify a contract at or near a specified delta, as demonstrated in the example.
Generally, you should use the overload method with currentDate. When missing, currentDate is assumed to be DateTime.Now for options with future expirations.
Remarks
- delta must be a positive number between 0 and 1. A negative sign is implied when calculating a price for a Put.
- iv is a positive number in percent, e.g., 0.10 is 10% implied volatility.
- IV varies by contract expiration. For accuracy, obtain a true IV for an expiration from an OptionGreek or GreekHistory request.
- If iv is 0 or Double.NaN, 0.2 is used for the calculation.
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using System.Collections.Generic; using WealthLab.Indicators; namespace WealthScript125 { public class StrikeAtDelta: UserStrategyBase { Parameter _deltaParam; bool _weeklies = false; WLColor _clr = WLColor.NeonGreen; TimeSeries _iv; public StrikeAtDelta() { _deltaParam = AddParameter("Target Delta", ParameterType.Double, 0.12, 0, 1, 0.01); } public override void Initialize(BarHistory bars) { double delta = _deltaParam.AsDouble; DateTime expiry = bars.NextOptionExpiryDate(bars.Count - 1); //estimate IV using HV _iv = HV.Series(bars.Close, 21, 252) / 100.0; DrawHeaderText($"Delta: {delta:N4}; expiry: {expiry:yyyy-MM-dd}", _clr, 14); DrawHeaderText($"IV: {_iv.LastValue :N4}", _clr, 14); double price = OptionsHelper.PriceAtDelta(OptionType.Call, delta, bars.LastValue, _iv.LastValue, expiry); DrawHeaderText($"Stock Price at Delta {delta:N4} is {price:N2}", _clr, 14); //use the price to identify the closest strike string osym = OptionSynthetic.GetOptionsSymbol(bars, OptionType.Call, price, bars.EndDate, 0); DrawHeaderText($"The nearest Call at Delta {delta:N4}: {osym}", _clr, 14); } public override void Execute(BarHistory bars, int idx) { } } }
Returns the expiration found in the option symbol as a DateTime.
Remarks
- optionSymbol can use any of the known option symbol formats (IB, Tradier, C2, Schwab, IQFeed, etc.)
Returns the right as an OptionType enum found in the option symbol, OptionType.Call or OptionType.Put.
Remarks
- optionSymbol can use any of the known option symbol formats (IB, Tradier, C2, Schwab, IQFeed, etc.)
Returns the strike found in the option symbol as a double value.
Remarks
- optionSymbol can use any of the known option symbol formats (IB, Tradier, C2, Schwab, IQFeed, etc.)
Returns the symbol part found in the option symbol. Usually the symbol is the same as the underlying symbol but it could contain a suffix, e.g., SPXW for the weekly SPX Index option contract.
Remarks
- optionSymbol can use any of the known option symbol formats (IB, Tradier, C2, Schwab, IQFeed, etc.)
Returns the OptionSymbolFormat enum value corresponding to the integer value. If value is not a valid OptionSymbolFormat, the fallback value is returned.