Esempio n. 1
0
    def connect(self):
        super().connect()

        try:
            identity = self.service.identity(self._name)

            if identity:
                self._host = identity.get('host')
                self._account_type = "LIVE" if self._host == "api.ig.com" else "demo"
                self._account_id = identity.get('account-id')

                self._connector = IGConnector(
                    self.service,
                    identity.get('username'),
                    identity.get('password'),
                    identity.get('account-id'),
                    identity.get('api-key'),
                    identity.get('host'))

                self._connector.connect()

                self._available_instruments = set()

        except Exception as e:
            logger.error(repr(e))
            error_logger.error(traceback.format_exc())

            self._connector = None
Esempio n. 2
0
    def connect(self):
        super().connect()

        with self._mutex:
            try:
                self._ready = False
                self._connecting = True

                identity = self.service.identity(self._name)
                self._subscriptions = []  # reset previous list

                if identity:
                    self._host = identity.get('host')
                    self._account_type = "LIVE" if self._host == "api.ig.com" else "demo"
                    self._account_id = identity.get('account-id')

                    self._connector = IGConnector(
                        self.service,
                        identity.get('username'),
                        identity.get('password'),
                        identity.get('account-id'),
                        identity.get('api-key'),
                        identity.get('host'))

                    self._connector.connect()

                    # from CST and XST
                    password = "******" % (self._connector.cst, self._connector.xst)
                    # logger.debug(self._connector.cst, self._connector.xst, self._connector.lightstreamer_endpoint, identity.get('account-id'), self._connector.client_id)

                    if self._lightstreamer:
                        # destroy previous connection
                        self._lightstreamer.destroy()

                    self._lightstreamer = LSClient(
                        self._connector.lightstreamer_endpoint,  # "https://push.lightstreamer.com",
                        adapter_set="DEFAULT",
                        user=self._connector.client_id,
                        password=password)

                    self._lightstreamer.connect()

                    # subscribe for account and trades to have a reactive feedback and don't saturate the REST API
                    self.subscribe_account(identity.get('account-id'))
                    self.subscribe_trades(identity.get('account-id'))

                    #
                    # default watched instruments
                    #

                    configured_instruments = self.configured_symbols()

                    # @todo could check with API if configured epic exists and put them into this list
                    instruments = copy.copy(configured_instruments)

                    self._available_instruments = copy.copy(instruments)

                    configured_symbols = self.configured_symbols()
                    matching_symbols = self.matching_symbols_set(configured_symbols, instruments)

                    # cache them
                    self.__configured_symbols = configured_symbols
                    self.__matching_symbols = matching_symbols

                self._ready = True
                self._connecting = False

            except Exception as e:
                error_logger.error(repr(e))
                traceback_logger.error(traceback.format_exc())

                self._connector = None
                self._lightstreamer = None

        if self._connector and self._connector.connected and self._ready:
            self.service.notify(Signal.SIGNAL_WATCHER_CONNECTED, self.name, time.time())
