Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Index option request data universe examples #1911

Merged

Conversation

baobach
Copy link
Contributor

@baobach baobach commented Sep 16, 2024

Description

Securities / Asset Classes / Index Option / Requesting Data / Universes

Related Issue

closes #1901

Motivation and Context

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Refactor (non-breaking change which improves implementation)
  • New feature (non-breaking change which adds functionality)
  • Non-functional change (xml comments/documentation/etc)

Checklist:

  • My code follows the code style of this project.
  • I have read the CONTRIBUTING document.
  • My branch follows the naming convention bug-<issue#>-<description> or feature-<issue#>-<description>

Copy link
Member

@AlexCatarino AlexCatarino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tthank you, @baobach.
Sorry for the long wait.
Please run the C# and Pyhon codes on QC Cloud to ensure there is no bugs and the results are the same.

_option.SetFilter(u => u.IncludeWeeklys().Expiration(0, 0).Strikes(-1, 1));

// Filter the option universe by Delta.
_option.SetFilter(optionFilterUniverse => optionFilterUniverse.Delta(0.25m, 0.75m));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding two universes is misleading, since users can think it is a combination of both.
This filter is not adding weeklies, and AddindexOption is not adding SPXW.
Generally, use Resolution.Minute.

if (calls.Count < 1) return;

// Sorted the contracts according to their strike prices.
calls = calls.OrderBy(x => x.Strike).ToList();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can condense this logic to:

var call = chain.Where(contract => contract.Right == OptionRight.Call).OrderBy(x => x.Strike).FirstOrDefault();
if (call != null) Buy(call.Symbol, 1);

The Python version is good, but we should leverage C# features.

# Filter the option universe to only select 0DTE options.
self._option.set_filter(lambda u: u.include_weeklys().expiration(0, 0).strikes(-1, 1))
# Filter the option universe by Delta.
self._option.set_filter(lambda option_filter_universe: option_filter_universe.delta(0.25, 0.75))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue of C# version.

var expiry = chain.Max(x => x.Expiry);
var atmCall = chain.Where(x => x.Expiry == expiry)
.OrderBy(x => Math.Abs(x.Strike - x.UnderlyingLastPrice))
.First();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be good, but ideally we use FirstOrDefault and check for null.

if chain:
# Obtain the ATM call that expires furthest (90 days).
expiry = max(x.expiry for x in chain)
atm_call = min(chain, key=lambda x: abs(x.strike - x.underlying_last_price))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expiry not used. Please run C# and Python examples. They should have the same results.

foreach (var removed in changes.RemovedSecurities)
{
// Liquidate the contracts that exit the universe (due to expiry).
if (Portfolio[removed.Symbol].Invested)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use: removed.Invested or we can remove this if-condition, since Liquidade does nothing if this case if the security is not invested.

@AlexCatarino AlexCatarino merged commit d91696e into QuantConnect:master Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Securities / Asset Classes / Index Option / Requesting Data / Universes
2 participants