예제 #1
0
    def _trade_manager(self):
        ''' 
        Here both the sell and buy conditions are managed by the trader.
        -> Sell conditions.
        -> Buy conditions.
        '''
        cMarket = self.currentMarket
        tInfo = self.TradesInformation
        candles = self.candles
        ind = self.normalIndicators

        updateOrder = tInfo['updateOrder']
        order = {'place': False}

        if tInfo['orderType']['S'] != None and not (tInfo['orderStatus']['S']
                                                    in ['Order_Lock']):
            '''##############                     ##############
            ##############      SELL CONDITIONS      ##############
            '''##############                     ##############
            logging.info('Checking for Sell condition. [{0}]'.format(
                self.runtime['market']))

            ##
            conOrder = con.sell_conditions(ind, cMarket, candles)

            if conOrder['place']:
                self.TradesInformation['orderType']['S'] = conOrder['tType']

            else:
                if tInfo['orderType']['S'] == 'signal':
                    if self.botRunType == 'real':
                        self.RESTapi.cancel_open_orders('BUY')
                    self.TradesInformation['orderType']['S'] = 'wait'
                    self.TradesInformation['sellPrice'] = 0
            '''############### SIGNAL SELL ###############'''
            if self.TradesInformation['orderType']['S'] == 'signal':
                price = float('{0:.{1}f}'.format(cMarket['askPrice'],
                                                 pRounding))
                if price < tInfo['sellPrice'] or tInfo['sellPrice'] == 0:
                    order = {'place': True, 'side': 'SELL'}

        elif tInfo['orderType']['B'] and not (tInfo['orderStatus']['B']
                                              in ['Order_Lock']):
            '''##############                    ##############
            ##############      BUY CONDITIONS      ##############
            '''##############                    ##############
            logging.debug('Checking for Buy condition. [{0}]'.format(
                self.runtime['market']))

            ## This will be used to determin what type of condition is to be placed.
            conOrder = con.buy_conditions(ind, cMarket, candles)

            if conOrder['place']:
                self.TradesInformation['orderType']['B'] = conOrder['tType']

            else:
                ## This will reset the orders back to waiting if there are no orders.
                if tInfo['orderType']['B'] == 'signal':
                    if self.botRunType == 'real':
                        self.RESTapi.cancel_open_orders('BUY')
                    self.TradesInformation['orderStatus']['B'] = None
                    self.TradesInformation['orderType']['B'] = 'wait'
                    self.TradesInformation['buyPrice'] = 0
            '''############### SIGNAL BUY ###############'''
            if self.TradesInformation['orderType']['B'] == 'signal':
                price = float('{0:.{1}f}'.format(cMarket['bidPrice'],
                                                 pRounding))
                if price > tInfo['buyPrice']:
                    order = {'place': True, 'side': 'BUY'}

        ## All orders to be placed will be managed via the manager.
        if order['place']:
            if self.botRunType == 'real':
                orderInfo = self.RESTapi.order_placer(self.marketRules,
                                                      self.TradesInformation,
                                                      order['side'],
                                                      'LIMIT',
                                                      price=price)

                logging.info('orderInfo: {0} [{1}]'.format(
                    orderInfo, self.runtime['market']))

                if orderInfo:
                    self._code_manager(orderInfo['code'],
                                       order['side'],
                                       price=price)

            elif self.botRunType == 'test':
                self._code_manager(0, order['side'], price=price)
예제 #2
0
    def _trade_manager(self):
        ''' 
        Here both the sell and buy conditions are managed by the trader.

        -> Manager Sell Conditions.
            Manage the placed sell condition as well as monitor conditions for the sell side.

        -> Manager Buy Conditions.
            Manage the placed buy condition as well as monitor conditions for the buy side.

        -> Place Market Order.
            Place orders on the market with real and assume order placemanet with test.

        '''
        tInfo = self.trade_information

        orderType = None
        updateOrder = tInfo['updateOrder']
        order = {'place': False}

        if tInfo['orderType'][
                'S'] and tInfo['orderStatus']['S'] != 'ORDER_LOCK':
            ## Manager Sell Conditions.

            logging.debug('Checking for Sell condition. [{0}]'.format(
                self.symbol))

            # Check the conditions in place to see if a order can be setup.
            new_order = con.sell_conditions(self.indicators, self.prices,
                                            tInfo, self.candles)

            if new_order['place']:
                orderType = new_order['tType']
                price = float('{0:.{1}f}'.format(new_order['price'],
                                                 pRounding))

                askPrice = self.prices['askPrice']

                if orderType == 'SIGNAL':
                    # Setup a signal sell order.
                    if (price != tInfo['sellPrice']
                            and tInfo['sellPrice'] != askPrice) or updateOrder:
                        order = {'place': True, 'side': 'SELL'}

                else:
                    logging.critical(
                        'The trade type [{0}] has not been configured'.format(
                            new_order['tType']))
            else:
                if orderType == 'SIGNAL':
                    if self.run_type == 'REAL':
                        self.rest_api.cancel_open_orders('SELL', self.symbol)
                    self.trade_information['orderStatus']['S'] = None
                    self.trade_information['orderType']['S'] = 'WAIT'

        elif tInfo['orderType'][
                'B'] and tInfo['orderStatus']['B'] != 'ORDER_LOCK':
            ## Manager Buy Conditions.

            logging.debug('Checking for Buy condition. [{0}]'.format(
                self.symbol))

            # Check the conditions in place to see if a order can be setup.
            new_order = con.buy_conditions(self.indicators, self.prices, tInfo,
                                           self.candles)

            if new_order['place']:
                orderType = new_order['tType']
                price = float('{0:.{1}f}'.format(new_order['price'],
                                                 pRounding))

                bidPrice = self.prices['bidPrice']

                if orderType == 'SIGNAL':
                    # Setup a signal buy order.
                    if (price != tInfo['buyPrice']
                            and tInfo['buyPrice'] != bidPrice) or updateOrder:
                        order = {'place': True, 'side': 'BUY'}

                else:
                    logging.critical(
                        'The trade type [{0}] has not been configured'.format(
                            new_order['tType']))

            else:
                # Cancel a existing signal buy order.
                if tInfo['orderType']['B'] == 'SIGNAL':
                    if self.run_type == 'REAL':
                        self.rest_api.cancel_open_orders('BUY', self.symbol)
                    self.trade_information['orderStatus']['B'] = None
                    self.trade_information['orderType']['B'] = 'WAIT'

        if updateOrder:
            self.trade_information['updateOrder'] = False

        ## Place Market Order.
        if order['place']:
            print('{0} : [{1}]'.format(new_order['description'], self.symbol))

            orderInfo = None

            if self.run_type == 'REAL':
                # Attempt to place an order on the market.
                orderInfo = self.rest_api.order_placer(self.symbol,
                                                       self.rules,
                                                       self.trade_information,
                                                       order['side'],
                                                       'LIMIT',
                                                       price=price)

                print('orderInfo: {0} [{1}]'.format(orderInfo, self.symbol))

                if orderInfo:
                    code = orderInfo['code']
                else:
                    self.runtime['state'] = 'FORCE_STANDBY'
                    return

            elif self.run_type == 'TEST':
                orderInfo = True
                code = 0

            # Check the status of the order code.
            if orderInfo:
                self._code_manager(code,
                                   order['side'],
                                   price=price,
                                   orderType=orderType)