Esempio n. 3
0
    def connect(self):
        super().connect()

        try:
            self.lock()
            self._ready = False

            identity = self.service.identity(self._name)
            self._subscriptions = []  # reset previous list

            if identity:
                self._host = identity.get('host')
                self._account_type = "LIVE" if self._host == "api.ig.com" else "demo"
                self._account_id = identity.get('account-id')

                self._connector = IGConnector(self.service,
                                              identity.get('username'),
                                              identity.get('password'),
                                              identity.get('account-id'),
                                              identity.get('api-key'),
                                              identity.get('host'))

                self._connector.connect()

                # from CST and XST
                password = "******" % (self._connector.cst,
                                              self._connector.xst)
                # logger.debug(self._connector.cst, self._connector.xst, self._connector.lightstreamer_endpoint, identity.get('account-id'), self._connector.client_id)

                if self._lightstreamer:
                    # destroy previous connection
                    self._lightstreamer.destroy()

                self._lightstreamer = LSClient(
                    self._connector.
                    lightstreamer_endpoint,  # "https://push.lightstreamer.com",
                    adapter_set="DEFAULT",
                    user=self._connector.client_id,
                    password=password)

                self._lightstreamer.connect()

                # subscribe for account and trades to have a reactive feedback and don't saturate the REST API
                self.subscribe_account(identity.get('account-id'))
                self.subscribe_trades(identity.get('account-id'))

                #
                # default watched instruments
                #

                all_instruments = []

                if '*' in self.configured_symbols():
                    self._available_instruments = set(all_instruments)
                    instruments = all_instruments
                else:
                    instruments = self.configured_symbols()

                # prefetching and then WS subscribtion because else it block the WS processing...
                # look to see for a better solution

                # susbcribe for symbols
                for symbol in instruments:
                    # fetch from 1m to 1w, we have a problem of the 10k candle limit per weekend, then we only
                    # prefetch for the last of each except for 1m and 5m we assume we have a delay of 5 minutes
                    # from the manual prefetch script execution and assuming the higher timeframe are already up-to-date.
                    self.fetch_and_generate(symbol, Instrument.TF_1M, 5, None)
                    self.fetch_and_generate(symbol, Instrument.TF_3M, 2, None)
                    self.fetch_and_generate(symbol, Instrument.TF_5M, 1, None)
                    self.fetch_and_generate(symbol, Instrument.TF_15M, 1, None)
                    self.fetch_and_generate(symbol, Instrument.TF_1H, 1, None)
                    self.fetch_and_generate(symbol, Instrument.TF_4H, 1, None)
                    self.fetch_and_generate(symbol, Instrument.TF_1D, 1, None)
                    self.fetch_and_generate(symbol, Instrument.TF_1W, 1, None)

                    logger.info("%s prefetch for %s" % (self.name, symbol))

                    self.insert_watched_instrument(symbol, [0])

                    # avoid blocking websocket during sleep
                    # self.unlock()
                    # time.sleep(8.0)  # 1 sec per query + 1 extra second
                    # self.lock()

                # logger.info("Watcher %s wait 10 seconds to limit to a fair API usage" % (self.name,))

                # self.unlock()
                # time.sleep(10.0)
                # self.lock()

                # susbcribe for symbols
                for symbol in instruments:
                    # to know when market close but could be an hourly REST API call, but it consume one subscriber...
                    # @todo so maybe prefers REST call hourly ? but need bid/ofr properly defined at signals on trader.market and strategy.instrument !
                    self.subscribe_market(symbol)

                    # tick data
                    self.subscribe_tick(symbol)

                    # ohlc data (now generated)
                    # for tf in IGWatcher.STORED_TIMEFRAMES:
                    #     self.subscribe_ohlc(symbol, tf)

            self._ready = True

        except Exception as e:
            logger.debug(repr(e))
            error_logger.error(traceback.format_exc())

            self._connector = None
            self._lightstreamer = None
        finally:
            self.unlock()

        if self._connector and self._connector.connected and self._ready:
            self.service.notify(Signal.SIGNAL_WATCHER_CONNECTED, self.name,
                                time.time())
Esempio n. 4
0
    def connect(self):
        super().connect()

        try:
            self.lock()

            identity = self.service.identity(self._name)
            self._subscriptions = []  # reset previous list

            if identity:
                self._host = identity.get('host')
                self._account_type = "LIVE" if self._host == "api.ig.com" else "demo"
                self._account_id = identity.get('account-id')

                self._connector = IGConnector(self.service,
                                              identity.get('username'),
                                              identity.get('password'),
                                              identity.get('account-id'),
                                              identity.get('api-key'),
                                              identity.get('host'))

                self._connector.connect()

                # from CST and XST
                password = "******" % (self._connector.cst,
                                              self._connector.xst)
                # logger.debug(self._connector.cst, self._connector.xst, self._connector.lightstreamer_endpoint, identity.get('account-id'), self._connector.client_id)

                if self._lightstreamer:
                    # destroy previous connection
                    self._lightstreamer.destroy()

                self._lightstreamer = LSClient(
                    self._connector.
                    lightstreamer_endpoint,  # "https://push.lightstreamer.com",
                    adapter_set="DEFAULT",
                    user=self._connector.client_id,
                    password=password)

                self._lightstreamer.connect()

                # subscribe for account and trades to have a reactive feedback and don't saturate the REST API
                self.subscribe_account(identity.get('account-id'))
                self.subscribe_trades(identity.get('account-id'))

                #
                # default watched instruments
                #

                all_instruments = []

                if '*' in self.configured_symbols():
                    self._available_instruments = set(all_instruments)
                    instruments = all_instruments
                else:
                    instruments = self.configured_symbols()

                # susbcribe for symbols
                for symbol in instruments:
                    # to know when market close but could be an hourly REST API call, but it consume one subscriber...
                    # @todo so maybe prefers REST call hourly ? but need bid/ofr properly defined at signals on trader.market and strategy.instrument !
                    self.subscribe_market(symbol)

                    # tick data
                    self.subscribe_tick(symbol)

                    # ohlc data (now generated)
                    # for tf in IGWatcher.STORED_TIMEFRAMES:
                    #     self.subscribe_ohlc(symbol, tf)

                    self.insert_watched_instrument(symbol, [0])

            self.service.notify(Signal.SIGNAL_WATCHER_CONNECTED, self.name,
                                time.time())

        except Exception as e:
            logger.debug(repr(e))
            error_logger.error(traceback.format_exc())

            self._connector = None
            self._lightstreamer = None
        finally:
            self.unlock()