Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions project-templates/csharp/index-options-static/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,29 @@ public override void Initialize()
// Warm-up the option contracts as soon as it is added to the algorithm
Settings.SeedInitialPrices = true;

// The EMA/price cross will determine we trade ATM contracts
// The EMA/price cross will determine we trade ATM contracts
var index = AddIndex("SPX");
EMA(index.Symbol, 60).Updated += TradeAtTheMoneyContract;
var ema = EMA(index.Symbol, 60);
// To use a manual EMA instead, replace the automatic indicator above with:
// var ema = new ExponentialMovingAverage(60);
// WarmUpIndicator<IndicatorDataPoint>(index.Symbol, ema);
// RegisterIndicator(index.Symbol, ema);
ema.Updated += TradeAtTheMoneyContract;

_optionChainSymbol = QuantConnect.Symbol.CreateCanonicalOption(index, "SPXW", Market.USA, "?SPXW");
}

public void TradeAtTheMoneyContract(object sender, IndicatorDataPoint current)
{
// Pace trades every 10 minutes
var lastTrateTime = _lastTicket?.Time ?? DateTime.MinValue;
if ((UtcTime-lastTrateTime).TotalMinutes < 10) return;
var lastTradeTime = _lastTicket?.Time ?? DateTime.MinValue;
if ((UtcTime-lastTradeTime).TotalMinutes < 10) return;

var ema = sender as ExponentialMovingAverage;
if (!ema.IsReady) return;

var spot = Securities[current.Symbol].Price;

if (spot > current && spot > ema[-1])
{
var atmCall = GetAtTheMoneyContract(OptionRight.Call, spot);
Expand Down Expand Up @@ -129,7 +134,7 @@ private Option GetAtTheMoneyContract(OptionRight right, decimal spot)
{
return null;
}

return AddOptionContract(atm);
}
}
11 changes: 8 additions & 3 deletions project-templates/python/index-options-static/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ def initialize(self) -> None:
# Warm-up the option contracts as soon as it is added to the algorithm
self.settings.seed_initial_prices = True

# The EMA/price cross will determine we trade ATM contracts
# The EMA/price cross will determine we trade ATM contracts
index = self.add_index("SPX")
self.ema(index, 60).updated += self._trade_at_the_money_contract
ema = self.ema(index, 60)
# To use a manual EMA instead, replace the automatic indicator above with:
# ema = ExponentialMovingAverage(60)
# self.warm_up_indicator(index, ema)
# self.register_indicator(index, ema)
ema.updated += self._trade_at_the_money_contract

self._option_chain_symbol = Symbol.create_canonical_option(index, "SPXW", Market.USA, "?SPXW")

Expand All @@ -29,7 +34,7 @@ def _trade_at_the_money_contract(self, ema: ExponentialMovingAverage, current: I
if not ema.is_ready: return

spot = self.securities[current.symbol].price

if spot > current.value and spot > ema[-1].value:
atm_call = self._get_at_the_money_contract(OptionRight.CALL, spot)
if atm_call and not atm_call.invested:
Expand Down