Exemple #1
0
class KucoinApi(PyexAPI):
    """kucoin API interface.
    """

    logger = logging.getLogger()

    def __init__(self,
                 api_server: str,
                 api_key: str,
                 secret_key: str,
                 timeout: float,
                 requests_params=None):
        assert (isinstance(api_server, str))
        assert (isinstance(api_key, str))
        assert (isinstance(secret_key, str))
        assert (isinstance(timeout, float))

        self.api_server = api_server
        self.api_key = api_key
        self.secret_key = secret_key
        self.timeout = timeout
        self.client = Client(api_key, secret_key)

    def get_markets(self):
        return self.client.get_trading_markets()

    def ticker(self, pair: str):
        assert (isinstance(pair, str))
        return self.client.get_tick(pair)

    def get_balances(self):
        return self.client.get_all_balances()

    def get_fiat_balance(self, fiat: str):
        assert (isinstance(fiat, str))
        return self.client.get_total_balance(fiat)

    def get_balance(self, coin: str):
        assert (isinstance(coin, str))
        return self.client.get_coin_balance(coin)

    def get_coin_info(self, coin: str):
        assert (isinstance(coin, str))
        return self.client.get_coin_info(coin)

    def get_user_info(self):
        return self.client.get_user()

    def order_book(self, pair: str, limit=None):
        assert (isinstance(pair, str))
        return self.client.get_order_book(pair, limit)

    def get_orders(self, pair: str) -> List[Order]:
        assert (isinstance(pair, str))

        orders = self.client.get_active_orders(pair)

        sell_orders = list(
            map(lambda item: Order.from_list(item, pair, True),
                orders[self.client.SIDE_SELL]))
        buy_orders = list(
            map(lambda item: Order.from_list(item, pair, False),
                orders[self.client.SIDE_BUY]))

        return sell_orders + buy_orders

    def place_order(self, pair: str, is_sell: bool, price: Wad,
                    amount: Wad) -> str:
        assert (isinstance(pair, str))
        assert (isinstance(is_sell, bool))
        assert (isinstance(price, Wad))
        assert (isinstance(amount, Wad))

        side = self.client.SIDE_SELL if is_sell else self.client.SIDE_BUY

        self.logger.info(f"Placing order ({side}, amount {amount} of {pair},"
                         f" price {price})...")

        result = self.client.create_order(pair, side, price, amount)
        order_id = result['orderOid']

        self.logger.info(f"Placed order as #{order_id}")
        return order_id

    def cancel_order(self, order_id: str, is_sell: bool, pair: str):
        assert (isinstance(order_id, str))
        assert (isinstance(is_sell, bool))
        assert (isinstance(pair, str))

        side = self.client.SIDE_SELL if is_sell else self.client.SIDE_BUY

        self.logger.info(f"Cancelling order #{order_id} of type {side}...")

        try:
            self.client.cancel_order(order_id, side, pair)
            self.logger.info(f"Canceled order #{order_id}...")
            return True
        except Exception as e:
            self.logger.error(f"Failed to cancel order #{order_id}... {e}")
            return False

    def get_trades(self, pair: str, page_number: int = 1) -> List[Trade]:
        assert (isinstance(pair, str))
        assert (isinstance(page_number, int))

        page_number = page_number - 1
        limit = 100

        result = self.client.get_symbol_dealt_orders(pair, page_number, limit)

        return list(
            map(lambda item: Trade.from_dict(pair, item), result['datas']))

    def get_all_trades(self, pair: str, page_number: int = 1) -> List[Trade]:
        assert (isinstance(pair, str))
        assert (page_number == 1)

        result = self.client.get_recent_orders(pair, 50)

        return list(map(lambda item: Trade.from_list(pair, item), result))
