def CreateTargets(self, algorithm, insights):
        """ 
        Create portfolio targets from the specified insights
        Args:
            algorithm: The algorithm instance
            insights: The insights to create portoflio targets from
        Returns: 
            An enumerable of portoflio targets to be sent to the execution model
        """
        targets = []

        for symbol in self.pendingRemoval:
            targets.append(PortfolioTarget.Percent(algorithm, symbol, 0))
        self.pendingRemoval.clear()

        # construct the dataframe of historical return
        price = {}
        for symbol, data in self.symbolDataDict.items():
            price[str(symbol)] = data.PriceSeries()
        df_price = pd.DataFrame(price)[::-1]

        returns = df_price.pct_change().dropna()

        # get view vectors
        P, Q = self.get_views(insights, returns)

        # get the implied excess equilibrium return vector
        equilibrium_return, cov = self.get_equilibrium_return(returns)

        # if view is empty, use equilibrium return as the expected return instead
        if P.size == 0:
            expected_return = equilibrium_return
        else:
            # create the diagonal covariance matrix of error terms from the expressed views
            omega = dot(dot(dot(self.tau, P), cov), transpose(P))

            if np.linalg.det(omega) == 0:
                expected_return = equilibrium_return
            else:
                A = inv(dot(self.tau, cov)) + dot(
                    dot(np.transpose(P), inv(omega)), P)
                B = dot(inv(dot(self.tau, cov)), equilibrium_return) + dot(
                    dot(np.transpose(P), inv(omega)), Q)
                # the new combined expected return vector
                expected_return = dot(inv(A), B)

        # the optimization method processes the data frame
        opt, weights = self.maximum_sharpe_ratio(self.risk_free_rate,
                                                 expected_return, returns)

        # create portfolio targets from the specified insights
        for insight in insights:
            weight = weights[str(insight.Symbol)]
            targets.append(
                PortfolioTarget.Percent(algorithm, insight.Symbol, weight))

        return targets
    def CreateTargets(self, algorithm, insights):
        """ 
        Create portfolio targets from the specified insights
        Args:
            algorithm: The algorithm instance
            insights: The insights to create portoflio targets from
        Returns: 
            An enumerable of portoflio targets to be sent to the execution model
        """
        targets = []

        for symbol in self.pendingRemoval:
            targets.append(PortfolioTarget.Percent(algorithm, symbol, 0))
        self.pendingRemoval.clear()

        symbols = [insight.Symbol for insight in insights]
        if len(symbols) == 0 or all(
            [insight.Magnitude == 0 for insight in insights]):
            return targets

        for insight in insights:
            symbolData = self.symbolDataBySymbol.get(insight.Symbol)
            if insight.Magnitude is None:
                algorithm.SetRunTimeError(
                    ArgumentNullException(
                        'MeanVarianceOptimizationPortfolioConstructionModel does not accept \'None\' as Insight.Magnitude. Please checkout the selected Alpha Model specifications.'
                    ))
            symbolData.Add(algorithm.Time, insight.Magnitude)

        # Create a dictionary keyed by the symbols in the insights with an pandas.Series as value to create a data frame
        returns = {
            str(symbol): data.Return
            for symbol, data in self.symbolDataBySymbol.items()
            if symbol in symbols
        }
        returns = pd.DataFrame(returns)

        # The portfolio optimizer finds the optional weights for the given data
        weights = self.optimizer.Optimize(returns)
        weights = pd.Series(weights, index=returns.columns)

        # Create portfolio targets from the specified insights
        for insight in insights:
            weight = weights[str(insight.Symbol)]
            targets.append(
                PortfolioTarget.Percent(algorithm, insight.Symbol, weight))

        return targets
Esempio n. 3
0
    def CreateTargets(self, algorithm, insights):
        '''Create portfolio targets from the specified insights
        Args:
            algorithm: The algorithm instance
            insights: The insights to create portoflio targets from
        Returns:
            An enumerable of portoflio targets to be sent to the execution model'''
        targets = []

        if self.removedSymbols is not None:
            # zero out securities removes from the universe
            for symbol in self.removedSymbols:
                targets.append(PortfolioTarget(symbol, 0))
                self.removedSymbols = None

        if len(self.securities) == 0:
            return []

        # give equal weighting to each security
        percent = 1.0 / len(self.securities)
        for insight in insights:
            targets.append(
                PortfolioTarget.Percent(algorithm, insight.Symbol,
                                        insight.Direction * percent))

        return targets
