Exemple #1
0
    def checkOrders(self, order_list):
        for order in order_list:
            order_info = _C(self.exchange.GetOrder, order['order_id'])
            if order_info['Status'] == self.ORDER_STATE_CLOSED:
                Sleep(self.checkTryTime)
                fee = self.getFee()
                account_info = self.getAccountInfo()
                if order['type'] == self.ORDER_TYPE_BUY:
                    price = _N(order['price'] * (1 + self.spreads),
                               self.FLOAT_LENGTH)
                    quantity = _N(order['quantity'] * (1 - fee.get('buy')),
                                  self.FLOAT_LENGTH)
                    order_id = self.transaction(self.exchange.Sell, price,
                                                quantity, self.ORDER_TYPE_SELL)
                    new_order = {
                        'price': price,
                        'quantity': quantity,
                        'order_id': order_id,
                        'type': self.ORDER_TYPE_SELL
                    }

                    Log(
                        '订单号%s 以%s价格买入%s个币,交易成功,现在以%s价格卖出币' %
                        (order['order_id'], order['price'], order['quantity'],
                         price), '#ff0000@')
                else:

                    price = _N(order['price'] * (1 - self.spreads),
                               self.FLOAT_LENGTH)
                    quantity = _N(order['quantity'] * (1 + fee.get('buy')),
                                  self.FLOAT_LENGTH)

                    order_id = self.transaction(self.exchange.Buy, price,
                                                quantity, self.ORDER_TYPE_BUY)
                    new_order = {
                        'price': price,
                        'quantity': quantity,
                        'order_id': order_id,
                        'type': self.ORDER_TYPE_BUY
                    }

                    Log(
                        '订单号%s 以%s价格卖出%s个币,交易成功,现在以%s价格买入币' %
                        (order['order_id'], order['price'], order['quantity'],
                         price), '#ff0000@')

                order_list.append(new_order)
                order_list.remove(order)
                Log(order_list)
                Log('---------------------------')

        return order_list
Exemple #2
0
    def generateOrders(self):
        """初始化订单"""
        if self.basePrice == 0:
            ticker = _C(self.exchange.GetTicker)
            base_price = ticker['Last']
        else:
            base_price = self.basePrice

        Log('入手币价:', base_price)
        if self.entry_type:
            Log('法币入场')
            each_balance = _N(self.balance / self.numberGrids,
                              self.FLOAT_LENGTH)
            for i in range(1, self.numberGrids + 1):
                price = _N(base_price * (1 - self.spreads * i),
                           self.FLOAT_LENGTH)
                buy_quantit = _N(each_balance / price, self.FLOAT_LENGTH)
                order_id = self.transaction(self.exchange.Buy, price,
                                            buy_quantit, self.ORDER_TYPE_BUY)
                buy_info = {
                    'price': price,
                    'quantity': buy_quantit,
                    'type': self.ORDER_TYPE_BUY,
                    'order_id': order_id
                }
                self.orderList.append(buy_info)
                Sleep(self.checkTryTime)

        else:
            Log('持币入场')
            sellQuantity = _N(self.stocks / self.numberGrids,
                              self.FLOAT_LENGTH)
            for i in range(1, self.numberGrids + 1):
                price = _N(base_price * (1 + self.spreads * i),
                           self.FLOAT_LENGTH)

                order_id = self.transaction(self.exchange.Sell, price,
                                            sellQuantity, self.ORDER_TYPE_SELL)
                sell_info = {
                    'price': price,
                    'quantity': sellQuantity,
                    'type': self.ORDER_TYPE_SELL,
                    'order_id': order_id
                }
                self.orderList.append(sell_info)
                Sleep(self.checkTryTime)

        return self.orderList
Exemple #3
0
    def transaction(self, _type, amount, price):
        """
        现货限价交易
        :param amount: 
        :param price: 
        :return: 
        """

        price = _N(price, self.price_precision)
        amount = _N(amount, self.amount_precision)
        data = self.send_order(_type=_type, amount=amount, price=price)
        if not self.check_response_status(data):
            return None
        if data.get('status') == 'ok':
            return data.get("data")
        return None