Esempio n. 1
0
    def run(self):
        if not self.initialize():
            return
        # 买入
        # 涨 1% 卖出盈利, 跌0.5% 卖出止损
        while True:
            if not self._order_status and self._usdt_qty > 10:  # 买入
                log.info("query trading pair")
                now_trading = self._query_trading_pair()
                log.info("ready to buy")
                if not self._buy(
                        float("%.1f" %
                              (self._usdt_qty / float(now_trading["seller"]))),
                        float("%.5f" %
                              (float(now_trading["seller"]) * 0.9999))):
                    log.info("buy error")
                    return
                self._order_start_time = time.time()
                flag = False
                while time.time() - self._order_start_time < 10:
                    query_order = self._query_order()
                    log.info(query_order)
                    if not query_order == "NEW":
                        flag = True
                        break

                if not flag:
                    self._cancel()
                    self._order_status = ""
                    log.info("buy doge failed! try to rebuy")
                    continue
                status, self._currency_qty = self._update_currency(
                    self._currency)
                status, self._usdt_qty = self._update_currency("USDT")
                self._hold_price = float(now_trading["seller"])

            elif self._order_status == "BUY":
                now_trading = self._query_trading_pair()
                log.info("i have usdt %s doge %s" %
                         (self._usdt_qty, self._currency_qty))
                log.info("now buyer:%s profit:%s loss:%s" %
                         (self._hold_price, 1.01 * self._hold_price,
                          0.995 * self._hold_price))
                if float(now_trading["buyer"]) > 1.01 * self._hold_price:
                    if not self._sell(
                            float("%.1f" % self._currency_qty),
                            float("%.5f" %
                                  (float(now_trading["buyer"] * 1.0002)))):
                        log.error("sell doge error")
                        return
                elif float(now_trading["buyer"]) < 0.995 * self._hold_price:
                    if not self._sell(
                            float("%.1f" % self._currency_qty),
                            float("%.5f" %
                                  (float(now_trading["buyer"] * 1.0002)))):
                        log.error("sell doge error")
                        return
            elif self._order_status == "SELL":
                if not self._query_order() == "NEW":
                    self._order_status = ""
Esempio n. 2
0
 def get_sys_status(self) -> bool:
     """get binance system status
     
     @return bool: True: normal, False: system maintenance"""
     "https://api.binance.com/sapi/v1/system/status"
     url = "%s/sapi/v1/system/status" % self.binance_base_url
     res = eval(requests.get(url, proxies=PROXIES).text)
     if not isinstance(res, dict):
         log.error("error responce:%s" % res)
         return False
     return True if not res.get("status", 1) else False
Esempio n. 3
0
    def initialize(self):
        if not BINANCE_INSTANCE.get_sys_status():
            log.error("remote binance system is maintenance!")
            return False
        log.info("binance system is normal")

        status, self._usdt_qty = self._update_currency("USDT")
        if not status:
            return
        log.info("the account has %f usdt free!" % self._usdt_qty)

        status, self._currency_qty = self._update_currency(self._currency)
        if not status:
            return
        log.info("the account has %f %s free!" % (self._currency_qty, self._currency))

        return True
Esempio n. 4
0
 def cancel_order(self, symbol, order_id):
     url = "%s/api/v3/order" % self.binance_base_url
     params = {
         "timestamp": str(int(time.time() * 1000)),
         "symbol": symbol,
         "orderId": order_id
     }
     params = self._update_headers_with_signature(params)
     headers = {"X-MBX-APIKEY": self.api_key}
     res = requests.delete(url,
                           headers=headers,
                           params=params,
                           proxies=PROXIES).text
     res = eval(res)
     if res["status"] == "CANCELED":
         return True
     log.error("cancled failed!!")
     return False
Esempio n. 5
0
    def get_recent_trades(self, symbol: str = None, limit: int = 1) -> List:
        """get_recent_trades

        @param symbol: trading pair
        @param limit: Trading volume 1 < limit < 1000

        @return list(if empty is not correct)
        """
        url = "%s/api/v3/trades" % self.binance_base_url

        if not symbol or limit > 1000 or limit < 1:
            err_info = \
                "symbol or limit is illegal." \
                "symbol must not None, 1 < limit < 1000!"
            log.error(err_info)
            return []

        params = {"symbol": symbol, "limit": limit}
        res = requests.get(url, params=params, proxies=PROXIES).text
        res = res.replace("false", "False").replace("true", "True")
        res = eval(res)
        return res
Esempio n. 6
0
 def _update_currency(self, currency):
     currency_data = BINANCE_INSTANCE.get_user_data(currency)
     if not currency_data:
         log.error("can not fetch account %s" % currency)
         return False, 0
     return True, float(currency_data["free"])