Beispiel #1
0
    def _main(self):
        '''
        Main body for the trader loop.
        -> Wait for candle data to be fed to trader.
            Infinite loop to check if candle has been populated with data,
        -> Call the updater.
            Updater is used to re-calculate the indicators as well as carry out timed checks.
        -> Call Order Manager.
            Order Manager is used to check on currently PLACED orders.
        -> Call Trader Manager.
            Trader Manager is used to check the current conditions of the indicators then set orders if any can be PLACED.
        '''
        sock_symbol = self.base_asset + self.quote_asset
        last_wallet_update_time = 0

        if self.configuration['market_type'] == 'SPOT':
            position_types = ['LONG']
        elif self.configuration['market_type'] == 'MARGIN':
            position_types = ['LONG', 'SHORT']

        while self.state_data['runtime_state'] != 'STOP':
            ## Call the update function for the trader.
            socket_buffer_symbol = None
            candles = self.candle_enpoint(sock_symbol)
            books_data = self.depth_endpoint(sock_symbol)
            indicators = TC.technical_indicators(candles)
            self.indicators = indicators
            logging.debug('[BaseTrader] Collected trader data. [{0}]'.format(
                self.print_pair))

            if self.configuration['run_type'] == 'REAL':
                socket_buffer_global = self.socket_api.socketBuffer

                if sock_symbol in self.socket_api.socketBuffer:
                    socket_buffer_symbol = self.socket_api.socketBuffer[
                        sock_symbol]

                ## Pull account balance data via the SPOT user data stream endpoint to update wallet pair information.
                if 'outboundAccountInfo' in socket_buffer_global:
                    if last_wallet_update_time != socket_buffer_global[
                            'outboundAccountInfo']['E']:
                        self.wallet_pair, last_wallet_update_time = self.update_wallets(
                            socket_buffer_global)

            self.market_prices = {
                'lastPrice': candles[0][4],
                'askPrice': books_data['a'][0][0],
                'bidPrice': books_data['b'][0][0]
            }

            if self.state_data['runtime_state'] == 'PAUSE_INSUFBALANCE':
                if self.wallet_pair['BTC'][0] > self.state_data['base_mac']:
                    self.state_data['runtime_state'] = 'RUN'

            if not self.state_data['runtime_state'] in [
                    'STANDBY', 'FORCE_STANDBY', 'FORCE_PAUSE'
            ]:
                ## Call for custom conditions that can be used for more advanced managemenet of the trader.

                for ptype in position_types:
                    self.custom_conditional_data, cp = TC.other_conditions(
                        self.custom_conditional_data, self.long_position
                        if ptype == 'LONG' else self.short_position, ptype,
                        candles, indicators, self.configuration['symbol'],
                        self.configuration['btc_base_pair'])

                    ## logic to force only short or long to be activly traded, both with still monitor passivly tho.
                    if self.configuration[
                            'trade_only_one'] and self.configuration[
                                'market_type'] == 'MARGIN':
                        if (self.long_position['order_type']['B'] != 'WAIT'
                                or self.long_position['order_type']['S'] !=
                                None) and ptype == 'SHORT':
                            continue
                        elif (self.short_position['order_type']['B'] != 'WAIT'
                              or self.short_position['order_type']['S'] != None
                              ) and ptype == 'LONG':
                            continue

                    logging.debug('[BaseTrader] {0} Checking for {1}'.format(
                        self.print_pair, ptype))

                    ## For managing active orders.
                    if socket_buffer_symbol != None or self.configuration[
                            'run_type'] == 'TEST':
                        cp = self._order_status_manager(
                            ptype, cp, socket_buffer_symbol)

                    ## For managing the placement of orders/condition checking.
                    if cp['can_order'] == True:
                        if self.state_data['runtime_state'] == 'RUN' and cp[
                                'market_status'] == 'TRADING':
                            tm_data = self._trade_manager(
                                ptype, cp, indicators, candles)
                            cp = tm_data if tm_data else cp

                    if not cp['market_status']:
                        cp['market_status'] = 'TRADING'

                    if ptype == 'long': self.long_position = cp
                    else: self.short_position = cp

                    if not BACKTESTER_MODE:
                        time.sleep(3)

            current_localtime = time.localtime()
            self.state_data['last_update_time'] = '{0}:{1}:{2}'.format(
                current_localtime[3], current_localtime[4],
                current_localtime[5])

            if self.state_data['runtime_state'] == 'SETUP':
                self.state_data['runtime_state'] = 'RUN'
    def _main(self):
        '''
        Main body for the trader loop.
        -> Wait for candle data to be fed to trader.
            Infinite loop to check if candle has been populated with data,
        -> Call the updater.
            Updater is used to re-calculate the indicators as well as carry out timed checks.
        -> Call Order Manager.
            Order Manager is used to check on currently PLACED orders.
        -> Call Trader Manager.
            Trader Manager is used to check the current conditions of the indicators then set orders if any can be PLACED.
        '''
        sock_symbol = self.base_asset + self.quote_asset
        last_wallet_update_time = 0

        if self.configuration['trading_type'] == 'SPOT':
            position_types = ['LONG']
        elif self.configuration['trading_type'] == 'MARGIN':
            position_types = ['LONG', 'SHORT']

        ## Main trader loop
        while self.state_data['runtime_state'] != 'STOP':
            # Pull required data for the trader.
            candles = self.candle_enpoint(sock_symbol)
            books_data = self.depth_endpoint(sock_symbol)
            indicators = TC.technical_indicators(candles)
            self.indicators = indicators

            logging.debug('[BaseTrader] Collected trader data. [{0}]'.format(
                self.print_pair))

            socket_buffer_symbol = None
            if self.configuration['run_type'] == 'REAL':

                if sock_symbol in self.socket_api.socketBuffer:
                    socket_buffer_symbol = self.socket_api.socketBuffer[
                        sock_symbol]

                # get the global socket buffer and update the wallets for the used markets.
                socket_buffer_global = self.socket_api.socketBuffer
                if 'outboundAccountPosition' in socket_buffer_global:
                    if last_wallet_update_time != socket_buffer_global[
                            'outboundAccountPosition']['E']:
                        self.wallet_pair, last_wallet_update_time = self.update_wallets(
                            socket_buffer_global)

            # Update martket prices with current data
            if books_data != None:
                self.market_prices = {
                    'lastPrice': candles[0][4],
                    'askPrice': books_data['a'][0][0],
                    'bidPrice': books_data['b'][0][0]
                }

            # Check to make sure there is enough crypto to place orders.
            if self.state_data['runtime_state'] == 'PAUSE_INSUFBALANCE':
                if self.wallet_pair[self.quote_asset][0] > self.state_data[
                        'base_currency']:
                    self.state_data['runtime_state'] = 'RUN'

            if not self.state_data['runtime_state'] in [
                    'STANDBY', 'FORCE_STANDBY', 'FORCE_PAUSE'
            ]:
                ## Call for custom conditions that can be used for more advanced managemenet of the trader.

                for market_type in position_types:
                    cp = self.market_activity

                    if cp['order_market_type'] != market_type and cp[
                            'order_market_type'] != None:
                        continue

                    self.custom_conditional_data, cp = TC.other_conditions(
                        self.custom_conditional_data, cp, self.trade_recorder,
                        market_type, candles, indicators,
                        self.configuration['symbol'])

                    ## For managing active orders.
                    if socket_buffer_symbol != None or self.configuration[
                            'run_type'] == 'TEST':
                        cp = self._order_status_manager(
                            market_type, cp, socket_buffer_symbol)

                    ## For managing the placement of orders/condition checking.
                    if cp['can_order'] == True and self.state_data[
                            'runtime_state'] == 'RUN' and cp[
                                'market_status'] == 'TRADING':
                        tm_data = self._trade_manager(market_type, cp,
                                                      indicators, candles)
                        cp = tm_data if tm_data else cp

                    if not cp['market_status']:
                        cp['market_status'] = 'TRADING'

                    self.market_activity = cp

                    time.sleep(.1)

            current_localtime = time.localtime()
            self.state_data['last_update_time'] = '{0}:{1}:{2}'.format(
                current_localtime[3], current_localtime[4],
                current_localtime[5])

            if self.state_data['runtime_state'] == 'SETUP':
                self.state_data['runtime_state'] = 'RUN'
