From b07093700fa30c6952491939b58b069567539422 Mon Sep 17 00:00:00 2001 From: Jerko J <83344666+JJ-Cro@users.noreply.github.com> Date: Fri, 20 Sep 2024 12:39:17 +0200 Subject: [PATCH] feat(): added more examples --- .../AdvancedTrade/Private/closePosition.ts | 43 ++++++++++++ examples/CBExchange/cb-exchange-public.ts | 33 +++++++++ .../CBInternationalExchange/cb-intx-public.ts | 68 +++++++++++++++++++ .../coinbase-international-client-rest.ts | 15 ---- 4 files changed, 144 insertions(+), 15 deletions(-) create mode 100644 examples/AdvancedTrade/Private/closePosition.ts create mode 100644 examples/CBExchange/cb-exchange-public.ts create mode 100644 examples/CBInternationalExchange/cb-intx-public.ts delete mode 100644 examples/CBInternationalExchange/coinbase-international-client-rest.ts diff --git a/examples/AdvancedTrade/Private/closePosition.ts b/examples/AdvancedTrade/Private/closePosition.ts new file mode 100644 index 0000000..80ff7db --- /dev/null +++ b/examples/AdvancedTrade/Private/closePosition.ts @@ -0,0 +1,43 @@ +import { CBAdvancedTradeClient } from '../../../src/index.js'; +// import { CBAdvancedTradeClient } from 'coinbase-api'; + +// initialise the client +const client = new CBAdvancedTradeClient({ + apiKey: process.env.API_KEY_NAME || 'insert_api_key_here', + apiSecret: process.env.API_PRIVATE_KEY || 'insert_api_secret_here', +}); + +// this function is suggested to used with spot market orders +// If you want to close futures, it is recommended to use submitOrder() with the opposite side of the current position + +/* Closing Futures Positions - Exchange docs +When a contract expires, we automatically close your open position at the exchange settlement price. +You can also close your position before the contract expires +(for example, you may want to close your position if you’ve reached your profit target, +you want to prevent further losses, or you need to satisfy a margin requirement). + +There are two ways to close your futures positions: +(1) Close your position with this endpoint, or +(2) Create a separate trade to take the opposite position in the same futures contract you are currently holding in your account. +For example, to close an open long position in the BTC 23 Feb 24 contract, place an order to sell the same number of BTC 23 Feb 24 contracts. +If you were short to begin with, go long the same number of contracts to close your position. + +More info on https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_closeposition#closing-futures-positions */ + +async function closePosition() { + try { + // close position + const closePosition = await client.closePosition({ + product_id: 'BTC-USD', + size: '0.1', + client_order_id: client.generateNewOrderId(), + }); + console.log('Result: ', closePosition); + } catch (e) { + console.error('Send new order error: ', e); + } + + // +} + +closePosition(); diff --git a/examples/CBExchange/cb-exchange-public.ts b/examples/CBExchange/cb-exchange-public.ts new file mode 100644 index 0000000..1f7f180 --- /dev/null +++ b/examples/CBExchange/cb-exchange-public.ts @@ -0,0 +1,33 @@ +import { CBExchangeClient } from '../../src/CBExchangeClient.js'; +// import { CBExchangeClient } from 'coinbase-api'; + +// Initialize the client, you can pass in api keys here if you have them but they are not required for public endpoints +const client = new CBExchangeClient(); + +async function publicExchangeCalls() { + try { + // Get all known currencies + const currencies = await client.getCurrencies(); + console.log('Currencies: ', currencies); + + // Get a single currency by id + const currency = await client.getCurrency('BTC'); + console.log('Currency (BTC): ', currency); + + // Get all known trading pairs + const tradingPairs = await client.getAllTradingPairs(); + console.log('Trading Pairs: ', tradingPairs); + + // Get all product volume + const productVolume = await client.getAllProductVolume(); + console.log('Product Volume: ', productVolume); + + // Get all wrapped assets + const wrappedAssets = await client.getAllWrappedAssets(); + console.log('Wrapped Assets: ', wrappedAssets); + } catch (e) { + console.error('Error: ', e); + } +} + +publicExchangeCalls(); diff --git a/examples/CBInternationalExchange/cb-intx-public.ts b/examples/CBInternationalExchange/cb-intx-public.ts new file mode 100644 index 0000000..a3b47ba --- /dev/null +++ b/examples/CBInternationalExchange/cb-intx-public.ts @@ -0,0 +1,68 @@ +import { CBInternationalClient } from '../../src/CBInternationalClient.js'; +// import { CBInternationalClient } from 'coinbase-api'; + +// Initialize the client, you can pass in api keys here if you have them but they are not required for public endpoints +const client = new CBInternationalClient(); + +async function publicInternationalCalls() { + try { + // List assets + const assets = await client.getAssets(); + console.log('Assets: ', assets); + + // Get asset details + const assetDetails = await client.getAssetDetails({ asset: 'BTC' }); + console.log('Asset Details (BTC): ', assetDetails); + + // Get supported networks per asset + const supportedNetworks = await client.getSupportedNetworksPerAsset({ + asset: 'BTC', + }); + console.log('Supported Networks (BTC): ', supportedNetworks); + + // List instruments + const instruments = await client.getInstruments(); + console.log('Instruments: ', instruments); + + // Get instrument details + const instrumentDetails = await client.getInstrumentDetails({ + instrument: 'BTC-PERP', + }); + console.log('Instrument Details (BTC-PERP): ', instrumentDetails); + + // Get quote per instrument + const quote = await client.getQuotePerInstrument({ + instrument: 'BTC-PERP', + }); + console.log('Quote (BTC-PERP): ', quote); + + // Get daily trading volumes + const dailyVolumes = await client.getDailyTradingVolumes({ + instruments: 'BTC-PERP', + }); + console.log('Daily Trading Volumes (BTC-PERP): ', dailyVolumes); + + // Get aggregated candles data per instrument + const candlesData = await client.getAggregatedCandlesData({ + instrument: 'BTC-PERP', + granularity: 'ONE_HOUR', + start: '2023-01-01T00:00:00Z', + end: '2023-01-02T00:00:00Z', + }); + console.log('Aggregated Candles Data (BTC-PERP): ', candlesData); + + // Get historical funding rates + const fundingRates = await client.getHistoricalFundingRates({ + instrument: 'BTC-PERP', + }); + console.log('Historical Funding Rates (BTC-PERP): ', fundingRates); + + // List position offsets + const positionOffsets = await client.getPositionOffsets(); + console.log('Position Offsets: ', positionOffsets); + } catch (e) { + console.error('Error: ', e); + } +} + +publicInternationalCalls(); diff --git a/examples/CBInternationalExchange/coinbase-international-client-rest.ts b/examples/CBInternationalExchange/coinbase-international-client-rest.ts deleted file mode 100644 index 260130e..0000000 --- a/examples/CBInternationalExchange/coinbase-international-client-rest.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { CBInternationalClient } from '../../src/index.js'; - -const coinbaseInternational = new CBInternationalClient(); - -async function main() { - try { - const res1 = await coinbaseInternational.getAssetDetails({ asset: 'BTC' }); - - console.log(res1); - } catch (error) { - console.error(error); - } -} - -main();