Exemple #2
0
class KucoinWrapper(BaseExchangeWrapper):
    INTERVAL_MAP = {
        CandleTicks.one_minute : Client.RESOLUTION_1MINUTE,
        CandleTicks.five_minutes : Client.RESOLUTION_5MINUTES,
        CandleTicks.thirty_minutes : Client.RESOLUTION_30MINUTES,
        CandleTicks.one_hour : Client.RESOLUTION_1HOUR,
        CandleTicks.one_day : Client.RESOLUTION_1DAY,
    }
    TIME_SINCE_MAP = {
        CandleTicks.one_minute : "1 hour ago",
        CandleTicks.five_minutes : "5 hours ago",
        CandleTicks.thirty_minutes : "26 hours ago",
        CandleTicks.one_hour : "52 hours ago",
        CandleTicks.one_day : "60 days ago",
    }
    ORDER_TYPE_MAPPINGS = {
        'BUY' : OrderType.limit_buy,
        'SELL' : OrderType.limit_sell,
    }

    def __init__(self, api_key, api_secret):
        BaseExchangeWrapper.__init__(self)
        self._handle = Client(api_key, api_secret)
        self._filters = {}
        self._load_markets()

    def _perform_request(self, request_lambda):
        try:
            return request_lambda()
        except KucoinAPIException as ex:
            raise ExchangeAPIException('Failed to perform request: {0}'.format(ex.message))

    def _load_currencies(self):
        result = self._perform_request(lambda: self._handle.get_coin_list())
        for coin in result:
            self.add_currency(
                Currency(
                    coin['coin'],
                    coin['name'],
                    coin['confirmationCount'],
                    coin['withdrawFeeRate'] # check if this is right
                )
            )

    def _load_markets(self):
        self._load_currencies()
        result = self._perform_request(lambda: self._handle.get_trading_symbols())
        for market in result:
            self.add_market(market['coinTypePair'], market['coinType'])

    def _make_symbol(self, base_currency_code, market_currency_code):
        return '{0}-{1}'.format(market_currency_code, base_currency_code)

    def _process_paged_request(self, make_request, callback):
        page = 1
        limit = 50
        data = self._perform_request(lambda: make_request(limit=limit, page=page))
        while len(data['datas']) > 0:
            for entry in data['datas']:
                callback(entry)
            page += 1
            data = make_request(limit=limit, page=page)

    def get_market_state(self, base_currency_code, market_currency_code):
        symbol = self._make_symbol(base_currency_code, market_currency_code)
        data = self._perform_request(lambda: self._handle.get_tick(symbol))
        return MarketState(data['sell'], data['buy'], data['lastDealPrice'])

    def get_orderbook(self, base_currency_code, market_currency_code):
        symbol = self._make_symbol(base_currency_code, market_currency_code)
        data = self._perform_request(lambda: self._handle.get_order_book(symbol))
        buy_orderbook = Orderbook()
        sell_orderbook = Orderbook()
        for item in data['BUY']:
            buy_orderbook.add_order(Order(item[0], item[1]))
        for item in data['SELL']:
            sell_orderbook.add_order(Order(item[0], item[1]))
        return (buy_orderbook, sell_orderbook)

    def get_candles(self, base_currency_code, market_currency_code, interval, limit):
        symbol = self._make_symbol(base_currency_code, market_currency_code)
        since = KucoinWrapper.TIME_SINCE_MAP[interval]
        kucoin_interval = KucoinWrapper.INTERVAL_MAP[interval]
        data = self._perform_request(
            lambda: self._handle.get_historical_klines_tv(symbol, kucoin_interval, since)
        )
        output = []
        for i in data:
            output.append(Candle(
                i[3], # Low
                i[2], # High
                i[1], # Open
                i[4], # Close
                dateparser.parse(str(i[0]))
            ))
        return output

    def get_wallets(self):
        output = []
        self._process_paged_request(
            self._handle.get_all_balances,
            lambda entry: output.append(Wallet(
                entry['coinType'],
                entry['balance'],
                entry['balance'] - entry['freezeBalance'],
                entry['freezeBalance']
            ))
        )
        return output

    def get_wallet(self, currency_code):
        self.check_valid_currency(currency_code)
        data = self._perform_request(lambda: self._handle.get_coin_balance(currency_code))
        return Wallet(
            self._currencies[currency_code],
            data['balance'],
            data['balance'] - data['freezeBalance'],
            data['freezeBalance']
        )

    def get_deposit_history(self, currency_code):
        request = lambda limit, page: self._handle.get_deposits(
            currency_code,
            limit=limit,
            page=page
        )
        output = []
        self._process_paged_request(
            request,
            lambda entry: output.append(Transfer(
                self._currencies[entry['coinType']],
                entry['amount'],
                entry['outerWalletTxid'], # is this right?
                1 if entry['status'] == 'FINISHED' else 0,
                0, # cost
                entry['status'] == 'CANCEL',
                dataparser.parse(str(entry['createdAt']))
            ))
        )
        return output

    def get_withdrawal_history(self, currency_code):
        request = lambda limit, page: self._handle.get_withdrawals(
            currency_code,
            limit=limit,
            page=page
        )
        output = []
        self._process_paged_request(
            request,
            lambda entry: output.append(Transfer(
                self._currencies[entry['coinType']],
                entry['amount'],
                entry['outerWalletTxid'], # is this right?
                0,
                entry['fee'],
                entry['status'] == 'CANCEL',
                dataparser.parse(str(entry['createdAt']))
            ))
        )
        return output

    def get_open_orders(self):
        raise ExchangeAPIException('Not implemented')

    def get_order_history(self):
        output = []
        self._process_paged_request(
            self._handle.get_dealt_orders,
            lambda entry: output.append(TradeOrder(
                data['orderOid'],
                self._currencies[base_currency],
                self._currencies[market_currency],
                None, # Date open
                dateparser.parse(str(data['createdAt'])),
                data['amount'],
                0, # Amount remaining
                data['dealPrice'],
                data['dealPrice'],
                KucoinWrapper.ORDER_TYPE_MAPPINGS[data['direction']]
            ))
        )
        return output

    def buy(self, base_currency_code, market_currency_code, amount, rate):
        symbol = self._make_symbol(base_currency_code, market_currency_code)
        result = self._perform_request(lambda: self._handle.create_buy_order(symbol, rate, amount))
        return result['orderOid']

    def sell(self, base_currency_code, market_currency_code, amount, rate):
        symbol = self._make_symbol(base_currency_code, market_currency_code)
        result = self._perform_request(lambda: self._handle.create_sell_order(symbol, rate, amount))
        return result['orderOid']

    def cancel_order(self, base_currency_code, market_currency_code, order_id):
        self._perform_request(lambda: self._handle.cancel_order(order_id, None))

    def withdraw(self, currency_code, amount, address, address_tag):
        if address_tag:
            raise ExchangeAPIException('Address tag not supported')
        self._perform_request(
            lambda: self._handle.create_withdrawal(currency_code, amount, address)
        )

    def get_deposit_address(self, currency_code):
        result = self._perform_request(lambda: self._handle.get_deposit_address(currency_code))
        return CryptoAddress(
            currency_code,
            result['address'],
            None
        )

    def transfers_needs_asset(self):
        return True
