Skip to content

Commit

Permalink
Release v1.4.0 (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
urischwartz-cb authored May 21, 2024
1 parent 5c13096 commit 509ce1b
Show file tree
Hide file tree
Showing 18 changed files with 414 additions and 150 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## [1.4.0] - 2024-MAY-21

### Added
- Support for market order buys in base currency with `base_size` in the `market_order_buy` and `preview_market_order_buy` methods
- Support for the following Futures Intraday Leverage API endpoints:
- GetCurrentMarginWindow
- GetIntradayMarginSetting
- SetIntradayMarginSetting
- Support for the following Perpetual Futures Trading API endpoints:
- Get Perpetuals Portfolio Asset Balances
- Opt-In Multi Asset Collateral

## [1.3.0] - 2024-MAY-6

### Added
Expand Down
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
[![License](https://img.shields.io/badge/License-Apache%202.0-green.svg)](https://opensource.org/license/apache-2-0/)
[![Code Style](https://img.shields.io/badge/code_style-black-black)](https://black.readthedocs.io/en/stable/)

Welcome to the official Coinbase Advanced API Python SDK. This python project was created to allow coders to easily plug into the [Coinbase Advanced API](https://docs.cloud.coinbase.com/advanced-trade-api/docs/welcome).
This SDK also supports easy connection to the [Coinbase Advanced Trade WebSocket API](https://docs.cloud.coinbase.com/advanced-trade-api/docs/ws-overview).
Welcome to the official Coinbase Advanced API Python SDK. This python project was created to allow coders to easily plug into the [Coinbase Advanced API](https://docs.cdp.coinbase.com/advanced-trade/docs/welcome).
This SDK also supports easy connection to the [Coinbase Advanced Trade WebSocket API](https://docs.cdp.coinbase.com/advanced-trade/docs/ws-overview).

For thorough documentation of all available functions, refer to the following link: https://coinbase.github.io/coinbase-advanced-py

Expand All @@ -18,7 +18,7 @@ pip3 install coinbase-advanced-py
___
## Coinbase Developer Platform (CDP) API Keys

This SDK uses Cloud Developer Platform (CDP) API keys. To use this SDK, you will need to create a CDP API key and secret by following the instructions [here](https://docs.cloud.coinbase.com/advanced-trade-api/docs/auth#cloud-api-keys).
This SDK uses Cloud Developer Platform (CDP) API keys. To use this SDK, you will need to create a CDP API key and secret by following the instructions [here](https://docs.cdp.coinbase.com/advanced-trade/docs/auth).
Make sure to save your API key and secret in a safe place. You will not be able to retrieve your secret again.

WARNING: We do not recommend that you save your API secrets directly in your code outside of testing purposes. Best practice is to use a secrets manager and access your secrets that way. You should be careful about exposing your secrets publicly if posting code that leverages this library.
Expand Down Expand Up @@ -74,7 +74,7 @@ print(dumps(order, indent=2))
```
This code calls the `get_accounts` and `market_order_buy` endpoints.

Refer to the [Advanced API Reference](https://docs.cloud.coinbase.com/advanced-trade-api/reference) for detailed information on each exposed endpoint.
Refer to the [Advanced API Reference](https://docs.cdp.coinbase.com/advanced-trade/reference) for detailed information on each exposed endpoint.
Look in the `coinbase.rest` module to see the API hooks that are exposed.

### Passing in additional parameters
Expand All @@ -94,7 +94,7 @@ market_trades = client.get("/api/v3/brokerage/products/BTC-USD/ticker", params={

portfolio = client.post("/api/v3/brokerage/portfolios", data={"name": "TestPortfolio"})
```
Here we are calling the [GetMarketTrades](https://docs.cloud.coinbase.com/advanced-trade-api/reference/retailbrokerageapi_getmarkettrades) and [CreatePortfolio](https://docs.cloud.coinbase.com/advanced-trade-api/reference/retailbrokerageapi_createportfolio) endpoints through the generic REST functions.
Here we are calling the [GetMarketTrades](https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_getmarkettrades) and [CreatePortfolio](https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_createportfolio) endpoints through the generic REST functions.
Once again, the built-in way to query these through the SDK would be:
```python
market_trades = client.get_market_trades(product_id="BTC-USD", limit=5)
Expand All @@ -104,8 +104,8 @@ portfolio = client.create_portfolio(name="TestPortfolio")

___
## WebSocket API Client
We offer a WebSocket API client that allows you to connect to the [Coinbase Advanced Trade WebSocket API](https://docs.cloud.coinbase.com/advanced-trade-api/docs/ws-overview).
Refer to the [Advanced Trade WebSocket Channels](https://docs.cloud.coinbase.com/advanced-trade-api/docs/ws-channels) page for detailed information on each offered channel.
We offer a WebSocket API client that allows you to connect to the [Coinbase Advanced Trade WebSocket API](https://docs.cdp.coinbase.com/advanced-trade/docs/ws-overview).
Refer to the [Advanced Trade WebSocket Channels](https://docs.cdp.coinbase.com/advanced-trade/docs/ws-channels) page for detailed information on each offered channel.

In your code, import the WSClient class and instantiate it. The WSClient requires an API key and secret to be passed in as arguments. You can also use a key file or environment variables as described in the RESTClient instructions above.

Expand Down Expand Up @@ -241,7 +241,7 @@ ___
## Authentication
Authentication of CDP API Keys is handled automatically by the SDK when making a REST request or sending a WebSocket message.

However, if you wish to handle this yourself, you must create a JWT token and attach it to your request as detailed in the API docs [here](https://docs.cloud.coinbase.com/advanced-trade-api/docs/rest-api-auth#making-requests). Use the built in `jwt_generator` to create your JWT token. For example:
However, if you wish to handle this yourself, you must create a JWT token and attach it to your request as detailed in the API docs [here](https://docs.cdp.coinbase.com/advanced-trade/docs/rest-api-auth#making-requests). Use the built in `jwt_generator` to create your JWT token. For example:
```python
from coinbase import jwt_generator

Expand All @@ -267,7 +267,7 @@ api_secret = "-----BEGIN EC PRIVATE KEY-----\nYOUR PRIVATE KEY\n-----END EC PRIV

jwt = jwt_generator.build_ws_jwt(api_key, api_secret)
```
Use this JWT to connect to the Websocket API by setting it in the "jwt" field of your subscription requests. See the docs [here](https://docs.cloud.coinbase.com/advanced-trade-api/docs/ws-overview#sending-messages-using-cloud-api-keys) for more details.
Use this JWT to connect to the Websocket API by setting it in the "jwt" field of your subscription requests. See the docs [here](https://docs.cdp.coinbase.com/advanced-trade/docs/ws-overview#sending-messages-with-cdp-keys) for more details.

___
## Accessing public endpoints without authentication
Expand All @@ -283,9 +283,9 @@ To do so, simply initialize the clients without providing any API keys as argume

### REST Client

In the REST client, here is an example calling [Get Public Products](https://docs.cloud.coinbase.com/advanced-trade/reference/retailbrokerageapi_getpublicproducts).
In the REST client, here is an example calling [Get Public Products](https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_getpublicproducts).
It does _not_ require authentication and is the public counterpart to
[Get Products](https://docs.cloud.coinbase.com/advanced-trade/reference/retailbrokerageapi_getproducts), which _does_ require authentication.
[Get Products](https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_getproducts), which _does_ require authentication.

Both endpoints return the same data.

Expand All @@ -298,13 +298,13 @@ client = RESTClient()
public_products = client.get_public_products()
print(public_products)
```
_Full list of all public REST endpoints [here](https://docs.cloud.coinbase.com/advanced-trade/docs/rest-api-overview#public-endpoints)_
_Full list of all public REST endpoints [here](https://docs.cdp.coinbase.com/advanced-trade/docs/rest-api-overview#public-endpoints)_

_Rate limit details for REST endpoints [here](https://docs.cloud.coinbase.com/advanced-trade/docs/rest-api-rate-limits)_
_Rate limit details for REST endpoints [here](https://docs.cdp.coinbase.com/advanced-trade/docs/rest-api-rate-limits)_

### Websocket Client

In the Websocket client, here is an example subscribing to the [ticker](https://docs.cloud.coinbase.com/advanced-trade/docs/ws-channels#ticker-channel) channel.
In the Websocket client, here is an example subscribing to the [ticker](https://docs.cdp.coinbase.com/advanced-trade/docs/ws-channels#ticker-channel) channel.
Unlike the REST client, Websocket channels handle both authenticated and unauthenticated requests.
At the moment, most channels in the Websocket client are public and can be used without keys.

Expand All @@ -326,9 +326,9 @@ client.ticker_unsubscribe(product_ids=["BTC-USD"])
client.close()
```

_Full list of all public Websocket channels [here](https://docs.cloud.coinbase.com/advanced-trade/docs/ws-channels)_
_Full list of all public Websocket channels [here](https://docs.cdp.coinbase.com/advanced-trade/docs/ws-channels)_

_Rate limit details for Websocket channels [here](https://docs.cloud.coinbase.com/advanced-trade/docs/ws-rate-limits)_
_Rate limit details for Websocket channels [here](https://docs.cdp.coinbase.com/advanced-trade/docs/ws-rate-limits)_


___
Expand All @@ -340,4 +340,4 @@ ___

If you've found a bug within this project, open an issue on this repo and add the "bug" label to it.
If you would like to request a new feature, open an issue on this repo and add the "enhancement" label to it.
Direct concerns or questions on the API to the [Advanced API Developer Forum](https://forums.coinbasecloud.dev/c/advanced-trade-api/20).
Direct concerns or questions on the API to the [Advanced API Discord](https://discord.com/channels/1220414409550336183/1220464268743278613) (use this [invite link](https://discord.com/invite/cdp) if it's your first time accessing the Discord).
2 changes: 1 addition & 1 deletion coinbase/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.3.0"
__version__ = "1.4.0"
5 changes: 5 additions & 0 deletions coinbase/rest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ class RESTClient(RESTBase):
from .futures import (
cancel_pending_futures_sweep,
close_position,
get_current_margin_window,
get_futures_balance_summary,
get_futures_position,
get_intraday_margin_setting,
list_futures_positions,
list_futures_sweeps,
schedule_futures_sweep,
set_intraday_margin_setting,
)
from .market_data import get_candles, get_market_trades
from .orders import (
Expand Down Expand Up @@ -82,9 +85,11 @@ class RESTClient(RESTBase):
from .payments import get_payment_method, list_payment_methods
from .perpetuals import (
allocate_portfolio,
get_perps_portfolio_balances,
get_perps_portfolio_summary,
get_perps_position,
list_perps_positions,
opt_in_or_out_multi_asset_collateral,
)
from .portfolios import (
create_portfolio,
Expand Down
4 changes: 2 additions & 2 deletions coinbase/rest/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_accounts(
__________
**Read more on the official documentation:** `List Accounts <https://docs.cloud.coinbase.com/advanced-trade-api/reference/retailbrokerageapi_getaccounts>`_
**Read more on the official documentation:** `List Accounts <https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_getaccounts>`_
"""
endpoint = f"{API_PREFIX}/accounts"
Expand Down Expand Up @@ -51,7 +51,7 @@ def get_account(self, account_uuid: str, **kwargs) -> Dict[str, Any]:
__________
**Read more on the official documentation:** `Get Account <https://docs.cloud.coinbase.com/advanced-trade-api/reference/retailbrokerageapi_getaccount>`_
**Read more on the official documentation:** `Get Account <https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_getaccount>`_
"""
endpoint = f"{API_PREFIX}/accounts/{account_uuid}"

Expand Down
6 changes: 3 additions & 3 deletions coinbase/rest/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def create_convert_quote(
__________
**Read more on the official documentation:** `Create Convert Quote <https://docs.cloud.coinbase.com/advanced-trade-api/reference/retailbrokerageapi_createconvertquote>`_
**Read more on the official documentation:** `Create Convert Quote <https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_createconvertquote>`_
"""
endpoint = f"{API_PREFIX}/convert/quote"

Expand Down Expand Up @@ -69,7 +69,7 @@ def get_convert_trade(
__________
**Read more on the official documentation:** `Get Convert Trade <https://docs.cloud.coinbase.com/advanced-trade-api/reference/retailbrokerageapi_getconverttrade>`_
**Read more on the official documentation:** `Get Convert Trade <https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_getconverttrade>`_
"""
endpoint = f"{API_PREFIX}/convert/trade/{trade_id}"

Expand Down Expand Up @@ -98,7 +98,7 @@ def commit_convert_trade(
__________
**Read more on the official documentation:** `Commit Convert Trade <https://docs.cloud.coinbase.com/advanced-trade-api/reference/retailbrokerageapi_commitconverttrade>`_
**Read more on the official documentation:** `Commit Convert Trade <https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_commitconverttrade>`_
"""
endpoint = f"{API_PREFIX}/convert/trade/{trade_id}"

Expand Down
2 changes: 1 addition & 1 deletion coinbase/rest/fees.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_transaction_summary(
__________
**Read more on the official documentation:** `Create Convert Quote <https://docs.cloud.coinbase.com/advanced-trade-api/reference/retailbrokerageapi_createconvertquote>`_
**Read more on the official documentation:** `Create Convert Quote <https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_createconvertquote>`_
"""
endpoint = f"{API_PREFIX}/transaction_summary"

Expand Down
91 changes: 84 additions & 7 deletions coinbase/rest/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def close_position(
__________
**Read more on the official documentation:** `Close Position
<https://docs.cloud.coinbase.com/advanced-trade-api/reference/retailbrokerageapi_closeposition>`_
<https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_closeposition>`_
"""
endpoint = f"{API_PREFIX}/orders/close_position"
data = {"client_order_id": client_order_id, "product_id": product_id, "size": size}
Expand All @@ -45,7 +45,7 @@ def get_futures_balance_summary(self, **kwargs) -> Dict[str, Any]:
__________
**Read more on the official documentation:** `Get Futures Balance Summary
<https://docs.cloud.coinbase.com/advanced-trade-api/reference/retailbrokerageapi_getfcmbalancesummary>`_
<https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_getfcmbalancesummary>`_
"""
endpoint = f"{API_PREFIX}/cfm/balance_summary"

Expand All @@ -68,7 +68,7 @@ def list_futures_positions(self, **kwargs) -> Dict[str, Any]:
__________
**Read more on the official documentation:** `List Futures Positions
<https://docs.cloud.coinbase.com/advanced-trade-api/reference/retailbrokerageapi_getfcmpositions>`_
<https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_getfcmpositions>`_
"""
endpoint = f"{API_PREFIX}/cfm/positions"

Expand All @@ -91,7 +91,7 @@ def get_futures_position(self, product_id: str, **kwargs) -> Dict[str, Any]:
__________
**Read more on the official documentation:** `Get Futures Position
<https://docs.cloud.coinbase.com/advanced-trade-api/reference/retailbrokerageapi_getfcmposition>`_
<https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_getfcmposition>`_
"""
endpoint = f"{API_PREFIX}/cfm/positions/{product_id}"

Expand All @@ -114,7 +114,7 @@ def schedule_futures_sweep(self, usd_amount: str, **kwargs) -> Dict[str, Any]:
__________
**Read more on the official documentation:** `Schedule Futures Sweep
<https://docs.cloud.coinbase.com/advanced-trade-api/reference/retailbrokerageapi_schedulefcmsweep>`_
<https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_schedulefcmsweep>`_
"""
endpoint = f"{API_PREFIX}/cfm/sweeps/schedule"

Expand All @@ -139,7 +139,7 @@ def list_futures_sweeps(self, **kwargs) -> Dict[str, Any]:
__________
**Read more on the official documentation:** `List Futures Sweeps
<https://docs.cloud.coinbase.com/advanced-trade-api/reference/retailbrokerageapi_getfcmsweeps>`_
<https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_getfcmsweeps>`_
"""
endpoint = f"{API_PREFIX}/cfm/sweeps"

Expand All @@ -162,8 +162,85 @@ def cancel_pending_futures_sweep(self, **kwargs) -> Dict[str, Any]:
__________
**Read more on the official documentation:** `Cancel Pending Futures Sweep
<https://docs.cloud.coinbase.com/advanced-trade-api/reference/retailbrokerageapi_cancelfcmsweep>`_
<https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_cancelfcmsweep>`_
"""
endpoint = f"{API_PREFIX}/cfm/sweeps"

return self.delete(endpoint, **kwargs)


def get_intraday_margin_setting(self, **kwargs) -> Dict[str, Any]:
"""
**Get Intraday Margin Setting**
_______________________________
[GET] https://api.coinbase.com/api/v3/brokerage/cfm/intraday/margin_setting
__________
**Description:**
Get the status of whether your account is opted in to receive increased leverage on futures trades on weekdays from 8am-4pm ET.
__________
**Read more on the official documentation:** `Get Intraday Margin Setting
<https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_getintradaymarginsetting>`_
"""
endpoint = f"{API_PREFIX}/cfm/intraday/margin_setting"

return self.get(endpoint, **kwargs)


def get_current_margin_window(
self, margin_profile_type: str, **kwargs
) -> Dict[str, Any]:
"""
**Get Current Margin Window**
________________________________
[GET] https://api.coinbase.com/api/v3/brokerage/cfm/intraday/current_margin_window
__________
**Description:**
Get the current margin window to determine whether intraday or overnight margin rates are in effect.
__________
**Read more on the official documentation:** `Get Current Margin Window
<https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_getcurrentmarginwindow>`_
"""

endpoint = f"{API_PREFIX}/cfm/intraday/current_margin_window"

params = {"margin_profile_type": margin_profile_type}

return self.get(endpoint, params=params, **kwargs)


def set_intraday_margin_setting(self, setting: str, **kwargs) -> Dict[str, Any]:
"""
**Set Intraday Margin Setting**
________________________________
[POST] https://api.coinbase.com/api/v3/brokerage/cfm/intraday/margin_setting
__________
**Description:**
Opt in to receive increased leverage on futures trades on weekdays from 8am-4pm ET.
__________
**Read more on the official documentation:** `Set Intraday Margin Setting
<https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_setintradaymarginsetting>`_
"""

endpoint = f"{API_PREFIX}/cfm/intraday/margin_setting"

data = {"setting": setting}

return self.post(endpoint, data=data, **kwargs)
4 changes: 2 additions & 2 deletions coinbase/rest/market_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get_candles(
__________
**Read more on the official documentation:** `Get Product Candles
<https://docs.cloud.coinbase.com/advanced-trade-api/reference/retailbrokerageapi_getcandles>`_
<https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_getcandles>`_
"""
endpoint = f"{API_PREFIX}/products/{product_id}/candles"

Expand Down Expand Up @@ -57,7 +57,7 @@ def get_market_trades(
__________
**Read more on the official documentation:** `Get Market Trades
<https://docs.cloud.coinbase.com/advanced-trade-api/reference/retailbrokerageapi_getmarkettrades>`_
<https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_getmarkettrades>`_
"""
endpoint = f"{API_PREFIX}/products/{product_id}/ticker"

Expand Down
Loading

0 comments on commit 509ce1b

Please sign in to comment.