danpaquin / coinbasepro-python

The unofficial Python client for the Coinbase Pro API
MIT License
1.82k stars 738 forks source link

Incompatible six 1.12.0 or https error #373

Closed Mat-C-uk closed 3 years ago

Mat-C-uk commented 4 years ago

Hi just trying to get the code to work - python beginner, but this is my 5 or 6th project and looking for a hand. Enjoying writing a front end for this one..., that is gradually getting more complex... however slight problem buying and selling!

I've used identical code to that in the git (bar the three variable names in auth client, which are adjusted in my code and the Auth Client, to which I've appended Test). The call to the function below has the "data" hard coded to confirm correct info is being passed to the function. Funds are defined below as a number, as required by the API spec, but I've tried a string too.

I note from the previous thread that six 1.12.0 is incompatable with cbpro, and the requirement is 1.11.0 (although 1.10.0 is referenced in previous post). Not sure if this is causing my error or not as I have 1.12.0 and not sure how to install by repository(?). 1.11.0 appears to be installing via pip but anaconda still shows .12 in the standard and a specific environment ive created. I've run in both environments and getting the same error, so i think its me and my code!!!

In addition, if I create an anaconda environment with the requirements list of packages, I get a conflict with some packages requiring a higher version than specified on the git. Is the list current? Thanks. At the day job at present, but can update later.

I can call prices etc, so don't think its an auth problem. Not getting any 401 to 403 errors, just 400.

The following is giving me a 400 error from the coinbase server (not the sandbox as trying direct). Formatting of the json data looks correct to me, as does the url.

I'm just looking to complete a market buy... my code is as follows, and any assistance greatly appreciated

cbpreply=AuthenticatedClientTest(key, privateKey, passPhrase) cbpreply.place_market_order(product_id='BTC-EUR', side='buy', funds=(0.5))

def __init__(self, key, privateKey, passPhrase,

api_url="https://api.pro.coinbase.com"):

    super(AuthenticatedClientTest, self).__init__(api_url)
    self.auth = CBProAuth(key, privateKey, passPhrase)
    self.session = requests.Session()

def place_market_order(self, product_id, side, size=None, funds=None,
                       client_oid=None,
                       stp=None,
                       overdraft_enabled=None,
                       funding_amount=None):

            params = {'product_id': product_id,
              'side': side,
              'order_type': 'market',
              'size': size,
              'funds': funds,
              'client_oid': client_oid,
              'stp': stp,
              'overdraft_enabled': overdraft_enabled,
              'funding_amount': funding_amount}
            params = dict((k, v) for k, v in params.items() if v is not None)
            print(params)
            return self.place_order(**params)

def place_order(self, product_id, side, order_type, **kwargs):

    # Margin parameter checks

    if kwargs.get('overdraft_enabled') is not None and \
            kwargs.get('funding_amount') is not None:
        raise ValueError('Margin funding must be specified through use of '
                         'overdraft or by setting a funding amount, but not'
                         ' both')

    # Limit order checks
    if order_type == 'limit':
        if kwargs.get('cancel_after') is not None and \
                kwargs.get('time_in_force') != 'GTT':
            raise ValueError('May only specify a cancel period when time '
                             'in_force is `GTT`')
        if kwargs.get('post_only') is not None and kwargs.get('time_in_force') in \
                ['IOC', 'FOK']:
            raise ValueError('post_only is invalid when time in force is '
                             '`IOC` or `FOK`')

    # Market and stop order checks
    if order_type == 'market' or order_type == 'stop':
        if not (kwargs.get('size') is None) ^ (kwargs.get('funds') is None):
            raise ValueError('Either `size` or `funds` must be specified '
                             'for market/stop orders (but not both).')

    # Build params dict
    params = {'product_id': product_id,
              'side': side,
              'type': order_type}
    params.update(kwargs)
    return self._send_message('POST', '/orders', data=json.dumps(params))

def _send_message(self, method, endpoint, params=None, data=None):
    """Send API request.
    Args:
        method (str): HTTP method (get, post, delete, etc.)
        endpoint (str): Endpoint (to be added to base URL)
        params (Optional[dict]): HTTP request parameters
        data (Optional[str]): JSON-encoded string payload for POST
    Returns:
        dict/list: JSON response
    """
    url = self.url + endpoint
    print("method= ",method, "URL=",url, "params= ",params, "Date= ", data)

    r = self.session.request(method, url, params, data=data,
                             auth=self.auth, timeout=30)
    print(r)
    return r.json()

Responce from print statements in the _send_message

`{'product_id': 'BTC-EUR', 'side': 'buy', 'order_type': 'market', 'funds': 5}

method= POST URL= https://api.pro.coinbase.com/orders params= None Date= {"product_id": "BTC-EUR", "side": "buy", "type": "market", "funds": 5}

<Response [400]>`

Thanks for any pointers or help.

Updated for clarification, added points and added further testing results.

Mat

Mat-C-uk commented 4 years ago

I think I've resolved the issue. Typical struggle for 3 days, ask the question and find the answer.

The code is working - I was below the minimum trade limit I think. Still not sure whats going on with the packages, but will install a clean environment for Anaconda at the weekend, and report the package conflicts.

Thanks for the code - posting in case any one else as stupid as me comes along!

pfabreu commented 4 years ago

They should upgrade it to be compatible with the new six anyway

mcardillo55 commented 3 years ago

Closing since it was resolved.