Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
bmino committed Aug 16, 2020
2 parents e3ccd48 + bcfb5e3 commit be6e8c9
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 96 deletions.
34 changes: 17 additions & 17 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "binance-triangle-arbitrage",
"version": "5.5.0",
"version": "5.5.1",
"repository": {
"type": "git",
"url": "https://github.com/bmino/binance-triangle-arbitrage.git"
Expand Down
12 changes: 6 additions & 6 deletions src/main/ArbitrageExecution.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const ArbitrageExecution = {
attemptedPositions: {},

executeCalculatedPosition(calculated) {
const startTime = new Date().getTime();
const startTime = Date.now();

if (!ArbitrageExecution.isSafeToExecute(calculated)) return false;

Expand All @@ -32,7 +32,7 @@ const ArbitrageExecution = {

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

// Results are only collected when a trade is executed
if (!CONFIG.TRADING.ENABLED) return;
Expand Down Expand Up @@ -95,7 +95,7 @@ const ArbitrageExecution = {
},

isSafeToExecute(calculated) {
const now = new Date().getTime();
const now = Date.now();
const { symbol } = calculated.trade;

// Profit Threshold is Not Satisfied
Expand Down Expand Up @@ -138,7 +138,7 @@ const ArbitrageExecution = {
},

getAttemptedPositionsCountInLastSecond() {
const timeFloor = new Date().getTime() - 1000;
const timeFloor = Date.now() - 1000;
return Object.keys(ArbitrageExecution.attemptedPositions).filter(time => time > timeFloor).length;
},

Expand Down Expand Up @@ -219,15 +219,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.getDepthSnapshots(calculated.trade.bc.ticker));
recalculated.bc = CalculationNode.recalculateTradeLeg(calculated.trade.bc, actual.b.earned, BinanceApi.depthCache(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.getDepthSnapshots(calculated.trade.ca.ticker));
recalculated.ca = CalculationNode.recalculateTradeLeg(calculated.trade.ca, actual.c.earned, BinanceApi.depthCache(calculated.trade.ca.ticker));
}
return BinanceApi.marketBuyOrSell(calculated.trade.ca.method)(calculated.trade.ca.ticker, recalculated.ca);
})
Expand Down
9 changes: 4 additions & 5 deletions src/main/BinanceApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const BinanceApi = {

getDepthSnapshots(tickers) {
const depthSnapshot = {};
if (!Array.isArray(tickers)) tickers = [tickers];
tickers.forEach((ticker) => {
depthSnapshot[ticker] = binance.depthCache(ticker);
});
Expand All @@ -46,14 +45,14 @@ const BinanceApi = {

marketBuy(ticker, quantity) {
logger.execution.info(`${binance.getOption('test') ? 'Test: Buying' : 'Buying'} ${quantity} ${ticker} @ market price`);
const before = new Date().getTime();
const before = Date.now();
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} in ${new Date().getTime() - before} ms`);
logger.execution.info(`Successfully bought ${response.executedQty} ${ticker} @ a quote of ${response.cummulativeQuoteQty} in ${Date.now() - before} ms`);
}
return resolve(response);
});
Expand All @@ -62,14 +61,14 @@ const BinanceApi = {

marketSell(ticker, quantity) {
logger.execution.info(`${binance.getOption('test') ? 'Test: Selling' : 'Selling'} ${quantity} ${ticker} @ market price`);
const before = new Date().getTime();
const before = Date.now();
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} in ${new Date().getTime() - before} ms`);
logger.execution.info(`Successfully sold ${response.executedQty} ${ticker} @ a quote of ${response.cummulativeQuoteQty} in ${Date.now() - before} ms`);
}
return resolve(response);
});
Expand Down
8 changes: 4 additions & 4 deletions src/main/CalculationNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const CalculationNode = {
cycleCount: 0,

cycle(relationships, depthCacheClone, errorCallback, executionCallback) {
const startTime = new Date().getTime();
const startTime = Date.now();

let successCount = 0;
let errorCount = 0;
Expand All @@ -30,7 +30,7 @@ const CalculationNode = {
}
});

const calculationTime = new Date().getTime() - startTime;
const calculationTime = Date.now() - startTime;

CalculationNode.cycleCount++;

Expand All @@ -41,7 +41,7 @@ const CalculationNode = {
let quantity, calculation;
let bestCalculation = null;

for (quantity = CONFIG.INVESTMENT.MIN || CONFIG.INVESTMENT.STEP; quantity <= CONFIG.INVESTMENT.MAX; quantity += CONFIG.INVESTMENT.STEP) {
for (quantity = CONFIG.INVESTMENT.MIN; quantity <= CONFIG.INVESTMENT.MAX; quantity += CONFIG.INVESTMENT.STEP) {
calculation = CalculationNode.calculate(quantity, trade, depthSnapshot);
if (!bestCalculation || calculation.percent > bestCalculation.percent) {
bestCalculation = calculation;
Expand Down Expand Up @@ -115,7 +115,7 @@ const CalculationNode = {
calculated.c.delta = calculated.c.earned - calculated.c.spent;

calculated.percent = (calculated.a.delta / calculated.a.spent * 100) - (CONFIG.TRADING.TAKER_FEE * 3);
if (!calculated.percent) calculated.percent = 0;
if (!calculated.percent) calculated.percent = -100;

return calculated;
},
Expand Down
2 changes: 1 addition & 1 deletion src/main/HUD.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const HUD = {
HUD.screen.append(HUD.objects.arbTable);
}

const now = new Date().getTime();
const now = Date.now();

let tableData = [HUD.headers.arb];
arbs.forEach(arb => {
Expand Down
33 changes: 14 additions & 19 deletions src/main/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ logger.performance.info(logger.LINE);

if (CONFIG.TRADING.ENABLED) console.log(`WARNING! Order execution is enabled!\n`);

SpeedTest.multiPing()
checkConfig()
.then(SpeedTest.multiPing)
.then((pings) => {
const msg = `Successfully pinged Binance in ${(pings.reduce((a,b) => a+b, 0) / pings.length).toFixed(0)} ms`;
console.log(msg);
logger.performance.info(msg);
})
.then(BinanceApi.exchangeInfo)
.then(exchangeInfo => MarketCache.initialize(exchangeInfo, CONFIG.TRADING.WHITELIST, CONFIG.INVESTMENT.BASE))
.then(checkConfig)
.then(checkBalances)
.then(() => {
// Listen for depth updates
const tickers = MarketCache.getTickerArray();
const tickers = MarketCache.tickers.watching;
console.log(`Opening ${tickers.length} depth websockets ...`);
return BinanceApi.depthCacheStaggered(tickers, CONFIG.DEPTH.SIZE, CONFIG.DEPTH.INITIALIZATION_INTERVAL);
})
Expand All @@ -52,7 +52,7 @@ function calculateArbitrage() {

const { calculationTime, successCount, errorCount, results } = CalculationNode.cycle(
MarketCache.relationships,
BinanceApi.getDepthSnapshots(MarketCache.getTickerArray()),
BinanceApi.getDepthSnapshots(MarketCache.tickers.watching),
(e) => logger.performance.warn(e),
ArbitrageExecution.executeCalculatedPosition
);
Expand All @@ -70,7 +70,7 @@ function displayCalculationResults(successCount, errorCount, calculationTime) {
}

if (CalculationNode.cycleCount % 500 === 0) {
const tickersWithoutDepthUpdate = MarketCache.getTickersWithoutDepthCacheUpdate();
const tickersWithoutDepthUpdate = MarketCache.getWatchedTickersWithoutDepthCacheUpdate();
if (tickersWithoutDepthUpdate.length > 0) {
logger.performance.debug(`Tickers without a depth cache update: [${tickersWithoutDepthUpdate}]`);
}
Expand All @@ -90,20 +90,8 @@ function checkConfig() {
}
};

if (MarketCache.getTickerArray().length < 3) {
const msg = `Watching ${MarketCache.getTickerArray().length} ticker(s) is not sufficient to engage in triangle arbitrage`;
logger.execution.debug(`Watched Tickers: [${MarketCache.getTickerArray()}]`);
logger.execution.error(msg);
throw new Error(msg);
}
if (MarketCache.symbols.size < 3) {
const msg = `Watching ${MarketCache.symbols.size} symbol(s) is not sufficient to engage in triangle arbitrage`;
logger.execution.debug(`Watched Symbols: [${Array.from(MarketCache.symbols)}]`);
logger.execution.error(msg);
throw new Error(msg);
}
if (MarketCache.relationships.length === 0) {
const msg = `Watching ${MarketCache.relationships.length} triangular relationships is not sufficient to engage in triangle arbitrage`;
if (CONFIG.INVESTMENT.MIN <= 0) {
const msg = `INVESTMENT.MIN must be a positive value`;
logger.execution.error(msg);
throw new Error(msg);
}
Expand All @@ -122,6 +110,11 @@ function checkConfig() {
logger.execution.error(msg);
throw new Error(msg);
}
if (CONFIG.TRADING.WHITELIST.some(sym => sym !== sym.toUpperCase())) {
const msg = `Whitelist symbols must all be uppercase`;
logger.execution.error(msg);
throw new Error(msg);
}
if (CONFIG.TRADING.WHITELIST.length > 0 && !CONFIG.TRADING.WHITELIST.includes(CONFIG.INVESTMENT.BASE)) {
const msg = `Whitelist must include the base symbol of ${CONFIG.INVESTMENT.BASE}`;
logger.execution.debug(`Whitelist: [${CONFIG.TRADING.WHITELIST}]`);
Expand Down Expand Up @@ -168,6 +161,8 @@ function checkConfig() {
logger.execution.error(msg);
throw new Error(msg);
}

return Promise.resolve();
}

function checkBalances() {
Expand Down
Loading

0 comments on commit be6e8c9

Please sign in to comment.