Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples - ohlcv and balance #16216

Merged
merged 1 commit into from Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions examples/js/advanced-error-handling.js
@@ -0,0 +1,39 @@
"use strict";

const ccxt = require ('../../ccxt.js');

// instantiate the exchange
let exchange = new ccxt.coinbasepro ({
'apiKey': 'XXXXXXXXXXXXXX',
'secret': 'YYYYYYYYYYYYYY',
});

async function checkOrders(){
try {
// fetch orders
let orders = await exchange.fetchOrders ('BTC/USDT');
// output the result
console.log (exchange.id, 'fetched orders', orders);
} catch (e) {
if (e instanceof ccxt.DDoSProtection || e.message.includes ('ECONNRESET')) {
console.log ('[DDoS Protection] ' + e.message);
} else if (e instanceof ccxt.RequestTimeout) {
console.log ('[Request Timeout] ' + e.message);
} else if (e instanceof ccxt.AuthenticationError) {
console.log ('[Authentication Error] ' + e.message);
} else if (e instanceof ccxt.ExchangeNotAvailable) {
console.log ('[Exchange Not Available Error] ' + e.message);
} else if (e instanceof ccxt.ExchangeError) {
console.log ('[Exchange Error] ' + e.message);
} else if (e instanceof ccxt.NetworkError) {
console.log ('[Network Error] ' + e.message);
} else {
// you can throw it if you want to stop the execution
// console.log ('[Exception ' + e.constructor.name + '] ' + e.message);
throw e;
}
}
}

// for demonstrational purposes, we use 1000 ms interval
setInterval(checkOrders, 1000);
61 changes: 0 additions & 61 deletions examples/js/balances.js

This file was deleted.

50 changes: 0 additions & 50 deletions examples/js/binance-fetch-ohlcv-many-symbols-since.js

This file was deleted.

27 changes: 0 additions & 27 deletions examples/js/binance-fetch-ohlcv.js

This file was deleted.

28 changes: 28 additions & 0 deletions examples/js/fetch-balance.js
@@ -0,0 +1,28 @@
"use strict";

const ccxt = require ('../../ccxt.js');

// instantiate the exchange
let exchange = new ccxt.coinbasepro ({
'apiKey': 'XXXXXXXXXXXXXX',
'secret': 'YYYYYYYYYYYYYY',
'password': 'ZZZZZZ', // if exchange requires password
});


async function checkMyBalance() {
try {
// fetch account balance from the exchange
let myBalance = await exchange.fetchBalance ();

// output the result
console.log (exchange.id, 'fetched balance', myBalance);

} catch (e) {
// fpr advanced error-handling, see the "advanced-error-handling.js" example file
console.log ('[' + e.constructor.name + '] ' + e.message);
throw e;
}
}

checkMyBalance();
72 changes: 72 additions & 0 deletions examples/js/fetch-ohlcv-from-to-mark-index-premium.js
@@ -0,0 +1,72 @@
"use strict";

const ccxt = require ('../../ccxt')

const exchange = new ccxt.binance ();

const symbols = [ 'BTC/USDT', 'ETH/USDT', 'ADA/USDT'];
// start from i.e. 01 february 2022
// you can use milliseconds integer or also parse uniform datetime string, i.e. exchange.parse8601 ('2020-02-01T00:00:00Z')
const fromTimestamp = 1643659200000;
const tillTimestamp = exchange.milliseconds ();
const timeframe = '1h';
const itemsLimit = 1000;
const fetchMethod = 'fetchOHLCV'; // if using swap exchanges, you can also use fetchMarkOHLCV, fetchIndexOHLCV, fetchPremiumIndexOHLCV

async function myDataFetch (symbol) {

await exchange.loadMarkets ();

// get the duration of one timeframe period in milliseconds
const duration = exchange.parseTimeframe (timeframe) * 1000;
console.log ('Fetching', symbol, timeframe, 'candles', 'from', exchange.iso8601 (fromTimestamp), 'to', exchange.iso8601 (tillTimestamp), '...');

let result = [];
let since = fromTimestamp;
do {

try {

const candles = await exchange[fetchMethod] (symbol, timeframe, since, itemsLimit);

const message = '[' + symbol + '] Fetched ' + candles.length + ' ' + timeframe + ' candles since ' + exchange.iso8601 (since);

if (candles.length) {

const first = candles[0];
const last = candles[candles.length - 1];
console.log ( message, ' | first', exchange.iso8601 (first[0]), ' | last', exchange.iso8601 (last[0]) );

// store your candles to a database or to a file here
// ...
result = result.concat (candles);
since = last[0] + duration // next start from last candle timestamp + duration

} else {
console.log ( message, ' | moving into next period');
since = since + duration * itemsLimit; // next start from the current period's end
}

} catch (e) {

console.log (symbol, e.constructor.name, e.message, ' Taking small pause...');
await exchange.sleep (2000);
// retry on next iteration
}

} while (since + duration <= tillTimestamp)

console.log (symbol + ' completed !');
return result;
}



async function checkAllSymbols() {
// download in parallel
await Promise.all (symbols.map (symbol => myDataFetch (symbol)));
// you can also do one by one (but that is not much optimal)
//for (const symbol of symbols) {
// const data = await myDataFetch (symbol);
}
checkAllSymbols();
37 changes: 0 additions & 37 deletions examples/js/fetch-ohlcv-mark-index-price.js

This file was deleted.

60 changes: 0 additions & 60 deletions examples/js/hitbtc-fetch-all-ohlcvs.js

This file was deleted.