Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16216 from ttodua/js-examples-cleanup
examples - ohlcv and balance
- Loading branch information
Showing
11 changed files
with
143 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.