Beispiel #3
0
    def _main(self):
        '''
        Main body for the trader loop.

        -> Wait for candle data to be fed to trader.
            Infinite loop to check if candle has been populated with data,

        -> Call the updater.
            Updater is used to re-calculate the indicators as well as carry out timed checks.

        -> Call Order Manager.
            Order Manager is used to check on currently PLACED orders.

        -> Call Trader Manager.
            Trader Manager is used to check the current conditions of the indicators then set orders if any can be PLACED.
        '''
        sock_symbol = self.base_asset + self.quote_asset

        while True:
            if self.socket_api.get_live_candles()[sock_symbol] and (
                    'a' in self.socket_api.get_live_depths()[sock_symbol]):
                break

        last_wallet_update_time = 0

        if self.market_type == 'SPOT':
            market_types = ['long']
        elif self.market_type == 'MARGIN':
            market_types = ['long', 'short']

        while self.trader_stats['runtime_state'] != 'STOP':
            ## Call the update function for the trader.
            candles = self.socket_api.get_live_candles()[sock_symbol]
            books_data = self.socket_api.get_live_depths()[sock_symbol]
            indicators = TC.technical_indicators(candles)
            self.indicators = indicators
            logging.debug(
                '[BaseTrader] Collected trader data from socket. [{0}]'.format(
                    self.print_pair))

            socket_buffer_global = self.socket_api.socketBuffer

            if sock_symbol in self.socket_api.socketBuffer:
                socket_buffer_symbol = self.socket_api.socketBuffer[
                    sock_symbol]
            else:
                socket_buffer_symbol = None

            if self.run_type == 'REAL':
                ## Pull account balance data via the SPOT user data stream endpoint to update wallet pair information.
                if 'outboundAccountInfo' in socket_buffer_global:
                    if last_wallet_update_time != socket_buffer_global[
                            'outboundAccountInfo']['E']:
                        self.trader_stats[
                            'wallet_pair'], last_wallet_update_time = self.update_wallets(
                                socket_buffer_global)

            self.prices = {
                'lastPrice': candles[0][4],
                'askPrice': books_data['a'][0][0],
                'bidPrice': books_data['b'][0][0]
            }

            if not self.trader_stats['runtime_state'] in [
                    'STANDBY', 'FORCE_STANDBY', 'FORCE_PAUSE'
            ]:
                ## Call for custom conditions that can be used for more advanced managemenet of the trader.
                self.custom_conditional_data, self.trade_information = TC.other_conditions(
                    self.custom_conditional_data, self.trade_information,
                    candles, indicators, self.print_pair, self.btc_base_pair)

                for market_type in market_types:
                    ## logic to force only short or long to be activly traded, both with still monitor passivly tho.
                    if self.trade_only_one:
                        if (self.trade_information['long_order_type']['B'] !=
                                'WAIT' or
                                self.trade_information['long_order_type']['S']
                                != None) and market_type == 'short':
                            continue
                        elif (self.trade_information['short_order_type']['B']
                              != 'WAIT' or
                              self.trade_information['short_order_type']['S']
                              != None) and market_type == 'long':
                            continue

                    logging.debug('[BaseTrader] {0} Checking for {1}'.format(
                        self.print_pair, market_type))

                    ## For managing active orders.
                    if socket_buffer_symbol != None or self.run_type == 'TEST':
                        self._order_status_manager(market_type,
                                                   socket_buffer_symbol)

                    ## For managing the placement of orders/condition checking.
                    if self.trade_information['can_order'][
                            market_type] == True:
                        if self.trader_stats[
                                'runtime_state'] == 'RUN' and self.trade_information[
                                    'market_states'][market_type] == 'TRADING':
                            self._trade_manager(market_type, indicators,
                                                candles)

                        if self.trade_information['market_states'][
                                market_type] == 'COMPLETE_TRADE':
                            self.trade_information['market_states'][
                                market_type] = 'TRADING'

                    if self.trader_stats['runtime_state'] == 'SETUP':
                        self.trade_information['market_states'][
                            market_type] = 'TRADING'

                    time.sleep(0.2)

            current_localtime = time.localtime()
            self.trader_stats['last_update_time'] = '{0}:{1}:{2}'.format(
                current_localtime[3], current_localtime[4],
                current_localtime[5])

            if self.trader_stats['runtime_state'] == 'SETUP':
                self.trader_stats['runtime_state'] = 'RUN'

            if self.run_type == 'REAL':
                pass