Search Framework:
OptionsHelper
Namespace: WealthLab.Backtest
Parent: Object

OptionsHelper is a static class with methods to assist with converting and parsing option symbols with different formast. The primary method is C2Symbol which converts an option symbol of any format to the Collective2 option symbol format.

OptionSynthetic is useful for devoloping strategies that trade options.

Backtest Related
C2Symbol
public static string C2Symbol(string root, OptionType optionType, double strike, DateTime expiry)
public static string C2Symbol(string optionSymbol)

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.
Example Code
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)
		{ }
	}
}

ConvertSymbol
public static string ConvertSymbol(string optionSymbol, int format)

Converts optionSymbol to another option symbol format, where the format number is as follows:

  • 1: Interactive Brokers provider format, e.g., IBM231215C150
  • 2: Tradier provider format, e.g., IBM231215C00150000
  • 3: IQFeed, C2 provider format, e.g., IBM2315L150
  • 4: TDA format, e.g., IBM_121523C150

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.

Example Code
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, 1);
			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)
        { }
    }
}

IsOption
public static bool IsOption(string symbol)

Returns true if symbol matches one of the known option symbol formats, otherwise false.


IsOptionFormat1
public static bool IsOptionFormat1(string symbol)

Returns true if symbol is properly formatted for OptionSynthetic, Interactive Brokers, e.g., ABC230818C90.5, otherwise false.


IsOptionFormat2
public static bool IsOptionFormat2(string symbol)

Returns true if symbol is properly formatted for Tradier options, e.g., ABC230818C00090500, otherwise false.


IsOptionFormat3
public static bool IsOptionFormat3(string symbol)

Returns true if symbol is properly formatted for C2, IQFeed options, e.g., ABC2318H90.5, otherwise false.


IsOptionFormat4
public static bool IsOptionFormat4(string symbol)

Returns true if symbol is properly formatted for TD Ameritrade options, e.g., ABC_081823C90.5, otherwise false.


ParseSymbol
public static (string, OptionType, double, DateTime) ParseSymbol(string symbol)

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).
Example Code
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;
	}
}

PriceAtDelta
public static double PriceAtDelta(OptionType optionType, double delta, double underlyingPrice, double iv, DateTime expiry)

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. 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 request.
Example Code
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)
        {  }
    }
}

SymbolExpiry
public static DateTime SymbolExpiry(string optionSymbol)

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, TDA, IQFeed, etc.)

SymbolOptionType
public static OptionType SymbolOptionType(string optionSymbol)

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, TDA, IQFeed, etc.)

SymbolStrike
public static double SymbolStrike(string optionSymbol)

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, TDA, IQFeed, etc.)

SymbolUnderlier
public static string SymbolUnderlier(string optionSymbol)

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, TDA, IQFeed, etc.)