Market trading app (sandbox)

Hey guys! I’m trying to make a app that gives my sandbox’s account balance and makes market’s orders. What docs should I look up? I’m getting burnout of look api references of prime, exchange/pro, etc… and can’t get a clue what is going on.

Hi @luizlacerdam! First of all, we’d like to thank you for taking an interest in using our Coinbase APIs.

There are various products Coinbase offers: Sign in with Coinbase, Exchange/Pro, Commerce, etc. These products offer different features and capabilities you can utilize in your application. Specifically for your use case which is to view account balances and make market orders, you may check out Coinbase Pro at this documentation. You may view then view the endpoints in here. Please note that this is also in line with your mention of Sandbox. As of now, Coinbase Exchange/Pro is the only API/service of Coinbase offering a sandbox environment for testing.

Specifically for viewing balances, you may use the /accounts endpoint. For placing market orders, on the other hand, you may check out the /orders endpoint.

For more information about our Coinbase APIs, you may check out our documentation at cloud.coinbase.com

We hope we were able to help you with your concerns. Feel free to reply if you have further concerns.

4 Likes

hey! I got some progress! I was able to creat a spot order but I can’t creat a market order.

class CoinbaseExchangeAuth(AuthBase):
    def __init__(self, api_key, secret_key, passphrase):
        self.api_key = api_key
        self.secret_key = secret_key
        self.passphrase = passphrase
def __call__(self, request):
    timestamp = str(time.time())
    message = timestamp + request.method + request.path_url + (request.body or b'').decode()
    hmac_key = base64.b64decode(self.secret_key)
    signature = hmac.new(hmac_key, message.encode(), hashlib.sha256)
    signature_b64 = base64.b64encode(signature.digest()).decode()

    request.headers.update({
        'CB-ACCESS-SIGN': signature_b64,
        'CB-ACCESS-TIMESTAMP': timestamp,
        'CB-ACCESS-KEY': self.api_key,
        'CB-ACCESS-PASSPHRASE': self.passphrase,
        'Content-Type': 'application/json'
    })
    return request

api_url = 'https://api-public.sandbox.pro.coinbase.com/
auth = CoinbaseExchangeAuth(APIKEY, API_SECRET, API_PASS)

PLACE MARKET ORDER

order = {
            'size': 0.001,
            'type': "market",
            'side': 'buy',
            'product_id': "BTC-USD"
        }
coinbase_order = requests.post(api_url + 'orders', json=order, auth=auth)
coinbase_order_response = coinbase_order.json()
print(coinbase_order_response)

RESPONSE

{‘id’: ‘********************’, ‘size’: ‘0.001’, ‘product_id’: ‘BTC-USD’, ‘side’: ‘buy’, ‘stp’: ‘dc’, ‘funds’: ‘204253.47316103’, ‘type’: ‘market’, ‘post_only’: False, ‘created_at’: ‘2022-06-21T12:42:54.556519Z’, ‘fill_fees’: ‘0’, ‘filled_size’: ‘0’, ‘executed_value’: ‘0’, ‘status’: ‘pending’, ‘settled’: False}
DEBUG: https://api-public.sandbox.pro.coinbase.com:443 “POST /orders HTTP/1.1” 200 None

So why it is not executing my market order?

UPDATE 1 - I added a new header called accept and the response was that but was no enought to buy. My balance still the same and the order was not created.

MY NEW HEADER

 request.headers.update({
            'CB-ACCESS-SIGN': signature_b64,
            'CB-ACCESS-TIMESTAMP': timestamp,
            'CB-ACCESS-KEY': self.api_key,
            'CB-ACCESS-PASSPHRASE': self.passphrase,
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        })

MY RESPONSE

{'id': 'my-account-id-changed', 'size': '0.001', 'product_id': 'BTC-USD', 'side': 'buy', 'stp': 'dc', 'funds': '204103.63040103', 'type': 'market', 'post_only': False, 'created_at': '2022-06-21T16:27:14.370385Z', 'fill_fees': '0', 'filled_size': '0', 'executed_value': '0', 'status': 'pending', 'settled': False}DEBUG: https://api-public.sandbox.pro.coinbase.com:443 "POST /orders HTTP/1.1" 200 None

Hello @luizlacerdam! Thank you for taking an interest in trying out Coinbase APIs. For the details regarding your concern, we will check on this for you with our team. We will get back to you once we have more information. Keep in touch!

2 Likes

Hi @luizlacerdam, as per checking on our side, we are able to post market and limit orders using our Coinbase Pro Sandbox API for both buy and sell. We’ve ruled out the body you are sending alongside the HTTP request as the cause of this behavior.

For us to be able to help you, we’d be needing some information. First, we’d like you to push for a new market order. When done, we’d like you to take note of the order_id listed as id in the JSON response. If you may, please check the details of your market order using the /orders endpoint documented here.

  • What is the value of the status parameter in the JSON response? If it is done, this must mean that the market order was successful. This would then rule out your code entirely. A screenshot of this response would also help in our investigation should the troubleshooting we would be recommending is still not enough to address your issue.

Additionally, we’d also like to know if you are using multiple portfolios. If yes, can you please verify if you are viewing the right portfolio when checking the Coinbase website? (This is assuming you are using the website to view your account balances). You may check if you are viewing the correct portfolio by double checking if the selected portfolio at the top-left region of the web page is the one your API key has access to. This is a simple troubleshooting step that might help you with your concern since different portfolios remain unaffected when you are pushing for cryptocurrency transactions on a different portfolio.

We’d like to hear from you should you be able to resolve this issue. If you need further help, just reply to this thread and we’ll be sure to continue giving you support.

3 Likes