Hello,
The “Send Support” button does not do anything, so I posted this as a discussion.
I have a routine that mathematically calculates a strike price. It calculated a strike that didn't exist, but the IF statement is issuing a Runtime Error:
if call_symbol not in slice.Bars:
I've reduced the code to create a sample of the issue:
def Initialize(self):
self.SetStartDate(2022, 4, 26)
self.SetEndDate(2022, 4, 26)
self.SetCash(100000)
self.index_symbol = self.AddIndex("SPX", Resolution.Minute).Symbol
option = self.AddIndexOption(self.index_symbol, "SPXW")
option.SetFilter(lambda universe: universe.IncludeWeeklys().Expiration(0, 0))
self.option_symbol = option.Symbol
def OnData(self, slice: Slice):
if slice.Time.hour == 10 and slice.Time.minute==15:
call_symbol = 'SPXW 220426C04195000'
if call_symbol not in slice.Bars:
self.Log("Call Not Found")
else:
self.Log("Call Found")
The error is:
[ERROR] FATAL UNHANDLED EXCEPTION:SecurityIdentifier.TryParseProperties(): Error parsing SecurityIdentifier: 'SPXW 220426C04195000', Exception: System.ArgumentOutOfRangeException: The specified market wasn't found in the markets lookup. Requested: 454. You can add markets by calling QuantConnect.Market.Add(string, int) (Parameter 'market'), at QuantConnect.SecurityIdentifier.GetMarketIdentifier(String market) in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/LeanEnterprise/Common/SecurityIdentifier.cs:line 951, at QuantConnect.SecurityIdentifier.TryParseProperties(String value, Exception& exception, SecurityIdentifier& identifier) in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/LeanEnterprise/Common/SecurityIdentifier.cs:line 899, Unhandled exception. System.ArgumentException: Object of type 'System.String' cannot be converted to type 'QuantConnect.Symbol'., at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast), at
Mia Alissi
The error you're encountering is due to the fact that you're trying to use a string representation of a symbol to access data in the
slice.Bars
object. In QuantConnect, symbols are not simple strings, but rather instances of theSymbol
class. This class contains additional information about the security, such as its market and security type.In your case,
call_symbol
is a string, and you're trying to use it as a key to access data inslice.Bars
. However,slice.Bars
expects aSymbol
object, not a string. This is why you're getting an error.To fix this, you need to convert your string to a
Symbol
object. Unfortunately, the information extracts provided do not contain the specific code to do this. However, based on the information provided in the extracts, you can create aSymbol
object using theQuantConnect.Symbol.Create
method, as shown in the following C# code snippet:In Python, you would do something similar. However, without the specific code, I can't provide a confident answer. I recommend checking the QuantConnect documentation or asking in the QuantConnect forums for more information on how to convert a string to a
Symbol
object in Python.The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Scott Taylor
Thank you Mia,
I see now that I need to create an options symbol. I tried to create the Symbol.Create method, but I am receiving an error: Runtime Error: Trying to dynamically access a method that does not exist throws a TypeError exception. To prevent the exception, ensure each parameter type matches those required by the 'datetime.datetime'>) method. Please checkout the API documentation.
The code with the issue is:
Here is the code:
Mia Alissi
The error you're encountering is due to the incorrect usage of the
datetime
function in yourSymbol.CreateOption
method. In Python, thedatetime
function is part of thedatetime
module, so you need to import thedatetime
module before you can use thedatetime
function.However, in your code, you're using
datetime
without importing thedatetime
module. This is why you're getting aTypeError
exception.To fix this, you need to import the
datetime
module at the beginning of your code. Here's how you can do it:In this code,
from datetime import datetime
imports thedatetime
function from thedatetime
module, allowing you to use thedatetime
function to createdatetime
objects.The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Scott Taylor
Hello Mia,
That did not seem to work. I am still getting the error:
Runtime Error: Trying to dynamically access a method that does not exist throws a TypeError exception. To prevent the exception, ensure each parameter type matches those required by the 'datetime.datetime'>) method. Please checkout the API documentation.
Here is the code:
Mia Alissi
The error you're encountering is due to the incorrect usage of the
Symbol.CreateOption
method. In your code, you're passingself.option
as the first argument toSymbol.CreateOption
, but this method expects a string representing the underlying symbol.Here's the correct usage of the
Symbol.CreateOption
method:In this code,
self.index_symbol.Value
is a string representing the underlying symbol, which is whatSymbol.CreateOption
expects as its first argument.Please note that the
Symbol.CreateOption
method creates aSymbol
object representing an option contract, but it doesn't guarantee that this option contract actually exists in the market. If the option contract doesn't exist, it won't be included inslice.Bars
, and yourif
condition will always beFalse
.The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Scott Taylor
That didn't work. The data in slice.Bars is SPXW, and index_symbol points to SPX. But, if I put SPXW into CreateOption, I get the error No option type exists for underlying SecurityType: Base at OnData
The issue seems to be that I'm needing the weekly options, and that isn't using SPX, but CreateOption doesn't handle SPXW. Any thoughts?
Mia Alissi
Based on the information provided in the extracts, it seems that you're trying to create a
Symbol
object for a weekly SPX option. However, theSymbol.CreateOption
method doesn't support weekly options directly. Instead, you need to use theOptionChainProvider
to get the list of available option contracts and then filter this list to get the weekly options.Here's an example of how you can do this in Python:
In this code,
Symbol.CreateCanonicalOption
is used to create a canonical symbol for the weekly SPX options. Then,OptionChainProvider.GetOptionContractList
is used to get the list of available option contracts for this canonical symbol. The list is then filtered to get only the weekly options, and the option with the nearest expiry and the desired strike price is selected.Please note that this code assumes that you have already added the SPX index and the SPXW options to your algorithm using the
AddIndex
andAddIndexOption
methods, respectively. Also,self.Time
should be the current time of your algorithm.This code should give you the
Symbol
object for the desired weekly SPX option. However, please note that this option might not exist in the market, and if it doesn't, it won't be included inslice.Bars
.The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Scott Taylor
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!