0
if condition2 < condition1:
        # Call the function to get the account balances
        balances = get_balances()
        quote_balance = balances['quote']
        base1_balance = balances['base1']
        base2_balance = balances['base2']
        now = int(time.time() * 1000)
        url_sell = 'https://api.kucoin.com/api/v1/orders'
        # Generate a unique clientOid
        client0id = str(uuid.uuid4())

        # Calculate the size based on the baseIncrement and baseMinSize
        baseMinSize = 0.1
        baseIncrement = 0.0001
        a = base1_balance
        decimal_val = decimal.Decimal(str(a)).quantize(
            decimal.Decimal('.000001'),
            rounding=decimal.ROUND_DOWN
        )
        size_base1 = float(decimal_val)

        data_sell = {
            "clientOid": client0id,
            "side": "sell",
            "symbol": "BASE1-QUOTE",
            "size": size_base1,
            "type": "market",
            "price": ""
        }
        str_to_sign_sell = str(now) + 'POST' + '/api/v1/orders' + json.dumps(data_sell)
        signature_sell = base64.b64encode(
            hmac.new(api_secret.encode('utf-8'), str_to_sign_sell.encode('utf-8'), hashlib.sha256).digest())
        passphrase_sell = base64.b64encode(
            hmac.new(api_secret.encode('utf-8'), api_passphrase.encode('utf-8'), hashlib.sha256).digest())
        headers_sell = {
            "Content-Type": "application/json",
            "KC-API-SIGN": signature_sell,
            "KC-API-TIMESTAMP": str(now),
            "KC-API-KEY": api_key,
            "KC-API-PASSPHRASE": passphrase_sell,
            "KC-API-KEY-VERSION": "2"
        }
        response_sell = requests.request('post', url_sell, headers=headers_sell, data=json.dumps(data_sell))
        print(response_sell.status_code)
        # wait 10 seconds before looping again
        while True:
            time.sleep(10)
            break

        # Call the function to get the account balances
        balances = get_balances()

        now = int(time.time() * 1000)
        url_buy = 'https://api.kucoin.com/api/v1/orders'
        # Generate a unique clientOid
        client0id = str(uuid.uuid4())

        # Calculate the funds based on the quoteIncrement and quoteMinSize
        quoteMinSize = 0.1
        quoteIncrement = 1
        a = quote_balance
        decimal_val = decimal.Decimal(str(a)).quantize(
            decimal.Decimal('.1'),
            rounding=decimal.ROUND_DOWN
        )
        funds_quote = float(decimal_val)

        data_buy = {
            "clientOid": client0id,
            "side": "buy",
            "symbol": "BASE2-QUOTE",
            "funds": funds_quote,
            "type": "market",
            "price": ""
        }
        str_to_sign_buy = str(now) + 'POST' + '/api/v1/orders' + json.dumps(data_buy)
        signature_buy = base64.b64encode(
            hmac.new(api_secret.encode('utf-8'), str_to_sign_buy.encode('utf-8'), hashlib.sha256).digest())
        passphrase_buy = base64.b64encode(
            hmac.new(api_secret.encode('utf-8'), api_passphrase.encode('utf-8'), hashlib.sha256).digest())
        headers_buy = {
            "Content-Type": "application/json",
            "KC-API-SIGN": signature_buy,
            "KC-API-TIMESTAMP": str(now),
            "KC-API-KEY": api_key,
            "KC-API-PASSPHRASE": passphrase_buy,
            "KC-API-KEY-VERSION": "2"
        }
        response_buy = requests.request('post', url_buy, headers=headers_buy, data=json.dumps(data_buy))
        print(response_buy.status_code)

I'm having a strange problem with the fragment of code that i pasted here. It's a trading bot that's supposed to sell and buy given assets if certain conditions are met. All the rest of my code works as intended. The problem most likely lies somewhere within this fragment.

there are two base currencies and one quote currency

PROBLEM:

1st scenario: base1 currency available --> sell proceed returns 200 --> balance reflects change in assets size --> quote currency available --> buy proceed returns 200 --> no change in balance of base2 currency

2nd scenario: base1 currency unavailable --> sell proceed returns 200 --> obviously no change in asset size --> quote currency available --> buy proceed returns 200 --> filled with balances of base2 currency appropriately changed

what I want:

base1 currency unavailable/available --> sell proceed returns 200 --> filled with balances appropriatlely changed --> quote currency available --> buy proceed returns 200 --> filled with balances of base2 currency appropriately changed

The best way to describe the issue would be to give an example:

1st scenario:

1500 units of base1 currency 0 units of base2 currency 0 units of quote currency *selling returns code 200 --> balance checked again --> balance changed * now I have (let's say): 0 units of base1 currency 0 units of base2 currency 3500 units of quote currency buying returns code 200 --> balance checked again --> balance not changed finally I have (here's the problem) still: 0 units of base1 currency 0 units of base2 currency 3500 units of quote currency

2nd scenario: 0 units of base1 currency 0 units of base2 currency 3500 units of quote currency selling returns code 200 --> balance checked again --> balance (obviously) not changed now I have (let's say): 0 units of base1 currency 0 units of base2 currency 3500 units of quote currency buying returns code 200 --> balance checked again --> balance changed finally I have: 0 units of base1 currency 1500 units of base2 currency 0 units of quote currency

In other words: if there is not enough base currency to make a sell operation then sell proceeds and then the buy operation proceeds with the account balance changing appropriately while if there is enough base currency to make a sell operation then sell proceeds and then the buy operation also proceeds but the account balance doesn't change

What could be the underlying cause of this issue?

I tried:

  • making pause between sell and buy operation longer - no result
  • trying to check if sell order was filled but it doesn't matter as the balance check after sell operation (if base currency available) shows that the sell operation did fill and quote currency is available for buying
  • indenting buying part of code more - no result
  • trying to change quote increment - no result
qas2100
  • 61
  • 1
  • 4

0 Answers0