Exemplo n.º 1
0
    def testEmitOrder(self):
        handlersData = []

        def handler3():
            handlersData.append(3)

        def handler1():
            handlersData.append(1)

        def handler2():
            handlersData.append(2)

        event = observer.Event()
        event.subscribe(handler1)
        event.subscribe(handler2)
        event.subscribe(handler3)
        event.emit()
        self.assertTrue(handlersData == [1, 2, 3])

        handlersData = []
        event = observer.Event()
        event.subscribe(handler3)
        event.subscribe(handler2)
        event.subscribe(handler1)
        event.emit()
        self.assertTrue(handlersData == [3, 2, 1])
Exemplo n.º 2
0
    def __init__(self, maxLen=None):
        super().__init__()
        maxLen = get_checked_max_len(maxLen)

        self.__newValueEvent = observer.Event()
        self.__values = collections.ListDeque(maxLen)
        self.__dateTimes = collections.ListDeque(maxLen)
Exemplo n.º 3
0
    def testReentrancy(self):
        handlersData = []
        event = observer.Event()

        def handler2():
            handlersData.append(2)

        def handler1():
            handlersData.append(1)
            event.subscribe(handler2)
            event.subscribe(handler1)

        event.subscribe(handler1)
        event.emit()

        self.assertTrue(handlersData == [1])
        event.emit()

        self.assertTrue(handlersData == [1, 1, 2])
        event.unsubscribe(handler1)
        event.emit()

        self.assertTrue(handlersData == [1, 1, 2, 2])
        event.unsubscribe(handler2)
        event.emit()

        self.assertTrue(handlersData == [1, 1, 2, 2])
Exemplo n.º 4
0
    def __init__(self, maxLen):
        super().__init__()

        maxLen = dataseries.get_checked_max_len(maxLen)

        self.__ds = {}
        self.__event = observer.Event()
        self.__maxLen = maxLen
Exemplo n.º 5
0
    def testDuplicateHandlers(self):
        def handler1():
            handlersData.append(1)

        handlersData = []
        event = observer.Event()
        event.subscribe(handler1)
        event.subscribe(handler1)
        event.emit()
        self.assertTrue(handlersData == [1])
Exemplo n.º 6
0
    def __init__(self, maxLen=None):
        super().__init__(bar.Frequency.TRADE, maxLen)

        self.registerInstrument(common.btc_symbol)
        self.__barDicts = []
        self.__prevTradeDateTime = None
        self.__thread = None
        self.__initializationOk = None
        self.__enableReconnection = True
        self.__stopped = False
        self.__orderBookUpdateEvent = observer.Event()
Exemplo n.º 7
0
    def __init__(self,
                 identifiers,
                 apiCallDelay=5,
                 maxLen=dataseries.DEFAULT_MAX_LEN):
        logger.info('Livefeed created')
        barfeed.BaseBarFeed.__init__(self, bar.Frequency.TRADE, maxLen)
        if not isinstance(identifiers, list):
            raise Exception("identifiers must be a list")

        self.__queue = queue.Queue()
        self.__orderBookUpdateEvent = observer.Event()
        self.__thread = TradesAPIThread(
            self.__queue, identifiers,
            datetime.timedelta(seconds=apiCallDelay))
        self.__bars = []
        for instrument in identifiers:
            self.registerInstrument(instrument)
Exemplo n.º 8
0
    def __init__(self, barFeed, broker):
        self.__barFeed = barFeed
        self.__broker = broker
        self.__activePositions = set()
        self.__orderToPosition = {}
        self.__barsProcessedEvent = observer.Event()
        self.__analyzers = []
        self.__namedAnalyzers = {}
        self.__resampledBarFeeds = []
        self.__dispatcher = dispatcher.Dispatcher()
        self.__broker.getOrderUpdatedEvent().subscribe(self.__onOrderEvent)
        self.__barFeed.getNewValuesEvent().subscribe(self.__onBars)

        self.__dispatcher.getStartEvent().subscribe(self.onStart)
        self.__dispatcher.getIdleEvent().subscribe(self.__onIdle)

        # It is important to dispatch broker events before feed events, specially if we're backtesting.
        self.__dispatcher.addSubject(self.__broker)
        self.__dispatcher.addSubject(self.__barFeed)

        # Initialize logging.
        self.__logger = logger.getLogger(BaseStrategy.LOGGER_NAME)
Exemplo n.º 9
0
 def __init__(self, datetimes, priority=None):
     super(RealtimeFeed, self).__init__()
     self.__datetimes = datetimes
     self.__event = observer.Event()
     self.__priority = priority
Exemplo n.º 10
0
 def __init__(self):
     super(ReturnsAnalyzerBase, self).__init__()
     self.__event = observer.Event()
     self.__portfolioReturns = None
Exemplo n.º 11
0
 def __init__(self):
     super(Broker, self).__init__()
     self.__orderEvent = observer.Event()
Exemplo n.º 12
0
 def __init__(self):
     self.__subjects = []
     self.__stop = False
     self.__currDateTime = None
     self.__startEvent = observer.Event()
     self.__idleEvent = observer.Event()