def optimize(self, date, to_buy, to_sell, parameters):
        allocations = {}

        # Considers only portfolio positions + future positions - positions
        # about to be sold
        #positions = set([p for p in self.portfolio.positions.keys()
                         #if self.portfolio.positions[p].amount]
                        #).union(to_buy.keys()).difference(to_sell.keys())

        for sid in to_sell:
            allocations[sid] = -parameters.get('perc_sell', 1.0)

        if len(to_buy) == 1:
            allocations.update(
                {to_buy.keys()[0]: parameters.get('max_weigths', 0.2)})
            return allocations, 0, 1

        if parameters in 'historical_returns':
            returns_df = parameters['historical_returns']
            #if not len(returns_df):
                #return allocations, 0, 1
        else:
            returns_df = self.price_transform.handle_data(to_buy)
        if returns_df is None:
            return allocations, 0, 1

        # Remove incomplete data
        for i, column in enumerate(returns_df):
            if returns_df[column].isnull().any():
                self.log.warning('missing data for {}, ignoring'
                                 .format(column))
                returns_df = returns_df.dropna(axis=1)

        returns = pd.rpy.common.convert_to_r_matrix(returns_df)
        frontier = self.r('getEfficientFrontier')(
            returns, points=500, Debug=False, graph=False)

        if not frontier:
            self.log.warning('No optimal frontier found')
            return allocations, 0, 1

        try:
            mp = self.r('marketPortfolio')(
                frontier, 0.02, Debug=False, graph=False)
        except:
            self.log.error('error running R optimizer')
            return allocations, 0, 1

        #FIXME Some key errors survive so far
        for sid in returns_df:
            #NOTE R change a bit names
            try:
                allocations[sid] = round(
                    mp.rx(re.sub("[-,!\ ]", ".", sid))[0][0], 2)
            except:
                allocations[sid] = 0.00

        er = round(mp.rx('er')[0][0], 2)
        eStd = round(mp.rx('eStd')[0][0], 2)
        self.log.info(
            'Allocation: {} With expected return: {} and expected risk: {}'
            .format(allocations, er, eStd))

        return allocations, er, eStd
dataset/ETHPy150Open intuition-io/insights/insights/managers/_review/optimalfrontier.py/OptimalFrontier.optimize/swapped operands of `in`