Exemple #3
0
def main():
	print("========== ArbBot - "+ symbol + '-' + symbol_base + ' [' + conf('general.instance_name') +"] ========== "+str(dt))
	actions = checkTelegramMessages()
	maxNow = getTradeMaxNow(symbol)

	bg = bitgrail.Bitgrail(conf('BitGrail.ApiKey'),conf('BitGrail.Secret'))
	kc_client = Client(conf('KuCoin.ApiKey'),conf('KuCoin.Secret'))
	bgm = bitgrail_mimic.Bitgrail_mimic()
	bgm.set_coin(symbol=symbol,symbol_base=symbol_base)

	if(conf('BitGrail.checkWithdrawals','bool')) and (random.randrange(10) > 5):
		if(bgm.checkWithdrawals(symbol)):
			print "BitGrail "+symbol+" withdrawals are open."
			telegramBot.text_message(symbol+" withdrawals just opened up on BitGrail!",topic="Mon.BG."+symbol+"_Withdrawals")
		else:
			print "BitGrail "+symbol+" withdrawals under maintenance."
			telegramBot.text_message(symbol+" withdrawals just became deactivated again! (under maintenance)",topic="Mon.BG."+symbol+"_Withdrawals")
	bg_balance = bgm.getBalance()

	print("\n========== Balances ==========")
	balanceStr = ''
	if(symbol in bg_balance):
		lastDealPrice = float(getConfig('cache','lastDealPrice'))
		coinValueBaseCurrency = bg_balance[symbol] * lastDealPrice
		total_in_BaseCurrency = coinValueBaseCurrency+bg_balance[symbol_base]
		balanceStr =              "BitGrail:    "+str(round(bg_balance[symbol_base],5)).ljust(8)+" "+symbol_base+" + "+str(round(bg_balance[symbol],5)).ljust(8)+" "+symbol+" = "+str(round(total_in_BaseCurrency,5)).ljust(8)+" "+symbol_base+"\n"
	balances = kc_client.get_all_balances()
	kc_balance = {}
	for balance in balances:
		if(balance['coinType'] == symbol_base):
			kc_balance[symbol_base] = balance['balance']
		if(balance['coinType'] == symbol):
			if(balance['balance'] == False): balance['balance'] = 0.0
			kc_balance[symbol] = balance['balance']
	if(symbol in kc_balance):
		coinValueBaseCurrency2 = kc_balance[symbol] * lastDealPrice
		total_in_BaseCurrency2 = coinValueBaseCurrency2+kc_balance[symbol_base]
		balanceStr = balanceStr + "KuCoin:      "+str(round(kc_balance[symbol_base],5)).ljust(8)+" "+symbol_base+" + "+str(round(kc_balance[symbol],5)).ljust(8)+" "+symbol+" = "+str(round(total_in_BaseCurrency2,5)).ljust(8)+" "+symbol_base+"\n"
	balanceStr = balanceStr +     "Grand total: "+str(round(bg_balance[symbol_base]+kc_balance[symbol_base],5)).ljust(8)+" "+symbol_base+" + "+str(round(bg_balance[symbol]+kc_balance[symbol],5)).ljust(8)+" "+symbol+" = "+str(round(total_in_BaseCurrency+total_in_BaseCurrency2,5)).ljust(8)+" "+symbol_base+"\n"

	btc_gains = bg_balance[symbol_base]+kc_balance[symbol_base]
	btc_gains-= conf('general.starting_balance_btc','float')
	coin_gains = bg_balance[symbol]+kc_balance[symbol]
	coin_gains-= float(getConfig(symbol,'starting_balance'))
	drift_max = 1.5 * float(getConfig(symbol,'max_per_trade'))
	if(abs(coin_gains) > drift_max):
		msg = "The amount of coins has drifted by "+str(coin_gains)+" "+symbol
		msg += ". This is more than the allowed "+str(drift_max)+" "+symbol+". "
		telegramBot.text_message(msg,topic = "coin_drift")
		updateLimits(symbol,0,abortReason="CoinDriftedBy"+str(coin_gains)+symbol)
	else:
		telegramBot.text_message("Coin drift acceptable again.",topic = "coin_drift")
	balanceStr = str(balanceStr + symbol_base+" gains: ").ljust(14)+str(round(btc_gains,5)).ljust(8)+" "+symbol_base+" (about € "+str(round(btc_gains*BTCEUR,2))+") + "+str(round(coin_gains,4))+" "+symbol+"\n"
	print balanceStr

	ch_s = "===== BALANCE CHANGE =====\n"
	if(float(getConfig(symbol,'KC_last_balance')) != kc_balance[symbol]):
		ch_s += "KuCoin balance changed from "+getConfig(symbol,'KC_last_balance_base')+" to "+str(kc_balance[symbol_base])+" "+symbol_base + "\n" + balanceStr
		telegramBot.text_message(ch_s)
		print(ch_s + "\n")
	elif(float(getConfig(symbol,'BG_last_balance_base')) != bg_balance[symbol_base]):
		ch_s += "BitGrail balance changed from "+getConfig(symbol,'BG_last_balance_base')+" to "+str(bg_balance[symbol_base])+" "+symbol_base  + "\n" + balanceStr
		telegramBot.text_message(ch_s)
		print(ch_s + "\n")
	elif(float(getConfig(symbol,'KC_last_balance')) != kc_balance[symbol]):
		ch_s += "KuCoin balance changed from "+getConfig(symbol,'KC_last_balance')+" to "+str(kc_balance[symbol])+" "+symbol + "\n" + balanceStr
		telegramBot.text_message(ch_s)
		print(ch_s + "\n")
	elif(float(getConfig(symbol,'BG_last_balance')) != bg_balance[symbol]):
		ch_s += "BitGrail balance changed from "+getConfig(symbol,'BG_last_balance')+" to "+str(bg_balance[symbol])+" "+symbol  + "\n" + balanceStr
		telegramBot.text_message(ch_s)
		print(ch_s + "\n")
	setConfig(symbol,'KC_last_balance_base',kc_balance[symbol_base])
	setConfig(symbol,'BG_last_balance_base',bg_balance[symbol_base])
	setConfig(symbol,'KC_last_balance',kc_balance[symbol])
	setConfig(symbol,'BG_last_balance',bg_balance[symbol])

	ticker = bg.get("ticker",symbol_base+'-'+symbol)
	tBG = {'sell': float(ticker['ask']), 'buy': float(ticker['bid'])}

	ps  = "========== Prices ==========\n"
	spread = 100 * round((tBG['sell'] - tBG['buy'])/tBG['buy'],2)
	ps += "BitGrail  sell: "+str(tBG['sell']).ljust(10)+"\n"
	ps += "BitGrail   buy: "+str(tBG['buy']).ljust(10)+" (spread: "+str(spread)+"%)\n"

	ticker = kc_client.get_tick(symbol+'-'+symbol_base)
	setConfig('cache','lastDealPrice',float(((ticker['lastDealPrice']+tBG['sell']+tBG['buy'])/3)))
	tKC = {}
	tKC['sell'] = float(ticker['sell'])
	tKC['buy'] = float(ticker['buy'])
	spread = 100 * round((tKC['sell'] - tKC['buy'])/tKC['buy'],2)
	ps += "KuCoin     buy: "+str(tKC['buy']).ljust(10)+" \n"
	ps += "KuCoin    sell: "+str(tKC['sell']).ljust(10)+" (spread: "+str(spread)+"%)"
	print(ps)
	if('sendBalance' in actions):
		telegramBot.text_message(balanceStr + "\n" + ps)


	print("\n========== Trade ==========")
	# cheapness_punchline, cheapness_details = compareCheapness(('BitGrail',tBG['sell'],tBG['buy']),('KuCoin',tKC['sell'],tKC['buy']))
	# telegramBot.text_message(cheapness_punchline,topic="Mon.Cheapness",msg_full=cheapness_details)
	# Buy on KuCoin, sell on BitGrail
	profit_BaseSymbol = tBG['buy'] - tKC['sell']
	profit1 = profit_BaseSymbol/tKC['sell']*100
	profit1 -= 0.2 + 0.1 # remove fees
	margin = profit1 - min_perc_profit['KC']
	traded, traded_amount, conclusion = (False, 0.0, "NO_ACTION")
	if(profit1 >= min_perc_profit['KC']):
		profit = profit1
		conclusion = "KC->BG"
		print("On KuCoin you can buy "+symbol+" for "+str(tKC['sell'])+" which sells for "+str(tBG['buy'])+" on BitGrail ("+str(round(profit1,2))+"% profit).")
		print("On KuCoin you can buy "+symbol+" for € "+str(tKC['sell']*BTCEUR)+" which sells for €"+str(tBG['buy']*BTCEUR)+" on BitGrail ("+str(round(profit1,2))+"% profit).")

		maxNow = tradeCap(margin,maxNow,availableForSale = bg_balance[symbol])
		if(maxNow > conf('general.min_trade_size','float')) and conf('general.trading_enabled','bool'):
			maxLeftTotal = getTradeLeftTotal(symbol)
			print("Most I can trade now is: "+str(maxNow)+" "+symbol+" of "+str(maxLeftTotal)+" total.")
			if(conf('KuCoin.disable_buy','bool')):
				print("Not allowed to buy from KuCoin. Skipping trade.")
				exit()
			if(conf('BitGrail.disable_sell','bool')):
				print("Not allowed to sell to BitGrail. Skipping trade.")
				exit()
			# Update limits (whatever happens next)
			updateLimits(symbol,maxNow)
			# Get buy price on market A (KC)
			buyAt = tKC['sell']*1.01 # asking price to get it at instantly

			# TODO: check order book if enough is actually available at that price!
			# Get selling price on market B (BG)
			sellAt = tBG['buy']*0.99 # price people already want to buy it at
			# Place order on market A (KC)
			if(buyAt > sellAt):
				s = "I'm paying more on BitGrail than I'm getting from the sale on KuCoin (buyAt: "+str(buyAt)+", sellAt: "+str(sellAt)+")!"
				print(s)
				telegramBot.text_message(s)
			print("kc_client.create_buy_order("+symbol+'-'+symbol_base+", "+str(buyAt)+", "+str(maxNow)+")")
			traded = True
			traded_amount = maxNow
			s = "Buying "+str(maxNow)+" "+symbol+" on KuCoin at "+str(buyAt) + ": "
			buy_order_result = kc_client.create_buy_order(symbol+'-'+symbol_base, str(buyAt), str(maxNow))
			if('orderOid' in buy_order_result):
				print "Order placed: "+buy_order_result['orderOid']
				s = s + "Order placed.\n"
			else:
				print "Order on KC probably wasnt placed! Server response: "
				pp.pprint(buy_order_result)
				err = "KC order placement failed."
				updateLimits(symbol,0,abortReason=err)
				telegramBot.text_message(s + balanceStr + err)
				quit("Dude, fix me! I guess I'll be nice and not sell your coins on the the other exchange.")

			# Place order on market B (BG)
			print("Creating sell order on BitGrail of "+str(maxNow)+" "+symbol+" at "+str(sellAt))

			balance = bgm.getBalance()
			if(balance[symbol] < maxNow):
				warning = "Whoa... I'm not going to short "+symbol+". I almost tried to sell "+str(maxNow)+" but I have a balance of "+str(balance[symbol])+" on BitGrail. Capping to that."
				s = s + warning
				print warning
				maxNow = min(maxNow,balance[symbol])
			result = bgm.createOrder(symbol_base+'-'+symbol,'sell',maxNow,sellAt)
			print("BG result:",result)
			s = s + "\nBitGrail sell order of "+str(maxNow)+" "+symbol+" placed at "+str(sellAt)+" "+symbol_base+". "
			s = s + "\nProfit "+str(round(profit1,1))+"% >= "+str(min_perc_profit['KC'])+"%.\n"
			s = s + balanceStr
			s = s + "Result: " + str(result)
			telegramBot.text_message(s + "\n" + graphURL)
		else:
			if(conf('general.trading_enabled','bool')):
				print("Not allowed to trade this small amount!")
			else:
				print("Not allowed to trade anymore!")
	else:
		print("KC->BG profit below "+str(min_perc_profit['KC'])+"%:\t"+str(round(profit1,2))+"%")

	# Buy on BitGrail, sell on KuCoin
	profit_BaseSymbol = tKC['buy'] - tBG['sell']
	profit2 = profit_BaseSymbol/tBG['sell']*100
	profit2 -= 0.2 + 0.1 # remove fees
	margin = profit2 - min_perc_profit['BG']
	if(profit2 >= min_perc_profit['BG']):
		conclusion = "BitGrail is CHEAPER!"
		print("On BitGrail you can buy "+symbol+" for "+str(tBG['sell'])+" which sells for "+str(tKC['buy'])+" on KuCoin ("+str(round(profit2,2))+"% profit).")
		print("On BitGrail you can buy "+symbol+" for € "+str(tBG['sell']*BTCEUR)+" which sells for € "+str(tKC['buy']*BTCEUR)+" on KuCoin ("+str(round(profit2,2))+"% profit).")
		maxNow = tradeCap(margin,maxNow,availableForSale = kc_balance[symbol])
		if(maxNow > conf('general.min_trade_size','float')) and conf('general.trading_enabled','bool'):
			maxLeftTotal = getTradeLeftTotal(symbol)
			print("Most I can trade now is: "+str(maxNow)+" "+symbol+" of "+str(maxLeftTotal)+" total.")
			if(conf('BitGrail.disable_buy','bool')):
				print("Not allowed to buy from BitGrail. Skipping trade.")
				exit()
			if(conf('KuCoin.disable_sell','bool')):
				print("Not allowed to sell to KuCoin. Skipping trade.")
				exit()
			# Update limits (whatever happens next)
			updateLimits(symbol,maxNow)
			# Get buy price on market A (BG)
			buyAt = tBG['sell']*1.01 # asking price to get it at instantly
			# TODO: check order book if enough is actually available at that price!
			# Get selling price on market B (KC)
			sellAt = tKC['buy']*0.99 # price people already want to buy it at
			# Place buy order on market A (BG)
			if(tBG['sell'] > tKC['buy']):
				s = "would potentially have paid more to buy that I'd get to sell. Not doing anything!"
				print(s)
				telegramBot.text_message(s)
			else:
				traded = True
				traded_amount = -maxNow
				s = "BitGrail: creating buy order of "+str(maxNow)+" "+symbol+" at "+str(buyAt)+"."
				print(s)
				bg_balance = bgm.getBalance()
				if(bg_balance[symbol_base] < maxNow * buyAt):
					s = s + "Not enough coins.\n"
					print "Crap. I ran out of "+symbol_base+" on the exchange... Want to buy: "+str(maxNow)+" for "+str(maxNow * buyAt)+" BTC but I have a balance of "+str(bg_balance[symbol_base])+" "+symbol_base+" on BitGrail."
					print("\nI N S E R T   C O I N\n")
					quit("I'll stop purchasing now.")
				result = bgm.createOrder(symbol_base+'-'+symbol,'buy',maxNow,buyAt)
				print("BG result:",result)
				s = s + "\nResult: "+str(result)

				# Place sell order on market B (KC)
				print("kc_client.create_sell_order("+symbol_base+'-'+symbol+", "+str(sellAt)+", "+str(maxNow)+")")
				sell_order_result = kc_client.create_sell_order(symbol+'-'+symbol_base, str(sellAt), str(maxNow))
				# {   u'orderOid': u'5a54f203de88b3646e127e2f'}
				if('orderOid' in sell_order_result):
					s = s + "\nKuCoin sell order of "+str(maxNow)+" "+symbol+" placed at "+str(sellAt)+" "+symbol_base+"."
					s = s + "\nProfit "+str(round(profit2,1))+"% >= "+str(min_perc_profit['BG'])+"%.\n"
					s = s + balanceStr
					print "Order placed: "+sell_order_result['orderOid']
				else:
					print "Order probably wasnt placed! Server response: "
					updateLimits(symbol,0,abortReason="KC sell order placement failed?")
					pp.pprint(sell_order_result)
					s = s + balanceStr
					s = s + "\nFailure: "+str(sell_order_result)

				telegramBot.text_message(s + "\n" + graphURL)
		else:
			if(conf('general.trading_enabled','bool')):
				print("Not allowed to trade this small amount!")
			else:
				print("Not allowed to trade anymore!")
	else:
		print("BG->KC profit below "+str(min_perc_profit['BG'])+"%:\t"+str(round(profit2,2))+"%")

	# time	bitgrail_sell	bitgrail_buy	kcoin_sell	kcoin_buy	profitKC2BG	profitBG2KC	traded
	logCSV([dt,tBG['sell'],tBG['buy'],tKC['sell'],tKC['buy'],str(round(profit1,2))+"%",str(round(profit2,2))+"%",traded_amount])
