Example #1
0
    def handleErr(self, tube_id, stage, ex):
        print(ex)

        cache.delete("tube_info")

        msg = "[Error] Tube ID : %s, Stage : %d\n%s" % (tube_id, stage, str(ex))
        telegram_handler.alert_admin(msg)

        self.reg_rdb(tube_id, stage)

        ei = ErrorInfo(desc=str(ex))
        ei.save()

        final_price = 0 #TODO 0이 아니라 마지막 step의 추정 가격을 가져올 것
        if stage > 1:
            el = EventLog.objects.filter(tube_id=tube_id).order_by('-stage').all()[:1].get()
            final_price = el.price
        EventLog(tube_id=tube_id, stage=stage, event=6, price=final_price, time_stamp=datetime.now(), eventinfo_id=ei.id)\
            .save()

        #redis truncate
        for i in range(1, 19):
            cache.delete("%s:%s" % (tube_id, i))

        msg = "튜브 진행이 오류로 인해 중단되었습니다.\nTube ID : %s, 최종 금액 : %s" % (tube_id, final_price)
        telegram_handler.alert_admin(msg)
Example #2
0
    def bithumb_deposit_complete(self, tube_id):
        step_val = cache.get(tube_id + ":" + str(15))
        if step_val is not None:
            if step_val['celery_checked'] is True:
                return False

        thumb_in_alt_code = get_cache_value(tube_id, 13, "trex_out_alt_code")
        thumb_in_alt_amt = get_cache_value(tube_id, 13, "trex_out_alt_amt")

        msg = "빗썸에 %f%s 입금 완료." % (thumb_in_alt_amt, thumb_in_alt_code)
        telegram_handler.alert_admin(msg)

        price = get_cache_value(tube_id, 13, "price")

        tube_step = 15
        executed_time = datetime.now()

        # redis
        set_key = "%s:%s" % (tube_id, tube_step)
        set_value = {
            "tube_id": tube_id,
            "tube_step": tube_step,
            "executed_time": executed_time,
            "thumb_in_alt_code": thumb_in_alt_code,
            "thumb_in_alt_amt": thumb_in_alt_amt,
            "price": price,
            "desc": "Deposit Complete in Bithumb",
            "celery_checked": False
        }
        cache.set(set_key, set_value, timeout=None)

        set_step_now(tube_id, tube_step)
Example #3
0
    def bittrex_withdrawal_complete(self, tube_id):
        step_val = cache.get(tube_id + ":" + str(14))
        if step_val is not None:
            if step_val['celery_checked'] is True:
                return False

        # get step 13
        trex_out_alt_amt = get_cache_value(tube_id, 13, "trex_out_alt_amt")
        trex_out_alt_code = get_cache_value(tube_id, 13, "trex_out_alt_code")
        msg = "Bittrex에서 %f%s 출금 완료." % (trex_out_alt_amt, trex_out_alt_code)
        telegram_handler.alert_admin(msg)

        price = get_cache_value(tube_id, 13, "price")

        tube_step = 14
        executed_time = datetime.now()

        # redis
        set_key = "%s:%s" % (tube_id, tube_step)
        set_value = {
            "tube_id": tube_id,
            "tube_step": tube_step,
            "executed_time": executed_time,
            "price": price,
            "desc": "Withdrawal complete in Bittrex",
            "celery_checked": False
        }
        cache.set(set_key, set_value, timeout=None)
Example #4
0
    def set_init_krw(self, init_price):
        #허매수
        currencypair = CurrencyPair(exchange.symbol.Currency('BTC'),
                                    exchange.symbol.Currency('KRW'))
        ticker = bithumb_manager.get_ticker(currencypair)
        price = int(round(ticker[0]['close'] * Decimal('0.2'), -3))
        amount = Decimal(init_price / price).quantize(Decimal('.0001'),
                                                      rounding=ROUND_DOWN)
        tube_id = bithumb_manager.bid(currencypair, amount, price)

        tube_step = 1
        executed_time = datetime.now()

        #redis
        key = "%s:%s" % (tube_id, tube_step)
        value = {
            "tube_id": tube_id,
            "tube_step": tube_step,
            "executed_time": executed_time,
            "init_price": init_price,
            "price": init_price,
            "desc": "Created Tube",
            "celery_checked": False
        }
        cache.set(key, value, timeout=None)

        msg = "튜브가 생성되었습니다.\nTube ID : %s번, 총액 : %s원" % (tube_id, init_price)
        telegram_handler.alert_admin(msg)

        set_step_now(tube_id, tube_step)
