Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix: handle execution report #495

Merged
merged 8 commits into from
Aug 27, 2022
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
35 changes: 27 additions & 8 deletions app/binance/user.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { v4: uuidv4 } = require('uuid');
const _ = require('lodash');
const { binance } = require('../helpers');
const queue = require('../cronjob/trailingTradeHelper/queue');
Expand Down Expand Up @@ -52,8 +53,15 @@ const setupUserWebsocket = async logger => {
totalTradeQuantity,
orderTime: transactTime // Transaction time
} = evt;
logger.info(
{ symbol, evt, saveLog: true },

const symbolLogger = logger.child({
jobName: 'trailingTrade',
correlationId: uuidv4(),
symbol
});

symbolLogger.info(
{ evt, saveLog: true },
`There is a new update in order. ${orderId} - ${side} - ${orderStatus}`
);

Expand All @@ -72,10 +80,14 @@ const setupUserWebsocket = async logger => {
orderId !== lastOrder.orderId ||
transactTime < lastOrder.transactTime
) {
symbolLogger.info(
{ lastOrder, evt, saveLog: true },
'This order update is an old order. Do not update last grid trade order.'
);
return;
}

await updateGridTradeLastOrder(logger, symbol, side.toLowerCase(), {
const updatedOrder = {
...lastOrder,
status: orderStatus,
type: orderType,
Expand All @@ -88,13 +100,20 @@ const setupUserWebsocket = async logger => {
isWorking: isOrderWorking,
updateTime: eventTime,
transactTime
});
logger.info(
{ symbol, lastOrder, saveLog: true },
'The last order has been updated.'
};

await updateGridTradeLastOrder(
logger,
symbol,
side.toLowerCase(),
updatedOrder
);
symbolLogger.info(
{ lastOrder, updatedOrder, saveLog: true },
`The last order has been updated. ${orderId} - ${side} - ${orderStatus}`
);

queue.executeFor(logger, symbol);
queue.executeFor(symbolLogger, symbol);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('ensure-grid-trade-order-executed.js', () => {
let mockIsExceedAPILimit;
let mockDisableAction;
let mockSaveOrderStats;
let mockRefreshOpenOrdersAndAccountInfo;

let mockSaveSymbolGridTrade;

Expand Down Expand Up @@ -46,6 +47,14 @@ describe('ensure-grid-trade-order-executed.js', () => {
mockIsExceedAPILimit = jest.fn().mockReturnValue(false);
mockDisableAction = jest.fn().mockResolvedValue(true);
mockSaveOrderStats = jest.fn().mockResolvedValue(true);
mockRefreshOpenOrdersAndAccountInfo = jest.fn().mockResolvedValue({
accountInfo: {
accountInfo: 'updated'
},
openOrders: [{ openOrders: 'retrieved' }],
buyOpenOrders: [{ buyOpenOrders: 'retrived' }],
sellOpenOrders: [{ sellOpenOrders: 'retrived' }]
});

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

Expand All @@ -62,7 +71,8 @@ describe('ensure-grid-trade-order-executed.js', () => {
getAPILimit: mockGetAPILimit,
isExceedAPILimit: mockIsExceedAPILimit,
disableAction: mockDisableAction,
saveOrderStats: mockSaveOrderStats
saveOrderStats: mockSaveOrderStats,
refreshOpenOrdersAndAccountInfo: mockRefreshOpenOrdersAndAccountInfo
}));

jest.mock('../../../trailingTradeHelper/configuration', () => ({
Expand Down Expand Up @@ -148,6 +158,10 @@ describe('ensure-grid-trade-order-executed.js', () => {
expect(mockSaveOrderStats).not.toHaveBeenCalled();
});

it('does not trigger refreshOpenOrdersAndAccountInfo', () => {
expect(mockRefreshOpenOrdersAndAccountInfo).not.toHaveBeenCalled();
});

it('does not trigger slack.sendMessage', () => {
expect(slackMock.sendMessage).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -337,7 +351,9 @@ describe('ensure-grid-trade-order-executed.js', () => {
getAPILimit: mockGetAPILimit,
isExceedAPILimit: mockIsExceedAPILimit,
disableAction: mockDisableAction,
saveOrderStats: mockSaveOrderStats
saveOrderStats: mockSaveOrderStats,
refreshOpenOrdersAndAccountInfo:
mockRefreshOpenOrdersAndAccountInfo
}));

jest.mock('../../../trailingTradeHelper/configuration', () => ({
Expand Down Expand Up @@ -417,6 +433,13 @@ describe('ensure-grid-trade-order-executed.js', () => {
checkOrderExecutePeriod: 10,
temporaryDisableActionAfterConfirmingOrder: 20
}
},
openOrders: [],
buy: {
openOrders: []
},
sell: {
openOrders: []
}
};

Expand Down Expand Up @@ -448,6 +471,12 @@ describe('ensure-grid-trade-order-executed.js', () => {
it('does not trigger saveOrderStats', () => {
expect(mockSaveOrderStats).not.toHaveBeenCalled();
});

it('does not trigger refreshOpenOrdersAndAccountInfo', () => {
expect(
mockRefreshOpenOrdersAndAccountInfo
).not.toHaveBeenCalled();
});
} else if (t.lastBuyOrder.status.includes('FILLED')) {
// do filled thing
it('triggers calculated last buy price as order filled', () => {
Expand Down Expand Up @@ -488,6 +517,13 @@ describe('ensure-grid-trade-order-executed.js', () => {
);
});

it('triggers refreshOpenOrdersAndAccountInfo as order filled', () => {
expect(mockRefreshOpenOrdersAndAccountInfo).toHaveBeenCalledWith(
loggerMock,
t.symbol
);
});

it('triggers PubSub.publish for check-open-orders channel', () => {
expect(PubSubMock.publish).toHaveBeenCalledWith(
'check-open-orders',
Expand Down Expand Up @@ -551,6 +587,13 @@ describe('ensure-grid-trade-order-executed.js', () => {
]);
});

it('triggers refreshOpenOrdersAndAccountInfo due to cancelled order', () => {
expect(mockRefreshOpenOrdersAndAccountInfo).toHaveBeenCalledWith(
loggerMock,
t.symbol
);
});

if (t.notifyOrderExecute === true) {
it('triggers slack.sendMessage due to cancelled order', () => {
expect(slackMock.sendMessage).toHaveBeenCalledWith(
Expand Down Expand Up @@ -760,7 +803,9 @@ describe('ensure-grid-trade-order-executed.js', () => {
getAPILimit: mockGetAPILimit,
isExceedAPILimit: mockIsExceedAPILimit,
disableAction: mockDisableAction,
saveOrderStats: mockSaveOrderStats
saveOrderStats: mockSaveOrderStats,
refreshOpenOrdersAndAccountInfo:
mockRefreshOpenOrdersAndAccountInfo
}));

jest.mock('../../../trailingTradeHelper/configuration', () => ({
Expand Down Expand Up @@ -840,6 +885,13 @@ describe('ensure-grid-trade-order-executed.js', () => {
checkOrderExecutePeriod: 10,
temporaryDisableActionAfterConfirmingOrder: 20
}
},
openOrders: [],
buy: {
openOrders: []
},
sell: {
openOrders: []
}
};

Expand Down Expand Up @@ -885,6 +937,13 @@ describe('ensure-grid-trade-order-executed.js', () => {
);
});

it('triggers refreshOpenOrdersAndAccountInfo as order filled', () => {
expect(mockRefreshOpenOrdersAndAccountInfo).toHaveBeenCalledWith(
loggerMock,
t.symbol
);
});

it('triggers disableAction as order filled', () => {
expect(mockDisableAction).toHaveBeenCalledWith(
loggerMock,
Expand Down Expand Up @@ -944,6 +1003,13 @@ describe('ensure-grid-trade-order-executed.js', () => {
]);
});

it('triggers refreshOpenOrdersAndAccountInfo due to cancelled order', () => {
expect(mockRefreshOpenOrdersAndAccountInfo).toHaveBeenCalledWith(
loggerMock,
t.symbol
);
});

it('triggers slack.sendMessage due to cancelled order', () => {
expect(slackMock.sendMessage).toHaveBeenCalledWith(
expect.stringContaining('Order Removed'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const {
getAPILimit,
isExceedAPILimit,
disableAction,
saveOrderStats
saveOrderStats,
refreshOpenOrdersAndAccountInfo
} = require('../../trailingTradeHelper/common');

const {
Expand Down Expand Up @@ -228,6 +229,16 @@ const execute = async (logger, rawData) => {
// Remove grid trade last order
await removeGridTradeLastOrder(logger, symbols, symbol, 'buy');

const {
accountInfo,
openOrders: updatedOpenOrders,
buyOpenOrders
} = await refreshOpenOrdersAndAccountInfo(logger, symbol);

data.accountInfo = accountInfo;
data.openOrders = updatedOpenOrders;
data.buy.openOrders = buyOpenOrders;

slackMessageOrderFilled(
logger,
symbol,
Expand Down Expand Up @@ -261,6 +272,16 @@ const execute = async (logger, rawData) => {
// If order is no longer available, then delete from cache
await removeGridTradeLastOrder(logger, symbols, symbol, 'buy');

const {
accountInfo,
openOrders: updatedOpenOrders,
buyOpenOrders
} = await refreshOpenOrdersAndAccountInfo(logger, symbol);

data.accountInfo = accountInfo;
data.openOrders = updatedOpenOrders;
data.buy.openOrders = buyOpenOrders;

slackMessageOrderDeleted(
logger,
symbol,
Expand Down Expand Up @@ -291,6 +312,16 @@ const execute = async (logger, rawData) => {
// Remove grid trade last order
await removeGridTradeLastOrder(logger, symbols, symbol, 'sell');

const {
accountInfo,
openOrders: updatedOpenOrders,
sellOpenOrders
} = await refreshOpenOrdersAndAccountInfo(logger, symbol);

data.accountInfo = accountInfo;
data.openOrders = updatedOpenOrders;
data.sell.openOrders = sellOpenOrders;

slackMessageOrderFilled(
logger,
symbol,
Expand All @@ -315,6 +346,16 @@ const execute = async (logger, rawData) => {
// If order is no longer available, then delete from cache
await removeGridTradeLastOrder(logger, symbols, symbol, 'sell');

const {
accountInfo,
openOrders: updatedOpenOrders,
sellOpenOrders
} = await refreshOpenOrdersAndAccountInfo(logger, symbol);

data.accountInfo = accountInfo;
data.openOrders = updatedOpenOrders;
data.sell.openOrders = sellOpenOrders;

slackMessageOrderDeleted(
logger,
symbol,
Expand Down