Skip to content
New issue

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

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

Already on GitHub? # to your account

refactor: deterministic queue execution #588

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file.
- Fixed the issue that cannot export huge logs - [#561](https://github.com/chrisleekr/binance-trading-bot/pull/561), [#567](https://github.com/chrisleekr/binance-trading-bot/pull/567)
- Fixed the balance calculation to include dust balances by [@uhliksk](https://github.com/uhliksk) - [#571](https://github.com/chrisleekr/binance-trading-bot/pull/571)
- Fixed the open orders to be cancelled when the current price is higher/lower than the order price by [@uhliksk](https://github.com/uhliksk) - [#569](https://github.com/chrisleekr/binance-trading-bot/pull/569)
- Improved queue processing by replacing Bull queue to customised queue system by [@uhliksk](https://github.com/uhliksk) - [#562](https://github.com/chrisleekr/binance-trading-bot/pull/562), [#581](https://github.com/chrisleekr/binance-trading-bot/pull/581)
- Improved queue processing by replacing Bull queue to customised queue system by [@uhliksk](https://github.com/uhliksk) - [#562](https://github.com/chrisleekr/binance-trading-bot/pull/562), [#581](https://github.com/chrisleekr/binance-trading-bot/pull/581), [#588](https://github.com/chrisleekr/binance-trading-bot/pull/588)
- Added conservative sell strategy, which can reduce the sell trigger price as the grid gets deeper by [@rando128](https://github.com/rando128) - [#585](https://github.com/chrisleekr/binance-trading-bot/pull/585)

Thanks [@uhliksk](https://github.com/uhliksk) and [@rando128](https://github.com/rando128) for your great contributions. 💯 :heart:
Expand Down
53 changes: 15 additions & 38 deletions app/__tests__/server-binance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ describe('server-binance', () => {
let mockGetGlobalConfiguration;

let mockGetAccountInfoFromAPI;
let mockLockSymbol;
let mockUnlockSymbol;
let mockCacheExchangeSymbols;

let mockSetupUserWebsocket;
Expand Down Expand Up @@ -57,8 +55,9 @@ describe('server-binance', () => {
deleteAll: jest.fn().mockResolvedValue(true)
};
mockQueue = {
init: jest.fn().mockResolvedValue(true),
execute: jest.fn().mockResolvedValue(true)
prepareJob: jest.fn().mockResolvedValue(true),
execute: jest.fn().mockResolvedValue(true),
completeJob: jest.fn().mockResolvedValue(true)
};
mockSlack = {
sendMessage: jest.fn().mockResolvedValue(true)
Expand Down Expand Up @@ -97,9 +96,6 @@ describe('server-binance', () => {
}
});

mockLockSymbol = jest.fn().mockResolvedValue(true);
mockUnlockSymbol = jest.fn().mockResolvedValue(true);

mockSetupUserWebsocket = jest.fn().mockResolvedValue(true);

mockSyncCandles = jest.fn().mockResolvedValue(true);
Expand Down Expand Up @@ -133,8 +129,6 @@ describe('server-binance', () => {

jest.mock('../cronjob/trailingTradeHelper/common', () => ({
getAccountInfoFromAPI: mockGetAccountInfoFromAPI,
lockSymbol: mockLockSymbol,
unlockSymbol: mockUnlockSymbol,
cacheExchangeSymbols: mockCacheExchangeSymbols
}));

Expand Down Expand Up @@ -237,8 +231,12 @@ describe('server-binance', () => {
);
});

it('triggers queue.init', () => {
expect(mockQueue.init).toHaveBeenCalled();
it('triggers queue.prepareJob', () => {
expect(mockQueue.prepareJob).toHaveBeenCalled();
});

it('triggers queue.completeJob', () => {
expect(mockQueue.completeJob).toHaveBeenCalled();
});
});

Expand All @@ -253,9 +251,6 @@ describe('server-binance', () => {
}
});

mockLockSymbol = jest.fn().mockResolvedValue(true);
mockUnlockSymbol = jest.fn().mockResolvedValue(true);

mockSetupUserWebsocket = jest.fn().mockResolvedValue(true);

mockSyncCandles = jest.fn().mockResolvedValue(true);
Expand Down Expand Up @@ -297,8 +292,6 @@ describe('server-binance', () => {

jest.mock('../cronjob/trailingTradeHelper/common', () => ({
getAccountInfoFromAPI: mockGetAccountInfoFromAPI,
lockSymbol: mockLockSymbol,
unlockSymbol: mockUnlockSymbol,
cacheExchangeSymbols: mockCacheExchangeSymbols
}));

Expand Down Expand Up @@ -370,9 +363,6 @@ describe('server-binance', () => {
}
});

mockLockSymbol = jest.fn().mockResolvedValue(true);
mockUnlockSymbol = jest.fn().mockResolvedValue(true);

mockSetupUserWebsocket = jest.fn().mockResolvedValue(true);

mockSyncCandles = jest.fn().mockResolvedValue(true);
Expand Down Expand Up @@ -414,8 +404,6 @@ describe('server-binance', () => {

jest.mock('../cronjob/trailingTradeHelper/common', () => ({
getAccountInfoFromAPI: mockGetAccountInfoFromAPI,
lockSymbol: mockLockSymbol,
unlockSymbol: mockUnlockSymbol,
cacheExchangeSymbols: mockCacheExchangeSymbols
}));

Expand Down Expand Up @@ -482,11 +470,15 @@ describe('server-binance', () => {
});

it('triggers queue.execute for BTCUSDT', () => {
expect(mockQueue.execute).toHaveBeenCalledWith(logger, 'BTCUSDT');
expect(mockQueue.execute).toHaveBeenCalledWith(logger, 'BTCUSDT', {
processFn: expect.any(Function)
});
});

it('triggers queue.execute for LTCUSDT', () => {
expect(mockQueue.execute).toHaveBeenCalledWith(logger, 'LTCUSDT');
expect(mockQueue.execute).toHaveBeenCalledWith(logger, 'LTCUSDT', {
processFn: expect.any(Function)
});
});
});
});
Expand All @@ -502,9 +494,6 @@ describe('server-binance', () => {
}
});

mockLockSymbol = jest.fn().mockResolvedValue(true);
mockUnlockSymbol = jest.fn().mockResolvedValue(true);

mockSetupUserWebsocket = jest.fn().mockResolvedValue(true);

mockSyncCandles = jest.fn().mockResolvedValue(true);
Expand Down Expand Up @@ -546,8 +535,6 @@ describe('server-binance', () => {

jest.mock('../cronjob/trailingTradeHelper/common', () => ({
getAccountInfoFromAPI: mockGetAccountInfoFromAPI,
lockSymbol: mockLockSymbol,
unlockSymbol: mockUnlockSymbol,
cacheExchangeSymbols: mockCacheExchangeSymbols
}));

Expand Down Expand Up @@ -607,9 +594,6 @@ describe('server-binance', () => {
}
});

mockLockSymbol = jest.fn().mockResolvedValue(true);
mockUnlockSymbol = jest.fn().mockResolvedValue(true);

mockSetupUserWebsocket = jest.fn().mockResolvedValue(true);

mockSyncCandles = jest.fn().mockResolvedValue(true);
Expand Down Expand Up @@ -643,8 +627,6 @@ describe('server-binance', () => {

jest.mock('../cronjob/trailingTradeHelper/common', () => ({
getAccountInfoFromAPI: mockGetAccountInfoFromAPI,
lockSymbol: mockLockSymbol,
unlockSymbol: mockUnlockSymbol,
cacheExchangeSymbols: mockCacheExchangeSymbols
}));

Expand Down Expand Up @@ -770,9 +752,6 @@ describe('server-binance', () => {
}
});

mockLockSymbol = jest.fn().mockResolvedValue(true);
mockUnlockSymbol = jest.fn().mockResolvedValue(true);

mockSetupUserWebsocket = jest.fn().mockResolvedValue(true);

mockSyncCandles = jest.fn().mockResolvedValue(true);
Expand Down Expand Up @@ -806,8 +785,6 @@ describe('server-binance', () => {

jest.mock('../cronjob/trailingTradeHelper/common', () => ({
getAccountInfoFromAPI: mockGetAccountInfoFromAPI,
lockSymbol: mockLockSymbol,
unlockSymbol: mockUnlockSymbol,
cacheExchangeSymbols: mockCacheExchangeSymbols
}));

Expand Down
3 changes: 2 additions & 1 deletion app/binance/__tests__/candles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ describe('candles.js', () => {
it('triggers queue.execute for ETHBTC', () => {
expect(mockExecute).toHaveBeenCalledWith(loggerMock, 'ETHBTC', {
correlationId: expect.any(String),
preprocessFn: expect.any(Function)
preprocessFn: expect.any(Function),
processFn: expect.any(Function)
});
});
});
Expand Down
9 changes: 6 additions & 3 deletions app/binance/__tests__/tickers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,16 @@ describe('tickers.js', () => {
it('triggers queue.execute for BTCUSDT', () => {
expect(mockExecute).toHaveBeenCalledWith(loggerMock, 'BTCUSDT', {
correlationId: expect.any(String),
preprocessFn: expect.any(Function)
preprocessFn: expect.any(Function),
processFn: expect.any(Function)
});
});

it('triggers queue.execute for BNBUSDT', () => {
expect(mockExecute).toHaveBeenCalledWith(loggerMock, 'BNBUSDT', {
correlationId: expect.any(String),
preprocessFn: expect.any(Function)
preprocessFn: expect.any(Function),
processFn: expect.any(Function)
});
});

Expand Down Expand Up @@ -152,7 +154,8 @@ describe('tickers.js', () => {
it('triggers queue.execute for BTCUSDT', () => {
expect(mockExecute).toHaveBeenCalledWith(loggerMock, 'BTCUSDT', {
correlationId: expect.any(String),
preprocessFn: expect.any(Function)
preprocessFn: expect.any(Function),
processFn: expect.any(Function)
});
});

Expand Down
6 changes: 4 additions & 2 deletions app/binance/__tests__/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,8 @@ describe('user.js', () => {
it('triggers queue.execute', () => {
expect(mockExecute).toHaveBeenCalledWith(loggerMock, 'ETHUSDT', {
correlationId: expect.any(String),
preprocessFn: expect.any(Function)
preprocessFn: expect.any(Function),
processFn: expect.any(Function)
});
});

Expand Down Expand Up @@ -680,7 +681,8 @@ describe('user.js', () => {
it('triggers queue.execute', () => {
expect(mockExecute).toHaveBeenCalledWith(loggerMock, 'ETHUSDT', {
correlationId: expect.any(String),
preprocessFn: expect.any(Function)
preprocessFn: expect.any(Function),
processFn: expect.any(Function)
});
});

Expand Down
4 changes: 3 additions & 1 deletion app/binance/candles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { v4: uuidv4 } = require('uuid');
const _ = require('lodash');
const queue = require('../cronjob/trailingTradeHelper/queue');
const { executeTrailingTrade } = require('../cronjob/index');
const { binance, mongo } = require('../helpers');
const {
getConfiguration
Expand Down Expand Up @@ -116,7 +117,8 @@ const syncCandles = async (logger, symbols) => {

queue.execute(logger, symbol, {
correlationId: uuidv4(),
preprocessFn: getCandles
preprocessFn: getCandles,
processFn: executeTrailingTrade
});
})
);
Expand Down
4 changes: 3 additions & 1 deletion app/binance/tickers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { v4: uuidv4 } = require('uuid');
const _ = require('lodash');
const { binance, cache } = require('../helpers');
const queue = require('../cronjob/trailingTradeHelper/queue');
const { executeTrailingTrade } = require('../cronjob/index');

const {
getAccountInfo,
Expand Down Expand Up @@ -78,7 +79,8 @@ const setupTickersWebsocket = async (logger, symbols) => {
if (canExecuteTrailingTrade) {
queue.execute(symbolLogger, monitoringSymbol, {
correlationId,
preprocessFn: saveCandle
preprocessFn: saveCandle,
processFn: executeTrailingTrade
});
} else {
saveCandle();
Expand Down
7 changes: 5 additions & 2 deletions app/binance/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { v4: uuidv4 } = require('uuid');
const _ = require('lodash');
const { binance } = require('../helpers');
const queue = require('../cronjob/trailingTradeHelper/queue');
const { executeTrailingTrade } = require('../cronjob/index');

const {
updateAccountInfo,
Expand Down Expand Up @@ -121,7 +122,8 @@ const setupUserWebsocket = async logger => {

queue.execute(symbolLogger, symbol, {
correlationId,
preprocessFn: checkLastOrder
preprocessFn: checkLastOrder,
processFn: executeTrailingTrade
});

const checkManualOrder = async () => {
Expand Down Expand Up @@ -155,7 +157,8 @@ const setupUserWebsocket = async logger => {

queue.execute(symbolLogger, symbol, {
correlationId,
preprocessFn: checkManualOrder
preprocessFn: checkManualOrder,
processFn: executeTrailingTrade
});
}
});
Expand Down
30 changes: 2 additions & 28 deletions app/cronjob/__tests__/trailingTrade.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ describe('trailingTrade', () => {
let mockConfigGet;

let mockGetAccountInfo;
let mockLockSymbol;
let mockIsSymbolLocked;
let mockUnlockSymbol;

let mockGetSymbolConfiguration;
let mockGetSymbolInfo;
Expand All @@ -40,9 +37,6 @@ describe('trailingTrade', () => {
beforeEach(() => {
jest.clearAllMocks().resetModules();

mockLockSymbol = jest.fn().mockResolvedValue(true);
mockUnlockSymbol = jest.fn().mockResolvedValue(true);

mockLoggerInfo = jest.fn();
mockSlackSendMessage = jest.fn().mockResolvedValue(true);

Expand Down Expand Up @@ -89,8 +83,6 @@ describe('trailingTrade', () => {
get: mockConfigGet
}));

mockIsSymbolLocked = jest.fn().mockResolvedValue(false);

mockGetAccountInfo = jest.fn().mockResolvedValue({
account: 'info'
});
Expand Down Expand Up @@ -268,10 +260,7 @@ describe('trailingTrade', () => {
}));

jest.mock('../trailingTradeHelper/common', () => ({
getAccountInfo: mockGetAccountInfo,
lockSymbol: mockLockSymbol,
isSymbolLocked: mockIsSymbolLocked,
unlockSymbol: mockUnlockSymbol
getAccountInfo: mockGetAccountInfo
}));

jest.mock('../trailingTrade/steps', () => ({
Expand Down Expand Up @@ -301,16 +290,11 @@ describe('trailingTrade', () => {
await trailingTradeExecute(logger, 'BTCUSDT');
});

it(`triggers isSymbolLocked - BTCUSDT`, () => {
expect(mockIsSymbolLocked).toHaveBeenCalledWith(logger, 'BTCUSDT');
});

it('returns expected result for BTCUSDT', () => {
it('returns expected result for BTCUSDT finish', () => {
expect(mockLoggerInfo).toHaveBeenCalledWith(
{
data: {
symbol: 'BTCUSDT',
isLocked: false,
featureToggle: { feature1Enabled: true },
lastCandle: { got: 'lowest value' },
accountInfo: { account: 'info' },
Expand Down Expand Up @@ -348,16 +332,11 @@ describe('trailingTrade', () => {
await trailingTradeExecute(logger, 'ETHUSDT');
});

it(`triggers isSymbolLocked - ETHUSDT`, () => {
expect(mockIsSymbolLocked).toHaveBeenCalledWith(logger, 'ETHUSDT');
});

it('returns expected result for ETHUSDT', () => {
expect(mockLoggerInfo).toHaveBeenCalledWith(
{
data: {
symbol: 'ETHUSDT',
isLocked: false,
featureToggle: { feature1Enabled: true },
lastCandle: { got: 'lowest value' },
accountInfo: { account: 'info' },
Expand Down Expand Up @@ -395,16 +374,11 @@ describe('trailingTrade', () => {
await trailingTradeExecute(logger, 'LTCUSDT');
});

it(`triggers isSymbolLocked - LTCUSDT`, () => {
expect(mockIsSymbolLocked).toHaveBeenCalledWith(logger, 'LTCUSDT');
});

it('returns expected result for LTCUSDT', async () => {
expect(mockLoggerInfo).toHaveBeenCalledWith(
{
data: {
symbol: 'LTCUSDT',
isLocked: false,
featureToggle: { feature1Enabled: true },
lastCandle: { got: 'lowest value' },
accountInfo: { account: 'info' },
Expand Down
Loading