Esempio n. 4
0
    def CreateTargets(self, algorithm, insights):
        '''Create portfolio targets from the specified insights
        Args:
            algorithm: The algorithm instance
            insights: The insights to create portoflio targets from
        Returns:
            An enumerable of portoflio targets to be sent to the execution model'''
        self.insightCollection.AddRange(insights)

        targets = []

        if self.removedSymbols is not None:
            # zero out securities removes from the universe
            for symbol in self.removedSymbols:
                targets.append(PortfolioTarget(symbol, 0))
                self.removedSymbols = None

        if len(insights) == 0:
            return targets

        # Get symbols that have emit insights and still in the universe
        symbols = list(set([x.Symbol for x in self.insightCollection if x.CloseTimeUtc > algorithm.UtcTime]))

        if len(symbols) == 0:
            return targets

        # give equal weighting to each security
        percent = 1.0 / len(symbols)
        for symbol in symbols:
            activeInsights = [ x for x in self.insightCollection if x.Symbol == symbol ]
            direction = activeInsights[-1].Direction
            targets.append(PortfolioTarget.Percent(algorithm, symbol, direction * percent))

        return targets
    def CreateTargets(self, algorithm, insights):
        '''Create portfolio targets from the specified insights
        Args:
            algorithm: The algorithm instance
            insights: The insights to create portoflio targets from
        Returns:
            An enumerable of portoflio targets to be sent to the execution model'''
        self.insightCollection.AddRange(insights)

        targets = []

        if self.removedSymbols is not None:
            # zero out securities removes from the universe
            for symbol in self.removedSymbols:
                targets.append(PortfolioTarget(symbol, 0))
                self.removedSymbols = None

        if len(insights) == 0:
            return targets

        # Get last insight that haven't expired of each symbol that is still in the universe
        activeInsights = list()
        # Remove expired insights
        validInsights = [
            i for i in self.insightCollection
            if i.CloseTimeUtc > algorithm.UtcTime
        ]
        # Force one group per symbol
        for symbol, g in groupby(validInsights, lambda x: x.Symbol):
            # For direction, we'll trust the most recent insight
            activeInsights.append(
                sorted(g, key=lambda x: x.GeneratedTimeUtc)[-1])

        if len(activeInsights) == 0:
            return targets

        # give equal weighting to each security
        count = sum(x.Direction != InsightDirection.Flat
                    for x in activeInsights)
        percent = 0 if count == 0 else 1.0 / count

        for insight in activeInsights:
            targets.append(
                PortfolioTarget.Percent(algorithm, insight.Symbol,
                                        insight.Direction * percent))

        return targets
    def CreateTargets(self, algorithm, insights):
        """
        Create portfolio targets from the specified insights
        Args:
            algorithm: The algorithm instance
            insights: The insights to create portfolio targets from
        Returns:
            An enumerable of portfolio targets to be sent to the execution model
        """
        targets = []

        if (algorithm.UtcTime <= self.nextExpiryTime
                and algorithm.UtcTime <= self.rebalancingTime
                and len(insights) == 0 and self.removedSymbols is None):
            return targets

        insights = PortfolioConstructionModel.FilterInvalidInsightMagnitude(
            algorithm, insights)

        self.insightCollection.AddRange(insights)

        # Create flatten target for each security that was removed from the universe
        if self.removedSymbols is not None:
            universeDeselectionTargets = [
                PortfolioTarget(symbol, 0) for symbol in self.removedSymbols
            ]
            targets.extend(universeDeselectionTargets)
            self.removedSymbols = None

        # Get insight that haven't expired of each symbol that is still in the universe
        activeInsights = self.insightCollection.GetActiveInsights(
            algorithm.UtcTime)

        # Get the last generated active insight for each symbol
        lastActiveInsights = []
        for sourceModel, f in groupby(
                sorted(activeInsights, key=lambda ff: ff.SourceModel),
                lambda fff: fff.SourceModel):
            for symbol, g in groupby(sorted(list(f), key=lambda gg: gg.Symbol),
                                     lambda ggg: ggg.Symbol):
                lastActiveInsights.append(
                    sorted(g, key=lambda x: x.GeneratedTimeUtc)[-1])

        # Get view vectors
        P, Q = self.get_views(lastActiveInsights)
        if P is not None:

            returns = dict()

            # Updates the BlackLittermanSymbolData with insights
            # Create a dictionary keyed by the symbols in the insights with an pandas.Series as value to create a data frame
            for insight in lastActiveInsights:
                symbol = insight.Symbol
                symbolData = self.symbolDataBySymbol.get(
                    symbol,
                    self.BlackLittermanSymbolData(insight.Symbol,
                                                  self.lookback, self.period))
                if insight.Magnitude is None:
                    algorithm.SetRunTimeError(
                        ArgumentNullExceptionArgumentNullException(
                            'BlackLittermanOptimizationPortfolioConstructionModel does not accept \'None\' as Insight.Magnitude. Please make sure your Alpha Model is generating Insights with the Magnitude property set.'
                        ))
                symbolData.Add(algorithm.Time, insight.Magnitude)
                returns[symbol] = symbolData.Return

            returns = pd.DataFrame(returns)

            # Calculate prior estimate of the mean and covariance
            Pi, Sigma = self.get_equilibrium_return(returns)

            # Calculate posterior estimate of the mean and covariance
            Pi, Sigma = self.apply_blacklitterman_master_formula(
                Pi, Sigma, P, Q)

            # Create portfolio targets from the specified insights
            weights = self.optimizer.Optimize(returns, Pi, Sigma)
            weights = pd.Series(weights, index=Sigma.columns)

            for symbol, weight in weights.items():
                target = PortfolioTarget.Percent(algorithm, symbol, weight)
                if target is not None:
                    targets.append(target)

        # Get expired insights and create flatten targets for each symbol
        expiredInsights = self.insightCollection.RemoveExpiredInsights(
            algorithm.UtcTime)

        expiredTargets = []
        for symbol, f in groupby(expiredInsights, lambda x: x.Symbol):
            if not self.insightCollection.HasActiveInsights(
                    symbol, algorithm.UtcTime):
                expiredTargets.append(PortfolioTarget(symbol, 0))
                continue

        targets.extend(expiredTargets)

        self.nextExpiryTime = self.insightCollection.GetNextExpiryTime()
        if self.nextExpiryTime is None:
            self.nextExpiryTime = UTCMIN

        self.rebalancingTime = algorithm.UtcTime + self.rebalancingPeriod

        return targets
