Skip to content

Instantly share code, notes, and snippets.

@LeonelF
Created August 9, 2019 13:36
Show Gist options
  • Save LeonelF/252659e73ae95edc7e952789afce2829 to your computer and use it in GitHub Desktop.
Save LeonelF/252659e73ae95edc7e952789afce2829 to your computer and use it in GitHub Desktop.
Coinbase-Pro Get open orders and Filled transactions
const CoinbasePro = require('coinbase-pro');
const key = 'App_key';
const secret = 'App_secret';
const passphrase = 'PassPhrase';
const apiURI = 'https://api.pro.coinbase.com';
const sandboxURI = 'https://api-public.sandbox.pro.coinbase.com';
const authedClient = new CoinbasePro.AuthenticatedClient(
key,
secret,
passphrase,
apiURI
);
const callbackFills = (err, response, data) => {
//console.log(data);
var odd = 1;
var vals = [0, 0];
console.log("*** Transaction history ***")
data.reverse().forEach((value, index) => {
console.log("Date: " + value.created_at + " Action: " + value.side + " Values: " + Math.round(value.price * 100) / 100 + "€ " + value.size + "BTC");
vals[odd % 2] = value.price * value.size;
if (odd % 2 == 0) {
if (vals[1] > vals[0]) {
console.log("Loss of: " + Math.round((vals[1] - vals[0]) * 1000) / 1000 + "€");
}
else if (vals[1] < vals[0]) {
console.log("Profit of: " + Math.round((vals[0] - vals[1]) * 1000) / 1000 + "€");
}
else {
console.log("The transaction was even, no profit and no loss!");
}
}
odd += 1;
});
}
const callbackOrders = (err, response, data) => {
console.log("*** List of open orders ***")
data.forEach((value, index) => {
console.log("Created at: " + value.created_at + " Type: " + value.type + "/" + value.side + " values: " + Math.round(value.price * 100) / 100 + "€ " + value.size + "BTC");
});
}
const params = {
product_id: 'BTC-EUR',
};
authedClient.getFills(params, callbackFills);
authedClient.getOrders(callbackOrders);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment