from AlgorithmImports import *
#https://www.quantconnect.com/forum/discussion/1956/sma-futures-algorithm/p1
class LogicalBrownManatee(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 1, 1)
self.SetEndDate(2022, 1, 1)
self.SetCash(100000)
self.dataByContract = {}
self.fut_sp = self.AddFuture(Futures.Indices.VIX, Resolution.Daily)
self.fut_sp.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(60))
def OnSecuritiesChanged(self,changes):
for security in changes.RemovedSecurities:
if security.Symbol in self.dataByContract:
self.Debug("removed contract: "+str(security.Symbol))
symbol_data = self.dataByContract.pop(security.Symbol, None)
if symbol_data:
symbol_data.dispose()
for security in changes.AddedSecurities:
if security.Symbol not in self.dataByContract:
self.dataByContract[security.Symbol] = SymbolData(self,security.Symbol)
def OnData(self, data):
for chain in data.FutureChains:
for contract in chain.Value:
if contract.Symbol in self.dataByContract:
self.Plot("STD", str(contract.Symbol), self.dataByContract[contract.Symbol].std.Current.Value)
pass
class SymbolData:
def __init__(self,algo,symbol):
self.symbol = symbol
self.algo = algo
self.std = StandardDeviation(50)
self.consolidator = algo.ResolveConsolidator(self.symbol, Resolution.Minute)
self.algo.RegisterIndicator(self.symbol, self.std, self.consolidator)
algo.WarmUpIndicator(self.symbol, self.std, Resolution.Minute)
def dispose(self):
self.algo.SubscriptionManager.RemoveConsolidator(self.symbol, self.consolidator)