Example #5
0
    def bittrex_bid_complete(self, tube_id):
        step_val = cache.get(tube_id + ":" + str(12))
        if step_val is not None:
            if step_val['celery_checked'] is True:
                return False

        # get step 11
        bid_alt_code = get_cache_value(tube_id, 11, "bid_alt_code")
        bid_alt_amt = get_cache_value(tube_id, 11, "bid_alt_amt")

        step_10_et = get_cache_value(tube_id, 10, "executed_time")

        currencypair = CurrencyPair(bid_alt_code,
                                    exchange.symbol.Currency('BTC'))
        r = bittrex_manager.get_order_history(currencypair)
        match = list(
            filter(
                lambda history: history['date'] > step_10_et and history[
                    'type'] == 'bid', r))
        amount = Decimal(0)
        total = Decimal(0)
        for h in match:
            amount = amount + h["amount"]
            total = total + h["price"] * h["amount"]
        trex_alt_value = (total / amount).quantize(Decimal('0.00000001'),
                                                   rounding=ROUND_DOWN)

        msg = "Bittrex에서 %s 매수 체결.\n가격 : %.8f, 수량 : %.8f, 총액 : %.8fBTC" %\
              (bid_alt_code, trex_alt_value, bid_alt_amt,
               trex_alt_value * bid_alt_amt)
        telegram_handler.alert_admin(msg)

        tube_step = 12
        executed_time = datetime.now()

        thumb_alt_price = get_cache_value(tube_id, 11, "thumb_alt_price")

        # redis
        set_key = "%s:%s" % (tube_id, tube_step)
        set_value = {
            "tube_id": tube_id,
            "tube_step": tube_step,
            "executed_time": executed_time,
            "price": int(bid_alt_amt * thumb_alt_price),
            "desc": "Bid Complete in Bittrex",
            "celery_checked": False
        }
        cache.set(set_key, set_value, timeout=None)

        set_step_now(tube_id, tube_step)
Example #6
0
    def bithumb_sell_complete(self, tube_id):
        step_val = cache.get(tube_id + ":" + str(17))
        if step_val is not None:
            if step_val['celery_checked'] is True:
                return False

        ask_alt_code = get_cache_value(tube_id, 16, "ask_alt_code")
        #thumb_alt_price = get_cache_value(tube_id, 16, "thumb_alt_price")
        ask_alt_amt = get_cache_value(tube_id, 16, "ask_alt_amt")
        step_15_et = get_cache_value(tube_id, 15, "executed_time")

        currencypair = CurrencyPair(ask_alt_code,
                                    exchange.symbol.Currency('KRW'))
        r = bithumb_manager.get_order_history(currencypair)
        match = list(
            filter(
                lambda history: history['date'] > step_15_et and history[
                    'type'] == 'ask', r))
        amount = Decimal(0)
        total = Decimal(0)
        for h in match:
            amount = amount + h["amount"]
            total = total + h["price"] * h["amount"]
        thumb_alt_price = (total / amount).quantize(Decimal('0.00000001'),
                                                    rounding=ROUND_DOWN)

        msg = "빗썸에서 %s 매도 체결.\n가격 : %d원, 수량 : %.4f, 총액 : %d원" % \
              (ask_alt_code, thumb_alt_price, ask_alt_amt,
               int(thumb_alt_price * ask_alt_amt))
        telegram_handler.alert_admin(msg)

        price = thumb_alt_price * ask_alt_amt

        tube_step = 17
        executed_time = datetime.now()

        # redis
        set_key = "%s:%s" % (tube_id, tube_step)
        set_value = {
            "tube_id": tube_id,
            "tube_step": tube_step,
            "executed_time": executed_time,
            "price": price,
            "desc": "Ask Complete in Bithumb",
            "celery_checked": False
        }
        cache.set(set_key, set_value, timeout=None)

        set_step_now(tube_id, tube_step)
Example #7
0
    def cancel(self, tube_id, tube_step):
        if tube_step == 1 or tube_step == 2:
            currencypair = CurrencyPair(exchange.symbol.Currency('BTC'),
                                        exchange.symbol.Currency('KRW'))
            bithumb_manager.cancel(tube_id, currencypair, 'bid')
        elif tube_step == 3:
            bid_order_id = get_cache_value(tube_id, 3, "bid_order_id")
            bid_alt_code = get_cache_value(tube_id, 3, "bid_alt_code")
            currencypair = CurrencyPair(bid_alt_code,
                                        exchange.symbol.Currency('KRW'))
            bithumb_manager.cancel(bid_order_id, currencypair, 'bid')
        elif tube_step == 8:
            ask_order_id = get_cache_value(tube_id, 8, "ask_order_id")
            ask_alt_code = get_cache_value(tube_id, 8, "ask_alt_code")
            currencypair = CurrencyPair(ask_alt_code,
                                        exchange.symbol.Currency('BTC'))
            bittrex_manager.cancel(ask_order_id, currencypair, 'ask')
        elif tube_step == 11:
            bid_order_id = get_cache_value(tube_id, 11, "bid_order_id")
            bid_alt_code = get_cache_value(tube_id, 11, "bid_alt_code")
            currencypair = CurrencyPair(bid_alt_code,
                                        exchange.symbol.Currency('BTC'))
            bittrex_manager.cancel(bid_order_id, currencypair, 'bid')
        elif tube_step == 16:
            ask_order_id = get_cache_value(tube_id, 16, "ask_order_id")
            ask_alt_code = get_cache_value(tube_id, 16, "ask_alt_code")
            currencypair = CurrencyPair(ask_alt_code,
                                        exchange.symbol.Currency('BTC'))
            bithumb_manager.cancel(ask_order_id, currencypair, 'ask')

        tm = tube_manager.TubeManager()
        tm.reg_rdb(tube_id, 10, True)

        for i in range(1, 19):
            cache.delete("%s:%s" % (tube_id, i))
        set_step_now(tube_id, 0)

        cache.delete("tube_info")

        msg = "튜브 진행을 취소하였습니다.\nTube ID : %s, Tube Step : %s" % (tube_id,
                                                                 tube_step)
        telegram_handler.alert_admin(msg)
Example #8
0
    def complete(self, tube_id):
        thumb_alt_price = get_cache_value(tube_id, 16, "thumb_alt_price")
        ask_alt_amt = get_cache_value(tube_id, 16, "ask_alt_amt")

        final_price = int(
            Decimal(thumb_alt_price) * ask_alt_amt *
            (Decimal('1.0') -
             Decimal(str(global_preferences['tube_bithumbTransactionFee'])) *
             Decimal('0.01')))

        # redis
        tube_step = 18
        executed_time = datetime.now()
        set_key = "%s:%s" % (tube_id, tube_step)
        set_value = {
            "tube_id": tube_id,
            "tube_step": tube_step,
            "executed_time": executed_time,
            "final_price": final_price,
            "price": final_price,
            "desc": "Closed Tube",
            "celery_checked": False
        }
        cache.set(set_key, set_value, timeout=None)

        print("before redis->mysql")

        # redis -> mysql
        tm = tube_manager.TubeManager()
        result = tm.reg_rdb(tube_id, 10)
        if result:
            # redis data 삭제
            for i in range(1, 19):
                cache.delete("%s:%s" % (tube_id, i))
            # tube_info['step_now'] 0으로 리셋
            set_step_now(tube_id, 0)

        cache.delete("tube_info")

        msg = "튜브 진행이 완료되었습니다.\nTube ID : %s, 최종 금액 : %s원" % (tube_id,
                                                              final_price)
        telegram_handler.alert_admin(msg)