예제 #3
0
    def _trade_manager(self):
        ''' 
        Here both the sell and buy conditions are managed by the trader.
        -> Manage sell conditions.
        -> Manage buy conditions.
        -> Place order on the market.
        '''
        symbol = self.runtime['symbol']
        cMarket = self.currentMarket
        tInfo = self.TradesInformation
        candles = self.candles
        ind = self.normalIndicators

        orderType = None
        updateOrder = tInfo['updateOrder']
        order = {'place': False}

        if tInfo['orderType'][
                'S'] and tInfo['orderStatus']['S'] != 'Order_Lock':
            ## <----------------------------------| SELL CONDITION |-----------------------------------> ##
            logging.info('Checking for Sell condition. [{0}]'.format(symbol))

            conOrder = con.sell_conditions(ind, cMarket, tInfo, candles)

            if conOrder['place']:
                orderType = conOrder['tType']
                price = float('{0:.{1}f}'.format(conOrder['price'], pRounding))

                askPrice = cMarket['askPrice']

                if orderType == 'signal':
                    '''############### SIGNAL SELL ###############'''
                    if (price != tInfo['sellPrice']
                            and tInfo['sellPrice'] != askPrice) or updateOrder:
                        order = {'place': True, 'side': 'SELL'}

                else:
                    logging.critical(
                        'The trade type [{0}] has not been configured'.format(
                            conOrder['tType']))

            else:
                '''############### CANCEL BUY ORDER ###############'''
                if tInfo['orderType']['S'] == 'signal':
                    if self.botRunType == 'real':
                        self.RESTapi.cancel_open_orders('BUY', symbol)
                    self.TradesInformation['orderStatus']['S'] = None
                    self.TradesInformation['orderType']['S'] = 'wait'

        elif tInfo['orderType'][
                'B'] and tInfo['orderStatus']['B'] != 'Order_Lock':
            ## <----------------------------------| BUY CONDITION |-----------------------------------> ##
            logging.info('Checking for Buy condition. [{0}]'.format(symbol))

            conOrder = con.buy_conditions(ind, cMarket, tInfo, candles)

            if conOrder['place']:
                orderType = conOrder['tType']
                price = float('{0:.{1}f}'.format(conOrder['price'], pRounding))

                bidPrice = cMarket['bidPrice']
                #print(price, bidPrice)

                if orderType == 'signal':
                    '''############### SIGNAL BUY ###############'''
                    if (price != tInfo['buyPrice']
                            and tInfo['buyPrice'] != bidPrice) or updateOrder:
                        order = {'place': True, 'side': 'BUY'}

                else:
                    loggin.critical(
                        'The trade type [{0}] has not been configured'.format(
                            conOrder['tType']))

            else:
                '''############### CANCEL BUY ORDER ###############'''
                if tInfo['orderType']['B'] == 'signal':
                    if self.botRunType == 'real':
                        self.RESTapi.cancel_open_orders('BUY', symbol)
                    self.TradesInformation['orderStatus']['B'] = None
                    self.TradesInformation['orderType']['B'] = 'wait'

        if updateOrder:
            self.TradesInformation['updateOrder'] = False

        ## <----------------------------------| ORDER MANAGER |-----------------------------------> ##
        if order['place']:
            print('{0} : [{1}]'.format(conOrder['description'], symbol))

            orderInfo = None

            if self.botRunType == 'real':
                ## Attempt to place an order on the market.
                orderInfo = self.RESTapi.order_placer(symbol,
                                                      self.marketRules,
                                                      self.TradesInformation,
                                                      order['side'],
                                                      'LIMIT',
                                                      price=price)

                logging.info('orderInfo: {0} [{1}]'.format(
                    orderInfo, self.runtime['symbol']))

                if orderInfo:
                    code = orderInfo['code']
                else:
                    return

            elif self.botRunType == 'test':
                orderInfo = True
                code = 0

            ## Check the status of the order code.
            if orderInfo:
                self._code_manager(code,
                                   order['side'],
                                   price=price,
                                   orderType=orderType)