Hi, I'm running the “BasicTemplateFuturesFrameworkAlgorithm” in Algorithm.Python directory.
The source code is as follows.
def filter(self, filter):
'''Defines the futures chain universe filter'''
return (filter.front_month()
.only_apply_filter_at_market_open())
class ConstantFutureContractAlphaModel(ConstantAlphaModel):
'''Implementation of a constant alpha model that only emits insights for future symbols'''
def __init__(self, _type, direction, period):
super().__init__(_type, direction, period)
def should_emit_insight(self, utc_time, symbol):
# only emit alpha for future symbols and not underlying equity symbols
if symbol.security_type != SecurityType.FUTURE:
return False
return super().should_emit_insight(utc_time, symbol)
class SingleSharePortfolioConstructionModel(PortfolioConstructionModel):
'''Portfolio construction model that sets target quantities to 1 for up insights and -1 for down insights'''
def create_targets(self, algorithm, insights):
targets = []
for insight in insights:
targets.append(PortfolioTarget(insight.symbol, insight.direction))
return targets
I have a question on the ConstantFutureContractAlphaModel. In the should_emit_insight, it uses should_emit_insight in ConstantAlphaModel.
In the should_emit_insight in ConstantAlphaModel listed below
def should_emit_insight(self, utc_time, symbol):
if symbol.is_canonical():
# canonical futures & options are none tradable
return False
generated_time_utc = self.insights_time_by_symbol.get(symbol)
if generated_time_utc is not None:
# we previously emitted a insight for this symbol, check it's period to see
# if we should emit another insight
if utc_time - generated_time_utc < self.period:
return False
# we either haven't emitted a insight for this symbol or the previous
# insight's period has expired, so emit a new insight now for this symbol
self.insights_time_by_symbol[symbol] = utc_time
return True
It checked if the symbol is canonical. (is_canonical).
- What does is_canonical do?
2. are all the futures are canonical?
Mia Alissi
The
is_canonicalmethod checks if a symbol is a canonical symbol. In the context of QuantConnect, a canonical symbol is a symbol that represents a "chain" of values, such as for options and futures. It does not represent a specific contract but rather the entire series of contracts for a particular future or option. You can find more about this in the forum discussion.Not all futures are canonical. A canonical future symbol represents the entire series of contracts for a particular future. When you add a future to your universe, you get a chain of contracts, and the symbol of this chain is a canonical symbol. However, each individual contract within this chain has its own symbol, which is not canonical. You can distinguish between the canonical symbol and the individual contract symbols using the
IsCanonicalmethod. For example, in the Futures Universes documentation, theOnSecuritiesChangedmethod checks if a symbol is canonical before assigning it to the_futurevariable:In this code,
self.futurewill only be assigned a security if its symbol is canonical.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.
Uhahahaha
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!