Hello,
This sample code from documentation doesnt work
class UpcomingSplitsExampleAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self.set_start_date(2020, 1, 1)
self.set_end_date(2024, 10, 1)
self.set_cash(100000)
# Trade on daily basis based on daily upcoming splits signals.
self.universe_settings.resolution = Resolution.DAILY
# Universe consists of equities with upcoming splits events.
self._universe = self.add_universe(EODHDUpcomingSplits, self.selection)
def selection(self, splits: List[EODHDUpcomingSplits]) -> List[Symbol]:
# Split (more shares with lower price) will make the stock more affordable and drive up the demand.
# Hence, include all stocks that will have a split within the next 7 days.
# Note that spliting up the stock means the split factor > 1.
return [x.symbol for x in splits if x.split_factor > 1]
def on_data(self, slice: Slice) -> None:
# Equally invest in each member of the universe to evenly dissipate the capital risk.
total_count = len(self._universe.members)
targets = [PortfolioTarget.percent(self, symbol, 1. / total_count) for symbol in self._universe.members.keys()]
self.set_holdings(targets)
def on_securities_changed(self, changes: SecurityChanges) -> None:
for removed in changes.removed_securities:
# Liquidate the ones exit the universe (already split) to capitalize the microeconomic demand.
self.liquidate(removed.symbol)
Link to the docs
Upcoming Splits - QuantConnect.com
Error I get:
25|
6:59:18
:
Runtime Error: 'Dictionary[Symbol,Security]' object is not callable
at on_data
targets = [PortfolioTarget.percent(self, symbol, 1. / total_count) for symbol in self._universe.members.keys()]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
in main.py: line 26 (Open Stack Trace)
Mia Alissi
Here is the corrected code to resolve the error:
Key Changes & Reasoning:
self._universe.members
directly as a dictionary, not as a callable function.self._universe.members
to get symbols (iterating the dict yields keys).if total_count == 0: return
) to prevent division by zero.This should resolve the error and correctly allocate holdings across the active universe.
Sources: docs writing algorithms datasets eod historical data upcoming splits, docs writing algorithms datasets eod historical data upcoming dividends, docs writing algorithms universes dataless scheduled universes, Earnings Quality Factor, True Beats - QuantConnect.com
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.
Mislav Sagovac
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!