# Path: insights/algorithms/utils.py
# def common_middlewares(properties, identity):
#     middlewares = []
# 
#     if properties.get('interactive'):
#         middlewares.append(msg.RedisProtocol(identity).check)
#     device = properties.get('mobile')
#     if device:
#         middlewares.append(mobile.AndroidPush(device).notify)
#     if properties.get('save'):
#         middlewares.append(database.RethinkdbFinance(
#             table=identity, db='portfolios', reset=True)
#             .save_portfolio)
#     hipchat_room = properties.get('hipchat')
#     if hipchat_room:
#         middlewares.append(hipchat.Bot(
#             hipchat_room, name=identity).notify)
# 
#     return middlewares


import random
import insights.transforms as transforms
from intuition.api.algorithm import TradingFactory
from insights.algorithms.utils import common_middlewares

        # Makes results reproductible
        random.seed(self.identity)

    def event(self, data):
        signals = {'buy': {}, 'sell': {}}
        # One shot or always buying or regularly
        for sid in data:
            luck = random.random()
            if luck >= self.buy_trigger:
                signals['buy'][sid] = data[sid]
            elif luck <= self.sell_trigger:
                signals['sell'][sid] = data[sid]

        return signals


# TODO Merge with buyandhold
# https://www.quantopian.com/posts/global-minimum-variance-portfolio?c=1
class RegularRebalance(TradingFactory):
    '''
    doc: Reconsidere the portfolio allocation regularly, providing to the
      portfolio strategy a moving window of quotes data.
    parameters:
      rate: Reallocation period [default 10]
      window: quotes window tracked [default 40]
    '''

    def initialize(self, properties):


# Path: insights/algorithms/utils.py
# def common_middlewares(properties, identity):
#     middlewares = []
# 
#     if properties.get('interactive'):
#         middlewares.append(msg.RedisProtocol(identity).check)
#     device = properties.get('mobile')
#     if device:
#         middlewares.append(mobile.AndroidPush(device).notify)
#     if properties.get('save'):
#         middlewares.append(database.RethinkdbFinance(
#             table=identity, db='portfolios', reset=True)
#             .save_portfolio)
#     hipchat_room = properties.get('hipchat')
#     if hipchat_room:
#         middlewares.append(hipchat.Bot(
#             hipchat_room, name=identity).notify)
# 
#     return middlewares


# Path: insights/algorithms/dummy.py
import random
import insights.transforms as transforms
from intuition.api.algorithm import TradingFactory
from insights.algorithms.utils import common_middlewares


        # Makes results reproductible
        random.seed(self.identity)

    def event(self, data):
        signals = {'buy': {}, 'sell': {}}
        # One shot or always buying or regularly
        for sid in data:
            luck = random.random()
            if luck >= self.buy_trigger:
                signals['buy'][sid] = data[sid]
            elif luck <= self.sell_trigger:
                signals['sell'][sid] = data[sid]

        return signals


# TODO Merge with buyandhold
# https://www.quantopian.com/posts/global-minimum-variance-portfolio?c=1
class RegularRebalance(TradingFactory):
    '''
    doc: Reconsidere the portfolio allocation regularly, providing to the
      portfolio strategy a moving window of quotes data.
    parameters:
      rate: Reallocation period [default 10]
      window: quotes window tracked [default 40]
    '''

    def initialize(self, properties):


        for middleware in common_middlewares(properties, self.identity):