Esempio n. 7
0
    def CreateTargets(self, algorithm, insights):
        '''Create portfolio targets from the specified insights
        Args:
            algorithm: The algorithm instance
            insights: The insights to create portoflio targets from
        Returns:
            An enumerable of portoflio targets to be sent to the execution model'''

        targets = []

        if (algorithm.UtcTime <= self.nextExpiryTime
                and algorithm.UtcTime <= self.rebalancingTime
                and len(insights) == 0 and self.removedSymbols is None):
            return targets

        self.insightCollection.AddRange(insights)

        # Create flatten target for each security that was removed from the universe
        if self.removedSymbols is not None:
            universeDeselectionTargets = [
                PortfolioTarget(symbol, 0) for symbol in self.removedSymbols
            ]
            targets.extend(universeDeselectionTargets)
            self.removedSymbols = None

        # Get insight that haven't expired of each symbol that is still in the universe
        activeInsights = self.insightCollection.GetActiveInsights(
            algorithm.UtcTime)

        # Get the last generated active insight for each symbol
        lastActiveInsights = []
        for symbol, g in groupby(activeInsights, lambda x: x.Symbol):
            lastActiveInsights.append(
                sorted(g, key=lambda x: x.GeneratedTimeUtc)[-1])

        # give equal weighting to each security
        count = sum(x.Direction != InsightDirection.Flat
                    for x in lastActiveInsights)
        percent = 0 if count == 0 else 1.0 / count

        errorSymbols = {}
        for insight in lastActiveInsights:
            target = PortfolioTarget.Percent(algorithm, insight.Symbol,
                                             insight.Direction * percent)
            if not target is None:
                targets.append(target)
            else:
                errorSymbols[insight.Symbol] = insight.Symbol

        # Get expired insights and create flatten targets for each symbol
        expiredInsights = self.insightCollection.RemoveExpiredInsights(
            algorithm.UtcTime)

        expiredTargets = []
        for symbol, f in groupby(expiredInsights, lambda x: x.Symbol):
            if not self.insightCollection.HasActiveInsights(
                    symbol, algorithm.UtcTime) and not symbol in errorSymbols:
                expiredTargets.append(PortfolioTarget(symbol, 0))
                continue

        targets.extend(expiredTargets)

        self.nextExpiryTime = self.insightCollection.GetNextExpiryTime()
        if self.nextExpiryTime is None:
            self.nextExpiryTime = UTCMIN

        self.rebalancingTime = algorithm.UtcTime + self.rebalancingPeriod

        return targets
Esempio n. 8
0
    def CreateTargets(self, algorithm, insights):
        """ 
        Create portfolio targets from the specified insights
        Args:
            algorithm: The algorithm instance
            insights: The insights to create portoflio targets from
        Returns: 
            An enumerable of portoflio targets to be sent to the execution model
        """
        targets = []

        for symbol in self.pendingRemoval:
            targets.append(PortfolioTarget.Percent(algorithm, symbol, 0))
        self.pendingRemoval.clear()

        symbols = [insight.Symbol for insight in insights]
        if len(symbols) == 0 or all(
            [insight.Magnitude == 0 for insight in insights]):
            return targets

        for insight in insights:
            symbolData = self.symbolDataBySymbol.get(insight.Symbol)
            if insight.Magnitude is None:
                algorithm.SetRunTimeError(
                    ArgumentNullException(
                        'BlackLittermanOptimizationPortfolioConstructionModel does not accept \'None\' as Insight.Magnitude. Please make sure your Alpha Model is generating Insights with the Magnitude property set.'
                    ))
            symbolData.Add(algorithm.Time, insight.Magnitude)

        # Create a dictionary keyed by the symbols in the insights with an pandas.Series as value to create a data frame
        historical_returns = {
            str(symbol): data.Return
            for symbol, data in self.symbolDataBySymbol.items()
            if symbol in symbols
        }
        historical_returns = pd.DataFrame(historical_returns)

        # Get view vectors
        P, Q = self.get_views(insights)

        # Get the implied excess equilibrium return vector
        equilibrium_return, cov = self.get_equilibrium_return(
            historical_returns)

        # If view is empty, use equilibrium return as the expected return instead
        if P.size == 0:
            investors_views_aggregation_returns = equilibrium_return
        else:
            # Create the diagonal covariance matrix of error terms from the expressed views
            omega = dot(dot(dot(self.tau, P), cov), transpose(P))

            if np.linalg.det(omega) == 0:
                investors_views_aggregation_returns = equilibrium_return
            else:
                A = inv(dot(self.tau, cov)) + dot(
                    dot(np.transpose(P), inv(omega)), P)
                B = dot(inv(dot(self.tau, cov)), equilibrium_return) + dot(
                    dot(np.transpose(P), inv(omega)), Q)
                # the new combined expected return vector
                investors_views_aggregation_returns = dot(inv(A), B)

        # The portfolio optimizer finds the optional weights for the given data
        weights = self.optimizer.Optimize(historical_returns,
                                          investors_views_aggregation_returns)
        weights = pd.Series(weights, index=historical_returns.columns)

        # create portfolio targets from the specified insights
        for insight in insights:
            weight = weights[str(insight.Symbol)]
            targets.append(
                PortfolioTarget.Percent(algorithm, insight.Symbol, weight))

        return targets