I am trying my hand at recreating some python code in C# but am having some trouble with typing, specifically I want to get the furtherest expiration for a chain.
The documentation is rather limited (and has a typo I believe):
// In Initialize
OptionSymol = option.Symbol; // (sp)?
// In OnData
OptionChain chain;
if (slice.OptionChains.TryGetValue(OptionSymbol, out chain))
{
// we find at the money (ATM) put contract with farthest expiration
var atmContract = chain
.OrderByDescending(x => x.Expiry)
.ThenBy(x => Math.Abs(chain.Underlying.Price - x.Strike))
.ThenByDescending(x => x.Right)
.FirstOrDefault();
}
I have:
private Symbol options_symbol; // should be OptionsSymbol?
public override void Initialize()
{
SetStartDate(2018, 9, 1); //Set Start Date
SetEndDate(2018, 9, 30); //Set End Date
SetCash(100000); //Set Strategy Cash
AddEquity("SPY", Resolution.Minute);
var option = AddOption("SPY", Resolution.Minute);
options_symbol = option.Symbol; // returns "OptionsSymbol"?
SetWarmUp(TimeSpan.FromDays(3)); //Warm up 7 days of data.
}
And than in `onData()`
public override void OnData(Slice data)
{
if(IsWarmingUp) return;
OptionChain chain;
// TryGetValue (doesn't work with option_symbol as type Symbol)
if(slice.OptionChains.TryGetValue(option_symbol, out chain)){
var spot_price = chain.Underlying.Price;
DateTime expiry = chain
.OrderByDescending(x => x.Expiry)
.FirstOrDefault().Expiry.Date;
Log("Expiry: " + expiry);
} else {
Debug("Didn't get options chain");
}
}
Jing Wu
You should declare the variable before assigning the value. "OptionSymol" is just a variable name, you can use option_symbol as well but you need to assign the value to this variable in Initialize()
option_symbol = option.Symbol;
In the documentation, the name should be "OptionSymbol" instead of "OptionSymol". We've fixed this typo.
Please see the attached algorithm
JordanBaucke
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!