Skip to content

Commit

Permalink
Merge pull request #35 from tiagosiebler/jerko
Browse files Browse the repository at this point in the history
feat(): added more examples
  • Loading branch information
JJ-Cro authored Sep 20, 2024
2 parents 846ce9a + b070937 commit 7e44693
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 15 deletions.
43 changes: 43 additions & 0 deletions examples/AdvancedTrade/Private/closePosition.ts
Original file line number Diff line number Diff line change
@@ -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();
33 changes: 33 additions & 0 deletions examples/CBExchange/cb-exchange-public.ts
Original file line number Diff line number Diff line change
@@ -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();
68 changes: 68 additions & 0 deletions examples/CBInternationalExchange/cb-intx-public.ts
Original file line number Diff line number Diff line change
@@ -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();

This file was deleted.

0 comments on commit 7e44693

Please sign in to comment.