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 WealthScript6 
{
    public class MyStrategy : UserStrategyBase
    {
        public override void Initialize(BarHistory bars)
        {
			// get the 230818 call for the ATM strike on that day
			DateTime exp = new DateTime(23, 8, 18);
			double strike = Math.Round(bars.Close[bars.DateTimes.IndexOf(exp)] / 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.2); 
			}
	        
	        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;
	}
}

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.)