Exemple #4
0
            logging.info("setting buy of " + str(buyAmount) + " at " +
                         str(buyPrice))
            logging.info(
                client.create_buy_order(tokenPair, buyPrice, buyAmount))
            sellAmount = determine_sell_amount(balance)
            sellPrice = determine_initial_sell_price(price)
            logging.info("setting sell of " + str(sellAmount) + " at " +
                         str(sellPrice))
            logging.info(
                client.create_sell_order(tokenPair, sellPrice, sellAmount))
        else:
            logging.info("No orders present...setting to ticker price")
            balance = client.get_coin_balance(currency)
            balance = (float(balance['balanceStr']) + float(extCoinBalance))
            buyAmount = determine_buy_amount(balance)
            price = (client.get_tick(tokenPair)['lastDealPrice'])
            buyPrice = determine_initial_buy_price(price)
            print(buyPrice)
            logging.info("setting buy of " + str(buyAmount) + " at " +
                         str(buyPrice))
            logging.info(
                client.create_buy_order(tokenPair, buyPrice, buyAmount))
            sellAmount = determine_sell_amount(balance)
            sellPrice = determine_initial_sell_price(price)
            logging.info("setting sell of " + str(sellAmount) + " at " +
                         str(sellPrice))
            logging.info(
                client.create_sell_order(tokenPair, sellPrice, sellAmount))
    except:
        logging.info("Shit went sideways...")