Skip to content

Commit

Permalink
renaming in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kroitor committed Aug 8, 2017
1 parent 623069c commit 5a4c09a
Show file tree
Hide file tree
Showing 13 changed files with 203 additions and 203 deletions.
52 changes: 26 additions & 26 deletions examples/js/arbitrage-pairs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ const log = require ('ololog').configure ({ locate: false })

require ('ansicolor').nice;

let printSupportedMarkets = function () {
log ('Supported markets:', ccxt.markets.join (', ').green)
let printSupportedExchanges = function () {
log ('Supported exchanges:', ccxt.exchanges.join (', ').green)
}

let printUsage = function () {
log ('Usage: node', process.argv[1], 'id1'.green, 'id2'.yellow, 'id3'.blue, '...')
printSupportedMarkets ()
printSupportedExchanges ()
}

let printMarketSymbolsAndProducts = function (market) {
log (getMarketSymbols (market))
log (getMarketProductsTable (market))
let printExchangeSymbolsAndMarkets = function (exchange) {
log (getExchangeSymbols (exchange))
log (getExchangeMarketsTable (exchange))
}

let getMarketProductsTable = (market) => {
return asTable.configure ({ delimiter: ' | ' }) (Object.values (products))
let getExchangeMarketsTable = (exchange) => {
return asTable.configure ({ delimiter: ' | ' }) (Object.values (markets))
}

let sleep = (ms) => new Promise (resolve => setTimeout (resolve, ms));
Expand All @@ -37,32 +37,32 @@ let proxies = [
if (process.argv.length > 3) {

let ids = process.argv.slice (2)
let markets = {}
let exchanges = {}

log (ids.join (', ').yellow)

// load all products from all exchange markets
// load all markets from all exchanges
for (let id of ids) {

// instantiate the exchange by id
let market = new ccxt[id] ()
let exchange = new ccxt[id] ()

// save it in a dictionary under its id for future use
markets[id] = market
exchanges[id] = exchange

// load all products from the exchange
let products = await market.loadProducts ()
// load all markets from the exchange
let markets = await exchange.loadMarkets ()

// basic round-robin proxy scheduler
let currentProxy = 0
let maxRetries = proxies.length

for (let numRetries = 0; numRetries < maxRetries; numRetries++) {

try { // try to load exchange products using current proxy
try { // try to load exchange markets using current proxy

market.proxy = proxies[currentProxy]
await market.loadProducts ()
exchange.proxy = proxies[currentProxy]
await exchange.loadMarkets ()

} catch (e) { // rotate proxies in case of connectivity errors, catch all other exceptions

Expand All @@ -73,10 +73,10 @@ let proxies = [
log.bright.yellow ('[Timeout Error] ' + e.message)
} else if (e instanceof ccxt.AuthenticationError) {
log.bright.yellow ('[Authentication Error] ' + e.message)
} else if (e instanceof ccxt.MarketNotAvailableError) {
log.bright.yellow ('[Market Not Available Error] ' + e.message)
} else if (e instanceof ccxt.MarketError) {
log.bright.yellow ('[Market Error] ' + e.message)
} else if (e instanceof ccxt.ExchangeNotAvailableError) {
log.bright.yellow ('[Exchange Not Available Error] ' + e.message)
} else if (e instanceof ccxt.ExchangeError) {
log.bright.yellow ('[Exchange Error] ' + e.message)
} else {
throw e; // rethrow all other exceptions
}
Expand All @@ -86,26 +86,26 @@ let proxies = [
}
}

log (id.green, 'loaded', market.symbols.length.green, 'products')
log (id.green, 'loaded', exchange.symbols.length.green, 'markets')
}

log ('Loaded all products'.green)
log ('Loaded all markets'.green)

// get all unique symbols
let uniqueSymbols = ccxt.unique (ccxt.flatten (ids.map (id => markets[id].symbols)))
let uniqueSymbols = ccxt.unique (ccxt.flatten (ids.map (id => exchanges[id].symbols)))

// filter out symbols that are not present on at least two exchanges
let arbitrableSymbols = uniqueSymbols
.filter (symbol =>
ids.filter (id =>
(markets[id].symbols.indexOf (symbol) >= 0)).length > 1)
(exchanges[id].symbols.indexOf (symbol) >= 0)).length > 1)
.sort ((id1, id2) => (id1 > id2) ? 1 : ((id2 > id1) ? -1 : 0))

// print a table of arbitrable symbols
let table = arbitrableSymbols.map (symbol => {
let row = { symbol }
for (let id of ids)
if (markets[id].symbols.indexOf (symbol) >= 0)
if (exchanges[id].symbols.indexOf (symbol) >= 0)
row[id] = id
return row
})
Expand Down
2 changes: 1 addition & 1 deletion examples/js/balances.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let sleep = (ms) => new Promise (resolve => setTimeout (resolve, ms));

try {

// fetch account balance from exchange market
// fetch account balance from the exchange
let gdaxBalance = await gdax.fetchBalance ()

// output the result
Expand Down
52 changes: 26 additions & 26 deletions examples/js/bcc-vs-bch.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ let proxies = [

(async function main () {

let ids = ccxt.markets
let markets = {}
let ids = ccxt.exchanges
let exchanges = {}

// instantiate all markets
ccxt.markets.forEach (id => {
markets[id] = new (ccxt)[id] ({
// instantiate all exchanges
ccxt.exchanges.forEach (id => {
exchanges[id] = new (ccxt)[id] ({
verbose: false,
substituteCommonCurrencyCodes: true,
})
Expand All @@ -35,40 +35,40 @@ let proxies = [
// set up api keys appropriately
for (let id in config)
for (let key in config[id])
markets[id][key] = config[id][key]
exchanges[id][key] = config[id][key]

log (ids.join (', ').yellow)

// load all products from all exchange markets
// load all markets from all exchanges

await Promise.all (ids.map (async id => {

let market = markets[id]
let exchange = exchanges[id]

// basic round-robin proxy scheduler
let currentProxy = 0
let maxRetries = proxies.length

for (let numRetries = 0; numRetries < maxRetries; numRetries++) {

try { // try to load exchange products using current proxy
try { // try to load exchange markets using current proxy

market.proxy = proxies[currentProxy]
await market.loadProducts ()
exchange.proxy = proxies[currentProxy]
await exchange.loadMarkets ()

} catch (e) { // rotate proxies in case of connectivity errors, catch all other exceptions

// swallow connectivity exceptions only
if ((e instanceof ccxt.DDoSProtectionError) || e.message.includes ('ECONNRESET')) {
log.bright.yellow (market.id + ' [DDoS Protection Error]')
log.bright.yellow (exchange.id + ' [DDoS Protection Error]')
} else if (e instanceof ccxt.TimeoutError) {
log.bright.yellow (market.id + ' [Timeout Error] ' + e.message)
log.bright.yellow (exchange.id + ' [Timeout Error] ' + e.message)
} else if (e instanceof ccxt.AuthenticationError) {
log.bright.yellow (market.id + ' [Authentication Error] ' + e.message)
} else if (e instanceof ccxt.MarketNotAvailableError) {
log.bright.yellow (market.id + ' [Market Not Available Error] ' + e.message)
} else if (e instanceof ccxt.MarketError) {
log.bright.yellow (market.id + ' [Market Error] ' + e.message)
log.bright.yellow (exchange.id + ' [Authentication Error] ' + e.message)
} else if (e instanceof ccxt.ExchangeNotAvailableError) {
log.bright.yellow (exchange.id + ' [Exchange Not Available Error] ' + e.message)
} else if (e instanceof ccxt.ExchangeError) {
log.bright.yellow (exchange.id + ' [Exchange Error] ' + e.message)
} else {
throw e; // rethrow all other exceptions
}
Expand All @@ -78,19 +78,19 @@ let proxies = [
}
}

if (market.symbols)
log (id.green, 'loaded', market.symbols.length.green, 'products')
if (exchange.symbols)
log (id.green, 'loaded', exchange.symbols.length.green, 'markets')

}))

log ('Loaded all products'.green)
log ('Loaded all markets'.green)

let table = ccxt.markets.map (id => {
let table = ccxt.exchanges.map (id => {
console.log (id)
let market = markets[id]
if (market.currencies) {
let hasBCC = market.currencies.includes ('BCC')
let hasBCH = market.currencies.includes ('BCH')
let exchange = exchanges[id]
if (exchange.currencies) {
let hasBCC = exchange.currencies.includes ('BCC')
let hasBCH = exchange.currencies.includes ('BCH')
let hasBoth = (hasBCC && hasBCH)
return {
id,
Expand Down
20 changes: 10 additions & 10 deletions examples/js/markets.js → examples/js/exchanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ require ('ansicolor').nice;
process.on ('uncaughtException', e => { log.bright.red.error (e); process.exit (1) })
process.on ('unhandledRejection', e => { log.bright.red.error (e); process.exit (1) })

let markets = {}
let exchanges = {}

ccxt.markets.forEach (id => { markets[id] = new (ccxt)[id] () })
ccxt.exchanges.forEach (id => { exchanges[id] = new (ccxt)[id] () })

log ('The ccxt library supports', (ccxt.markets.length.toString ()).green, 'markets:')
log ('The ccxt library supports', (ccxt.exchanges.length.toString ()).green, 'exchanges:')

var countryName = function (code) {
return ((typeof countries[code] !== 'undefined') ? countries[code] : code)
}

log (asTable.configure ({ delimiter: ' | ' }) (Object.values (markets).map (market => {
log (asTable.configure ({ delimiter: ' | ' }) (Object.values (exchanges).map (exchange => {

let countries = Array.isArray (market.countries) ?
market.countries.map (countryName).join (', ') :
countryName (market.countries)
let countries = Array.isArray (exchange.countries) ?
exchange.countries.map (countryName).join (', ') :
countryName (exchange.countries)

let website = Array.isArray (market.urls.www) ? market.urls.www[0] : market.urls.www
let website = Array.isArray (exchange.urls.www) ? exchange.urls.www[0] : exchange.urls.www

return {
id: market.id,
name: market.name,
id: exchange.id,
name: exchange.name,
url: website,
countries: countries,
}
Expand Down
30 changes: 15 additions & 15 deletions examples/js/instantiate-all-at-once.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const ccxt = require ('../../ccxt');

async function test () {

let markets = {
let exchanges = {
"bittrex": {
"apiKey": "60f38a5818934fc08308778f94d3d8c4",
"secret": "9d294ddb5b944403b58e5298653720c1",
Expand All @@ -15,32 +15,32 @@ async function test () {
},
}

let ids = ccxt.markets.filter (id => id in markets)
let ids = ccxt.exchanges.filter (id => id in exchanges)

await Promise.all (ids.map (async id => {

console.log (markets[id])
console.log (exchanges[id])

// // instantiate the market
let market = new ccxt[id] (markets[id])
console.log (market.id, market.apiKey)
markets[id] = market
// // instantiate the exchange
let exchange = new ccxt[id] (exchanges[id])
console.log (exchange.id, exchange.apiKey)
exchanges[id] = exchange

// load products
await market.loadProducts ()
console.log (market.id, 'loaded')
// load markets
await exchange.loadMarkets ()
console.log (exchange.id, 'loaded')

// check the balance
if (market.apiKey) {
let balance = await market.fetchBalance ()
console.log (market.id, balance)
if (exchange.apiKey) {
let balance = await exchange.fetchBalance ()
console.log (exchange.id, balance)
}

return market
return exchange
}))

// when all of them are ready, do your other things
console.log ('Loaded markets:', ids.join (', '))
console.log ('Loaded exchanges:', ids.join (', '))
}

test ()
Loading

0 comments on commit 5a4c09a

Please sign in to comment.