Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
bmino committed Mar 7, 2020
2 parents 7b1ab04 + c9a3b41 commit 96b4b81
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 134 deletions.
2 changes: 1 addition & 1 deletion config/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},

"LOG": {
"LEVEL": "info",
"LEVEL": "debug",
"PRETTY_PRINT": true
},

Expand Down
7 changes: 3 additions & 4 deletions config/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Upon each version update you should copy the new syntax from `config.json.exampl
### `LOG`

#### `LOG.LEVEL` (String)
* Default: `"info"`
* Default: `"debug"`
* Description: Log level to configure how verbose logging messages are
* Values:
* `"fatal"`
Expand Down Expand Up @@ -142,20 +142,19 @@ Upon each version update you should copy the new syntax from `config.json.exampl
* `50`
* `100`
* `500`
* `1000`

#### `DEPTH.PRUNE` (Boolean)
* Default: `false`
* Description: Remove depth cache entries with a depth greater than `DEPTH.SIZE` before each calculation cycle

#### `DEPTH.INITIALIZATION_INTERVAL` (Number)
* Default: `50`
* Default: `75`
* Description: Delay (ms) between the initialization of each depth websocket


---


#### `CALCULATION_COOLDOWN` (Number)
* Default: `500`
* Default: `250`
* Description: Delay (ms) after calculations are performed before starting another cycle
79 changes: 36 additions & 43 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "binance-triangle-arbitrage",
"version": "5.2.4",
"version": "5.2.5",
"repository": {
"type": "git",
"url": "https://github.com/bmino/binance-triangle-arbitrage.git"
Expand All @@ -17,9 +17,9 @@
},
"dependencies": {
"blessed": "^0.1.81",
"node-binance-api": "^0.9.9",
"pino": "^5.16.0",
"pino-pretty": "^3.5.0"
"node-binance-api": "^0.10.3",
"pino": "^5.17.0",
"pino-pretty": "^3.6.1"
},
"license": "MIT"
}
12 changes: 4 additions & 8 deletions src/main/ArbitrageExecution.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const ArbitrageExecution = {

logger.execution.info(`Attempting to execute ${calculated.id} with an age of ${Math.max(age.ab, age.bc, age.ca).toFixed(0)} ms and expected profit of ${calculated.percent.toFixed(4)}%`);

return ArbitrageExecution.execute(calculated)
return ArbitrageExecution.getExecutionStrategy()(calculated)
.then((actual) => {
logger.execution.info(`${CONFIG.TRADING.ENABLED ? 'Executed' : 'Test: Executed'} ${calculated.id} position in ${new Date().getTime() - startTime} ms`);

Expand Down Expand Up @@ -90,7 +90,7 @@ const ArbitrageExecution = {
ArbitrageExecution.inProgressSymbols.delete(symbol.c);

if (CONFIG.TRADING.EXECUTION_CAP && ArbitrageExecution.inProgressIds.size === 0 && ArbitrageExecution.getAttemptedPositionsCount() >= CONFIG.TRADING.EXECUTION_CAP) {
logger.execution.error(`Cannot exceed user defined execution cap of ${CONFIG.TRADING.EXECUTION_CAP} executions`);
logger.execution.fatal(`Cannot exceed user defined execution cap of ${CONFIG.TRADING.EXECUTION_CAP} executions`);
process.exit();
}
});
Expand Down Expand Up @@ -149,10 +149,6 @@ const ArbitrageExecution = {
return Object.keys(ArbitrageExecution.attemptedPositions).filter(time => time > timeFloor).length;
},

execute(calculated) {
return ArbitrageExecution.getExecutionStrategy()(calculated);
},

getExecutionStrategy() {
switch (CONFIG.TRADING.EXECUTION_STRATEGY.toLowerCase()) {
case 'parallel':
Expand Down Expand Up @@ -230,15 +226,15 @@ const ArbitrageExecution = {
if (results.orderId) {
[actual.a.spent, actual.b.earned, fees] = ArbitrageExecution.parseActualResults(calculated.trade.ab.method, results);
actual.fees += fees;
recalculated.bc = CalculationNode.recalculateTradeLeg(calculated.trade.bc, actual.b.earned, BinanceApi.cloneDepth(calculated.trade.bc.ticker, CONFIG.DEPTH.SIZE));
recalculated.bc = CalculationNode.recalculateTradeLeg(calculated.trade.bc, actual.b.earned, BinanceApi.getDepthSnapshots(calculated.trade.bc.ticker));
}
return BinanceApi.marketBuyOrSell(calculated.trade.bc.method)(calculated.trade.bc.ticker, recalculated.bc);
})
.then((results) => {
if (results.orderId) {
[actual.b.spent, actual.c.earned, fees] = ArbitrageExecution.parseActualResults(calculated.trade.bc.method, results);
actual.fees += fees;
recalculated.ca = CalculationNode.recalculateTradeLeg(calculated.trade.ca, actual.c.earned, BinanceApi.cloneDepth(calculated.trade.ca.ticker, CONFIG.DEPTH.SIZE));
recalculated.ca = CalculationNode.recalculateTradeLeg(calculated.trade.ca, actual.c.earned, BinanceApi.getDepthSnapshots(calculated.trade.ca.ticker));
}
return BinanceApi.marketBuyOrSell(calculated.trade.ca.method)(calculated.trade.ca.ticker, recalculated.ca);
})
Expand Down
38 changes: 11 additions & 27 deletions src/main/BinanceApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,24 @@ const BinanceApi = {
});
},

cloneDepth(ticker, levels) {
const tmp = binance.depthCache(ticker);
const prune = (depthSnapshot, levels) => {
if (!levels) return depthSnapshot;
return Object.keys(depthSnapshot)
.slice(0, levels)
.reduce((prunedDepthSnapshot, key) => {
prunedDepthSnapshot[key] = depthSnapshot[key];
return prunedDepthSnapshot;
}, {});
};
return {
eventTime: tmp.eventTime,
lastUpdateId: tmp.lastUpdateId,
asks: prune({...tmp.asks}, levels),
bids: prune({...tmp.bids}, levels)
};
},

cloneDepths(tickers, levels) {
return tickers.reduce((clone, ticker) => {
clone[ticker] = BinanceApi.cloneDepth(ticker, levels);
return clone;
}, {});
getDepthSnapshots(tickers) {
const depthSnapshot = {};
tickers.forEach((ticker) => {
depthSnapshot[ticker] = binance.depthCache(ticker);
});
return depthSnapshot;
},

marketBuy(ticker, quantity) {
logger.execution.info(`${binance.getOption('test') ? 'Test: Buying' : 'Buying'} ${quantity} ${ticker} @ market price`);
const before = new Date().getTime();
return new Promise((resolve, reject) => {
binance.marketBuy(ticker, quantity, (error, response) => {
if (error) return BinanceApi.handleBuyOrSellError(error, reject);
if (binance.getOption('test')) {
logger.execution.info(`Test: Successfully bought ${ticker} @ market price`);
} else {
logger.execution.info(`Successfully bought ${response.executedQty} ${ticker} @ a quote of ${response.cummulativeQuoteQty}`);
logger.execution.info(`Successfully bought ${response.executedQty} ${ticker} @ a quote of ${response.cummulativeQuoteQty} in ${new Date().getTime() - before} ms`);
}
return resolve(response);
});
Expand All @@ -68,13 +51,14 @@ const BinanceApi = {

marketSell(ticker, quantity) {
logger.execution.info(`${binance.getOption('test') ? 'Test: Selling' : 'Selling'} ${quantity} ${ticker} @ market price`);
const before = new Date().getTime();
return new Promise((resolve, reject) => {
binance.marketSell(ticker, quantity, (error, response) => {
if (error) return BinanceApi.handleBuyOrSellError(error, reject);
if (binance.getOption('test')) {
logger.execution.info(`Test: Successfully sold ${ticker} @ market price`);
} else {
logger.execution.info(`Successfully sold ${response.executedQty} ${ticker} @ a quote of ${response.cummulativeQuoteQty}`);
logger.execution.info(`Successfully sold ${response.executedQty} ${ticker} @ a quote of ${response.cummulativeQuoteQty} in ${new Date().getTime() - before} ms`);
}
return resolve(response);
});
Expand Down Expand Up @@ -103,7 +87,7 @@ const BinanceApi = {
});
},

depthCache(tickers, limit=100, stagger=200) {
depthCacheStaggered(tickers, limit, stagger) {
return binance.websockets.depthCacheStaggered(tickers, BinanceApi.sortDepthCache, limit, stagger);
},

Expand Down
Loading

0 comments on commit 96b4b81

Please sign in to comment.